Android USB开发麻烦还是比较多的。

第一种:host模式


这种模式比较不错,由Android设备提供电源,然后与外部设备通信。举个例子来说:电脑连接USB设备,都是这个模式,非常常见的模式。

但是有一个万恶的问题,android接外部USB设备的时候,驱动怎么办?又有那款芯片敢说Android系统支持他们家的芯片,又有哪个厂家说不动android系统装上他们家的驱动,他们家的设备就可以在Android上使用,或许这点上Android很难超越windows。

造成想现状:想加外部设备,都要:重新自己做底层驱动程序--->编译系统--->刷机--->编写android程序--->接入硬件实现功能。

整个一套下来饭都吃好几顿了。还是希望以后android发展发展能向window一样支持多设备驱动吧。


第二种:Accessory模式


这个模式比较揪心,外部设备要供给电源,数据间通信:电脑---手机就是这种模式,手机作为Accessory设备,电脑供给它电源,同时进行数据通信。

恰巧我也是用了这种模式:

程序需要做的:

(1)添加Action BoardCast

private static final String ACTION_USB_PERMISSION ="com.ukey.USB_PERMISSION";

/***********************USB handling******************************************/usbmanager = (UsbManager) context.getSystemService(Context.USB_SERVICE);mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);context.registerReceiver(mUsbReceiver, filter);inputstream = null;outputstream = null;

(2)编写对应的boradCaset信息

/***********USB broadcast receiver*******************************************/private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (ACTION_USB_PERMISSION.equals(action)) {synchronized (this){UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)){Toast.makeText(global_context, "Allow USB Permission", Toast.LENGTH_SHORT).show();OpenAccessory(accessory);}else{Toast.makeText(global_context, "Deny USB Permission", Toast.LENGTH_SHORT).show();}mPermissionRequestPending = false;}} else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)){DestroyAccessory(true);}else{Log.d("LED", "....");}}};
(3)又来一个比较麻烦的事

android每次使用Accessory的时候都会询问你是否允许设备访问,这会点击是或否的结果又(2)中代码

if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
去获得,然后才能使用OpenAccessory功能。

public int OpenAccessory(){// Intent intent = getIntent();if (inputstream != null && outputstream != null) {return 1;}UsbAccessory[] accessories = usbmanager.getAccessoryList();if(accessories != null){Toast.makeText(global_context, "Accessory Attached", Toast.LENGTH_SHORT).show();}else{// return 2 for accessory detached casereturn 2;}UsbAccessory accessory = (accessories == null ? null : accessories[0]);if (accessory != null) {if( -1 == accessory.toString().indexOf(ManufacturerString)){Toast.makeText(global_context, "Manufacturer is not matched!", Toast.LENGTH_SHORT).show();return 1;}if( -1 == accessory.toString().indexOf(ModelString1) && -1 == accessory.toString().indexOf(ModelString2)){Toast.makeText(global_context, "Model is not matched!", Toast.LENGTH_SHORT).show();return 1;}if( -1 == accessory.toString().indexOf(VersionString)){Toast.makeText(global_context, "Version is not matched!", Toast.LENGTH_SHORT).show();return 1;}Toast.makeText(global_context, "Manufacturer, Model & Version are matched!", Toast.LENGTH_SHORT).show();if (usbmanager.hasPermission(accessory)) {OpenAccessory(accessory);}else{synchronized (mUsbReceiver) {if (!mPermissionRequestPending) {Toast.makeText(global_context, "Request USB Permission", Toast.LENGTH_SHORT).show();usbmanager.requestPermission(accessory,mPermissionIntent);mPermissionRequestPending = true;}}}}return 0;}

启动请求。


(4)openAccessory功能

/*destroy accessory*/public void DestroyAccessory(boolean bConfiged){if(true == bConfiged){READ_ENABLE = false;  // set false condition for handler_thread to exit waiting data loopwriteusbdata[0] = 0;  // send dummy data for instream.read goingSendPacket(1, writeusbdata);}else{SetConfig((int)9600,(byte)8,(byte)1,(byte)0,(byte)0);  // send default setting data for configtry{Thread.sleep(10);}catch(Exception e){}READ_ENABLE = false;  // set false condition for handler_thread to exit waiting data loopwriteusbdata[0] = 0;  // send dummy data for instream.read goingSendPacket(1, writeusbdata);}try{Thread.sleep(10);}catch(Exception e){}CloseAccessory();}/*********************helper routines*************************************************/public void OpenAccessory(UsbAccessory accessory){filedescriptor = usbmanager.openAccessory(accessory);if(filedescriptor != null){usbaccessory = accessory;FileDescriptor fd = filedescriptor.getFileDescriptor();inputstream = new FileInputStream(fd);outputstream = new FileOutputStream(fd);/*check if any of them are null*/if(inputstream == null || outputstream==null){return;}if(READ_ENABLE == false){READ_ENABLE = true;readThread = new read_thread(inputstream);readThread.start();}}SetConfig((int)9600,(byte)8,(byte)1,(byte)0,(byte)0);}public void CloseAccessory(){try{if(filedescriptor != null)filedescriptor.close();}catch (IOException e){}try {if(inputstream != null)inputstream.close();} catch(IOException e){}try {if(outputstream != null)outputstream.close();}catch(IOException e){}/*FIXME, add the notfication also to close the application*/filedescriptor = null;inputstream = null;outputstream = null;//System.exit(0);}






更多相关文章

  1. 专题 - Web应用
  2. 关于 Android(安卓)程序使用 Support Library 属性的几点说明
  3. Android(安卓)ADB命令的使用
  4. Android架构模式一:MVC
  5. Android(安卓)GUI编程模型(MVC设计模式)
  6. Android中Activity的四种启动模式
  7. One省电卫士 - Android内核级省电App
  8. Android系统移植与调试之------->如何修改Android设备添加重启、
  9. Google I/O 2014 New

随机推荐

  1. 小姐姐用动画图解Git命令,一看就懂!
  2. 利用 Harbor 搭建企业级私有镜像仓库
  3. 如何在Mac上重置SMC?
  4. 虎虎生威,挑战云上魔方(活动期完成可得实
  5. 介绍一款免费好用的可视化数据库管理工具
  6. 微软被指剽窃他人开源作品!作者被迫终止该
  7. 老司机常用的kafka监控-eagle
  8. 不可不知的zookeeper小工具-zkui
  9. vscode编辑器和markdown/Emmet语法的使用
  10. 接口类和抽象类