package com.hisign.android.test.usb;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.hisign.android.test.R;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class UsbMainActivity extends Activity {
private final static String TAG = "lixm_UsbMainActivity";
private UsbManager mUsbManager;
private final static String ACTION_DEVICE_PERMISSION = "ACTION_DEVICE_PERMISSION";
private Map<String,UsbEndpoint> mUsbEndpointOutMap;
private Map<String,UsbEndpoint> mUsbEndpointInMap;
private Map<String,UsbDeviceConnection> mUsbDeviceConnectionMap;
private UsbDevice mTargetDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_usb_main);
IntentFilter filter = new IntentFilter(); //注册USB设备插入,拔出动作
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(deviceChangedReceiver, filter);
mUsbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
initMaps();
}
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)) {
}
}
};
/**
* 根据设备的PID和VID判断是否是目标设备
* @param device
* @return
*/
private boolean isTargetDevice(UsbDevice device){
boolean isTargetDevice = false;
if(device != null){
if(1024 == device.getVendorId()
&& 50010 ==device.getProductId()){
isTargetDevice = true;
}
}
return isTargetDevice;
}
/**
* 获取目标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);
Log.d("lixm","searchDevice() hasPermission = " + hasPermission + ", mTargetDevice = " + 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);
}
}
private void initMaps(){
if(mUsbEndpointOutMap == null){
mUsbEndpointOutMap = new HashMap<>();
}else{
mUsbEndpointOutMap.clear();
}
if(mUsbEndpointInMap == null){
mUsbEndpointInMap = new HashMap<>();
}else{
mUsbEndpointInMap.clear();
}
if(mUsbDeviceConnectionMap == null){
mUsbDeviceConnectionMap = new HashMap<>();
}else{
mUsbEndpointInMap.clear();
}
}
/**
* 发送数据到USB设备
*
* @param view
*/
public void sendData2UsbDevice(View view) {
if(mTargetDevice != null){
readContent(mTargetDevice.getDeviceName());
}else{
Log.d("lixm","sendData2UsbDevice(), mTargetDevice = " + mTargetDevice);
}
}
/**
* 授权结果的监听
*/
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{
}
}
}
};
/**
* 获取UsbInterface以及对应得UsbEndpoints,UsbDeviceConnection
* @param device
*/
private void usbDeviceInit(UsbDevice device) {
mTargetDevice = device;
int interfaceCount = device.getInterfaceCount();
UsbInterface usbInterface = null;
for (int i = 0; i < interfaceCount; i++) {
usbInterface = device.getInterface(i);
int interfaceClass = usbInterface.getInterfaceClass();
Log.d("lixm","usbInterface.getInterfaceClass() = " + interfaceClass);
if (interfaceClass == UsbConstants.USB_CLASS_CSCID) {
break;
}
}
if (usbInterface != null) { //获取UsbDeviceConnection
UsbDeviceConnection connection = mUsbManager.openDevice(device);
mUsbDeviceConnectionMap.put(device.getDeviceName(),connection);
if (connection != null) {
if (connection.claimInterface(usbInterface, true)) {
for (int j = 0; j < usbInterface.getEndpointCount(); j++) {
UsbEndpoint endpoint = usbInterface.getEndpoint(j); //类型为大块传输
if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
mUsbEndpointOutMap.put(device.getDeviceName(), endpoint);
} else {
mUsbEndpointInMap.put(device.getDeviceName(), endpoint);
}
}
}
}
}
}
}
/**
* 读取身份证信息
* @param deviceName
* @return
*/
public int readContent(String deviceName) {
Log.d("lixm","readContent start , deviceName = " + deviceName);
byte[] recvbuffer = new byte[65536