]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Beacon.java
Add connect/disconnect and iBeacon data
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Beacon.java
1 package edu.ucla.iBeaconNav;
2
3 import java.util.List;
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;
9
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;
22
23 import com.radiusnetworks.ibeacon.*;
24 import com.radiusnetworks.ibeacon.client.*;
25 import com.radiusnetworks.ibeacon.service.*;
26
27 public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier
28 {
29         /* Private data */
30         private Task           task;
31
32         private IBeaconManager ibeacon;
33         private String         uuid;
34         private HashMap<Integer,LatLon> locations;
35
36         /* Public methods */
37         public Beacon(Task task) {
38                 IBeaconManager.LOG_DEBUG = true;
39
40                 // Setup object
41                 this.uuid = "iBeaconNav";
42                 this.task = task;
43
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);
51         }
52
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);
59         }
60
61         public void disconnect() {
62                 Util.debug("Task: handle - register");
63                 this.ibeacon.unBind(this);
64                 //this.ibeacon.setBackgroundMode(this, true);
65         }
66
67         /* Private methods */
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);
72         }
73
74         /* IBeaconConsumer Methods
75          *   Pass most of these off to Task,
76          *   I don't even know why they're here */
77         @Override
78         public boolean bindService(Intent intent, ServiceConnection connection, int mode) {
79                 return this.task.bindService(intent, connection, mode);
80         }
81
82         @Override
83         public Context getApplicationContext() {
84                 return this.task.getApplicationContext();
85         }
86
87         @Override
88         public void unbindService(ServiceConnection connection)  {
89                 this.task.unbindService(connection);
90         }
91
92         @Override
93         public void onIBeaconServiceConnect() {
94                 Util.debug("Task: onIBeaconServiceConnect");
95
96                 this.ibeacon.setRangeNotifier(this);
97                 this.ibeacon.setMonitorNotifier(this);
98
99                 try {
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");
105                 }
106         }
107
108         /* RangeNotifier Methods */
109         @Override
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);
121                         } else {
122                                 Util.debug("Unknown beacon:" +
123                                                 " "   + ib.getProximityUuid() +
124                                                 ":"   + ib.getMajor() +
125                                                 ":"   + ib.getMinor() +
126                                                 " @ " + ib.getAccuracy());
127                         }
128                 }
129         }
130
131         /* MonitorNotifier Methods */
132         @Override
133         public void didEnterRegion(Region region) {
134                 Util.debug("Task: didEnterRegion");
135         }
136
137         @Override
138         public void didExitRegion(Region region) {
139                 Util.debug("Task: didExitRegion");
140         }
141
142         @Override
143         public void didDetermineStateForRegion(int state, Region region) {
144                 Util.debug("Task: didDetermineStateForRegion");
145         }
146
147         /* Location class */
148         class LatLon {
149                 public double lat = 0;
150                 public double lon = 0;
151
152                 public LatLon(double lat, double lon) {
153                         this.lat = lat;
154                         this.lon = lon;
155                 }
156         }
157 }