Are you struggling with filtering BLE devices on certain Android smartphones? Do you want to increase the flexibility of your Bluetooth Low Energy (BLE) scanning by filtering devices based on their UUID? In this article, we’ll explore a common issue faced by developers and provide a solution to filter BLE devices using Service Data UUID. Let’s dive in!
Understanding the Problem: MAC Address Filtering Limitations
When it comes to filtering BLE devices, many developers rely on MAC address filtering. While this approach works fine for most scenarios, it lacks the flexibility to filter devices based on other criteria. To address this limitation, we seek an alternative solution using UUID-based filtering.
Introducing Service Data UUID Filtering
The Service Data – 128-bit UUID AD type, identified as 0x21, allows devices to beacon data with a specific UUID. By leveraging this feature, we can implement more advanced filtering mechanisms for BLE devices. However, there might be compatibility issues with certain Android devices.
The Compatibility Conundrum: Xiaomi MI9 vs. Xiaomi MI A1 and Huawei P10
In our initial testing, we found that the code for UUID-based filtering works flawlessly on the Xiaomi MI9 running Android 9. However, when we tested it on the Xiaomi MI A1 (Android 9) and Huawei P10 (Android 8), it failed to filter devices properly. Interestingly, the filtering worked fine when using other apps like nRF Connect’s BLE scanner.
Cracking the Compatibility Code: Adjusting the Service Data Filter
To solve this compatibility issue, we need to make an adjustment to the filter code. The issue might be related to an all-zero mask used for the Service UUID filtering, which can cause inconsistent behavior across different BLE chipsets. To ensure broad compatibility, we suggest using a more inclusive mask. Here’s an example of how to set the mask:
String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"; ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString); builder.setServiceUuid(new ParcelUuid(SERVICE_DATA_UUID), parcelUuidMask);
By applying this adjustment, you should be able to achieve consistent filtering across various Android devices.
Why Other Apps Might Mislead You
While other BLE scanner apps may work fine without using scan filters, they aren’t an accurate representation of the behavior of your filtering implementation. Most off-the-shelf mobile apps don’t utilize scan filters, so their successful scanning doesn’t necessarily reflect the effectiveness of your custom filtering code.
Implementing BLE scan filters by Service Data UUID provides developers with increased flexibility in filtering devices based on their UUID. Although certain Android devices may exhibit compatibility issues, adjusting the mask as explained can help resolve the problem. It’s important to remember that relying on the behavior of other apps may lead to misleading results. By customizing your scan filters, you can enhance the accuracy and efficiency of your BLE scanning process.
We hope this article has shed light on the challenges of implementing BLE scan filters by Service Data UUID and provided a viable solution. Stay tuned for more informative content on BLE development!