android usb 通信
2015-07-31 09:52
309 查看
package com.example.usbhost; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; 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.net.Uri; import android.os.IBinder; import android.util.Log; public class UsbService extends Service { private final String TAG = "UsbService"; /** * 允许您枚举已连接的USB设备并且与其进行“交流”。 */ private UsbManager mUsbManager; /** * usb 厂家标识 */ private int vendorId; /** * usb 产品标识 */ private int productId; /** * usb名字 */ private String usbName; /** * 代表了一个已连接的USB的设备并且包含具有该设备验证信息,接口和接入点的方法。 */ private UsbDevice mUsbDevice = null; /*** * 代表了一个USB设备的一个接口,该接口定义了一系列关于设备的函数。一个设备在进行“交流”的时候可以有一个或者多个接口 */ private UsbInterface interface1, interface2; /** * 代表该设备的一个连接,用来在接入点上传输数据。这个类允许您能用同步或者异步的方式发送和返回数据 */ private UsbDeviceConnection mDeviceConnection; private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); enumUsb(); requestPermission(); } /** * 获取权限 */ private void requestPermission() { if (mUsbDevice == null) { return; } BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // 在您使用一个USB设备前,您的应用必须从用户那里获得权限。 if (ACTION_USB_PERMISSION.equals(action)) { Log.i(TAG, "ACTION_USB_PERMISSION"); synchronized (this) { UsbDevice device = (UsbDevice) intent .getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { // 获取到用户的权限 if (device != null) { // call method to set up device communication getDeviceInterface(); // assignEndpoint(interface1); openDevice(interface1); } } else { Log.i(TAG, "permission denied for device " + device); } } } // // 当您在完成和设备的“交流”之后,又或者该设备被移除了,通过调用releaseInterface()和close()的方法来关闭UseInterface和UsbDeviceConnection。为了监听分离这样的事件 // if (Intent.ACTION_MEDIA_MOUNTED.equals(action)||Intent.ACTION_MEDIA_CHECKING.equals(action)) { // UsbDevice device = (UsbDevice) intent // .getParcelableExtra(UsbManager.EXTRA_DEVICE); // if (device != null) { // Log.i(TAG, "ACTION_USB_DEVICE_DETACHED"); // Uri url=intent.getData(); // Log.i(TAG, url.getPath()); // mDeviceConnection.close(); // } // } // if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { // Log.i(TAG, "ACTION_USB_DEVICE_ATTACHED"); // } } }; PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent( ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_USB_PERMISSION); // filter.addAction(Intent.ACTION_MEDIA_MOUNTED); // filter.addAction(Intent.ACTION_MEDIA_CHECKING); // filter.addAction(Intent.ACTION_MEDIA_EJECT); // filter.addAction(Intent.ACTION_MEDIA_REMOVED); // filter.addDataScheme("file"); registerReceiver(mUsbReceiver, filter); mUsbManager.requestPermission(mUsbDevice, mPermissionIntent); } /** * 枚举usb设备 * * @param manager */ private void enumUsb() { mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);// 获取manager if (mUsbManager == null) { return; } HashMap<String, UsbDevice> usbList = mUsbManager.getDeviceList(); if (!usbList.isEmpty()) { Iterator<UsbDevice> usbIterable = usbList.values().iterator(); while (usbIterable.hasNext()) { UsbDevice device = usbIterable.next(); usbName = device.getDeviceName(); vendorId = device.getVendorId(); productId = device.getProductId(); mUsbDevice = device; Log.i(TAG, "usbName=" + usbName); Log.i(TAG, "vendorId=" + vendorId); Log.i(TAG, "productId=" + productId); } } } // 获取usb的接口0 1 private void getDeviceInterface() { if (mUsbDevice == null) { return; } for (int i = 0; i < mUsbDevice.getInterfaceCount(); i++) { Log.i(TAG, "获取usb的接口!" + i); UsbInterface usbInterface = mUsbDevice.getInterface(i); if (i == 0) { interface1 = usbInterface; } if (i == 1) { interface2 = usbInterface; } } } // // 分配端点,IN | OUT,即输入输出;可以通过判断 // private UsbEndpoint assignEndpoint(UsbInterface mInterface) { // // UsbEndpoint epBulkIn = null; // UsbEndpoint epBulkOut = null; // UsbEndpoint epControl = null; // UsbEndpoint epIntEndpointOut = null; // UsbEndpoint epIntEndpointIn = null; // if (mInterface == null) { // Log.i(TAG, "UsbEndpoint=null"); // return null; // } // for (int i = 0; i < mInterface.getEndpointCount(); i++) { // UsbEndpoint ep = mInterface.getEndpoint(i); // // look for bulk endpoint // if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { // Log.i(TAG, "getType=USB_ENDPOINT_XFER_BULK"); // if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { // epBulkOut = ep; // } else { // epBulkIn = ep; // } // } // // look for contorl endpoint // if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_CONTROL) { // Log.i(TAG, "getType=USB_ENDPOINT_XFER_CONTROL"); // epControl = ep; // } // // look for interrupte endpoint // if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) { // Log.i(TAG, "getType=USB_ENDPOINT_XFER_INT"); // if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { // epIntEndpointOut = ep; // } // if (ep.getDirection() == UsbConstants.USB_DIR_IN) { // epIntEndpointIn = ep; // } // } // } // if (epBulkOut == null && epBulkIn == null && epControl == null && // epIntEndpointOut == null // && epIntEndpointIn == null) { // throw new IllegalArgumentException("not endpoint is founded!"); // } // return epIntEndpointIn; // } // 打开设备 private void openDevice(UsbInterface mInterface) { if (mInterface != null) { // USBEndpoint为读写数据所需的节点 final UsbEndpoint inEndpoint = mInterface.getEndpoint(0); // 读数据节点 UsbEndpoint outEndpoint = mInterface.getEndpoint(1); // 写数据节点 // 在open前判断是否有连接权限;对于连接权限可以静态分配,也可以动态分配权限 if (mUsbManager.hasPermission(mUsbDevice)) { Log.i(TAG, "hasPermission"); mDeviceConnection = mUsbManager.openDevice(mUsbDevice); } if (mDeviceConnection == null) { Log.i(TAG, "conn==null"); return; } if (mDeviceConnection.claimInterface(mInterface, true)) { // 到此你的android设备已经连上设备 Log.i(TAG, "open设备成功!"); final String mySerial = mDeviceConnection.getSerial(); Log.i(TAG, "设备serial number:" + mySerial); // 读取数据1 两种方法读取数据 new Thread(new Runnable() { @Override public void run() { byte[] byte2 = new byte[64]; int ret = mDeviceConnection.bulkTransfer(inEndpoint, byte2, byte2.length, 3000); for (Byte byte1 : byte2) { Log.i(TAG, "byte1=" + byte1); } } }).start(); } else { Log.i(TAG, "无法打开连接通道。"); mDeviceConnection.close(); } } } }
相关文章推荐
- android开发环境 国内镜像 及Android SDK manager使用国内服务器方法
- Android Api Demos登顶之路(十二)Presentation
- Handler介绍及简单使用
- android 收集已发布版本的错误信息(UncaughtExceptionHandler)
- Android shape & selector
- Android ble 蓝牙4.0 总结
- Android弹出菜单
- 被墙后如何获取android源代码
- Android SDK开发包国内下载地址
- Android SDK开发包国内下载地址
- 利用adb安装和卸载android APK
- android 百度地图API 使用Marker和InfoWindow
- android图片处理之图像模糊
- Java4Android-为什么使用抽象类和抽象函数
- 转:Android设置全局变量
- Butterknife框架导致Android 项目中有隐藏代码文件
- Android学习之路
- android应用开发-你有多熟悉listview?
- android邮件发送几种方式
- Android--Support V4, V7, V13的作用与用法