Bluetooth LE Scan Filter not working

I only want to scan BLE beacons with a specific UUID in my Android code. Although I can add a filter for specific MAC addresses, I cannot get it to work with UUIDs. The onScanResult function is never called. Why could this be? I am using API 21 and I am not getting any errors for the project.

final String tagUUID = "01122334-4556-6778-899a-abbccddeeff0"; //does not work ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(UUID.fromString(tagUUID))).build(); //works ScanFilter filter = new ScanFilter.Builder().setDeviceAddress(tagMAC).build(); 
+7
android bluetooth-lowenergy
source share
2 answers

I am the author of the blog post mentioned above. Here's how to fix your problem for Android 21+.

 // Empty data byte[] manData = new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Data Mask byte[] mask = new byte[]{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}; // Copy UUID into data array and remove all "-" System.arraycopy(hexStringToByteArray("YOUR_UUID_TO_FILTER".replace("-","")), 0, manData, 2, 16); // Add data array to filters ScanFilter filter = new ScanFilter.Builder().setManufacturerData(76, manData, mask).build()); public static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } 

The problem here is that you can add UUID filtering, but this is not entirely straightforward.

+8
source share

It is possible that filtering looks for the service UUID in the AD Service "UUID" ad structure. What really makes sense and how it should work.

For the beacons, the UUID you are trying to find is actually in the AD Type Specific Data structure. And no one cares about finding Service UUIDs .

I believe that service UUID filtering is only for filtering UUIDs from services in the GATT database ; these UUIDs will be located, as I explained in the first paragraph.

This UUID in non -UUID beacons of the service as such. It is rather a beacon identifier with the UUID format.

+2
source share

All Articles