/**
* 检查iC 读卡器usb 权限
*/
private void checkICUsbPermissson() {
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// 获取一个已连接的USB设备,并且包含方法,以访问其标识信息、 接口和端点
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
if (deviceList.size() == 0) {
LogUtil.e("--未找到IC读卡器");
ToastUtil.show(this, "--未找到IC读卡器");
return;
}
// 获取deviceList迭代器
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
// 判断迭代器中是否有元素
while (deviceIterator.hasNext()) {
// 如果有,获取元素
UsbDevice usbDevice = deviceIterator.next();
//product-id="1638" vendor-id="1137
if (!(usbDevice.getProductId() == 1638 && usbDevice.getVendorId() == 1137)) {
// if (!ReaderAndroidUsb.isSupported(usbDevice)) {
Log.e("lsw", "is not Supported" + usbDevice.getDeviceName() + " -- " + usbDevice.getDeviceName());
continue;
}
Log.e("lsw", " Supported" + usbDevice.getDeviceName());
// 判断是否拥有该设备的连接权限
if (!manager.hasPermission(usbDevice)) {
// 如果没有则请求权限
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0,
new Intent(Device_USB), PendingIntent.FLAG_UPDATE_CURRENT);
/*
* 展示征求用户同意连接这个设备的权限的对话框。 当用户回应这个对话框时,
* 广播接收器就会收到一个包含用一个boolean值来表示结果的EXTRA_PERMISSION_GRANTED字段的意图。
* 在连接设备之前检查这个字段的值是否为true和设备之间的“交流”
*/
manager.requestPermission(usbDevice, mPermissionIntent);
LogUtil.e(" -- not hasPermission IC -- ");
handler.postDelayed(new Runnable() {
@Override
public void run() {
start2XICDevice();
}
}, 1000);
} else {
LogUtil.e(" -- hasPermission IC ");
start2XICDevice();
}
}
}
需要和硬件厂商沟通 ,知道硬件的 pid 和vid 如: product-id="1638" vendor-id="1137
private String Device_USB = "com.android.example.USB";