- Common ways to obtain UsbDevice
private void myMethod () {UsbDevice u =
Intent intent;String name;intent.getParcelableExtra(name)
Intent intent;String name;(UsbDevice) intent.getParcelableExtra(name)
- Smart code suggestions by Codota
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static String getName(UsbDevice device){ return Utils.hasLollipop() ? device.getManufacturerName() : nameForClass(device); }
/** * Get UsbDevice's manufacturer name * * @param usbDevice the UsbDevice * @param usbDeviceConnection the UsbDeviceConnection * @return the manufacturer name */ @SuppressLint("NewApi") @Nullable public static String getManufacturerName(@NonNull UsbDevice usbDevice, @NonNull UsbDeviceConnection usbDeviceConnection) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return usbDevice.getManufacturerName(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { byte[] rawDescriptors = usbDeviceConnection.getRawDescriptors(); try { byte[] buffer = new byte[255]; int indexOfManufacturerName = rawDescriptors[14] & 0xff; int manufacturerNameLength = usbDeviceConnection.controlTransfer(UsbConstants.USB_DIR_IN, USB_REQUEST_GET_DESCRIPTOR, (USB_DATA_TYPE_STRING << 8) | indexOfManufacturerName, 0, buffer, 255, 0); return new String(buffer, 2, manufacturerNameLength - 2, "UTF-16LE"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return null; } }
viewHolder.getReportedVendor().setText(device.getManufacturerName()); viewHolder.getReportedProduct().setText(device.getProductName()); } else { loadAsyncData(viewHolder, vid, pid, device.getManufacturerName()); } else { loadAsyncData(viewHolder, vid, pid, null);