GhostRavenstorm
GhostRavenstorm

Reputation: 754

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

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?

enter image description here

Upvotes: 2

Views: 4439

Answers (2)

kai-morich
kai-morich

Reputation: 437

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

Upvotes: 1

Turbo J
Turbo J

Reputation: 7691

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.

Upvotes: 1

Related Questions