Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add background scan period APIs for Android #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions android/src/main/java/com/flutterbeacon/FlutterBeaconPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,32 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result)
}
}

if (call.method.equals("setBackgroundScanPeriod")) {
int scanPeriod = call.argument("scanPeriod");
this.beaconManager.setBackgroundScanPeriod(scanPeriod);
try {
this.beaconManager.updateScanPeriods();
result.success(true);
return;
} catch (RemoteException e) {
result.success(false);
return;
}
}

if (call.method.equals("setBackgroundBetweenScanPeriod")) {
int betweenScanPeriod = call.argument("betweenScanPeriod");
this.beaconManager.setBackgroundBetweenScanPeriod(betweenScanPeriod);
try {
this.beaconManager.updateScanPeriods();
result.success(true);
return;
} catch (RemoteException e) {
result.success(false);
return;
}
}

if (call.method.equals("setLocationAuthorizationTypeDefault")) {
// Android does not have the concept of "requestWhenInUse" and "requestAlways" like iOS does,
// so this method does nothing.
Expand Down
14 changes: 14 additions & 0 deletions ios/Classes/FlutterBeaconPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result(@(YES));
return;
}

if ([@"setBackgroundScanPeriod" isEqualToString:call.method]) {
// do nothing

result(@(YES));
return;
}

if ([@"setBackgroundBetweenScanPeriod" isEqualToString:call.method]) {
// do nothing

result(@(YES));
return;
}

if ([@"openApplicationSettings" isEqualToString:call.method]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
Expand Down
20 changes: 20 additions & 0 deletions lib/flutter_beacon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ class FlutterBeacon {
'setBetweenScanPeriod', {"betweenScanPeriod": scanPeriod});
}

/// Customize the duration of the beacon scan in the background on the Android platform.
///
/// [scanPeriod] specifies the duration of the scan in milliseconds.
///
/// Returns a [Future] that completes with `true` if the operation was successful.
Future<bool> setBackgroundScanPeriod(int scanPeriod) async {
return await _methodChannel
.invokeMethod('setBackgroundScanPeriod', {"scanPeriod": scanPeriod});
}

/// Customize the duration between beacon scans in the background on the Android platform.
///
/// [scanPeriod] specifies the wait time between scans in milliseconds.
///
/// Returns a [Future] that completes with `true` if the operation was successful.
Future<bool> setBackgroundBetweenScanPeriod(int scanPeriod) async {
return await _methodChannel.invokeMethod(
'setBackgroundBetweenScanPeriod', {"betweenScanPeriod": scanPeriod});
}

/// Close scanning API.
Future<bool> get close async {
final result = await _methodChannel.invokeMethod('close');
Expand Down
22 changes: 22 additions & 0 deletions test/flutter_beacon_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ void main() {
return true;
}

if (method == 'setBackgroundScanPeriod') {
return true;
}

if (method == 'setBackgroundBetweenScanPeriod') {
return true;
}

throw MissingPluginException(
'No implementation found for method $method on channel ${channel.name}');
});
Expand Down Expand Up @@ -285,6 +293,20 @@ void main() {
true,
);
});

test('SetBackgroundScanPeriod return "true"', () async {
expect(
await flutterBeacon.setBackgroundScanPeriod(1000),
true,
);
});

test('SetBackgroundBetweenScanPeriod return "true"', () async {
expect(
await flutterBeacon.setBackgroundBetweenScanPeriod(400),
true,
);
});
});

group('Event channel - ranging', () {
Expand Down