我的应用程序没有捕获“ACTION_USB_DEVICE_ATTACHED”,但“ACTION_USB_DEVICE_DETACHED”工作正常。如果我连接 USB 设备但我在执行过程中断开连接BroadcastReceiver
正确捕获“ACTION_USB_DEVICE_DETACHED”,应用程序将正确启动。如果我再次附加,BroadcastReceiver
则什么也抓不到。
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
rocrailService.registerReceiver(mUsbReceiver, filter);
// BroadcastReceiver when insert/remove the device USB plug into/from a USB port
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("BroadcastReceiver Event");
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
mSerial.usbAttached(intent);
mSerial.begin(mBaudrate);
loadDefaultSettingValues();
Run=true;
start();
System.out.println("BroadcastReceiver USB Connected");
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
mSerial.usbDetached(intent);
mSerial.end();
Run=false;
System.out.println("BroadcastReceiver USB Disconnected");
}
}
并体现:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.DCCWLocoDisplay"
android:versionCode="0"
android:versionName="0" >
<uses-sdk android:targetSdkVersion="12"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:icon="@drawable/dccw"
android:label="@string/app_name" >
<activity android:icon="@drawable/cab_64" android:name="net.DCCWLocoDisplay.activities.ActRCCab" android:theme="@style/FullHeightMainDialog" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
<activity android:name="net.DCCWLocoDisplay.activities.ActPreferences" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActAccessory" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActAbout" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActProgramming" android:theme="@style/FullHeightDialog" />
<activity android:name="net.DCCWLocoDisplay.activities.ActSteps" android:theme="@style/FullHeightDialog" />
<service android:name="net.DCCWLocoDisplay.DCCWLocoDisplay"/>
</application>
</manifest>
设备过滤器(我检查供应商和产品 ID):
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" /> <!-- FT232RL -->
</resources>