From b85026993103810feb8091212a0914e40fc2e774 Mon Sep 17 00:00:00 2001 From: younesspotmaster <142569654+younesspotmaster@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:45:13 +0200 Subject: [PATCH] Implement forceScanFilter for Android (#989) * Implement forceScanFilter for Android * Only forceScanFilter when no uuids are provided --------- Co-authored-by: Younes --- README.md | 3 ++- src/android/BLECentralPlugin.java | 7 +++++++ types.d.ts | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index de323646..ac9dcb37 100644 --- a/README.md +++ b/README.md @@ -255,9 +255,10 @@ See the [location permission notes](#location-permission-notes) above for inform Can be one of: _aggressive_ | _sticky_ - _numOfMatches_: String defines [setNumOfMatches()](https://developer.android.com/reference/kotlin/android/bluetooth/le/ScanSettings.Builder#setnumofmatches) argument on Android. Can be one of: _one_ | _few_ | _max_ - - _phy_: String for [setPhy()](https://developer.android.com/reference/kotlin/android/bluetooth/le/ScanSettings.Builder#setphy) on Android. + - _phy_: String for [setPhy()](https://developer.android.com/reference/kotlin/android/bluetooth/le/ScanSettings.Builder#setphy) on Android. Can be one of: _1m_ | _coded_ | _all_ - _legacy_: _true_ or _false_ to [control filtering](https://developer.android.com/reference/kotlin/android/bluetooth/le/ScanSettings.Builder#setlegacy) bluetooth spec.pre-4.2 advertisements on Android. + - _forceScanFilter_: _true_ or _false_ to [to force an empty scan filter](https://github.com/don/cordova-plugin-ble-central/issues/987) if no other filters are provided on Android. - _reportDelay_: Milliseconds for [setReportDelay()](https://developer.android.com/reference/kotlin/android/bluetooth/le/ScanSettings.Builder#setreportdelay) on Android. _0_ to be notified of results immediately. Values > _0_ causes the scan results to be queued up and delivered after the requested delay or when the internal buffers fill up. - **success**: Success callback function that is invoked which each discovered device. - **failure**: Error callback function, invoked when error occurs. [optional] diff --git a/src/android/BLECentralPlugin.java b/src/android/BLECentralPlugin.java index b1662a46..0d820c64 100644 --- a/src/android/BLECentralPlugin.java +++ b/src/android/BLECentralPlugin.java @@ -127,6 +127,7 @@ public class BLECentralPlugin extends CordovaPlugin { // scan options boolean reportDuplicates = false; + boolean forceScanFilter = false; private static final int REQUEST_BLUETOOTH_SCAN = 2; private static final int REQUEST_BLUETOOTH_CONNECT = 3; @@ -410,6 +411,7 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback resetScanOptions(); this.reportDuplicates = options.optBoolean("reportDuplicates", false); + this.forceScanFilter = options.optBoolean("forceScanFilter", false); ScanSettings.Builder scanSettings = new ScanSettings.Builder(); switch (options.optString("scanMode", "")) { @@ -1254,7 +1256,11 @@ private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] servic new ParcelUuid(uuid)).build(); filters.add(filter); } + } else if (this.forceScanFilter) { + ScanFilter filter = new ScanFilter.Builder().build(); + filters.add(filter); } + stopScanHandler.removeCallbacks(stopScanRunnable); bluetoothLeScanner.startScan(filters, scanSettings, leScanCallback); @@ -1450,6 +1456,7 @@ private UUID uuidFromString(String uuid) { */ private void resetScanOptions() { this.reportDuplicates = false; + this.forceScanFilter = false; } private void addBondStateListener() { diff --git a/types.d.ts b/types.d.ts index c3cbfa89..18a33985 100644 --- a/types.d.ts +++ b/types.d.ts @@ -43,6 +43,8 @@ declare namespace BLECentralPlugin { legacy?: boolean; /* Android only */ reportDelay?: number; + /* Android only */ + forceScanFilter?: number; reportDuplicates?: boolean; /** Scanning duration in seconds */