当前位置: 首页 > 工具软件 > USBManager > 使用案例 >

android usb attached,android – 无法接收UsbManager.ACTION_USB_DEVICE_ATTACHED / UsbManager.ACTION_USB_DEV...

欧阳骏俊
2023-12-01

我最近正在编写一个USB主机应用程序,但由于我无法检测到设备连接/分离事件,因为我无法检测到设备附加/分离事件,我按照

http://developer.android.com/guide/topics/connectivity/usb/host.html的编码说明并参考网络中的其他编码,经过多次检查后,我仍然可以’找不到问题.在调试之后,似乎没有发生UsbManager.ACTION_USB_DEVICE_ATTACHED / UsbManager.ACTION_USB_DEVICE_DETACHED意图,因为我尝试使用Context.sendBroadcast()发送自定义的Intent,而我的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);

}

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

android:label="USBActivity"

android:screenOrientation="portrait">

android:resource="@xml/device_filter" />

android:resource="@xml/device_filter" />

res / xml中的device_filter,尝试了所有3个设置并且不使用:

如果有人知道发生了什么?或者告诉我如何检测广播意图是否有效,非常感谢.

 类似资料: