精彩文章免费看

Android设备通过数据线进行相互通信

具体步骤

UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
Map<String, UsbDevice> usbList = usbManager.getDeviceList();

官方注释:

This class represents a USB device attached to the android device with the android device
 acting as the USB host.

这个类代表了android所连接的usb设备。

需要打开刚刚搜索到的usb设备。

一般来说,在没有定制的android设备上首次访问usb设备的时候,默认我们是没有访问权限的,因此我们首先要判断对当前要打开的usbDevice是否有访问权限:

if (!usbManager.hasPermission(usbDevice)) {
    usbPermissionReceiver = new UsbPermissionReceiver();
   //申请权限
   Intent intent = new Intent(ACTION_DEVICE_PERMISSION);
   PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
   IntentFilter permissionFilter = new IntentFilter(ACTION_DEVICE_PERMISSION);
   context.registerReceiver(usbPermissionReceiver, permissionFilter);
   usbManager.requestPermission(usbDevice, mPermissionIntent);

需要声明一个广播UsbPermissionReceiver,当接受到授权成功的广播后做一些其他处理。

private class UsbPermissionReceiver extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (ACTION_DEVICE_PERMISSION.equals(action)) {
          synchronized (this) {
              UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
              if (device.getDeviceName().equals(usbDevice.getDeviceName()) {
                  if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    //授权成功,在这里进行打开设备操作
                  } else {
                    //授权失败

接下来,要找到具有数据传输功能的接口UsbInterface,从它里面找到数据输入和输出端口UsbEndpoint,一般情况下,一个usbDevice有多个UsbInterface,我们需要的一般是第一个,所以:

usbInterface=usbDevice.getInterface(0);

同样的,一个usbInterface有多个UsbEndpoint,有控制端口和数据端口等,因此我们需要根据类型和数据流向来找到需要的数据输入和输出两个端口:

for (int index = 0; index < usbInterface.getEndpointCount(); index++) {
    UsbEndpoint point = usbInterface.getEndpoint(index);
    if (point.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
        if (point.getDirection() == UsbConstants.USB_DIR_IN) {
            usbEndpointIn = point;
        } else if (point.getDirection() == UsbConstants.USB_DIR_OUT) {
            usbEndpointOut = point;

最后,才是真正的打开usb设备,我们需要和usb外设建立一个UsbDeviceConnection,官方注释:

This class is used for sending and receiving data and control messages to a USB device.

UsbDeviceConnection的获取:

usbDeviceConnection = usbManager.openDevice(usbDevice);
  • 向usb外设发送数据
  • 在第二步中,我们已经获取了数据的输出端口usbEndpointIn,我们向外设发送数据就是通过这个端口来实现的。

    int ret = usbDeviceConnection.bulkTransfer(usbEndpointOut, data, data.length, DEFAULT_TIMEOUT);
    

    bulkTransfer这个函数用于在给定的端口进行数据传输,第一个参数就是此次传输的端口,这里我们用的输出端口,第二个参数是要发送的数据,类型为字节数组,第三个参数代表要发送的数据长度,最后一个参数是超时,返回值代表发送成功的字节数,如果返回-1,那就是发送失败了。

  • 接受usb外设发送来的数据
  • 已经找到了数据输入端口usbEndpointIn,因为数据的输入是不定时的,因此我们可以另开一个线程,来专门接受数据。

    int inMax = inEndpoint.getMaxPacketSize(); 
    ByteBuffer byteBuffer = ByteBuffer.allocate(inMax); 
    UsbRequest usbRequest = new UsbRequest(); 
    usbRequest.initialize(connection, inEndpoint); 
    usbRequest.queue(byteBuffer, inMax); 
    if(connection.requestWait() == usbRequest){ 
        byte[] retData = byteBuffer.array(); 
        for(Byte byte1 : retData){ 
            System.err.println(byte1); 
    

    注:理论上设备之间的连接已经建立了,也可以首发数据了,但是,我们大部分情况下还需要对usb串口进行一些配置,比如波特率,停止位,数据控制等,不然两边配置不同,收到的数据会乱码,具体怎么配置,需要看串口芯片。

    测试设备需求

  • Android版本>3.2的设备
  • OTG转接线
  • 项目地址, 请戳我