1.说在前面的话
这次做了一个项目,要求android端与外接设备进行通信,用到的是otg线连接开发板。然而网上有关USB host通信的资料很少,这里我就把自己的想法以及遇到的坑说一下吧()。
2.Android Studio 配置
2.1Manifest配置
<!--android作为host端的权限-->
<uses-feature android:name="android.hardware.usb.host" android:required="true" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/usbfilter">
</meta-data>
</activity>
这里的action是外接设备被检测的动作,下面的usbfilter是过滤文件,里面存放外接设备的vendor-id和product-id。
2.2Usbfilter配置
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<usb-device vendor-id="1234" product-id="1234" />
</resources>
配置这个主要为了过滤出你的设备,然后再android端会提示你打开相应的app。这两个id可以在电脑上面获取,将你的设备利用usb线插入电脑,然后再设备管理器里面找到你的设备,右键属性,在详细信息