Footjy
Footjy

Reputation: 269

Bluetooth socket read slowing down with PAN1026

I am developing an Android app that connects via Bluetooth SPP to a PAN1026 chip and is supposed to receive data (about 80 bytes) at a rate of 50 Hz.

The chip works fine with a python script run on a mac. It works fine as well with another cross-platform module (linux, mac, windows).

I wrote another Android app that does the same thing as the chip: sending data (about 80 bytes) at a rate of 50 Hz. And my app works fine with that second Android app. My app works fine as well with a python script running on mac.

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

So basically the problem only happens between Android and the PAN1026. I use Android 4.4.

I tried to: - wrap my InputStream in a BufferedInputStream in case I was not reading fast enough but it was actually worth with it. Maybe because the InputStream is actually a BluetoothInputStream. - profile memory usage but I did not find any suspicious leak. - connect using reflection method (it did not even connect). - connect using insecure method: same lag problem.

I did not try to: - use another version of Android as eventually the app will use also BLE so will need no earlier version.

I am going to try to: - check on the side of the chip to see whether it is actually sending data at a rate of 50 Hz or not. But it won't solve the problem in any case.

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

I would greatly appreciate any help on the subject.

Here is the 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 is my ConnectedThread: I simplified the code so it's easily understandable and limited to the problem. Indeed, the problem also happens when the following thread only does reading from the socket. So the bottleneck is in the read method if it is not on the side of the chip.

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

Upvotes: 3

Views: 813

Answers (1)

maxFS
maxFS

Reputation: 46

Your question looks like this one. I bet your device is going into Sniff Mode because it does not send any info to the PAN1026.You can check this by getting your HCI logs available in your device.

Android only monitors the tx channel (transmission) and not the rx channel (reception). From what I can see in your code you're not sending any data to the device, hence you're not using the tx channel. As mentioned in the post above, you should try to send data every ~500ms to avoid your device going into sniff mode.

Upvotes: 3

Related Questions