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

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. android 如何用代码生成圆角Bitmap图片
  3. android的aidl进程间通讯(一)
  4. Android实战教程第四篇之简单实现短信发送器
  5. android 蓝牙hfp client实现简介
  6. Android(安卓)Studio 小技巧合集
  7. 【Android】Android清除本地数据缓存代码(这些功能很强大不要乱
  8. Android屏幕截图实现
  9. Android不同版本功能升级汇总

随机推荐

  1. 关于Android(安卓)的Log使用
  2. TabHost自定义标签页(二)
  3. Android 触摸屏手势识别资料汇总
  4. Android学习杂记(二):异步任务AsyncTask详解
  5. Android Api WindowManager.LayoutParams
  6. FloatingActionButton
  7. The method Inflate() in android
  8. Android HAL实例解析
  9. Android基础篇——创建一个Android工程
  10. Android监控软键盘变化实例