USB_DEVICE_ ATTACH仅在断开USB设备时射击

发布于 2025-02-06 13:19:22 字数 1931 浏览 1 评论 0原文

我有一个广播员可以通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2025-02-13 13:19:22

将其添加到您的清单文件中:

<service
    android:name=".UsbService"
    android:enabled="true"
    android:exported="true">

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

Add this in your manifest file:

<service
    android:name=".UsbService"
    android:enabled="true"
    android:exported="true">

    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
</service>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文