Skip to content

Commit 096ed42

Browse files
authored
Merge pull request #241 from sebj54/master
feat: `avoidDuplicates` option on `startScanning` method
2 parents a81ffcf + 25dba13 commit 096ed42

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ A few of the optional params require a bit of explanation:
119119
#### seconds
120120
Scanning for peripherals drains the battery quickly, so you better not scan any longer than necessary. If a peripheral is in range and not engaged in another connection it usually pops up in under a second. If you don't pass in a number of seconds you will need to manually call `stopScanning`.
121121

122+
#### avoidDuplicates
123+
Set this to true if you don't want duplicates with the same serviceUUID reported in "onDiscovered" callback.
124+
If true, only the first discovered peripheral with the same serviceUUID will be reported.
125+
122126
#### skipPermissionCheck
123127
Set this to true if you don't want the plugin to check (and request) the required Bluetooth permissions.
124128
Particularly useful if you're running this function on a non-UI thread (ie. a Worker).

src/bluetooth.android.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,23 @@ export class Bluetooth extends BluetoothCommon {
15061506
// }
15071507
// }
15081508

1509+
let onPeripheralDiscovered;
1510+
1511+
if (args.onDiscovered) {
1512+
if (args.avoidDuplicates) {
1513+
const discoveredUUIDs = [];
1514+
onPeripheralDiscovered = (data: Peripheral) => {
1515+
const hasBeenDiscovered = discoveredUUIDs.includes(data.UUID);
1516+
if (!hasBeenDiscovered) {
1517+
discoveredUUIDs.push(data.UUID);
1518+
args.onDiscovered(data);
1519+
}
1520+
};
1521+
} else {
1522+
onPeripheralDiscovered = args.onDiscovered;
1523+
}
1524+
}
1525+
15091526
const filters = args.filters || [];
15101527

15111528
if (this.scanningReferTimer) {
@@ -1519,7 +1536,7 @@ export class Bluetooth extends BluetoothCommon {
15191536
uuids.push(stringToUuid(f.serviceUUID));
15201537
}
15211538
});
1522-
this.LeScanCallback.onPeripheralDiscovered = args.onDiscovered;
1539+
this.LeScanCallback.onPeripheralDiscovered = onPeripheralDiscovered;
15231540
const didStart = uuids.length === 0 ? this.adapter.startLeScan(this.LeScanCallback) : this.adapter.startLeScan(uuids, this.LeScanCallback);
15241541
if (Trace.isEnabled()) {
15251542
CLog(CLogTypes.info, methodName, '---- PreLollipop ---- didStart scanning:', didStart, JSON.stringify(uuids));
@@ -1577,7 +1594,7 @@ export class Bluetooth extends BluetoothCommon {
15771594
scanSettings.setCallbackType(androidCallbackType(callbackType));
15781595
}
15791596

1580-
this.scanCallback.onPeripheralDiscovered = args.onDiscovered;
1597+
this.scanCallback.onPeripheralDiscovered = onPeripheralDiscovered;
15811598
this.adapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings.build(), this.scanCallback);
15821599
if (Trace.isEnabled()) {
15831600
CLog(CLogTypes.info, methodName, ' ---- PostLollipop ---- didStart scanning:', JSON.stringify(filters));

src/bluetooth.common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ export interface StartScanningOptions {
220220
*/
221221
seconds?: number;
222222

223+
/**
224+
* Avoid duplicates with the same serviceUUID in "onDiscovered" callback.
225+
* If true, only the first discovered peripheral with the same serviceUUID will be reported.
226+
*/
227+
avoidDuplicates?: boolean;
228+
223229
/**
224230
* This callback is invoked when a peripheral is found.
225231
*/

src/bluetooth.ios.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,13 @@ export class Bluetooth extends BluetoothCommon {
823823
CLog(CLogTypes.info, methodName, '---- services:', services);
824824
}
825825

826+
const options: NSMutableDictionary<any, any> = new (NSMutableDictionary as any)();
827+
if (!args.avoidDuplicates) {
828+
options.setObjectForKey(true, CBCentralManagerScanOptionAllowDuplicatesKey);
829+
}
830+
826831
// TODO: check on the services as any casting
827-
this.centralManager.scanForPeripheralsWithServicesOptions(services as any, null);
832+
this.centralManager.scanForPeripheralsWithServicesOptions(services as any, options);
828833
if (this.scanningReferTimer) {
829834
clearTimeout(this.scanningReferTimer.timer);
830835
this.scanningReferTimer.resolve();

0 commit comments

Comments
 (0)