关于详细的实现Google的教程上有个快速简易的介绍,一般跟着做是可以实现效果的-USB Host and Accessory。稍微了解过Android开发的看着玩意儿没啥问题。基本上就是在manifest里对指定的activity配置下权限就妥了,它还提供了一个device_filter的xml文件,可以过滤指定的USB设备。
privatestaticfinalStringACTION_USB_PERMISSION="com.android.example.USB_PERMISSION";privatefinalBroadcastReceivermUsbReceiver=newBroadcastReceiver(){publicvoidonReceive(Contextcontext,Intentintent){Stringaction=intent.getAction();if(ACTION_USB_PERMISSION.equals(action)){synchronized(this){UsbDevicedevice=(UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,false)){if(device!=null){//call method to set up device communication}}else{Log.d(TAG,"permission denied for device "+device);}}}}};
privateByte[]bytesprivatestaticintTIMEOUT=0;privatebooleanforceClaim=true;UsbInterfaceintf=device.getInterface(0);UsbEndpointendpoint=intf.getEndpoint(0);UsbDeviceConnectionconnection=mUsbManager.openDevice(device);connection.claimInterface(intf,forceClaim);connection.bulkTransfer(endpoint,bytes,bytes.length,TIMEOUT);//do in another thread
四,Terminating communication with a device
1234567891011
BroadcastReceivermUsbReceiver=newBroadcastReceiver(){publicvoidonReceive(Contextcontext,Intentintent){Stringaction=intent.getAction();if(UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)){UsbDevicedevice=(UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if(device!=null){// call your method that cleans up and closes communication with the device}}}};
privateStringchangeEscapeSequence(Stringin){Stringout=newString();try{out=unescapeJava(in);}catch(IOExceptione){return"";}out=out+"\n";returnout;}privateStringunescapeJava(Stringstr)throwsIOException{if(str==null){return"";}intsz=str.length();StringBufferunicode=newStringBuffer(4);StringBuilderstrout=newStringBuilder();booleanhadSlash=false;booleaninUnicode=false;for(inti=0;i<sz;i++){charch=str.charAt(i);if(inUnicode){// if in unicode, then we're reading unicode// values in somehowunicode.append(ch);if(unicode.length()==4){// unicode now contains the four hex digits// which represents our unicode charactertry{intvalue=Integer.parseInt(unicode.toString(),16);strout.append((char)value);unicode.setLength(0);inUnicode=false;hadSlash=false;}catch(NumberFormatExceptionnfe){// throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);thrownewIOException("Unable to parse unicode value: "+unicode,nfe);}}continue;}if(hadSlash){// handle an escaped valuehadSlash=false;switch(ch){case'\\':strout.append('\\');break;case'\'':strout.append('\'');break;case'\"':strout.append('"');break;case'r':strout.append('\r');break;case'f':strout.append('\f');break;case't':strout.append('\t');break;case'n':strout.append('\n');break;case'b':strout.append('\b');break;case'u':{// uh-oh, we're in unicode country....inUnicode=true;break;}default:strout.append(ch);break;}continue;}elseif(ch=='\\'){hadSlash=true;continue;}strout.append(ch);}if(hadSlash){// then we're in the weird case of a \ at the end of the// string, let's output it anyway.strout.append('\\');}returnnewString(strout.toString());}