I am writing an application that contains a service that handles Bluetooth device discovery. My discovered devices are received in the ScanCallback method. I want to make sure that everything that happens inside ScanCallback is handled in the background thread and not on the main thread. My problem is that with my implementation, each callback creates a separate thread. I was wondering if this is ok or not and if not, how can I reuse the same thread to handle all the callbacks. Here is my code.
@TargetApi(21)
private ScanCallback GetMScanCallbackForApi21AndAbove() {
return new ScanCallback() {
@Override
public void onScanResult(int callbackType,final ScanResult result) {
new Thread(new Runnable() {
@Override
public void run() {
sendBtDevice(result.getDevice());
}
}).start();
}
@Override
public void onScanFailed(final int errorCode) {
new Thread(new Runnable() {
@Override
public void run() {
//Do something else
}
}).start();
};
}