-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from louisg1337/android_bluetooth
π€π Android BLE Integration
- Loading branch information
Showing
10 changed files
with
389 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package edu.berkeley.eecs.emission.cordova.tracker.bluetooth; | ||
|
||
// Android imports | ||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.IBinder; | ||
|
||
// Altbeacon imports | ||
import org.altbeacon.beacon.BeaconManager; | ||
import org.altbeacon.beacon.Region; | ||
import org.altbeacon.beacon.MonitorNotifier; | ||
import org.altbeacon.beacon.Beacon; | ||
|
||
// Other plugin imports | ||
import edu.berkeley.eecs.emission.R; | ||
import edu.berkeley.eecs.emission.cordova.unifiedlogger.Log; | ||
import edu.berkeley.eecs.emission.cordova.tracker.ExplicitIntent; | ||
import edu.berkeley.eecs.emission.cordova.tracker.verification.SensorControlChecks; | ||
|
||
|
||
public class BluetoothMonitoringService extends Service { | ||
private static String TAG = "BluetoothMonitoringService"; | ||
private BeaconManager beaconManager; | ||
private String uuid = "bf3df3b1-6e46-35fa-86e5-927c95dd096c"; | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
Log.d(this, TAG, "onStartCommand called!!!!"); | ||
|
||
// Instantiate variable | ||
beaconManager = BeaconManager.getInstanceForApplication(this); | ||
// This line will ensure that we always get a first callback if beacon is in region when we start monitoring | ||
// https://github.com/AltBeacon/android-beacon-library/issues/708#issuecomment-399513853 | ||
beaconManager.setRegionStatePersistenceEnabled(false); | ||
|
||
// Start monitoring for BLE beacons | ||
startMonitoring(); | ||
|
||
// Start sticky | ||
return 1; | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
|
||
private void startMonitoring() { | ||
Log.d(this, TAG, "Start monitoring has been called!"); | ||
|
||
// Code to start monitoring for BLE beacons using AltBeacon library | ||
beaconManager.addMonitorNotifier(new MonitorNotifier() { | ||
@Override | ||
public void didEnterRegion(Region region) { | ||
Log.d(BluetoothMonitoringService.this, TAG, "I just saw a beacon for the first time!"); | ||
} | ||
|
||
@Override | ||
public void didExitRegion(Region region) { | ||
Log.d(BluetoothMonitoringService.this, TAG, "I no longer see an beacon"); | ||
} | ||
|
||
@Override | ||
public void didDetermineStateForRegion(int state, Region region) { | ||
Log.d(BluetoothMonitoringService.this, TAG, "I have just switched from seeing/not seeing beacons: "+state); | ||
} | ||
}); | ||
|
||
// Define our region and start monitoring | ||
Region region = new Region(uuid, null, null, null); | ||
beaconManager.startMonitoring(region); | ||
Log.d(this, TAG, "Starting to monitor for our region: " + region.toString()); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
stopMonitoring(); | ||
super.onDestroy(); | ||
} | ||
|
||
private void stopMonitoring() { | ||
Log.d(this, TAG, "Stopping monitoring for the beacon."); | ||
beaconManager.stopMonitoring(new Region(uuid, null, null, null)); | ||
beaconManager.removeAllMonitorNotifiers(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package edu.berkeley.eecs.emission.cordova.tracker.bluetooth; | ||
|
||
// Android imports | ||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.IBinder; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.HashSet; | ||
|
||
// Altbeacon imports | ||
import org.altbeacon.beacon.BeaconManager; | ||
import org.altbeacon.beacon.Region; | ||
import org.altbeacon.beacon.RangeNotifier; | ||
import org.altbeacon.beacon.Beacon; | ||
|
||
// Other plugin imports | ||
import edu.berkeley.eecs.emission.R; | ||
import edu.berkeley.eecs.emission.cordova.unifiedlogger.Log; | ||
import edu.berkeley.eecs.emission.cordova.tracker.ExplicitIntent; | ||
import edu.berkeley.eecs.emission.cordova.tracker.verification.SensorControlChecks; | ||
|
||
// Saving data | ||
import edu.berkeley.eecs.emission.cordova.usercache.UserCacheFactory; | ||
import edu.berkeley.eecs.emission.cordova.tracker.wrapper.BluetoothBLE; | ||
|
||
public class BluetoothService extends Service { | ||
private static String TAG = "BluetoothService"; | ||
private BeaconManager beaconManager; | ||
private Set<Beacon> scanned; | ||
private String uuid = "bf3df3b1-6e46-35fa-86e5-927c95dd096c"; | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
Log.d(this, TAG, "onStartCommand called!!!!"); | ||
|
||
// Instantiate variables | ||
beaconManager = BeaconManager.getInstanceForApplication(this); | ||
scanned = new HashSet<>(); | ||
|
||
// Check to see if we even have permission to scan at all | ||
boolean bluetoothPermissions = SensorControlChecks.checkBluetoothScanningPermissions(this); | ||
|
||
if (!bluetoothPermissions) { | ||
return 1; | ||
} | ||
|
||
// Start scanning for BLE beacons | ||
startBeaconScan(); | ||
|
||
// Start sticky | ||
return 1; | ||
} | ||
|
||
// @Nullable | ||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
|
||
private void isInRange() { | ||
Log.d(this, TAG, "Is in range has been called!"); | ||
|
||
stopBeaconScan(); | ||
|
||
Log.d(this, TAG, "Done waiting, results are... " + scanned.size()); | ||
|
||
if (scanned.size() > 0) { | ||
Log.d(this, TAG, "Found something!"); | ||
Log.d(this, TAG, scanned.toString()); | ||
this.sendBroadcast(new ExplicitIntent(this, R.string.transition_ble_beacon_found)); | ||
} | ||
onDestroy(); | ||
} | ||
|
||
private void startBeaconScan() { | ||
// Code to start scanning for BLE beacons using AltBeacon library | ||
Log.d(this, TAG, "startBeaconScan called!!!!"); | ||
|
||
// Keep track of how many times we have scanned | ||
beaconManager.addRangeNotifier(new RangeNotifier() { | ||
int numScans = 0; | ||
|
||
@Override | ||
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { | ||
Log.d(BluetoothService.this, TAG, "Range notifier called..."); | ||
|
||
if (beacons.size() > 0) { | ||
Log.d(BluetoothService.this, TAG, "Found beacons " + beacons.toString()); | ||
|
||
for (Beacon beacon : beacons) { | ||
// Even though we are scanning for beacons in a certain region, beacons with different UUID's still come up. | ||
// Until we figure out why that is the case, put this check in place so we only save the ones with the right UUID. | ||
if (beacon.getId1().toString().equals(uuid)) { | ||
scanned.add(beacon); | ||
BluetoothBLE currWrapper = BluetoothBLE.initRangeUpdate( | ||
beacon.getId1().toString(), | ||
System.currentTimeMillis() / 1000, // timestamp in always in secs for us | ||
beacon.getId2().toInt(), | ||
beacon.getId3().toInt(), | ||
// TODO: Figure out what to do with the distance calculations | ||
"ProximityNear", | ||
// accuracy = rough distance estimate limited to two decimal places (in metres) | ||
// NO NOT ASSUME THIS IS ACCURATE - it is effected by radio interference and obstacles | ||
// from https://github.com/petermetz/cordova-plugin-ibeacon | ||
Math.round((beacon.getDistance() * 100)/100), | ||
beacon.getRssi()); | ||
UserCacheFactory.getUserCache(BluetoothService.this) | ||
.putSensorData(R.string.key_usercache_bluetooth_ble, currWrapper); | ||
} | ||
} | ||
} | ||
|
||
numScans++; | ||
|
||
if (numScans >= 4) { | ||
// Once we have hit certain number of scans, stop and determine if any beacons are in range | ||
isInRange(); | ||
} | ||
} | ||
}); | ||
|
||
beaconManager.startRangingBeacons(new Region(uuid, null, null, null)); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
// stopBeaconScan(); | ||
super.onDestroy(); | ||
} | ||
|
||
private void stopBeaconScan() { | ||
Log.d(this, TAG, "Stopping monitoring for the beacon."); | ||
beaconManager.stopRangingBeacons(new Region(uuid, null, null, null)); | ||
beaconManager.removeAllRangeNotifiers(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.altbeacon:android-beacon-library:2.19' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.