一、USB的通信流程
搜索设备—>建立连接—>通信 , 全部代码请 “戳这”
1、搜索设备
/**
* 获取目标UsbDevice
* @param view
*/
public void searchUsbDevice(View view) {
HashMap<String, UsbDevice> deviceMap = mUsbManager.getDeviceList();
Iterator<UsbDevice> iterator = deviceMap.values().iterator();
while (iterator.hasNext()) {
UsbDevice device = iterator.next();
boolean isTargetDevice = isTargetDevice(device);
Log.d(TAG,"searchUsbDevice(), deviceName = " + device.getDeviceName()
+ ", " + device.getVendorId() + ", " + device.getProductId() + ", isTargetDevice = " + isTargetDevice);
if (isTargetDevice) {
mTargetDevice = device;
break;
}
}
//申请USB使用权限
//获取到UsbDevice后需要检查该UsbDevice是否已经获取到使用权限。如果没有权限则要申请USB使用权限
boolean hasPermission = mUsbManager.hasPermission(mTargetDevice);
if (!hasPermission) {
IntentFilter filter = new IntentFilter(ACTION_DEVICE_PERMISSION);
this.registerReceiver(mUsbPermissionReceiver, filter);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_DEVICE_PERMISSION), 0);
mUsbManager.requestPermission(mTargetDevice, pendingIntent); // 申请获取权限
}
if(mTargetDevice != null){
usbDeviceInit(mTargetDevice);
}
}
2、注册USB设备插入,拔出动作
BroadcastReceiver deviceChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (!isTargetDevice(device)) {
return;
}
String action = intent.getAction();
if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
usbDeviceInit(device);
} else if (action.equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
}
}
};
3、权限监听的广播
/**
* 授权结果的监听
*/
private BroadcastReceiver mUsbPermissionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_DEVICE_PERMISSION.equals(action)) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (granted) { //获得了usb使用权限
usbDeviceInit(device);
}else{
}
}
}
};
4、在res下面建立一个xml文件夹,里面放入USB设备的过滤文件(usb_device_filter.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device
product-id="50010"
vendor-id="1024" />
</resources>
5、在AndroidManifest.xml中的声明
<uses-feature android:name="android.hardware.usb.host"/>
二、通过USB读卡器读取身份证信息
/**
* 读取身份证信息
* @param deviceName
* @return
*/
public int readContent(String deviceName) {
Log.d("lixm","readContent start , deviceName = " + deviceName);
byte[] recvbuffer = new byte[65536];
int ret = -1;
byte[] cmd_init = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x12, (byte) 0xFF, (byte) 0xEE};
byte[] cmd_find = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x22};
byte[] cmd_selt = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x20, 0x02, 0x21};
byte[] cmd_readAll = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x30, 0x10, 0x23};
UsbEndpoint mEndpointOut = mUsbEndpointOutMap.get(deviceName);
UsbDeviceConnection mConnection = mUsbDeviceConnectionMap.get(deviceName);
UsbEndpoint mEndpointIn = mUsbEndpointInMap.get(deviceName);
if (mConnection != null) {
mConnection.bulkTransfer(mEndpointOut, cmd_init, cmd_init.length, 2000);
int recvSize = mConnection.bulkTransfer(mEndpointIn, recvbuffer, 27, 2000);
Log.d(TAG, "bulkTransfer recv(bytes)_0: " + recvSize);
if (27 == recvSize) {
mConnection.bulkTransfer(mEndpointOut, cmd_find, cmd_find.length, 2000);
recvSize = mConnection.bulkTransfer(mEndpointIn, recvbuffer, 15, 2000);
Log.d(TAG, "bulkTransfer recv(bytes)_1: " + recvSize);
mConnection.bulkTransfer(mEndpointOut, cmd_selt, cmd_selt.length, 2000);
recvSize = mConnection.bulkTransfer(mEndpointIn, recvbuffer, 19, 2000);
Log.d(TAG, "bulkTransfer recv(bytes)_2: " + recvSize);
mConnection.bulkTransfer(mEndpointOut, cmd_readAll, cmd_readAll.length, 2000);
recvSize = mConnection.bulkTransfer(mEndpointIn, recvbuffer, 2321, 2000);
Log.d(TAG, "bulkTransfer recv(bytes)_3: " + recvSize);
if (1297 == recvSize || 2321 == recvSize) { // 读到数据,带指纹信息和不带指纹信息
parseData(recvbuffer, recvSize); // 解析数据
}
}
}
return ret;
}
/**
* 解析身份证信息
* @param recevBuffer
* @param length
*/
private void parseData(byte[] recevBuffer,int length){
if (length == 1290 + 7 || 2321 == length) {
for (int i = 14; i < 1295; i++)
recevBuffer[i] = recevBuffer[i + 2];
// 指纹信息
fingerInfo[0] = 0;
fingerInfo[512] = 0;
if (2321 == length) {
for (int i = 0; i < 1024; i++)
fingerInfo[i] = recevBuffer[1296 + i];
}
}
for (int i = 0; i < 256; i++)
persionInfo[i] = recevBuffer[i + 14];
parsePersonInfo(persionInfo);
}
private void parsePersonInfo(byte[]personInfo){
try {
String TmpStr = new String( personInfo,"UTF16-LE");
TmpStr = new String(TmpStr.getBytes("UTF-8"));
decodeInfo[0] = TmpStr.substring(0, 15); //姓名
decodeInfo[1] = TmpStr.substring(15, 16); //性别
decodeInfo[2] = TmpStr.substring(16, 18); //民族
decodeInfo[3] = TmpStr.substring(18, 26); //出生
decodeInfo[4] = TmpStr.substring(26, 61); //住址
decodeInfo[5] = TmpStr.substring(61, 79); //号码
decodeInfo[6] = TmpStr.substring(79, 94); //机关
decodeInfo[7] = TmpStr.substring(94, 102); //有效期起始
decodeInfo[8] = TmpStr.substring(102, 110); //有效期截止
decodeInfo[9] = TmpStr.substring(110, 128); //最新住址
if(decodeInfo[1].equals("1"))
decodeInfo[1] = "男";
else
decodeInfo[1] = "女";
try{
int code =Integer.parseInt(decodeInfo[2].toString());
decodeInfo[2] = String.valueOf(code);
}catch(Exception e){
decodeInfo[2] = "";
}
for(String str:decodeInfo){
Log.d(TAG,"personInfo --> " + str);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}