Method did not recognyze

Hello,

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 : UsbManager  |  Android Developers)

this in my code :

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

    //USB
    var m_device :UsbDevice? = null
    var mUsbReceiver = UsbReceiver()
    var ACTION_USB_PERMISSION :String = "com.android.example.USB_PERMISSION";
    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

1 Like

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.