简体   繁体   中英

BLE scan filter by Service Data UUID

We have devices that beacon data with «Service Data - 128-bit UUID» AD type: 0x21. For Android scan, we are filtering BLE device by MAC addresses. It works fine.

Because we need more flexibility, we want to filter by UUID.

My code works fine only with Xiaomi MI9 (Android 9).

It doesn't work on Xiaomi MI A1 (Android 9) and Huawei P10 (Android 8). (But it works when I launch a BLE scanner with another app, like nRF Connect)

Anyone knows how to solve this or does some phones simply not support certain types of filtering?

static final UUID SERVICE_DATA_UUID = UUID.fromString("00010001-0000-1000-8000-112233445566");

byte[] setServiceData = new byte[] {
        0x11
};

filters = new ArrayList<>();
ScanFilter.Builder builder = new ScanFilter.Builder()
        .setServiceData(new ParcelUuid(SERVICE_DATA_UUID), setServiceData);
        //.setDeviceAddress(Globals.MAC[1]);
        filters.add(builder.build());

mBluetoothLeScanner.startScan(filters, settings, mLeScanCallback);

Logcat when it works (MI9 or when I activate nRF in parallel)

D/Constraints: ScanRecord [mAdvertiseFlags=2, mServiceUuids=null, mServiceSolicitationUuids=null, mManufacturerSpecificData={}, mServiceData={00020001-0000-1000-8000-112233445566=[17]}, mTxPowerLevel=-2147483648, mDeviceName=null]

The mask should not be all zero -- if so it won't match anything. It should generally be set to something like below so that it matches all bytes of the Service UUID:

 String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
 ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString);
 builder.setServiceUuid(new ParcelUuid(SERVICE_DATA_UUID), parcelUuidMask);

I suspect that the all zero mask you are using works on some devices and not other devices because the behavior of an all zero mask is indeterminate. It might vary from one BLE chipset to the next.

Most off the shelf mobile apps do not use scan filters, so they are not a good judge of whether filters work on a specific device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM