Skip to content

Commit

Permalink
changed the way scanning starts and stops on monitoring/ranging start…
Browse files Browse the repository at this point in the history
…s and stops to prevent multiple cycles from running simultaneously
  • Loading branch information
David G. Young authored and David G. Young committed Apr 16, 2014
1 parent b17ab85 commit 1a2aa13
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 112 deletions.
80 changes: 0 additions & 80 deletions AndroidIBeaconLibrary.iml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class IBeaconService extends Service {
private Handler handler = new Handler();
private int bindCount = 0;
private BluetoothCrashResolver bluetoothCrashResolver;
private boolean scanCyclerStarted = false;
private boolean scanningEnabled = false;

/*
* The scan period is how long we wait between restarting the BLE advertisement scans
Expand Down Expand Up @@ -253,17 +255,22 @@ public void startRangingBeaconsInRegion(Region region, Callback callback) {
}
rangedRegionState.put(region, new RangeState(callback));
}
if (!scanning) {
scanLeDevice(true);
if (IBeaconManager.LOG_DEBUG)
Log.d(TAG, "Currently ranging " + rangedRegionState.size() + " regions.");
if (!scanningEnabled) {
enableScanning();
}
}

public void stopRangingBeaconsInRegion(Region region) {
synchronized (rangedRegionState) {
rangedRegionState.remove(region);
}
if (scanning && rangedRegionState.size() == 0 && monitoredRegionState.size() == 0) {
scanLeDevice(false);
if (IBeaconManager.LOG_DEBUG)
Log.d(TAG, "Currently ranging " + rangedRegionState.size() + " regions.");

if (scanningEnabled && rangedRegionState.size() == 0 && monitoredRegionState.size() == 0) {
disableScanning();
}
}

Expand All @@ -278,8 +285,8 @@ public void startMonitoringBeaconsInRegion(Region region, Callback callback) {
}
if (IBeaconManager.LOG_DEBUG)
Log.d(TAG, "Currently monitoring " + monitoredRegionState.size() + " regions.");
if (!scanning) {
scanLeDevice(true);
if (!scanningEnabled) {
enableScanning();
}
}

Expand All @@ -290,8 +297,8 @@ public void stopMonitoringBeaconsInRegion(Region region) {
}
if (IBeaconManager.LOG_DEBUG)
Log.d(TAG, "Currently monitoring " + monitoredRegionState.size() + " regions.");
if (scanning && rangedRegionState.size() == 0 && monitoredRegionState.size() == 0) {
scanLeDevice(false);
if (scanningEnabled && rangedRegionState.size() == 0 && monitoredRegionState.size() == 0) {
disableScanning();
}
}

Expand Down Expand Up @@ -326,8 +333,19 @@ public void setScanPeriods(long scanPeriod, long betweenScanPeriod) {
private long nextScanStartTime = 0l;
private long scanStopTime = 0l;

public void enableScanning() {
scanningEnabled = true;
if (!scanCyclerStarted) {
scanLeDevice(true);
}
}
public void disableScanning() {
scanningEnabled = false;
}

@TargetApi(18)
private void scanLeDevice(final Boolean enable) {
scanCyclerStarted = true;
if (android.os.Build.VERSION.SDK_INT < 18) {
Log.w(TAG, "Not supported prior to API 18.");
return;
Expand Down Expand Up @@ -370,7 +388,12 @@ public void run() {
Log.w(TAG, "Skipping scan because crash recovery is in progress.");
}
else {
getBluetoothAdapter().startLeScan((BluetoothAdapter.LeScanCallback)getLeScanCallback());
if (scanningEnabled) {
getBluetoothAdapter().startLeScan((BluetoothAdapter.LeScanCallback)getLeScanCallback());
}
else {
if (IBeaconManager.LOG_DEBUG) Log.d(TAG, "Scanning unnecessary - no monitoring or ranging active.");
}
}
lastScanStartTime = new Date().getTime();
} else {
Expand Down Expand Up @@ -425,37 +448,45 @@ private void finishScanCycle() {
if (IBeaconManager.LOG_DEBUG) Log.d(TAG, "Done with scan cycle");
processExpiredMonitors();
if (scanning == true) {
processRangeData();
// If we want to use simulated scanning data, do it here. This is used for testing in an emulator
if (simulatedScanData != null) {
// if simulatedScanData is provided, it will be seen every scan cycle. *in addition* to anything actually seen in the air
// it will not be used if we are not in debug mode
if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
for (IBeacon iBeacon : simulatedScanData) {
processIBeaconFromScan(iBeacon);
}
} else {
Log.w(TAG, "Simulated scan data provided, but ignored because we are not running in debug mode. Please remove simulated scan data for production.");
}
}
if (getBluetoothAdapter() != null) {
if (getBluetoothAdapter().isEnabled()) {
getBluetoothAdapter().stopLeScan((BluetoothAdapter.LeScanCallback)getLeScanCallback());
lastScanEndTime = new Date().getTime();
} else {
Log.w(TAG, "Bluetooth is disabled. Cannot scan for iBeacons.");
}
}

if (!anyRangingOrMonitoringRegionsActive()) {
if (IBeaconManager.LOG_DEBUG)
Log.d(TAG, "Not starting scan because no monitoring or ranging regions are defined.");
scanCyclerStarted = false;
} else {
processRangeData();
if (IBeaconManager.LOG_DEBUG)
Log.d(TAG, "Restarting scan. Unique beacons seen last cycle: " + trackedBeacons.size()+" Total iBeacon advertisment packets seen: "+trackedBeaconsPacketCount);
if (getBluetoothAdapter() != null) {
if (getBluetoothAdapter().isEnabled()) {
getBluetoothAdapter().stopLeScan((BluetoothAdapter.LeScanCallback)getLeScanCallback());
lastScanEndTime = new Date().getTime();
} else {
Log.w(TAG, "Bluetooth is disabled. Cannot scan for iBeacons.");
}
}
Log.d(TAG, "Restarting scan. Unique beacons seen last cycle: " + trackedBeacons.size()+" Total iBeacon advertisement packets seen: "+trackedBeaconsPacketCount);

scanningPaused = true;
// If we want to use simulated scanning data, do it here. This is used for testing in an emulator
if (simulatedScanData != null) {
// if simulatedScanData is provided, it will be seen every scan cycle. *in addition* to anything actually seen in the air
// it will not be used if we are not in debug mode
if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
for (IBeacon iBeacon : simulatedScanData) {
processIBeaconFromScan(iBeacon);
}
} else {
Log.w(TAG, "Simulated scan data provided, but ignored because we are not running in debug mode. Please remove simulated scan data for production.");
}
}
nextScanStartTime = (new Date().getTime() + betweenScanPeriod);
scanLeDevice(true);
if (scanningEnabled) {
scanLeDevice(true);
}
else {
if (IBeaconManager.LOG_DEBUG) Log.d(TAG, "Scanning disabled. No ranging or monitoring regions are active.");
scanCyclerStarted = false;
}
}
}
}
Expand Down

0 comments on commit 1a2aa13

Please sign in to comment.