I have a BroadcastReceiver to listen to the connection and disconnection of the device via USB. USB_DEVICE_ATTACHED does not work when connecting the scanner (scanner #1) to the device (Android OS 6) via USB. When the scanner is disconnected, USB_DEVICE_ATTACHED is triggered, and then immediately USB_DEVICE_DETACHED. Why doesn't USB_DEVICE_ATTACHED fire when connected? If you connect another scanner, then everything is fine, immediately upon connection, USB_DEVICE_ATTACHED is triggered. In addition, if you connect scanner No. 1 to another device (Android OS 10), then everything also works when connected. What could be the reason? I would be glad for any hint.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.evotor.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".TestReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
</receiver>
</application>
</manifest>
TestReceiver
class TestReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("MyLog", "onReceive")
intent?.let { intent: Intent ->
intent.action?.let { action: String ->
Log.d("MyLog", "action $action")
}
}
}
}