做android 与USB HID设备的通信有段时间了,总结一下遇到的问题和解决方法:
1,第一次遇到的问题:android 版本低不支持usb hid,
被要求做相关项目的时候,就从mUsbManager.getDeviceList开始,郁闷了一周,就是返回NULL,从google上查找所有关于android usb的资料,最后才发现,android设备是2.3的不支持android.usb.hardware的包(android3.0之后才支持);
2,第二次:android的etc/permissions没有android.hardware.usb.host.xml权限。
发现版本低之后赶快去买了一个android4.0的设备来测试,发现还是返回NULL。还得从google寻找答案,这篇对我的帮助很大:http://stackoverflow.com/questions/11183792/android-usb-host-and-hidden-devices。最后把设备root了一下在系统文件etc/permissions里面加入权限后OK, mUsbManager.getDeviceList() !=null了。
3,获取需要连接的USB 设备权限
可以参考http://developer.android.com/guide/topics/connectivity/usb/host.html;
获取连接权限有两种方式,
在调试过程中就遇到方法一无法连接的问题,还是使用mUsbManager.requestPermission()解决
4,usb hid只能是中断传输,或者控制传输
这个问题纠结很久,因为根据android开发的api Guider使用connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);无论如何得不到数据。后来在stackoverflow上去翻所有关于android usb hid的文章结果发现说是需要中断传输。
UsbRequest request = new UsbRequest();
Boolean isOpenRequest = request.initialize(connection,
mUsbEndpoint);
if(isOpenRequest){
request.queue(buffer, buffer.length);
if(communication.waitRequest==request){
//do something like as receiver date
// 如何这时收到某个数据时需要向设备发送命令则需要使用
controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
}
}
关于controlTransfer的参数的说明可以参考:http://www.csdn123.com/html/blogs/20130508/10500.htm#6608049-tsina-1-89011-aeef9ead4b41d50c27ea4ebb8e597ac3
下面贴代码:
public class GetUsbDevice extends Activity{
private UsbManager mUsbManager;
public UsbDevice mUsbDevice;
public UsbInterface mUsbInterface;
private UsbEndpoint mUsbEndpoint;
public UsbDeviceConnection connection;
private ByteBuffer buffer;
public static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private static PendingIntent mPendingIntent;
public MyThread usbThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buffer = ByteBuffer.allocate(128);
mUsbManager=(UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
usbThread = new MyThread();
if(getTheTargetDevice!=null){
commWithUsbDevice();
``` }
}
/*
* 获取product id为1234的设备
*
*
* @param the target usb device product ID.
*/
public UsbDevice getTheTargetDevice() {
HashMap
Iterator
.iterator();
while (usbDeviceIterator.hasNext()) {
UsbDevice device = usbDeviceIterator.next();
if (“1234”.equals(device.getProductId() + "")) {
mUsbDevice = device;
mPendingIntent = PendingIntent.getBroadcast(context, 0,
new Intent(ACTION_USB_PERMISSION), 0);
mUsbManager.requestPermission(mUsbDevice, mPendingIntent);
}
return mUsbDevice;
}
}
return mUsbDevice;
}
public void commWithUsbDevice() {
if (mUsbDevice.getInterfaceCount() > 0) {
mUsbInterface = mUsbDevice.getInterface(0);
}
if (mUsbInterface != null && mUsbInterface.getEndpointCount() > 0) {
mUsbEndpoint = mUsbInterface.getEndpoint(0);
}
connection = mUsbManager.openDevice(mUsbDevice);
if (connection != null) {
claim = connection.claimInterface(mUsbInterface, true);
if(claim){
usbThread.start();
}
}
}
public class MyThread extends Thread {
boolean claim=false;
@Override
public void run() {
super.run();
if (mUsbEndpoint != null) {
UsbRequest request = new UsbRequest();
boolean isOpenRequest = request.initialize(connection,
mUsbEndpoint);
if (isOpenRequest){
boolean isQueueOK = request.queue(buffer, 128);
while(true){
if (isQueueOK && connection.requestWait() == request) {
}
Log.e("", "ggggg" + " " + buffer.get(0) + " " + buffer.get(1)
+ " " + buffer.get(2) + " " + buffer.get(3)
+ " " + buffer.get(4) + " " + buffer.get(5)
+ " " + buffer.get(6) + " " + buffer.get(7));
//向设备发送命令
connection.controlTransfer(0x21, 0x09, 0x300, 0x00,
resultOkBuffer2, resultOkBuffer2.length, 100);
}
}
}
}
BroadcastReceiver mUsbReceiver=new BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
false)) {
if(device!=null){
log.e("","已连接该USB设备“);
}
} else {
Toast.makeText(context, "拒绝连接",
Toast.LENGTH_LONG).show();
}
}
} }
};
}