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

时间: 2024-10-12 18:07:46

android获取USB设备的名称的相关文章

C# 获取USB设备信息

WMI方式 using System; using System.Management; using System.Text.RegularExpressions; using System.Collections.Generic; namespace Splash.IO.PORTS { /// <summary> /// 即插即用设备信息结构 /// </summary> public struct PnPEntityInfo { public String PNPDeviceI

windows平台下获取USB设备&ldquo;身份证&rdquo;

当我们将U盘插入windows主机时,会提示安装驱动,那么windows是如何判断应该安装哪个版本的驱动的呢?在这里将用到设备的身份信息,包括厂商.型号.序列号等.大概半年前做了一个关于设备检测的小工程,看了很多资料和帖子,将获取设备身份信息的方法在此记录,方便以后使用. 0x01 USB设备有哪些身份信息 根据USB规范中的说明USB规范官方文档,供应商ID(VID)/产品识别码(PID)唯一标识一个设备,VID和PID都是两个字节长,其中,供应商ID(VID)由供应商向USB执行论坛申请,每

android 获取手机设备品牌

在有些数据要获取手机设备是什么品牌,特别做一些适配的时候,好了就讲下怎样或者手机是什么品牌: String brand =android.os.Build.BRAND; 就这么简单!

Android 获取手机设备等的信息

获取手机设备型号等信息: 如图华为P6手机获取是手机设备信息值: 代码如下: tvStr = (TextView) findViewById(R.id.tv_titlebar); String phoneInfo = "Product: " + android.os.Build.PRODUCT + "\n"; phoneInfo += "CPU_ABI: " + android.os.Build.CPU_ABI + "\n";

Android获取当前设备屏幕信息(分辨率,密度以及物理尺寸)

要想知道屏幕的基本信息,就要先获取一个基本的类(DisplayMetics),这个对象存放着当前的窗口的一些通用信息,如显示大小,分辨率和字体等等. 如何获取DisplayMetics对象: DisplayMetrics display= new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(display);//将当前窗口的一些信息放在DisplayMetrics类中, 然后就可以通过dispalyMet

获取usb设备父系或子代

/// <summary> /// 获取设备父系 /// </summary> /// <param name="driver"></param> /// <returns></returns> private static bool TryGetDriverIdParent(string driver, out string resultDeviceID) { resultDeviceID = "&qu

android -------- 获取手机设备信息

最近在开发中,需要用到一些系统信息,总结了一下 /** * Created by zhangqie on 2019/2/26 * Describe: 系统工具类 */ public class EquipmentUtil { /** * 获取当前手机系统语言. * * @return 返回当前系统语言.例如:当前设置的是“中文-中国”,则返回“zh-CN” */ public static String getSystemLanguage() { return Locale.getDefault

Android获取各种设备的状态

WIFI 获取WIFI状态 WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); if(wifiManager != null){ int wifiState = wifiManager.getWifiState(); } 附:Wifi状态有以下几种:(括号内为所对应的的int值) 1. wifiManager.WIFI_STATE_DISABLED (1) 2. wifiMa

获取显示设备的名称及PNPDeviceID

实现效果: 知识运用: ManagementObjectSearcher类和ManagementObject类 实现代码: private void button1_Click(object sender, EventArgs e) { ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController"); foreach (ManagementOb