I\'m trying to detect usb devices which are already connected to android. I understand there are actions to detect when USB is either attached or detached. But I don\'t rea
Try this:
:
<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();
}}
The answer from "Christopher Garza" is probably in Kotlin. I'll send you the code but in Java.
String actionString = getApplicationContext().getPackageName() + ".action.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new
Intent(actionString), 0);
You can remove "getApplicationContext()" if you are pasting this code in an Activity. Otherwise if you are pasting this in other class not specified with an Activity, then you need some sort of this code:
public class Example {
private Context context;
public Example(Context context) {
this.context = context;
}
}
Just to answer Benny's question here is what the mPermissionIntent could look like:
string actionString = context.PackageName + ".action.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(context, 0, new
Intent(actionString), 0);
mUsbManager.RequestPermission(device, permissionIntent);