Skip to content

Commit

Permalink
AllowDuplicates option in scan function (pauldemarco#460)
Browse files Browse the repository at this point in the history
* added variable to enable or disable duplicate bluetooth devices

* updated proto with new allowDuplicates field in ScanSettings

* implemented allowDuplicates opts in FlutterBluePlugin.m

* implementing allowDuplicates opt in dart

* android: connected allowDuplicates

* missing parameter in startScan method

* fix typo

Co-Authored-By: Maurits van Beusekom <maurits@baseflow.com>

Co-authored-by: Simone Venturini <simone.venturini87@gmail.com>
Co-authored-by: Enrico Zamagni <ezamagni@technogym.com>
Co-authored-by: Maurits van Beusekom <maurits@baseflow.com>
  • Loading branch information
4 people authored Apr 20, 2020
1 parent 47067e4 commit 453dc5a
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public class FlutterBluePlugin implements FlutterPlugin, ActivityAware, MethodCa
// Pending call and result for startScan, in the case where permissions are needed
private MethodCall pendingCall;
private Result pendingResult;
private ArrayList<String> macDeviceScanned = new ArrayList<>();
private boolean allowDuplicates = false;

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
Expand Down Expand Up @@ -741,6 +743,7 @@ private void startScan(MethodCall call, Result result) {
Protos.ScanSettings settings;
try {
settings = Protos.ScanSettings.newBuilder().mergeFrom(data).build();
allowDuplicates = settings.getAllowDuplicates();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startScan21(settings);
} else {
Expand Down Expand Up @@ -770,6 +773,10 @@ private ScanCallback getScanCallback21() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (!allowDuplicates && result != null && result.getDevice() != null && result.getDevice().getAddress() != null) {
if (macDeviceScanned.contains(result.getDevice().getAddress())) return;
macDeviceScanned.add(result.getDevice().getAddress());
}
Protos.ScanResult scanResult = ProtoMaker.from(result.getDevice(), result);
invokeMethodUIThread("ScanResult", scanResult.toByteArray());
}
Expand Down Expand Up @@ -819,6 +826,11 @@ private BluetoothAdapter.LeScanCallback getScanCallback18() {
@Override
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi,
byte[] scanRecord) {
if (!allowDuplicates && bluetoothDevice != null && bluetoothDevice.getAddress() != null) {
if (macDeviceScanned.contains(bluetoothDevice.getAddress())) return;
macDeviceScanned.add(bluetoothDevice.getAddress());
}

Protos.ScanResult scanResult = ProtoMaker.from(bluetoothDevice, scanRecord, rssi);
invokeMethodUIThread("ScanResult", scanResult.toByteArray());
}
Expand Down Expand Up @@ -1005,4 +1017,4 @@ class BluetoothDeviceCache {
}
}

}
}
7 changes: 5 additions & 2 deletions ios/Classes/FlutterBluePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSString *u = [request.serviceUuidsArray objectAtIndex:i];
uuids = [uuids arrayByAddingObject:[CBUUID UUIDWithString:u]];
}
// TODO: iOS Scan Options (#35)
[self->_centralManager scanForPeripheralsWithServices:uuids options:nil];
NSMutableDictionary<NSString *, id> *scanOpts = [NSMutableDictionary new];
if (request.allowDuplicates) {
[scanOpts setObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
}
[self->_centralManager scanForPeripheralsWithServices:uuids options:scanOpts];
result(nil);
} else if([@"stopScan" isEqualToString:call.method]) {
[self->_centralManager stopScan];
Expand Down
5 changes: 4 additions & 1 deletion ios/gen/Flutterblue.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion ios/gen/Flutterblue.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 453dc5a

Please sign in to comment.