How to send/receive data to USB device connection using Android Hardware USB API?

安稳与你 提交于 2019-12-20 07:28:21

问题


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?


回答1:


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




回答2:


java.lang.NullPointerException: Attempt to invoke virtual method 'int android.hardware.usb.UsbDeviceConnection.bulkTransfer'

Translation: usbConnection is null.

You cannot open some USB devices, especially if there are other programs (or the kernel itself) using it.



来源:https://stackoverflow.com/questions/59001759/how-to-send-receive-data-to-usb-device-connection-using-android-hardware-usb-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!