1 package edu.ucla.iBeaconNav;
4 import java.util.LinkedList;
5 import java.util.Collection;
6 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock;
9 import android.app.Notification;
10 import android.app.PendingIntent;
11 import android.app.Service;
12 import android.content.Context;
13 import android.content.Intent;
14 import android.content.SharedPreferences;
15 import android.os.IBinder;
16 import android.os.Looper;
17 import android.os.Messenger;
18 import android.os.RemoteException;
19 import android.preference.PreferenceManager;
21 import com.radiusnetworks.ibeacon.*;
22 import com.radiusnetworks.ibeacon.client.*;
23 import com.radiusnetworks.ibeacon.service.*;
25 public class Task extends Service implements IBeaconConsumer, RangeNotifier, MonitorNotifier
28 private Messenger messenger;
29 private IBeaconManager ibeacon;
32 private void tellMain(CMD.Response cmd, Object value)
35 android.os.Message msg = android.os.Message.obtain();
36 msg.what = cmd.ordinal();
38 this.messenger.send(msg);
39 } catch (Exception e) {
40 Util.debug("Task: error sending message", e);
44 private void notify(String text, int icon)
47 this.tellMain(CMD.Response.NOTIFY, text);
50 //Notification note = new Notification(icon, null, 0);
51 //Intent intent = new Intent(this, Main.class);
52 //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
53 //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
54 //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
56 Notification note = new Notification.Builder(this)
57 .setContentTitle("iBeaconNav!")
58 .setContentText("iBeaconNav!")
62 this.startForeground(1, note);
65 private void handle(CMD.Command cmd, Messenger mgr)
68 if (cmd != CMD.Command.REGISTER && mgr != null && mgr != this.messenger)
69 Util.debug("Task: handle - invalid messenger");
73 // Setup communication with Main
75 Util.debug("Task: handle - register");
79 // Create client thread
81 Util.debug("Task: handle - connect");
82 this.notify("Connected", android.R.drawable.presence_online);
83 this.ibeacon = IBeaconManager.getInstanceForApplication(this);
84 this.ibeacon.bind(this);
85 //this.ibeacon.setBackgroundMode(this, false);
90 Util.debug("Task: handle - register");
91 //this.ibeacon.setBackgroundMode(this, true);
92 this.ibeacon.unBind(this);
94 this.stopForeground(true);
100 public boolean isRunning()
102 return this.ibeacon != null;
105 /* Service Methods */
107 public void onCreate()
109 Util.debug("Task: onCreate");
111 IBeaconManager.LOG_DEBUG = true;
115 public void onDestroy()
117 Util.debug("Task: onDestroy");
122 public int onStartCommand(Intent intent, int flags, int startId)
124 Util.debug("Task: onStartCommand");
125 int rval = super.onStartCommand(intent, flags, startId);
126 CMD.Command cmd = (CMD.Command)intent.getExtras().get("Command");
127 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
128 this.handle(cmd, mgr);
133 public IBinder onBind(Intent intent)
135 Util.debug("Task: onBind");
139 /* IBeaconConsumer Methods */
141 public void onIBeaconServiceConnect() {
142 Util.debug("Task: onIBeaconServiceConnect");
144 this.ibeacon.setRangeNotifier(this);
145 this.ibeacon.setMonitorNotifier(this);
148 Region region = new Region("iBeaconNavMonitoringId", null, null, null);
149 this.ibeacon.startRangingBeaconsInRegion(region);
150 this.ibeacon.startMonitoringBeaconsInRegion(region);
151 } catch (RemoteException e) {
152 Util.debug("Task: onIBeaconServiceConnect - error");
156 /* RangeNotifier Methods */
158 public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
159 Util.debug("Task: didRangeBeaconsInRegion");
162 /* MonitorNotifier Methods */
164 public void didEnterRegion(Region region) {
165 Util.debug("Task: didEnterRegion");
169 public void didExitRegion(Region region) {
170 Util.debug("Task: didExitRegion");
174 public void didDetermineStateForRegion(int state, Region region) {
175 Util.debug("Task: didDetermineStateForRegion");