Reputation: 137
How can I get the id (or some unique properties) of the USB devices connected to my Android device?
Upvotes: 2
Views: 4661
Reputation: 21361
You can use a combination of UsbDevice.getVendorId()
and UsbDevice.getProductId()
which is unique as long as the user doesn't connect the very same model of device twice, which should happen close to never.
Given that vendor ids are max. 4 hex digits you can left-shift the vendor ID by 16 bits and then add the product ID to get a model specific unique int
identifier. Kotlin code:
val device: UsbDevice = ...
int modelId = (device.vendorId shl 16) + device.productId
Upvotes: 0
Reputation: 484
For anyone new to UsbDevice
, it may be worth noting the use of UsbManager
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList();
Read more at http://developer.android.com/reference/android/hardware/usb/UsbManager.html
Upvotes: 4
Reputation: 2077
You can get the unique ID of an android connected USB device using UsbDevice.getDeviceId() method.
Please check the below links for more details. http://developer.android.com/reference/android/hardware/usb/UsbDevice.html#getDeviceId()
More than Id, Please check other methods of class 'UsbDevice'. http://developer.android.com/reference/android/hardware/usb/UsbDevice.html
Upvotes: 1