Skip to content

Commit

Permalink
Implement forceScanFilter for Android (#989)
Browse files Browse the repository at this point in the history
* Implement forceScanFilter for Android
* Only forceScanFilter when no uuids are provided
---------

Co-authored-by: Younes <[email protected]>
  • Loading branch information
younesspotmaster and Younes authored Sep 21, 2023
1 parent aaa158e commit b850269
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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", "")) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1450,6 +1456,7 @@ private UUID uuidFromString(String uuid) {
*/
private void resetScanOptions() {
this.reportDuplicates = false;
this.forceScanFilter = false;
}

private void addBondStateListener() {
Expand Down
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ declare namespace BLECentralPlugin {
legacy?: boolean;
/* Android only */
reportDelay?: number;
/* Android only */
forceScanFilter?: number;

reportDuplicates?: boolean;
/** Scanning duration in seconds */
Expand Down

0 comments on commit b850269

Please sign in to comment.