Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BLE device scan with filter is not working

I am working with android BLE (Bluetooth Low Energy). I am having trouble in scanning BLE device using
startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) method
while startLeScan(BluetoothAdapter.LeScanCallback callback) is working fine.
When i use filter to scan specific serviceUUIDs , the callback is not executing. I am testing with samsung galaxy s6.
I want to know whether this issue is device specific or there is some bug in scaning function.

like image 321
Akhilesh Kumar Avatar asked Dec 03 '15 11:12

Akhilesh Kumar


3 Answers

I'm pretty sure it's not device specific. First of all as IshArt mentioned you should use startScan from Android 5.0 on.

startScan(List<ScanFilter> filters, ScanSettings settings, ScanCallback callback)

From my experience the implementation of Scanfilters works fine if you go for MAC addresses but other filter setup parameters are problematic:

ScanFilter filter = new ScanFilter.Builder().setDeviceAddress(deviceMacAddress).build();
filters.add(filter);

If that is not an option for you, you can also implement your own filter. If you leave the filters list for the startScan() empty, it ignores all filtering and receives everything. Then in the callback you can write your own method to check whether the result meets your requirements or not.

like image 101
Chris Avatar answered Nov 03 '22 04:11

Chris


This is a specific problem I've found with the Samsung Galaxy S6. I use the newer scanner API and only support devices on Android 5.0+. My testing has shown the S4, (haven't tested S5), S7, and S8 all work; I don't know why the S6 has issues.

The workaround it is, lamentably, to just filter manually on mac address after the devices are found.

Update

This fixed my issues above that I was having with the Galaxy S6.

Previously, I was adding two conditions to the same ScanFilter like this (Kotlin):

ScanFilter.Builder()
    .setServiceUuid(BluetoothBlind.Service.ParcelUUID)
    .setDeviceAddress(macAddress)
    .build()

Changing it to split the conditions into multiple filters fixes it:

ScanFilter.Builder()
    .setDeviceAddress(macAddress)
    .build()

ScanFilter.Builder()
    .setServiceUuid(BluetoothBlind.Service.ParcelUUID)
    .build()
like image 3
Bryan Bryce Avatar answered Nov 03 '22 05:11

Bryan Bryce


The issue with BLE filtered scanning is a known issue. See https://github.com/iDevicesInc/SweetBlue/wiki/Android-BLE-Issues for this and other BLE issues. The conclusion is simple: "You have to scan for all devices and do filtering yourself."

like image 2
p2pkit Avatar answered Nov 03 '22 04:11

p2pkit