如何通过USB串口获取USB设备型号?

时间:2018-09-03 02:37:50

标签: android printing usb usbserial

我有一个型号为GP-3120TN的USB设备,产品名称为Gprinter USB打印机。我用otg和usb串行线将其插入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打印机是哪种类型?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用以下代码获取已连接的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设备的商业名称。