我最近在编写一个USB host应用程序,但是它被卡住了,因为我检测不到设备的附加/分离事件,我遵循了http://developer.android.com/guide/topics/connectivity/usb/host.html的编码说明,并参考了网络中其他人的编码,经过几次检查,我仍然找不到问题。经过我的调试,UsbManager.ACTION_USB_DEVICE_ATTACHED/UsbManager.ACTION_USB_DEVICE_DETACHED意图似乎没有发生,因为我尝试使用Context.sendBroadcast()发送一个定制的意图,而我的BroadcastReceiver可以接收该意图。但是当我连接/拆卸USB设备时,BroadcastReceiver不能运行。我使用的手机是HTC One-X,我确信OTG功能是正确的,因为鼠标功能工作得很好。这是我的代码片段。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
if(mUsbManager == null) {
Log.d(TAG, "mUsbManager is null");
}
// listen for new devices
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
// filter.addAction("MyTest");
registerReceiver(mUsbReceiver, filter);
}The BroadcastReceiver
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "mUsbReceiver.onReceive start");
String action = intent.getAction();
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
setDevice(device);
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
if (mDevice != null && mDevice.equals(device)) {
setDevice(null);
}
}
}
};Manifest.xml
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-sdk android:minSdkVersion="12" />
<application>
<activity android:name=".USBActivity"
android:label="USBActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- <receiver android:name=".mUsbReceiver"> -->
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
android:resource="@xml/device_filter" />
<!-- </receiver> -->
</activity>
</application>
device_filter在res/xml中,所有3个设置都已尝试且未使用:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- iCooby mouse -->
<!-- <usb-device vendor-id="15d9" , /> -->
<!-- <usb-device vendor-id="5593" product-id="2637"/> -->
<usb-device />
</resources>如果有人知道发生了什么?或者告诉我如何检测广播意图是否有效,非常感谢。
发布于 2013-10-15 17:00:40
可能有点晚了,但它可能会对其他人有所帮助。刚刚解决了检测USB设备插入的类似问题。事实证明--因为你在清单中指定了一个意图过滤器--当插入某些东西时,安卓会调用onResume。您可以尝试添加以下内容:
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null) {
Log.d("onResume", "intent: " + intent.toString());
if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
// Do your thing ...
}然后,您也不需要在onCreate()中调用registerReceiver()。还请记住,意图过滤器中的ID是十进制的。因此,您必须按照'lsusb‘之类的命令行工具提供的值进行转换。
发布于 2013-08-02 19:37:58
接收器标签被注释掉了,我猜你知道这一点,但只是以防万一。此外,它还应声明为<receiver android:name="mUsbReceiver">,您的应用程序具有‘’。它不需要出现在那里
https://stackoverflow.com/questions/18015656
复制相似问题