android usb设备列表,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 deviceList = mUsbManager.getDeviceList();

if (deviceList.size() == 0) {

return strusbName;

}

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

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值