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.ServiceConnection;
15 import android.content.SharedPreferences;
16 import android.os.IBinder;
17 import android.os.Looper;
18 import android.os.Messenger;
19 import android.os.RemoteException;
20 import android.preference.PreferenceManager;
22 import com.radiusnetworks.ibeacon.*;
23 import com.radiusnetworks.ibeacon.client.*;
24 import com.radiusnetworks.ibeacon.service.*;
26 public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier
31 private IBeaconManager ibeacon;
34 public Beacon(Task task) {
36 IBeaconManager.LOG_DEBUG = true;
39 public void connect() {
40 Util.debug("Task: handle - connect");
41 this.task.notify("Connected", android.R.drawable.presence_online);
42 this.ibeacon = IBeaconManager.getInstanceForApplication(this.task);
43 this.ibeacon.bind(this);
44 //this.ibeacon.setBackgroundMode(this, false);
47 public void disconnect() {
48 Util.debug("Task: handle - register");
49 this.ibeacon.unBind(this);
50 //this.ibeacon.setBackgroundMode(this, true);
53 /* IBeaconConsumer Methods
54 * Pass most of these off to Task,
55 * I don't even know why they're here */
57 public boolean bindService(Intent intent, ServiceConnection connection, int mode) {
58 return this.task.bindService(intent, connection, mode);
62 public Context getApplicationContext() {
63 return this.task.getApplicationContext();
67 public void unbindService(ServiceConnection connection) {
68 this.task.unbindService(connection);
72 public void onIBeaconServiceConnect() {
73 Util.debug("Task: onIBeaconServiceConnect");
75 this.ibeacon.setRangeNotifier(this);
76 this.ibeacon.setMonitorNotifier(this);
79 Region region = new Region("iBeaconNavMonitoringId", null, null, null);
80 this.ibeacon.startRangingBeaconsInRegion(region);
81 this.ibeacon.startMonitoringBeaconsInRegion(region);
82 } catch (RemoteException e) {
83 Util.debug("Task: onIBeaconServiceConnect - error");
87 /* RangeNotifier Methods */
89 public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
90 Util.debug("Task: didRangeBeaconsInRegion");
93 /* MonitorNotifier Methods */
95 public void didEnterRegion(Region region) {
96 Util.debug("Task: didEnterRegion");
100 public void didExitRegion(Region region) {
101 Util.debug("Task: didExitRegion");
105 public void didDetermineStateForRegion(int state, Region region) {
106 Util.debug("Task: didDetermineStateForRegion");