Bad bluetooth data input stream in Android 7 & 8, but not 4.4

Viewed 170

Why would the newer Android OS's do a worse job of reading data over bluetooth?

I'm building an app that collects data from a bluetooth-enabled medical device (2.1 + edr). It sends out data at around 27kB/s, which I believe is well within the range of what Android bluetooth can handle. For some reason, though, there is significant enough loss of data that the signal I can collect on the phone is unusable for research. I've spent a week tracing down the source of the data loss and think I've pinpointed it to having more to do with the OS itself than any code I've written.

First off, I'm using this BluetoothService class from the Android Open Source Project to connect and stream data from the device. It's a Service that has a seperate thread (ConnectedThread) for reading data from the bluetooth socket.

In order to see test whether my app's parsing or graphing code was the problem, I've been testing incoming data quality by writing straight to a file with as little computation as possible. Afterwards, I parse the data and plot it with a MATLAB script that's been validated with to work with data from all our other client applications (non-mobile). Here's my writing code:

public void run() {
  byte[] buffer = new byte[1024];
  int bytes;
  while (true) {
    try {
      bytes = mmInStream.read(buffer);
      if (bytes > 0) {
        byte[] data = Arrays.copyOf(buffer, bytes);
        mFileOutputStream.write(data);
      }
    } catch (IOException e) {
      break;
    }
  }
}

and here's what 5s of a controlled, 7hz sine wave coming from the device looks like: note the data issues around 1000 and 4300ms

I've tried running this code on a few different phones and they were all the same: a Nexus 5 running Android 7.1, and a Pixel 1 and Xiaomi Mi 6 running Android 8.0. I also tried things like giving the ConnectedThread high priority and making sure no apps were running in the background and disconnecting the phone from the internet. No change.

However, much to my surprise, a lowly Samsung Galaxy S III running an ancient version of Android had an immaculate data stream running the exact same code: a beautiful 7hz wave

I then validated that this was due to the OS rather than hardware by downgrading my Nexus 5 to Android 4.4 and getting another immaculate data sample: another beautiful 7hz wave

So, my questions is, what's going on here? What's different between Android 4.4 and >= 7.1 that could cause such a dramatic difference in the quality of the data I'm getting over bluetooth? We can collect data just fine on a laptop, but Android is causing issues and I feel like I'm in over my head.


Addendum:

The only difference in OS's that I turned up with googling was the addition of battery optimization for apps after Android 6.0. Interestingly, disabling this actually makes the data worse! high noise and distortion along with dropped data at 3000ms

0 Answers
Related