0

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?

yongle zhu
  • 73
  • 4

1 Answers1

1

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.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • This way I can get the some of the device info, but I can't get the device model identifier. For example, when I use a EPSON's printer, I want to get the model like " LW-600P" from the device info. – yongle zhu Sep 12 '18 at 02:11
  • @mingrenzhu if this is not helping then I am afraid that you can find the model name. Looks not possible. – Vikasdeep Singh Sep 12 '18 at 02:17
  • I also think this. I have downloaded some apps about usb-device-info from google play. They can only provide the above info too. – yongle zhu Sep 16 '18 at 15:30