cordova-plugin-ble-central icon indicating copy to clipboard operation
cordova-plugin-ble-central copied to clipboard

startScan() doesn't support filtering by manufacturer data

Open paesedombre opened this issue 2 years ago • 3 comments

There is any option to set an empty list of service UUID or filter for manufacturer data? I want decode a iBeacon frame when i turn off the screen but Android doesn't allow.

paesedombre avatar Jun 09 '22 15:06 paesedombre

Hello! For iBeacon scanning specifically, this plugin probably won't be the best fit as it can't scan by manufacturer data (iOS has no equivalent interface that I'm aware of).

You can attempt to scan with an empty service UUID list, but many Android manufacturers will block background scanning like this.

peitschie avatar Jun 09 '22 23:06 peitschie

It might help someone, I was able to make the app work in the background by adding a filter (for iBeacon) in the file node_modules > cordova-plugin-ble-central > src > android > BLECentralPlugin.java

and inserted these instructions at about line 980:

discoverCallback = callbackContext;
bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
List<ScanFilter> filters = new ArrayList<ScanFilter>();
ScanFilter.Builder builder = new ScanFilter.Builder();
                builder.setManufacturerData(0x004c, new byte[] {});
                ScanFilter filter = builder.build();
                filters.add(filter);
bluetoothLeScanner.startScan(filters, scanSettings, leScanCallback);

paesedombre avatar Jul 18 '22 09:07 paesedombre

Handy to know @paesedombre ... thanks for sharing!

If you feel so inspired, I'd be happy to have a patch that adds support for this to the startScanWithOptions method: https://github.com/don/cordova-plugin-ble-central#startscanwithoptions

There are a bunch of Android-specific ones there already, so adding an option like this would be fine:

"manufacturerData": {
  "id": 10,
  "data": ArrayBuffer,
  "dataMask": ArrayBuffer
}

peitschie avatar Jul 18 '22 23:07 peitschie