zoukankan      html  css  js  c++  java
  • android获取USB设备的名称

    1.注释内 。是三星设备可能不支持,需要更换的代码。

    2.mUsbManager。是getSystemService(Context.USB_SERVICE)获的。

    3. 从stackoverflow摘过来的。源地址找不到咧。

    protected static final int STD_USB_REQUEST_GET_DESCRIPTOR = 0x06;
            // http://libusb.sourceforge.net/api-1.0/group__desc.html
            protected static final int LIBUSB_DT_STRING = 0x03;

        public String getUSBName() {
            String strusbName = null;
            HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
            if (deviceList.size() == 0) {
                return strusbName;
            }
            
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

            if (deviceIterator.hasNext()) {
                UsbDevice device = (UsbDevice) deviceIterator.next();
                strusbName = device.getDeviceName();
                
                Log.d("", "Name: " + device.getDeviceName()+" "
                         + "VID: " + device.getVendorId()
                                    + "       PID: " + device.getProductId());
                
            UsbInterface intf = device.getInterface(0);
            int epc = 0;
            epc = intf.getEndpointCount();
            Log.d("","Endpoints:" + epc + " ");

            Log.d("","Permission:" + Boolean.toString(mUsbManager.hasPermission(device))  + " ");

            UsbDeviceConnection connection = mUsbManager.openDevice(device);
            if(null==connection){
                Log.d("","(unable to establish connection) ");
            } else {

                // Claims exclusive access to a UsbInterface.
                // This must be done before sending or receiving data on
                // any UsbEndpoints belonging to the interface.
                connection.claimInterface(intf, true);

                // getRawDescriptors can be used to access descriptors
                // not supported directly via the higher level APIs,
                // like getting the manufacturer and product names.
                // because it returns bytes, you can get a variety of
                // different data types.
                byte[] rawDescs = connection.getRawDescriptors();
                String manufacturer = "", product = "";

                try
                {
                    byte[] buffer = new byte[255];
                    int idxMan = rawDescs[14];
                    int idxPrd = rawDescs[15];

                    int rdo = connection.controlTransfer(UsbConstants.USB_DIR_IN
                            | UsbConstants.USB_TYPE_STANDARD, STD_USB_REQUEST_GET_DESCRIPTOR,
                            (LIBUSB_DT_STRING << 8) | idxMan, 0, buffer, 0xFF, 0);
                    manufacturer = new String(buffer, 2, rdo - 2, "UTF-16LE");

                    rdo = connection.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");
                    
    /*                int rdo = connection.controlTransfer(UsbConstants.USB_DIR_IN
                            | UsbConstants.USB_TYPE_STANDARD, STD_USB_REQUEST_GET_DESCRIPTOR,
                            (LIBUSB_DT_STRING << 8) | idxMan, 0x0409, buffer, 0xFF, 0);*/

                } catch (UnsupportedEncodingException e)
                {
                e.printStackTrace();
                }

                Log.d("","Manufacturer:" + manufacturer + " ");                      
                Log.d("","Product:" + product + " ");                        
                Log.d("","Serial#:" + connection.getSerial() + " ");                     
            }
            
            }
            return strusbName;
        }

  • 相关阅读:
    前端代码美化的艺术
    CSS3 简单的砸金蛋样式
    为什么 VS Code 能迅速占领 JavaScript 开发者社区
    了解并使用 CSS 中的 rem 单位
    CSS中一些利用伪类、伪元素和相邻元素选择器的技巧
    SVG入门指南
    PAT 1035 Password [字符串][简单]
    生信学习-二代测序知乎专栏总结[转]
    PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]
    生信笔记-mooc【武大】
  • 原文地址:https://www.cnblogs.com/deityde1127/p/4122705.html
Copyright © 2011-2022 走看看