Tôi đang mã hóa ứng dụng Máy chủ lưu trữ USB gần đây, nhưng nó bị kẹt vì tôi không thể phát hiện sự kiện được gắn/tháo rời thiết bị, tôi đã theo dõi ghi chú mã hóa http://developer.android.com/guide/topics/connectivity/usb/host.html và tham khảo mã hóa của người khác trong mạng, Sau khi kiểm tra nhiều lần, tôi vẫn không thể tìm thấy vấn đề. Sau khi gỡ lỗi của tôi, có vẻ như mục đích UsbManager.ACTION_USB_DEVICE_ATTACHED/UsbManager.ACTION_USB_DEVICE_DETACHED không xảy ra, Bởi vì tôi cố gắng sử dụng Context.sendBroadcast() để gửi Intent tùy chỉnh và BroadcastReceiver của tôi có thể nhận được ý định. Nhưng khi tôi gắn/tháo thiết bị USB, BroadcastReceiver không chạy. Điện thoại di động tôi sử dụng là HTC One-X, tôi chắc chắn chức năng OTG là chính xác khi chức năng chuột hoạt động hoàn hảo. Đây là đoạn mã của tôi.Không thể nhận được mục đích phát sóng của UsbManager.ACTION_USB_DEVICE_ATTACHED/UsbManager.ACTION_USB_DEVICE_DETACHED
@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);
}
Các 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 trong res/xml, tất cả 3 thiết lập đang cố gắng và không sử dụng:
<?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>
Nếu ai đó biết điều gì đã xảy ra? hoặc cho tôi biết cách phát hiện xem mục đích phát có đang hoạt động hay không, cảm ơn rất nhiều.
Điều này đã giúp tôi: ** [Cách nhận trạng thái kết nối USB phát sóng?] (Http://stackoverflow.com/questions/39942453/how-to-receive-usb-connection-status-broadcast) ** –