最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android设备作为Host希望实现可以检测到USB设备插入

Android crifan 10708浏览 0评论

【背景】

需要在一个android的app中实现检测usb插入的事件。

【折腾过程】

1.参考:

Detect USB connection

看到这个:

android.intent.action.UMS_CONNECTED

2.去搜:

UMS_CONNECTED

找到官网的:

http://developer.android.com/reference/android/content/Intent.html

中的:

String
ACTION_UMS_CONNECTED
This constant was deprecated in API level 14. replaced by android.os.storage.StorageEventListener

3.所以再去找StorageEventListener,找到:

Class StorageEventListener

但是感觉有问题:

USB的class中,有很多种。

此处,我希望检测的是android设备作为HOST,然后外接USB设备:一个USB接口的HART猫,内部就是USB接口的串口,所以,不是这个MSC的类。

所以,感觉应该不是用这个“USB Mass Storage”才对。

4.再去继续找。

http://developer.android.com/guide/topics/connectivity/usb/index.html

->

http://developer.android.com/guide/topics/connectivity/usb/host.html

看到有:

android.hardware.usb.action.USB_DEVICE_ATTACHED
ACTION_USB_DEVICE_DETACHED

比较像是我要找的。

5.然后就是:

去看

http://developer.android.com/guide/topics/connectivity/usb/host.html

确保了解Android中USB的Host的逻辑。

6.关于“uses-feature”去学习和总结一下:

【整理】android程序中的AndroidManifest.xml中的uses-feature详解

然后对应的此处这里的android的app,需要硬件USB OTG以支持USB Host功能,所以写成:

<uses-feature android:required="true" android:name="android.hardware.usb.host" />

7.接着去学习USB知识。

看了:

http://developer.android.com/guide/topics/connectivity/usb/host.html

的解释才知道:

原来对于之前加的那个intent-filter和meta-data的话,是:

When users connect a device that matches your device filter, the system presents them with a dialog that asks if they want to start your application.

也就是:

我之前就看到的:

当插入USB,程序还没有运行,就可以看到:

系统检测到对应的USB插入了,然后系统提供的弹出的对话框,问是否打开此程序

但是:

此种效果,明显不是我所需要的。

我需要的是:

在程序已经运行的情况下,去:

动态检测,当前所希望的USB设备,是否插入。

所以,貌似需要去使用其所说的:

getDeviceList()

了。

8.参考:

detecting usb devices android

去找找:

android.hardware.usb.action

找到:

https://developer.android.com/reference/android/hardware/usb/UsbManager.html

中的:

public static final String ACTION_USB_DEVICE_ATTACHED

Added in API level 12

Broadcast Action: A broadcast for USB device attached event. This intent is sent when a USB device is attached to the USB bus when in host mode.

Constant Value: "android.hardware.usb.action.USB_DEVICE_ATTACHED"

public static final String ACTION_USB_DEVICE_DETACHED

Added in API level 12

Broadcast Action: A broadcast for USB device detached event. This intent is sent when a USB device is detached from the USB bus when in host mode.

Constant Value: "android.hardware.usb.action.USB_DEVICE_DETACHED"

9.再去搜:

android.hardware.usb.action.USB_DEVICE_ATTACHED

结果回到了:

https://developer.android.com/guide/topics/connectivity/usb/host.html

10.参考:

[Android] Joystick游戏手柄开发

需要去搞懂如何才能:

”注册一个广播接收“

11.参考:

android监听usb拔插事件

再去搜:

onReceive

找到:

https://developer.android.com/reference/android/content/BroadcastReceiver.html

和:

Android 中的BroadCastReceiver

12.搜:

registerReceiver

看到:

使用registerReceiver注册BroadcastReceiver

再去参考:

增加 addDataScheme("file") 才能收到SD卡插拔事件的原因分析 — 浅析android事件过滤策略

13.最后参考:

Detecting when a USB device is detached on Android

再去参考:

http://www.3dconnexion.com/forum/viewtopic.php?f=22&t=5339

14.后来差点解决了,结果遇到android的bug导致没解决:

【未解决】android程序运行时可以检测到ACTION_USB_DEVICE_DETACHED,但是检测不到ACTION_USB_DEVICE_ATTACHED

15.现在,只能再去想其他办法,解决这个动态监听检测USB设备插入的事件。

在此之前,先要去解决:

【已解决】ADT中通过TCPIP的WIFI网络调试Android设备

16.目前,能想到的是,通过另起一个进程,做这个事情,

所以,先去搞懂:

【记录】Android中创建进程或线程去实现USB设备插入的状态检测

17.上述已经实现了thread+handler去处理usb的检测了。

此处,后来看到:

Why does the thread run only once?

中的Timer的schedule的TimerTask,所以打算去试试:

【记录】android中尝试使用Timer的schedule的TimerTask去实现循环的USB设备插入动作的检测

 

【总结】

至此:

既可以用thread,也可以用timer的TimeTask去实现:

动态检测USB设备插入的动作。

都还是不错的。

转载请注明:在路上 » 【已解决】Android设备作为Host希望实现可以检测到USB设备插入

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 事实上,对于收到usb插入的广播时,此时usb并没有完成被驱动读取。开一个线程,sleep一会,再检测就可以了。但,很尴尬的是,attach这个广播会收到两次。我曹......
    一中6年前 (2017-12-22)回复
  2. 好文~调研的思路很清晰,赞一个
    song8年前 (2015-11-18)回复
86 queries in 0.165 seconds, using 22.11MB memory