question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to scan for peripherals that have a given service UUID as a key in their service data?

See original GitHub issue

I’m trying to scan for peripherals that advertise a key-value pair in their service data where the key is a given service UUID and the value is a unique identifier, different for each peripheral. Those peripherals only use serviceData, they do not advertise the service UUID in serviceUuids.

Here is what I tried so far:

AsyncTask.execute {
       val scannedBleServices = mapOf(
            "ABC" to ParcelUuid(UUID.fromString("00001234-5678-9012-3456-789012345678"))
       )
       val scanFilters = scannedBleServices.values.map {
           ScanFilter.Builder().setServiceData(it, ByteArray(0)).build()
       }
       val scanSettings = ScanSettings.Builder()
                .setLegacy(false)
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .setReportDelay(0L)
                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
                .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
                .setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
                .build()
        BluetoothLeScannerCompat.getScanner().startScan(
                scanFilters,
                scanSettings,
                leScanCallback
        )
}

But with that, the callback is never called. Note that if I pass null as a first parameter to startScan, my callback does get called, which seems to indicate that I have all the permissions working properly, and I have just a problem with my filters.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
philips77commented, Nov 14, 2019

As a matter of fact, on iOS, I’m able to set a filter on serviceUUID and my peripherals show up in the background even if I’m only advertising this service UUID in serviceData apparently.

Oh, good to know. Did you try setting setUseHardwareFilteringIfSupported(false) and see the difference? Is it an issue in the library or just the system does not filter well? https://github.com/NordicSemiconductor/Android-Scanner-Compat-Library/blob/8df7799441def4db80b7c9055ff99ecb57d9dad3/scanner/src/main/java/no/nordicsemi/android/support/v18/scanner/ScanSettings.java#L511

0reactions
sarbogastcommented, Nov 14, 2019

The phone I’m testing with is pretty standard and modern: Samsung Galaxy S9. So if that’s what it takes for it to work, I don’t see a lot of other choices.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scanForPeripherals(withServices:options:) - Apple Developer
To do this, your app must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter. The CBCentralManager scan...
Read more >
Scanning for BLE peripherals with a scan filter based on ...
My peripheral only advertises the UUID as a key in its serviceData associative array, so I switched to the serviceData filter as follows, ......
Read more >
The Ultimate Guide to Apple's Core Bluetooth - Punch Through
Learn the major components of Core Bluetooth, basic steps for scanning, connecting to, and interacting with a BLE peripheral, ...
Read more >
Best practice: Advanced BLE scanning process on iOS
The data can be exchange a small of information via BLE packets or the ... First, I will generate 5 UUIDs as the...
Read more >
Scan peripherals and filter by service UUID #230 - GitHub
Hi, Is it possible to scan peripherals and only consider those with a specific service UUID? BLE addresses change often if using Android...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found