Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanning using BluetoothLeScanner calls onScanResult multiple times for the same device

I'm implementing a simple advertise + scan functionality using BLE on android, and for some reason I get a lot of calls to the onScanResult callback passing the same device.

For advertising:

//Advertise settings build
AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
builder.setConnectable(true);

AdvertiseSettings advSettings = builder.build();

//Advertise data build
AdvertiseData.Builder advDataBuilder = new AdvertiseData.Builder();
advDataBuilder.addServiceUuid(ParcelUuid.fromString(SFGattAttributes.SERVICE));

AdvertiseData advertiseData = advDataBuilder.build();

//Start Advertising
bluetoothLeAdvertiser.startAdvertising(advSettings, advertiseData, advertiseData, new BLEAdvertiserCallback());

For scanning:

BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
bluetoothLeScanner.startScan(getScanFilters(), getScanSettings(), new BLEScanCallback());

The only difference each time seems to be a difference in the rssi value.

Is there a way to avoid this multiple calls?

like image 544
Omer Avatar asked Mar 29 '16 23:03

Omer


1 Answers

This is actually a feature and can be for used ranging a (advertising) BLE device. There are also (older) devices that don't get multiple scan results for a specific device during a scan cycle. This then causes problems for ranging other devices including BLE beacons. The multiple calls also let you know (over time) that the device is still reachable/accessible.

So if you don't want the multiple calls just ignore the calls for known devices (MAC addresses). It can not be deactivated.

Keep in mind that many devices (especially phones) change their mac address. Some even every 2 minutes. It's not easy to map the new mac address to the old device (old mac address). You have to handle the behaviour accordingly.

like image 142
p2pkit Avatar answered Dec 15 '22 01:12

p2pkit