Android: BroadcastReceiver when inserting / removing a device USB plug to / from a USB port

My application does not capture "ACTION_USB_DEVICE_ATTACHED", but "ACTION_USB_DEVICE_DETACHED" is working fine. The application starts correctly if I plug in a USB device, but disconnect while BroadcastReceiver right catch "ACTION_USB_DEVICE_DETACHED" is executed. If I join BroadcastReceiver again I won’t catch anything.

  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"); } } 

And the manifest:

 <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> 

Device filter (I check the vendor and product identifier):

 <resources> <!-- 0x0403 / 0x6001: FTDI FT232R UART --> <usb-device vendor-id="1027" product-id="24577" /> <!-- FT232RL --> </resources> 
+4
source share
5 answers

Everything works as described above, except for minor changes! In my research, singleTask in the application tag did not help, but it helped when I put it in the activity tag

 <activity ... android:launchMode="singleTask"> 

He provided me ANDROID to send onNewIntent for activity. And on onNewIntent, I just needed to get the device from intent, without any permission requests:

 @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 
+8
source

add this permission also :)

 <uses-feature android:name="android.hardware.usb.host" /> <uses-permission android:name="android.permission.USB_PERMISSION" /> 
+2
source

Also try adding <application android:launchMode="singleTask"> . This works for me with USB_ACCESSORY_ATTACHED , may work for you USB_ACCESSORY_ATTACHED .

+2
source

I managed to solve this problem:

  • Add android:launchMode="singleTask" to Launcher Activity
  • Add this intent filter to your Launcher activity.

     <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> 
  • Add metadata to your Launcher activity

     <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> 
  • Override the OnNewIntent method in your Launcher activity

     protected void onNewIntent(Intent intent) { if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)){ getPermission(); } }; 
    1. Now, if you disconnect β†’ reconnect the USB device, the onNewIntent() method will be called ..
+1
source

Android Reference for Activity says to use onNewIntent ():

"This is called for actions that set launchMode to" singleTop "in their package, or if the client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity (intent)"

So, launchMode singleTop == singleTask?

0
source

All Articles