1 package edu.ucla.iBeaconNav;
4 import java.util.LinkedList;
5 import java.util.HashMap;
6 import java.util.Collection;
7 import java.util.concurrent.locks.Lock;
8 import java.util.concurrent.locks.ReentrantLock;
10 import android.app.Notification;
11 import android.app.PendingIntent;
12 import android.app.Service;
13 import android.content.Context;
14 import android.content.Intent;
15 import android.content.ServiceConnection;
16 import android.content.SharedPreferences;
17 import android.os.IBinder;
18 import android.os.Looper;
19 import android.os.Messenger;
20 import android.os.RemoteException;
21 import android.preference.PreferenceManager;
23 import com.radiusnetworks.ibeacon.*;
24 import com.radiusnetworks.ibeacon.client.*;
25 import com.radiusnetworks.ibeacon.service.*;
27 public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier
32 private IBeaconManager ibeacon;
34 private HashMap<Integer,LatLon> locations;
37 public Beacon(Task task) {
38 IBeaconManager.LOG_DEBUG = true;
41 this.uuid = "iBeaconNav";
44 // Initialize iBeacons
45 this.locations = new HashMap<Integer,LatLon>();
46 this.addIBeacon(1, 1, 34.0722, -118.4441);
47 this.addIBeacon(1, 2, 34.0722, -118.4441);
48 this.addIBeacon(1, 3, 34.0722, -118.4441);
49 this.addIBeacon(1, 5, 34.0722, -118.4441);
50 this.addIBeacon(1, 5, 34.0722, -118.4441);
53 public void connect() {
54 Util.debug("Task: handle - connect");
55 this.task.notify("Connected", android.R.drawable.presence_online);
56 this.ibeacon = IBeaconManager.getInstanceForApplication(this.task);
57 this.ibeacon.bind(this);
58 //this.ibeacon.setBackgroundMode(this, false);
61 public void disconnect() {
62 Util.debug("Task: handle - register");
63 this.ibeacon.unBind(this);
64 //this.ibeacon.setBackgroundMode(this, true);
68 private void addIBeacon(int major, int minor, double lat, double lon) {
69 IBeacon ib = new IBeacon(this.uuid, major, minor);
70 LatLon loc = new LatLon(lat, lon);
71 this.locations.put(ib.getMinor(), loc);
74 /* IBeaconConsumer Methods
75 * Pass most of these off to Task,
76 * I don't even know why they're here */
78 public boolean bindService(Intent intent, ServiceConnection connection, int mode) {
79 return this.task.bindService(intent, connection, mode);
83 public Context getApplicationContext() {
84 return this.task.getApplicationContext();
88 public void unbindService(ServiceConnection connection) {
89 this.task.unbindService(connection);
93 public void onIBeaconServiceConnect() {
94 Util.debug("Task: onIBeaconServiceConnect");
96 this.ibeacon.setRangeNotifier(this);
97 this.ibeacon.setMonitorNotifier(this);
100 Region region = new Region("iBeaconNavMonitoringId", null, null, null);
101 this.ibeacon.startRangingBeaconsInRegion(region);
102 this.ibeacon.startMonitoringBeaconsInRegion(region);
103 } catch (RemoteException e) {
104 Util.debug("Task: onIBeaconServiceConnect - error");
108 /* RangeNotifier Methods */
110 public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
111 Util.debug("Task: didRangeBeaconsInRegion - " + iBeacons.size() + " beacons");
112 for (IBeacon ib : iBeacons) {
113 if (this.locations.containsKey(ib.getMinor())) {
114 LatLon loc = this.locations.get(ib.getMinor());
115 float[] data = new float[4];
116 data[0] = CMD.Data.BEACON.ordinal();
117 data[1] = (float)loc.lat;
118 data[2] = (float)loc.lon;
119 data[3] = (float)ib.getAccuracy();
120 this.task.tellMain(CMD.Response.SHOWDATA, data);
122 Util.debug("Unknown beacon:" +
123 " " + ib.getProximityUuid() +
124 ":" + ib.getMajor() +
125 ":" + ib.getMinor() +
126 " @ " + ib.getAccuracy());
131 /* MonitorNotifier Methods */
133 public void didEnterRegion(Region region) {
134 Util.debug("Task: didEnterRegion");
138 public void didExitRegion(Region region) {
139 Util.debug("Task: didExitRegion");
143 public void didDetermineStateForRegion(int state, Region region) {
144 Util.debug("Task: didDetermineStateForRegion");
149 public double lat = 0;
150 public double lon = 0;
152 public LatLon(double lat, double lon) {