Android: BroadcastReceiver when insert/remove the device USB plug into/from a USB port
Solution 1
Everything works as was described above, except little changes! On my research singleTask in application tag didn't help, but helped when I put it in activity tag
<activity
...
android:launchMode="singleTask">
It provided me with ANDROID started to send onNewIntent to activity. And on onNewIntent I just had to get device from intent, with out any requests for permissions:
@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);
Solution 2
add this permission also :)
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
Solution 3
Also try adding in <application android:launchMode="singleTask">
. This works for me with USB_ACCESSORY_ATTACHED
, may work for you as well.
Solution 4
I was able to resolve this issue by:
- Add
android:launchMode="singleTask"
attribute in Launcher Activity Add this Intent Filter in 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(); } };
- Now if you will detach -> attach again a USB Device,
onNewIntent()
Method will be called..
- Now if you will detach -> attach again a USB Device,
Admin
Updated on September 30, 2020Comments
-
Admin over 3 years
My application don't catch "ACTION_USB_DEVICE_ATTACHED" but "ACTION_USB_DEVICE_DETACHED" work fine. Application start correctly if I connect usb device but I disconnect during execution
BroadcastReceiver
right catch "ACTION_USB_DEVICE_DETACHED". If I attach againBroadcastReceiver
don'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 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 vendor and product id):
<resources> <!-- 0x0403 / 0x6001: FTDI FT232R UART --> <usb-device vendor-id="1027" product-id="24577" /> <!-- FT232RL --> </resources>
-
user1480734 over 10 yearsThe answer is found here: link. In both cases there is only one instance created (with a little difference for 'singleTask'). So launchMode singleTop = singleTask in this case here...
-
Mohd Qasim over 3 yearswhere is device_filter file snippet