Use the usb


#1

Hello everybody,

I am trying to use the usb but it does not work, though I followed the instructions from Android. My problem is that my app does not detect the usb. In my code the variable ‘device’ is always null and I do not understand why.
Have you got an idea ?
this is my code:

var ACTION_USB_ATTACHED :String = “android.hardware.usb.action.USB_DEVICE_ATTACHED”
var ACTION_USB_DETACHED :String = “android.hardware.usb.action.USB_DEVICE_DETACHED”
var ACTION_USB_PERMISSION :String = “com.android.example.USB_PERMISSION”
class MainActivity : AppCompatActivity(){

var switch_on_off :Switch? = null
var tv_info : TextView? = null

//USB
var mUsbReceiver :UsbReceiver? = null
var mfilter :IntentFilter? = null
var mUsbManager :UsbManager? = null
var mPermissionIntent : PendingIntent? = null
var device :UsbDevice? = null

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

    tv_info = findViewById<TextView>(R.id.textView)
    switch_on_off = findViewById<Switch>(R.id.switch1)

    switch_on_off!!.setChecked(false)
    switch_on_off!!.setText("OFF")

    //USB
    mUsbReceiver = UsbReceiver()
    mfilter = IntentFilter()
    mfilter!!.addAction(ACTION_USB_ATTACHED)
    mfilter!!.addAction(ACTION_USB_DETACHED)
    registerReceiver(mUsbReceiver, mfilter) //appelle la méthode onReceive de l'objet mUsbReceiver si il y a un event attached or detached via mfilter
    mUsbManager = getSystemService(Context.USB_SERVICE) as UsbManager

    //threads
    t_switchOnOff.start()
}

val t_switchOnOff = thread(start = false, priority = 10) // //priorité du thread minimal
{

    while(true){
        Thread.sleep(300)

        this@MainActivity.runOnUiThread(java.lang.Runnable {

            if(switch_on_off!!.isChecked())
            {
                switch_on_off!!.setText("ON")

                device = intent?.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE) 

                if(device != null) 
                {
                    if(!mUsbManager!!.hasPermission(device)){//si on a pas encore la permission d'utiliser le port USB

                        mPermissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0);
                        mUsbManager!!.requestPermission(device, mPermissionIntent);// la méthode onReceive de l'objet mUsbmanager

                    }
                    else //si on a déja la permission d'utiliser ce port USB
                    {
                        tv_info!!.setText("")
                        tv_info!!.setText("device USB detected : ${device!!.deviceName} \n& Authorization accorded")
                    }
                }
                else
                {
                    tv_info!!.setText("device USB NOT detected \n& usb Detached")
                }
            }
            else
            {
                tv_info!!.setText("")
                switch_on_off!!.setText("OFF")
            }
        })//end this@MainActivity.runOnUiThread(java.lang.Runnable ...
    }
}//end thread

class UsbReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) { //cette méthode est lancée dès qu'il y a un event Attached, Detached or la fonction requestPermission()
        val action = intent?.action
        var ACTION_USB_PERMISSION :String = "com.android.example.USB_PERMISSION"


        if (action!!.equals(ACTION_USB_DETACHED)) 
        {

        }
        if (action!!.equals(ACTION_USB_ATTACHED))
        {

        }


        if (action!!.equals(ACTION_USB_PERMISSION))
        {
            if (!intent!!.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) 
            {
                //Authoization refused
            } else {
                //Authorization accepted
            }
        }
    }
}//end class UsbReceiver

}// end MainActivity

thank you for your help


#2

This isn’t really a Kotlin issue. You may try stackoverflow.com, but in general you’d want to declutter your code. I would suspect that there is a permission issue (did you also declare the permission in the manifest?)


#3

Thanks for your answer, I just add in the manifest :

uses-feature android:name=“android.hardware.usb.host”

and
intent-filter
action android:name=“android.hardware.usb.action.USB_DEVICE_ATTACHED”
intent-filter
intent-filter
action android:name=“android.hardware.usb.action.USB_DEVICE_DETACHED”
intent-filter

that’s all what ask by Android