BroadcastReciever does not receive UsbManager.ACTION_USB_ACCESSORY_ATTACHED

70 views Asked by At

I tried using a broadcast receiver monitor USB_ACCESSORY, but could not get the android device to register any usb attach/detach events. However, when monitoringUSB_DEVICES this code worked perfectly fine.

usbBroadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {

                Toast.makeText(context, "Action Received", Toast.LENGTH_SHORT).show()

                when (intent?.action) {

                    UsbManager.ACTION_USB_ACCESSORY_ATTACHED -> {
                        Toast.makeText(context, "USB accessory attached", Toast.LENGTH_SHORT).show()
                    }
                    UsbManager.ACTION_USB_ACCESSORY_DETACHED -> {
                        Toast.makeText(context, "USB accessory detached", Toast.LENGTH_SHORT).show()
                    }
                    UsbManager.ACTION_USB_DEVICE_ATTACHED -> {
                        Toast.makeText(context, "USB Device attached", Toast.LENGTH_SHORT).show()
                    }
                    UsbManager.ACTION_USB_DEVICE_DETACHED -> {
                        Toast.makeText(context, "USB Device detached", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }

        val intentFilter = IntentFilter().apply {
            addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED)
            addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED)
            addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
            addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
        }

        registerReceiver(usbBroadcastReceiver, intentFilter)

Here is my manifest file where I declare these intents.

<activity android:name="com.example.the.MainActivity"
            android:screenOrientation="landscape"
            android:theme="@style/AppTheme">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_ACCESSORY_DETACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

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




        </activity>

Here is my devices.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-accessory />
</resources>


I did lots of digging but could not find anybody who had recently had this issue. I eventually devised a workaround where I use the battery manager and wait for the charging/discharging events to occur.

BatteryManager.ACTION_CHARGING -> {
Toast.makeText(context, "USB accessory attached", Toast.LENGTH_SHORT).show()
}

BatteryManager.ACTION_DISCHARGING -> {
Toast.makeText(context, "USB accessory detached", Toast.LENGTH_SHORT).show()
}
            
addAction(BatteryManager.ACTION_CHARGING)
addAction(BatteryManager.ACTION_DISCHARGING)

0

There are 0 answers