0

I am sending data over to an Android 9 SBC, via rawhid on a teensy2++.

I have managed to achieve two way communication however receiving the update is very slow, approx every half second.

Here's the relevant code the I have working so far.

I open the device with:

 public void openUsb(View view){
    if(connected == true){
        HashMap<String , UsbDevice> deviceList = mManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice tempDevice = deviceIterator.next();
            if ((Integer.toHexString(tempDevice.getVendorId()).equalsIgnoreCase(Vid)) && (Integer.toHexString(tempDevice.getProductId()).equalsIgnoreCase(Pid))) {

                {
                    mDevice = tempDevice;

                    mUsbInterface = mDevice.getInterface(0);
                    mEndpointWrite = mUsbInterface.getEndpoint(1);
                    mEndpointRead = mUsbInterface.getEndpoint(0);

                    connection = mManager.openDevice(mDevice);

                    boolean claim = connection.claimInterface(mUsbInterface, true);

                    if (claim == true) {
                        writeToStatus("USB Connection Claimed");
                    }
                    else{
                        writeToStatus("USB Connection Claim failed");
                    }
                }
            }
        }
    }
    else{
        writeToStatus("Check USB Connection");
    }
}

I can write to the device with:

    public void writeStart(View view) {

    if(connected == true) {

        final int BUF_LEN = 2;

        byte buf[] = new byte[BUF_LEN];
        buf[0] =1;
        buf[1] =5;

        int res = connection.bulkTransfer(mEndpointWrite, buf, buf.length, 1000);
        writeToStatus(String.valueOf(res));
    }
    else{
        writeToStatus("Check USB Connection");
    }
}

and reading is done in its own runable thread

    public void readUsb(View view) {
     handler.postDelayed(runnable, 1);
}

  private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if(connected == true) {

            String strYaw ;
            String strPitch ;
            String strRoll ;

            byte[] recordIn = new byte[16];
            int receivedLength = connection.bulkTransfer(mEndpointRead, recordIn, recordIn.length, 10);
            if (receivedLength > -1) {

                byte[] tempYaw = new byte[4];
                byte[] tempPitch = new byte[4];
                byte[] tempRoll = new byte[4];

                tempYaw[0] = recordIn[3];
                tempYaw[1] = recordIn[2];
                tempYaw[2] = recordIn[1];
                tempYaw[3] = recordIn[0];

                float Yaw = bytearray2float(tempYaw);

                strYaw = "Y:" + Yaw;

                Message msg = handler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("yaw", strYaw );
                msg.setData(bundle);
                handler.sendMessage(msg);
            }

            handler.postDelayed(this, 10); 
        }
    }
};

Has anyone seen this before or have any ideas on how to speed up communication? If I connect the teensy device into windows and run SimpleHidWrite I get expected update rates so I believe the device is working correctly.

Thanks for any help on this

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Spriggsy
  • 196
  • 1
  • 18
  • Try to acquire a WAKE_LOCK for your app and disable battery optimization for the app - may be Android tries to save battery too aggressive. BTW Which Android device do you use for testing? – Robert Jan 07 '20 at 19:30
  • thanks for the hints, ill give them a go when I get back to work tomorrow, the board we are using is a snapdragon 605. very good results so far ad runs Unreal and unity perfectly, just trying to get USB data into it. https://www.intrinsyc.com/open-q-605-single-board-computer/ – Spriggsy Jan 07 '20 at 19:38
  • Ive tried using a WAKE_LOCK and disabled battery optimization, as suggested by Robert above, also changed from bulk transfer to UsbRequest request, however no change in speed. Have noticed that it is the same if using USB HID Terminal from the app store? – Spriggsy Jan 08 '20 at 10:01

1 Answers1

0

The problem has been fixed by removing all uses of Serial in the teensy code,

I was duplicating to Hid writes to Serial writes for debugging, as I'm not reading the serial endpoints in Android I guess it was pausing on a timeout or something?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Spriggsy
  • 196
  • 1
  • 18