Android : how to detect already connected usb device?
Try this:
- First register Broadcast for USB connection.
manifest permission:
:
<intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter>
Get the List of USB Device with details by using this
public void getDetail() {
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
manager.requestPermission(device, mPermissionIntent);
String Model = device.getDeviceName();
int DeviceID = device.getDeviceId();
int Vendor = device.getVendorId();
int Product = device.getProductId();
int Class = device.getDeviceClass();
int Subclass = device.getDeviceSubclass();
}}
Detect connected USB device
You should use a USB_DEVICE_ATTACHED intent in your manifest. To get your app automatically started when the device is attached you should also specify a device_filter.xml
<activity ...>
...
<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>
You can find more instructions here
Android: Detecting USB
Some people suggested using UMS_CONNECTED
which is deprecated as of recent version of Android
The other problem with that is that it does not work with MTP enabled devices
Others suggested the use of the BatteryManager
, more precisely ACTION_BATTERY_CHANGED
as well as BATTERY_PLUGGED_AC
and BATTERY_PLUGGED_USB
This is perfect if you want to detect the Battery or Charging status of the device, but is not a really good indicator of a USB connection.
Using the battery manager is prone to failure on older android tablets such as the XOOM, the ICONIA tab A510, and the older Asus tablets.
To purely detect that the device was plugged on a PC you can:
Use android.hardware.usb.action.USB_STATE
and connected
in place of the BatteryManager
stuff
Code sample
public static boolean isConnected(Context context) {
intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
return intent.getExtras().getBoolean("connected");
}
Hope this helps
Android: Detect USB flash drive plugged in
Android, at the SDK level, has no concept of USB drives. There are no rules for where they should be mounted, broadcasts for when they appear/disappear, etc. Perhaps some standardization in this area will come in future Android releases, but it is not there today.
detecting usb devices android
Edit2:
trying to connect a samsung hard keyboard to a samsung tablet
If I am not mistaken: Problem is that a keyboard is not an USB Acessory and should therefore not trigger the USB_ACCESSORY_ATTACHED part.
forget the part below :)
try replacing
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
//final String ACTION_USB_PERMISSION ="com.android.example.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
with
IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED");
registerReceiver(mUsbReceiver, filter);
and remove
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
from your AndroidManifest.xml
You are confusing permissions with broadcast intents.
Edit: Uhm likely this is completely wrong: http://developer.android.com/guide/topics/usb/accessory.html does what you do.
Related Topics
How to Run an Android App in Background
App Not Run in Visual Studio Emulator for Android in Visual Studio 2015
The Import Com.Google.Android.Gms Cannot Be Resolved
How to Include a Username When Storing Email and Password Using Firebase (Baas) in an Android App
Manifest Merger Failed Targeting Android 12
Android: How to Check If the Server Is Available
Android:Maximum Allowed Width & Height of Bitmap
Ongloballayoutlistener: Deprecation and Compatibility
My Current Location Always Returns Null. How to Fix This
Avoid Reloading Activity with Asynctask on Orientation Change in Android
How to Change the Package Name of an Android App on Google Play
Android Kitkat Securityexception When Trying to Read from Mediastore
Android Share Images from Assets Folder
Best Practices: Layouts on Android (Programmatic VS Xml)