【问题标题】:How to send/receive data to USB device connection using Android Hardware USB API?如何使用 Android Hardware USB API 向 USB 设备连接发送/接收数据?
【发布时间】:2020-03-18 23:16:19
【问题描述】:

我这里有一块 Bluefruit LE Feather 32u4 板连接到我正在尝试发送和接收数据的 Android 设备。它目前被编程为回显发送的内容,因此我正在尝试找出 Android API 以对其进行读写。

以下是我尝试与之通信的硬件的一些详细信息: https://pastebin.com/ktyBhzE8

在测试应用中,有一个运行以下代码的按钮。

   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);
   }

在我尝试批量传输之前,一切似乎都正常。我得到以下异常:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'int android.hardware.usb.UsbDeviceConnection.bulkTransfer(android.hardware.usb.UsbEndpoint, byte[], int, int)'。

我对 IN 端点的引用无效吗?
UsbEndpoint int1_ep0_in = m_usbDevice.getInterface(1).getEndpoint(0);
如何判断是不是?

我也在试图弄清楚如何从 USB 设备接收数据,因为我的 Feather 板应该回显它接收到的数据。我知道应该使用像bulkTransfer 这样的东西,但是怎么做呢?什么调用它?是否需要设置一些事件侦听器来等待数据进入特定端点?

【问题讨论】:

标签: android usb


【解决方案1】:

从您的 pastebin sn-p 看起来像一个 CDC 设备。您可以使用usb-serial-for-android 库与此类设备进行通信

【讨论】:

  • 暂时接受这个答案,直到我可以进一步了解 USB 设备。
  • 所以,我不认为该库是我正在寻找的。试一试,它无法获得可用的驱动程序。
  • 对于 CDC 设备,您通常必须为您的特定 VID/PID 添加 customProber,如下所示:github.com/mik3y/…
  • 我收回我的最后评论。正如@kai-morich 所说,我需要指定 VID 和 PID。
【解决方案2】:

java.lang.NullPointerException:尝试调用虚拟方法“int android.hardware.usb.UsbDeviceConnection.bulkTransfer”

翻译:usbConnectionnull

您无法打开某些 USB 设备,尤其是当有其他程序(或内核本身)使用它时。

【讨论】:

  • 这是为了让它们永远无法打开,还是需要从使用它们的东西中解放出来?我肯定看到UsbConnection 为空。所以,我是他们来弄清楚失败的确切原因。我可以立即访问的另一个 USB 设备是闪存驱动器。当我尝试它也说设备无法打开。
猜你喜欢
  • 2010-10-20
  • 2015-03-04
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
相关资源
最近更新 更多