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

CycledLeScanner : a version scanning when screen turns on or off #450

Open
wants to merge 15 commits 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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ android {
targetCompatibility JavaVersion.VERSION_1_7
}

sourceSets {
/*sourceSets {
androidTest {
setRoot('src/test')
}
}
}*/

lintOptions {
abortOnError false
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/altbeacon/beacon/BeaconManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.altbeacon.beacon.service.RegionMonitoringState;
import org.altbeacon.beacon.service.RunningAverageRssiFilter;
import org.altbeacon.beacon.service.StartRMData;
import org.altbeacon.beacon.service.scanner.CycledLeScanner;
import org.altbeacon.beacon.service.scanner.NonBeaconLeScanCallback;
import org.altbeacon.beacon.simulator.BeaconSimulator;

Expand Down Expand Up @@ -117,6 +118,7 @@ public class BeaconManager {
protected final Set<MonitorNotifier> monitorNotifiers = new CopyOnWriteArraySet<>();
private final ArrayList<Region> rangedRegions = new ArrayList<Region>();
private final List<BeaconParser> beaconParsers = new CopyOnWriteArrayList<>();
private CycledLeScanner cycledLeScanner;
private NonBeaconLeScanCallback mNonBeaconLeScanCallback;
private boolean mBackgroundMode = false;
private boolean mBackgroundModeUninitialized = true;
Expand Down Expand Up @@ -253,6 +255,8 @@ protected BeaconManager(Context context) {
verifyServiceDeclaration();
}
this.beaconParsers.add(new AltBeaconParser());
cycledLeScanner = new CycledLeScanner(BeaconManager.DEFAULT_FOREGROUND_SCAN_PERIOD,
BeaconManager.DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD, false);
}

/**
Expand Down Expand Up @@ -997,4 +1001,20 @@ public static void setAndroidLScanningDisabled(boolean disabled) {
public static void setsManifestCheckingDisabled(boolean disabled) {
sManifestCheckingDisabled = disabled;
}

/**
*
* @return the CycledLeScanner to use
*/
public CycledLeScanner getCycledLeScanner(){
return cycledLeScanner;
}

/**
* Permits to update the way the library scans for beacon
* @param cycledLeScanner : the CycledLeScanner to use to scan for beacons
*/
public void setCycledLeScanner(CycledLeScanner cycledLeScanner){
this.cycledLeScanner = cycledLeScanner;
}
}
20 changes: 15 additions & 5 deletions src/main/java/org/altbeacon/beacon/service/BeaconService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import android.os.Binder;
Expand All @@ -51,6 +52,8 @@
import org.altbeacon.beacon.service.scanner.CycledLeScanCallback;
import org.altbeacon.beacon.service.scanner.CycledLeScanner;
import org.altbeacon.beacon.service.scanner.NonBeaconLeScanCallback;
import org.altbeacon.beacon.service.scanner.screenstate.ScreenStateBroadcastReceiver;
import org.altbeacon.beacon.service.scanner.screenstate.ScreenStateListener;
import org.altbeacon.beacon.startup.StartupBroadcastReceiver;
import org.altbeacon.bluetooth.BluetoothCrashResolver;

Expand Down Expand Up @@ -91,6 +94,7 @@ public class BeaconService extends Service {
private boolean mBackgroundFlag = false;
private ExtraDataBeaconTracker mExtraDataBeaconTracker;
private ExecutorService mExecutor;
private ScreenStateBroadcastReceiver screenStateBroadcastReceiver;

/*
* The scan period is how long we wait between restarting the BLE advertisement scans
Expand Down Expand Up @@ -197,12 +201,15 @@ public void onCreate() {
// Create a private executor so we don't compete with threads used by AsyncTask
// This uses fewer threads than the default executor so it won't hog CPU
mExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);

mCycledScanner = CycledLeScanner.createScanner(this, BeaconManager.DEFAULT_FOREGROUND_SCAN_PERIOD,
BeaconManager.DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD, mBackgroundFlag, mCycledLeScanCallback, bluetoothCrashResolver);

beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());

mCycledScanner = beaconManager.getCycledLeScanner();
mCycledScanner.initScanner(this, mCycledLeScanCallback, bluetoothCrashResolver);
if(mCycledScanner instanceof ScreenStateListener){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
screenStateBroadcastReceiver = new ScreenStateBroadcastReceiver();
registerReceiver(screenStateBroadcastReceiver, filter);
}
//flatMap all beacon parsers
boolean matchBeaconsByServiceUUID = true;
if (beaconManager.getBeaconParsers() != null) {
Expand Down Expand Up @@ -275,6 +282,9 @@ public void onDestroy() {
mCycledScanner.stop();
mCycledScanner.destroy();
monitoringStatus.stopStatusPreservation();
if(screenStateBroadcastReceiver != null){
unregisterReceiver(screenStateBroadcastReceiver);
}
}

@Override
Expand Down
Loading