Search code examples
androidkotlinandroid-permissionsusbserial

Usb device is null even when plugged usb cable to mobile


Try to get usb device when plug phone. I tried to plugged in computer with mobile and also plugged-in mobile power charge. But always device return null. I don't know if it's problem because of permission. I saw few similar questions that about device get null always. But can't see any solution.

AndroidManifest.xml

    <intent-filter>
        <action android:name="anandroid.hardware.usb.action.USB_DEVICE_DETACHED" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>

    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />

device_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" />
</resources>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private val ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"
    private val ACTION_USB_DETACHED = "com.android.example.USB_DEVICE.DETACHED"
    private val ACTION_USB_ATTACHED = "com.android.example.USB_DEVICE.DETACHED"

    //USB
    // var UsbReceiver :UsbReceiver? = null
    var filter: IntentFilter? = null
    var manager: UsbManager? = null
    var permissionIntent: PendingIntent? = null
    var device: UsbDevice? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        filter = IntentFilter()
        filter!!.addAction(ACTION_USB_ATTACHED)
        filter!!.addAction(ACTION_USB_DETACHED)
        registerReceiver(usbReceiver, filter)
        manager = getSystemService(Context.USB_SERVICE) as UsbManager
        permissionIntent = PendingIntent.getBroadcast(
            this, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
        device = intent?.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
        val deviceList = manager!!.deviceList
        deviceList // Just Tried here to see if get deviceList
        if (device != null){
            if (!manager!!.hasPermission(device)){
                device?.let {
                    requestPermissions(it)
                }
            }
        }
        connectUsb.start()

    }

    val connectUsb = thread(start = false, priority = 10)
    {
        Thread.sleep(300)

        [email protected](java.lang.Runnable {
            if (device != null){
                if (!manager!!.hasPermission(device)){
                    device?.let {
                        requestPermissions(it)
                    }
                } else {
                    device
                }
            } else {
                Toast.makeText(applicationContext, "this device is null", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private var usbReceiver = object : BroadcastReceiver() {

        override fun onReceive(context: Context, intent: Intent) {

            if (intent.action.equals(ACTION_USB_DETACHED))
            {}
            if (intent.action.equals(ACTION_USB_ATTACHED))
            {}
    }

    private fun requestPermissions(device: UsbDevice) {
        manager?.requestPermission(device, permissionIntent)
    }
}

Solution

  • I wrote this if someone look in future, the problem is not with code. I was trying to plug phone to computer and expected to have computer as usb device. I got an OTC adapter and plugged in my USB HDD to phone. It show HDD in deviceList now.