如何使用 Android 硬件 USB API 向 USB 设备连接发送/接收数据? [英] How to send/receive data to USB device connection using Android Hardware USB API?

查看:63
本文介绍了如何使用 Android 硬件 USB API 向 USB 设备连接发送/接收数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Bluefruit LE Feather 32u4 板连接到我的 Android 设备,我正尝试向其发送和接收数据.它目前被编程为回显发送的内容,所以我试图找出 Android API 来读写它.

以下是我尝试与之通信的硬件的一些详细信息:

解决方案

来自您的 pastebin 片段看起来像一个 CDC 设备.您可以使用 usb-serial-for-android 库与此类设备进行通信

I have here a Bluefruit LE Feather 32u4 board connected to my Android device that I'm trying to send and receive data to. It's currently programed to echo back what is sent, so I'm trying to figure out the Android API to write and read from it.

Here's some verbose info on the hardware I'm trying to communicate with: https://pastebin.com/ktyBhzE8

In the test app, there's a button that runs the following code.

   private void DoTheThing() {
      String textMessage = "";

      m_usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

      HashMap<String, UsbDevice> deviceList = m_usbManager.getDeviceList();
      Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
      while (deviceIterator.hasNext()) {
         m_usbDevice = deviceIterator.next();
         // I know I'm expecting just one device.
      }

      textMessage += "Devices: " + deviceList.size() + "\n";

      textMessage += "Interfaces: " + m_usbDevice.getInterfaceCount() + "\n";
      for ( int i = 0; i < m_usbDevice.getInterfaceCount(); i++) {
         textMessage += "Interface " + i + ":\n";
         textMessage += "   ID: " + m_usbDevice.getInterface(i).getId() + "\n";
         textMessage += "   EP: " + m_usbDevice.getInterface(i).getEndpointCount() + "\n";

      }


      UsbEndpoint int0_ep0_out = m_usbDevice.getInterface(0).getEndpoint(0);
      UsbEndpoint int1_ep0_in  = m_usbDevice.getInterface(1).getEndpoint(0);
      UsbEndpoint int1_ep1_out = m_usbDevice.getInterface(1).getEndpoint(1);
      // I know I'm expecting two interfaces and three endpoints.

      UsbDeviceConnection usbConnection = m_usbManager.openDevice(m_usbDevice);

      byte[] message = new byte[1];
      message[0] = (byte) 0;

      int success = 0;

      try {
         success = usbConnection.bulkTransfer(int1_ep0_in, message, message.length, 0);
         textMessage += success + "\n";
      } catch (Exception e) {
         textMessage += e + "\n";
      }


      m_textView.setText(textMessage);
   }

Everything seems to work until I try a bulk transfer. I get the following exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.hardware.usb.UsbDeviceConnection.bulkTransfer(android.hardware.usb.UsbEndpoint, byte[], int, int)' on a null object reference.

Is my reference to the IN endpoint not valid?
UsbEndpoint int1_ep0_in = m_usbDevice.getInterface(1).getEndpoint(0);
How do I determine if it is?

I'm also trying to figure out how to receive data from the USB device, because my Feather board here should echo back what it receives. I understand something like bulkTransfer should be used, but how? What invokes it? Is there some event listener that I need to setup that waits for data to come in on a specific endpoint?

解决方案

from your pastebin snippet looks like a CDC device. You can use usb-serial-for-android library to communicate with such devices

这篇关于如何使用 Android 硬件 USB API 向 USB 设备连接发送/接收数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆