【问题标题】:Device Disconnection during Android Accessory ModeAndroid 附件模式期间设备断开连接
【发布时间】:2016-11-24 16:23:21
【问题描述】:

是否可以以编程方式检测我的设备是否断开连接 (USB)?

我正在尝试在我的附件模式应用程序中标记断开连接事件。 那么有可能吗?如果是,如何?

【问题讨论】:

    标签: android usb accessory


    【解决方案1】:

    您可以使用 BroadcastReceiver 来监听 UsbManager.ACTION_USB_DEVICE_DETACHED 和 UsbManager.ACTION_USB_DEVICE_ATTACHED 事件。

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

    或者您可以定期循环通过连接的设备(使用计时器或 ScheduledExecutorService)检查您的设备是否在列表中。

    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    UsbDevice device = deviceList.get("deviceName");
    
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while(deviceIterator.hasNext()){
    UsbDevice device = deviceIterator.next();
    if (usbDeviceIsMyDevice(device)){
        // your code
    }
    }
    

    【讨论】:

      【解决方案2】:

      您可能无法使用UsbManager.ACTION_USB_DEVICE_ATTACHUsbManagerACTION_USB_DEVICE_DETACH,因为UsbManager 仅支持托管USB 连接的Android。来自UsbManager 的 Android 开发者文档:

      该类允许您访问 USB 的状态并与 USB 设备进行通信。目前公共 API 仅支持主机模式。

      Android 附件不允许 Android 托管 USB 连接,而是需要连接的设备来执行此操作。通过让后台服务定期运行以下自定义方法,我能够在我的应用程序中检测到附件断开连接。

      void checkForAccessory(){
              UsbAccessory[] deviceList = mUsbManager.getAccessoryList();
              if (deviceList != null){
                  // My app checks if the device has already been initialized or if it needs to be initialized.
              } else {
                  // Perform actions associated with a disconnect
              }
          }
      

      只要经常运行checkForAccessory(),您就会得到相当最新的附件断开检测。

      【讨论】:

        【解决方案3】:

        您需要 ACTION_USB_ACCESSORY_DETACHED,它从 API 级别 12 开始就存在。

        只需为它注册一个BroadcastReceiver,如下所示:

        val detachedFilter = IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED)
        context.registerReceiver(usbReceiver, detachedFilter)
        

        usbReceiver 看起来与此类似:

        object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val action = intent.action
                if (action == UsbManager.ACTION_USB_ACCESSORY_DETACHED) {
                    // Do something about it
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-14
          • 1970-01-01
          • 2014-07-17
          • 1970-01-01
          • 1970-01-01
          • 2022-06-15
          相关资源
          最近更新 更多