Method did not recognyze



I’am trying to communicate with my port USB but I have a problem with the method requestPermission() from the class UsbManager. the method requestPermission is not recognyzed whereas in the datasheet it does exist. (datasheet :

this in my code :

override fun onCreate(savedInstanceState: Bundle?) {

    var m_device :UsbDevice? = null
    var mUsbReceiver = UsbReceiver()
    var ACTION_USB_PERMISSION :String = "";
    var mUsbManager = getSystemService(Context.USB_SERVICE);
    var mPermissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0);
    var filter :IntentFilter = IntentFilter(ACTION_USB_PERMISSION)
    registerReceiver(mUsbReceiver, filter);

    mUsbManager.requestPermission(m_device, mPermissionIntent);

to create an instance of the class UsbManager we use the method getSystemService. But I don’t understand why the method is not recognyzed

thank you for your help


Can you post your error message? And also, double check the type of mUsbManager. If I remember right getSytemService can return a lot of different objects.


my error message is unresolved reference : requestPermission
for the other thing I cannot do a cast or I do not know how can I do :frowning: because
var mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE) do not work and nor
var mUsbManager = getSystemService(Context.USB_SERVICE)


I have tried with

var mUsbManager = getSystemService(Context.USB_SERVICE) is UsbManager

but it does not work :frowning:


alright, the solution is

var mUsbManager = getSystemService(Context.USB_SERVICE) as UsbManager

thank you for your help


As you probably figured out is tests whether or not a value is of a certain type. So

would return either true or false.

as casts a variable from one type to the other. If it is not possible an exception gets thrown. You can also use as? in which case the result will be null in case of an error.