Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ A few of the optional params require a bit of explanation:
#### seconds
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`.

#### avoidDuplicates
Set this to true if you don't want duplicates with the same serviceUUID reported in "onDiscovered" callback.
If true, only the first discovered peripheral with the same serviceUUID will be reported.

#### skipPermissionCheck
Set this to true if you don't want the plugin to check (and request) the required Bluetooth permissions.
Particularly useful if you're running this function on a non-UI thread (ie. a Worker).
Expand Down
21 changes: 19 additions & 2 deletions src/bluetooth.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,23 @@ export class Bluetooth extends BluetoothCommon {
// }
// }

let onPeripheralDiscovered;

if (args.onDiscovered) {
if (args.avoidDuplicates) {
const discoveredUUIDs = [];
onPeripheralDiscovered = (data: Peripheral) => {
const hasBeenDiscovered = discoveredUUIDs.includes(data.UUID);
if (!hasBeenDiscovered) {
discoveredUUIDs.push(data.UUID);
args.onDiscovered(data);
}
};
} else {
onPeripheralDiscovered = args.onDiscovered;
}
}

const filters = args.filters || [];

if (this.scanningReferTimer) {
Expand All @@ -1519,7 +1536,7 @@ export class Bluetooth extends BluetoothCommon {
uuids.push(stringToUuid(f.serviceUUID));
}
});
this.LeScanCallback.onPeripheralDiscovered = args.onDiscovered;
this.LeScanCallback.onPeripheralDiscovered = onPeripheralDiscovered;
const didStart = uuids.length === 0 ? this.adapter.startLeScan(this.LeScanCallback) : this.adapter.startLeScan(uuids, this.LeScanCallback);
if (Trace.isEnabled()) {
CLog(CLogTypes.info, methodName, '---- PreLollipop ---- didStart scanning:', didStart, JSON.stringify(uuids));
Expand Down Expand Up @@ -1577,7 +1594,7 @@ export class Bluetooth extends BluetoothCommon {
scanSettings.setCallbackType(androidCallbackType(callbackType));
}

this.scanCallback.onPeripheralDiscovered = args.onDiscovered;
this.scanCallback.onPeripheralDiscovered = onPeripheralDiscovered;
this.adapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings.build(), this.scanCallback);
if (Trace.isEnabled()) {
CLog(CLogTypes.info, methodName, ' ---- PostLollipop ---- didStart scanning:', JSON.stringify(filters));
Expand Down
6 changes: 6 additions & 0 deletions src/bluetooth.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ export interface StartScanningOptions {
*/
seconds?: number;

/**
* Avoid duplicates with the same serviceUUID in "onDiscovered" callback.
* If true, only the first discovered peripheral with the same serviceUUID will be reported.
*/
avoidDuplicates?: boolean;

/**
* This callback is invoked when a peripheral is found.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/bluetooth.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,13 @@ export class Bluetooth extends BluetoothCommon {
CLog(CLogTypes.info, methodName, '---- services:', services);
}

const options: NSMutableDictionary<any, any> = new (NSMutableDictionary as any)();
if (!args.avoidDuplicates) {
options.setObjectForKey(true, CBCentralManagerScanOptionAllowDuplicatesKey);
}

// TODO: check on the services as any casting
this.centralManager.scanForPeripheralsWithServicesOptions(services as any, null);
this.centralManager.scanForPeripheralsWithServicesOptions(services as any, options);
if (this.scanningReferTimer) {
clearTimeout(this.scanningReferTimer.timer);
this.scanningReferTimer.resolve();
Expand Down