Question

I have a bluetooth GPS that outputs data at around 2000bytes/sec. When I first start my app it is able to keep up with this rate, but within about 5-10 seconds the rate falls all the way down to 500bytes/sec. From there it goes up and down (between 300bytes/sec and 700bytes/sec usually, but I've seen as high as 6000bytes/sec as a quick spike when it tries to catch up). The stream just falls further and further behind and data ends up getting dropped (the GPS is outputting 10 samples per second and it gets to the point where I will miss several seconds worth of data).

When I connect to this same device via bluetooth from my laptop I get all the data no matter how long it runs. So I know the device itself is able to transmit at this rate. But on android (HTC Droid DNA) it falls behind right away. I have tried bumping up the thread priority and that didn't help. The app stays in the foreground the entire time with the screen on. I have also tried it without the phone plugged into the debugger just in case that was slowing things down and it's still the same issue. I don't know if this is a bluetooth stack speed issue, or a thread priority issue or what. Any ideas?

UPDATE: I just tested the same code on my Galaxy Tab 10.1 and it is able to maintain around 2000 bytes/sec indefinitely. I then tested on an old Motorola Photon 4G and it also is able to maintain the data rate. On the Droid DNA I tested with WiFi disabled as well to see if that was hurting bluetooth performance but it didn't make a difference. And because the DNA is able to do the higher rate for 5-6 seconds I would think the hardware has the capability. For some reason it just falls off after that...

OutputStream mmOutputStream;
InputStream mmInputStream;

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();

dataReader();   


...
...


void dataReader()
{
    worker = new Thread(new Runnable()
    {
        public void run()
        {                
            int priority = Process.getThreadPriority(Process.myTid());
            Log.d("testApp", String.format("data thread priority %d", priority));
            Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);
            priority = Process.getThreadPriority(Process.myTid());
            Log.d("testApp", String.format("data thread priority %d", priority));

            int bufferSize = 1024;
            byte[] readBuffer = new byte[bufferSize];

            long time1 = System.currentTimeMillis();
            long time2 = 0;
            long datacount = 0;

            while(!Thread.currentThread().isInterrupted() && !stopWorker)
            {
                try 
                {                       
                    // read what we can
                    int bytesRead = mmInputStream.read(readBuffer);

                    datacount += bytesRead;
                    time2 = System.currentTimeMillis();

                    // every second output the data rate
                    if (time2 - time1 > 1000)
                    {
                        final float rate = ((float)datacount * 1000.0F) / (float)(time2 - time1);

                        handler.post(new Runnable() {
                            public void run()
                            {
                                String text = String.format("%.1f bytes/sec", rate);
                                myLabel.setText(text);

                            }                               
                        });

                        time1 = time2;
                        datacount = 0;
                    }
                }
                catch (IOException ex) 
                {
                    stopWorker = true;
                }
            }
        }
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top