Im trying to discover bluetooth low energy devices but I have a problem with onLeScan method. It is getting called twice and as a result all devices doubled.
Method that i use to initiate scanning:
private void scanLeDevice(final boolean enable) {
if (enable) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
bluetoothAdapter.stopLeScan(getLeScanCallback());
scanning = false;
}
}, SCAN_PERIOD);
scanning = true;
bluetoothAdapter.startLeScan(getLeScanCallback());
} else {
bluetoothAdapter.stopLeScan(getLeScanCallback());
scanning = false;
}
}
Method returning LeScanCallback:
private BluetoothAdapter.LeScanCallback getLeScanCallback(){
BluetoothAdapter.LeScanCallback leScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi
, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "device found");
Device device = new Device();
device.setName(bluetoothDevice.getName());
device.setAddress(bluetoothDevice.getAddress());
devices.add(device);
deviceListAdapter.notifyDataSetChanged();
}
});
}
};
return leScanCallback;
}