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?