USB_DEVICE_ ATTACH仅在断开USB设备时射击
我有一个广播员可以通过USB聆听设备的连接和断开连接。 通过USB将扫描仪(扫描仪#1)连接到设备(Android OS 6)时,USB_DEVICE_ATTACHED不起作用。当扫描仪断开连接时,USB_DEVICE_ATTACHED将触发,然后立即USB_DEVICE_DETACHED。为什么连接时USB_DEVICE_ ATTACHACH fire不了? 如果您连接另一个扫描仪,则一切都很好,在连接时,usb_device_attached会触发。此外,如果将扫描仪1号连接到另一台设备(Android OS 10),则连接时所有内容也有效。什么原因是什么?我会为任何提示感到高兴。
清单
<?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>
测试人员
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")
}
}
}
}
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")
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其添加到您的清单文件中:
Add this in your manifest file: