博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android获取USB设备的名称
阅读量:5024 次
发布时间:2019-06-12

本文共 2693 字,大约阅读时间需要 8 分钟。

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

转载于:https://www.cnblogs.com/deityde1127/p/4122705.html

你可能感兴趣的文章
rxjs一句话描述一个操作符(1)
查看>>
第一次独立上手多线程高并发的项目的心路历程
查看>>
ServiceStack 介绍
查看>>
Centos7下载和安装教程
查看>>
无谓的通宵加班之后的思索
查看>>
S1的小成果:MyKTV系统
查看>>
从setting文件导包
查看>>
编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成...
查看>>
union和union all
查看>>
Github 开源:使用控制器操作 WinForm/WPF 控件( Sheng.Winform.Controls.Controller)
查看>>
PMD使用提醒
查看>>
Codeforces 887D Ratings and Reality Shows
查看>>
论文《A Generative Entity-Mention Model for Linking Entities with Knowledge Base》
查看>>
CentOS 6.7编译安装PHP 5.6
查看>>
Linux记录-salt分析
查看>>
Android Studio默认快捷键
查看>>
发布开源库到JCenter所遇到的一些问题记录
查看>>
第七周作业
查看>>
函数式编程与参数
查看>>
flush caches
查看>>