0

In Android, BluetoothAdapter.LeScanCallback runs on main thread.

private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                //The code here is executed on main thread
                Log.e("LeScanCallback", Thread.currentThread().getName());//Prints main
            }
        };

I can make the code run on a seperate thread by doing the following:

private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //The code here is executed on on new thread everytime 
                            Log.e("LeScanCallback", Thread.currentThread().getName());//Prints Thread-xxxx
                        }
                    }).start();
                }
            };

But this creates a new thread for every callback. I want the code inside the callback to run on one single thread that is not the main thread. What is the right way of doing this?

MeesterPatat
  • 2,671
  • 9
  • 31
  • 54

1 Answers1

3

You can create a thread and looper:

import android.os.Looper;
import android.os.Handler;
public class LooperThread extends Thread {
       public Handler handler;
       public void run(){
            Looper.prepare();
            handler = new Handler();
            looper.loop(); 
            /*
                 if you want to stop thread you can create you own exception, throw it and catch. Example: 
                 try
                 {
                       looper.loop();
                 } catch(MyException e){

                 }
                 And now thread is stopping (and not handling runnables from handler.post()
                 In other thread write this: 
                 thread.handler.post(new Runnable(){
                          throw new MyException(); // And now exception will be caught and thread will be stopping.
                 });
            */
       }
}



private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                private LooperThread thread = new LooperThread();
                {
                      thread.start(); // start thread once
                }
                @Override
                public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                    if(thread.handler != null)
                        thread.handler.post(new Runnable() {
                        @Override
                        public void run() {
                            // And now this code is running on seperate thread. (Not creating new)
                            Log.e("LeScanCallback", Thread.currentThread().getName());//Prints Thread-xxxx
                        }
                    });
                }
            };   
krystian71115
  • 1,927
  • 17
  • 36