Bluetooth connector read in slow motion with PAN1026

I am developing an Android application that connects via Bluetooth SPP to a PAN1026 chip and needs to receive data (about 80 bytes) at 50Hz.

The chip works fine with a python script running on a Mac. It works great with other cross platform module (linux, mac, windows).

I wrote another Android app that does the same thing as the chip: send data (about 80 bytes) at 50Hz. And my app works fine with this second android app. My application works fine with python script running on Mac.

BUT: when my app connects to the chip, it only accepts data at 2Hz, which causes unacceptable lag. This also happens when I use the Bluetooth Debug app from the Play Store (BlueSPP or Bluetooth SPP pro).

So basically the problem only occurs between Android and PAN1026. I am using Android 4.4.

I tried:   - wrap my InputStream in a BufferedInputStream if I don't read fast enough, but it's actually worth it. Possibly because the InputStream is actually a BluetoothInputStream. - memory usage in the profile, but I found no suspicious leak. - connect using reflection method (it didn't even connect). - connect using an unsafe method: same latency problem.

I have not tried:   - use a different android version as the app will eventually use BLE as well, so no earlier version is required.

I'm going to try:   - check on the side of the chip to check if it is actually sending data at 50Hz or not. But that won't fix the problem anyway.

Am I doing something wrong? Or is there a bug between Android and PAN1026? What to do in this case?

I would really appreciate any help on this.

Here's ConnectThread, similar to the Bluetooth chat example:

private class ConnectThread extends Thread{
        public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        private final BluetoothSocket mmSocket;

        public ConnectThread(BluetoothDevice device) {
            BluetoothSocket tmp = null;
            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, e.toString());
            }
            mmSocket = tmp;
        }

        public void run() {
            mAdapter.cancelDiscovery();

            try {
                mmSocket.connect();
            } catch (IOException connectException) {
                try {
                    mmSocket.close();
                } catch (IOException closeException) { Log.e(TAG, closeException.toString());}
                return;
            }
            synchronized (this) {
                resetConnectThread();//properly deallocate memory
            }
            connected(mmSocket);//start the following thread
        }
    }

      

Here's my ConnectedThread: I've simplified the code so it's easy to understand and limited by the problem. Indeed, the problem also occurs when the next thread only reads from the socket. So the bottleneck is in the read method if it is not on the chip side.

private class ConnectedThread extends Thread{

        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            this.mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                BluetoothService.this.stop();
            }

            this.mmInStream = tmpIn;
            this.mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[80];
            int bytes;

            while (true) {
                try {
                    bytes = mmInStream.read(buffer, 0, 80);
                } catch (IOException e) {
                    BluetoothService.this.stop();
                    break;
                }
            }
        }
}

      

+3


source to share


1 answer


Your question looks like this . I bet your device goes into Sniff mode because it is not sending information to PAN1026. You can check this by getting your HCI logs on your device.



Android only monitors the tx channel (transmit), not the rx channel (receive). From what I can see in your code, you are not sending any data to the device, so you are not using a tx channel. As mentioned above, you should try to send data every ~ 500ms to prevent your device from going into scent mode.

+3


source







All Articles