@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;
}
}