Search code examples
androidprintingusbusbserial

How can I get the usb device model through usb serial?


I have a usb device which model is GP-3120TN, productName is Gprinter USB Printer. I plug it with otg and usb-serial line to Android phone(API 19). Now I want to get the device model like GP-3120TN. I can't find the field in UsbDevice or UsbDeviceConnection. I can only get the productName through below code

`
        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();
            }
        }`

In which way can I get the device model? Does the model is written to the hardware? I need the device model to know which kind the usb-printer is?


Solution

  • You can try to use below code to get connected USB device information:

    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();
    
    }}
    

    Edit:

    Only above information can be obtained using UsbDevice but it can not detect commercial name of attached USB device.