Skip to content

Commit

Permalink
Standardise all scans to go via scanWithOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed May 6, 2023
1 parent f131861 commit 6d507fd
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 85 deletions.
22 changes: 1 addition & 21 deletions src/android/BLECentralPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public class BLECentralPlugin extends CordovaPlugin {
private static final String BLUETOOTH_SCAN = "android.permission.BLUETOOTH_SCAN" ; // API 31

// actions
private static final String SCAN = "scan";
private static final String START_SCAN = "startScan";
private static final String STOP_SCAN = "stopScan";
private static final String START_SCAN_WITH_OPTIONS = "startScanWithOptions";
private static final String BONDED_DEVICES = "bondedDevices";
Expand Down Expand Up @@ -210,21 +208,7 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
}

boolean validAction = true;

if (action.equals(SCAN)) {

UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0));
int scanSeconds = args.getInt(1);
resetScanOptions();
findLowEnergyDevices(callbackContext, serviceUUIDs, scanSeconds);

} else if (action.equals(START_SCAN)) {

UUID[] serviceUUIDs = parseServiceUUIDList(args.getJSONArray(0));
resetScanOptions();
findLowEnergyDevices(callbackContext, serviceUUIDs, -1);

} else if (action.equals(STOP_SCAN)) {
if (action.equals(STOP_SCAN)) {
stopScan();
callbackContext.success();

Expand Down Expand Up @@ -1187,10 +1171,6 @@ public void onScanFailed(int errorCode) {
};


private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] serviceUUIDs, int scanSeconds) {
findLowEnergyDevices( callbackContext, serviceUUIDs, scanSeconds, new ScanSettings.Builder().build() );
}

private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] serviceUUIDs, int scanSeconds, ScanSettings scanSettings) {

if (!locationServicesEnabled() && Build.VERSION.SDK_INT < 31) {
Expand Down
9 changes: 1 addition & 8 deletions src/browser/BLECentralPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ function formatUUID(uuid) {

module.exports = {
deviceInfos: new Map(),

scan: function(services, seconds, success, failure) {
return this.startScanWithOptions(services, {}, success, failure);
},
startScan: function(services, success, failure) {
return this.startScanWithOptions(services, {}, success, failure);
},
startScanWithOptions: function(services, options, success, failure) {
startScanWithOptions: function (services, options, success, failure) {
if (!navigator.bluetooth) {
failure('Bluetooth is not supported on this browser.');
return;
Expand Down
2 changes: 0 additions & 2 deletions src/ios/BLECentralPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
@property (strong, nonatomic) NSMutableSet *peripherals;
@property (strong, nonatomic) CBCentralManager *manager;

- (void)scan:(CDVInvokedUrlCommand *)command;
- (void)startScan:(CDVInvokedUrlCommand *)command;
- (void)startScanWithOptions:(CDVInvokedUrlCommand *)command;
- (void)stopScan:(CDVInvokedUrlCommand *)command;
- (void)connectedPeripheralsWithServices:(CDVInvokedUrlCommand*)command;
Expand Down
44 changes: 0 additions & 44 deletions src/ios/BLECentralPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -354,50 +354,6 @@ - (void)isEnabled:(CDVInvokedUrlCommand*)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)scan:(CDVInvokedUrlCommand*)command {
NSLog(@"scan");
if ([manager state] != CBManagerStatePoweredOn) {
NSString *error = @"Bluetooth is disabled";
NSLog(@"%@", error);
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:error];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

discoverPeripheralCallbackId = [command.callbackId copy];

NSArray<NSString *> *serviceUUIDStrings = [command argumentAtIndex:0];
NSNumber *timeoutSeconds = [command argumentAtIndex:1];
NSArray<CBUUID *> *serviceUUIDs = [self uuidStringsToCBUUIDs:serviceUUIDStrings];

[manager scanForPeripheralsWithServices:serviceUUIDs options:nil];

[NSTimer scheduledTimerWithTimeInterval:[timeoutSeconds floatValue]
target:self
selector:@selector(stopScanTimer:)
userInfo:[command.callbackId copy]
repeats:NO];
}

- (void)startScan:(CDVInvokedUrlCommand*)command {
NSLog(@"startScan");
if ([manager state] != CBManagerStatePoweredOn) {
NSString *error = @"Bluetooth is disabled";
NSLog(@"%@", error);
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:error];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

discoverPeripheralCallbackId = [command.callbackId copy];
NSArray<NSString *> *serviceUUIDStrings = [command argumentAtIndex:0];
NSArray<CBUUID *> *serviceUUIDs = [self uuidStringsToCBUUIDs:serviceUUIDStrings];

[manager scanForPeripheralsWithServices:serviceUUIDs options:nil];
}

- (void)startScanWithOptions:(CDVInvokedUrlCommand*)command {
NSLog(@"startScanWithOptions");
if ([manager state] != CBManagerStatePoweredOn) {
Expand Down
12 changes: 2 additions & 10 deletions www/ble.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,11 @@ var autoconnected = {};

module.exports = {
scan: function (services, seconds, success, failure) {
var successWrapper = function (peripheral) {
convertToNativeJS(peripheral);
success(peripheral);
};
cordova.exec(successWrapper, failure, 'BLE', 'scan', [services, seconds]);
module.exports.startScanWithOptions(services, { duration: seconds }, success, failure);
},

startScan: function (services, success, failure) {
var successWrapper = function (peripheral) {
convertToNativeJS(peripheral);
success(peripheral);
};
cordova.exec(successWrapper, failure, 'BLE', 'startScan', [services]);
module.exports.startScanWithOptions(services, undefined, success, failure);
},

stopScan: function (success, failure) {
Expand Down

0 comments on commit 6d507fd

Please sign in to comment.