我有一个usb设备,型号是GP-3120TN,productName是Gprinter USB打印机。我用otg和usb-serial线将它插入Android手机(API 19)。现在我想得到像GP-3120TN这样的设备型号。我在UsbDevice或UsbDeviceConnection中找不到该字段。我只能通过下面的代码获取productName
`
usbDeviceConnection.claimInterface(usbInterface, true);
byte[] rawDescs = usbDeviceConnection.getRawDescriptors();
String manufacturer, product;
byte[] buffer = new byte[255];
int idxMan = rawDescs[14];
int idxPrd = rawDescs[15];
try {
int rdo = usbDeviceConnection.controlTransfer(UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_STANDARD, STD_USB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | idxPrd, 0, buffer, 0xFF, 0);
product = new String(buffer, 2, rdo - 2, "UTF-16LE");
DeviceModel deviceModel = new DeviceModel(serialNumber, product, manufacturer, usbDevice, usbInterface);
Log.e(TAG, "deviceModel: " + deviceModel.toString());
String productBase64 = Base64.encodeToString(buffer, 2, rdo - 2, Base64.NO_WRAP);
deviceModel.productNameBase64 = productBase64;
deviceModelList.add(deviceModel);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (usbDeviceConnection != null) {
//usbDeviceConnection.releaseInterface(usbInterface);
usbDeviceConnection.close();
}
}`
我可以通过哪种方式获得设备型号?模型是否写入硬件?我需要设备型号才能知道usb打印机是哪种?
您可以尝试使用以下代码来获取连接的USB设备信息:
public void getDeviceInfo() {
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 id = device.getDeviceId();
int vendor = device.getVendorId();
int product = device.getProductId();
int class = device.getDeviceClass();
int subclass = device.getDeviceSubclass();
}}
编辑:
只有上面的信息可以使用UsbDevice
获得,但它无法检测连接的USB设备的商业名称。