码迷,mamicode.com
首页 > 移动开发 > 详细

android获取USB设备的名称

时间:2014-11-26 13:53:52      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:des   android   http   io   ar   os   sp   for   on   

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()+"\n"
                     + "VID: " + device.getVendorId()
                                + "       PID: " + device.getProductId());
            
        UsbInterface intf = device.getInterface(0);
        int epc = 0;
        epc = intf.getEndpointCount();
        Log.d("","Endpoints:" + epc + "\n");

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

        UsbDeviceConnection connection = mUsbManager.openDevice(device);
        if(null==connection){
            Log.d("","(unable to establish connection)\n");
        } 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 + "\n");                      
            Log.d("","Product:" + product + "\n");                        
            Log.d("","Serial#:" + connection.getSerial() + "\n");                     
        }
        
        }
        return strusbName;
    }

android获取USB设备的名称

标签:des   android   http   io   ar   os   sp   for   on   

原文地址:http://www.cnblogs.com/deityde1127/p/4122705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!