]> Pileus Git - ~andy/iBeaconNav/blobdiff - src/edu/ucla/iBeaconNav/Task.java
Get iBeacon service running
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Task.java
index 58ef0031c4324135b66f273cae858215286cc81e..049776ed9b0929c55096a709b31cb03af9e0a781 100644 (file)
@@ -2,6 +2,7 @@ package edu.ucla.iBeaconNav;
 
 import java.util.List;
 import java.util.LinkedList;
+import java.util.Collection;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -14,28 +15,38 @@ import android.content.SharedPreferences;
 import android.os.IBinder;
 import android.os.Looper;
 import android.os.Messenger;
+import android.os.RemoteException;
 import android.preference.PreferenceManager;
 
-public class Task extends Service implements Runnable
+import com.radiusnetworks.ibeacon.*;
+import com.radiusnetworks.ibeacon.client.*;
+import com.radiusnetworks.ibeacon.service.*;
+
+public class Task extends Service implements IBeaconConsumer, RangeNotifier, MonitorNotifier
 {
-       /* Commands */
-       public static final int REGISTER   = 0;
-       public static final int POSITION   = 1;
-       public static final int CONNECT    = 2;
-       public static final int DISCONNECT = 3;
-       public static final int NOTIFY     = 4;
+       /* Main -> Task Messsages */
+       static enum Command {
+               REGISTER,
+               CONNECT,
+               DISCONNECT,
+       };
+
+       /* Task -> Main messages */
+       static enum Response {
+               POSITION,
+               NOTIFY,
+       };
 
        /* Private data */
-       private Messenger  messenger;
-       private Thread     thread;
-       private boolean    active;
+       private Messenger      messenger;
+       private IBeaconManager ibeacon;
 
        /* Private methods */
-       private void tellMain(int cmd, Object value)
+       private void tellMain(Response cmd, Object value)
        {
                try {
                        android.os.Message msg = android.os.Message.obtain();
-                       msg.what = cmd;
+                       msg.what = cmd.ordinal();
                        msg.obj  = value;
                        this.messenger.send(msg);
                } catch (Exception e) {
@@ -46,68 +57,62 @@ public class Task extends Service implements Runnable
        private void notify(String text, int icon)
        {
                // Notify Main
-               this.tellMain(NOTIFY, text);
+               this.tellMain(Task.Response.NOTIFY, text);
 
                // Notification bar
                //Notification  note   = new Notification(icon, null, 0);
                //Intent        intent = new Intent(this, Main.class);
                //PendingIntent pend   = PendingIntent.getActivity(this, 0, intent, 0);
-
                //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
-               //this.startForeground(1, note);
+               //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
+
+               Notification  note = new Notification.Builder(this)
+                       .setContentTitle("iBeaconNav!")
+                       .setContentText("iBeaconNav!")
+                       .setSmallIcon(icon)
+                       .build();
+
+               this.startForeground(1, note);
        }
 
-       private void handle(int cmd, Messenger mgr)
+       private void handle(Command cmd, Messenger mgr)
        {
                // Validate messenger
-               if (cmd != REGISTER && mgr != null && mgr != this.messenger) {
+               if (cmd != Task.Command.REGISTER && mgr != null && mgr != this.messenger)
                        Util.debug("Task: handle - invalid messenger");
-               }
-
-               // Setup communication with Main
-               if (cmd == REGISTER) {
-                       Util.debug("Task: handle - register");
-                       this.messenger = mgr;
-                       this.tellMain(REGISTER, this);
-               }
-
-               // Create client thread
-               if (cmd == CONNECT && this.thread == null) {
-                       Util.debug("Task: handle - connect");
-                       this.thread = new Thread(this);
-                       this.thread.start();
-               }
 
-               // Stop client thread
-               if (cmd == DISCONNECT && this.thread != null) {
-                       Util.debug("Task: handle - register");
-                       try {
-                               this.thread.join();
-                       } catch (Exception e) {
-                               Util.debug("Task: error stopping service", e);
-                       }
+               // Handle the command
+               switch (cmd) {
+                       // Setup communication with Main
+                       case REGISTER:
+                               Util.debug("Task: handle - register");
+                               this.messenger = mgr;
+                               break;
+
+                       // Create client thread
+                       case CONNECT:
+                               Util.debug("Task: handle - connect");
+                               this.notify("Connected", android.R.drawable.presence_online);
+                               this.ibeacon = IBeaconManager.getInstanceForApplication(this);
+                               this.ibeacon.bind(this);
+                               //this.ibeacon.setBackgroundMode(this, false);
+                               break;
+
+                       // Stop client thread
+                       case DISCONNECT:
+                               Util.debug("Task: handle - register");
+                               //this.ibeacon.setBackgroundMode(this, true);
+                               this.ibeacon.unBind(this);
+                               this.ibeacon = null;
+                               this.stopForeground(true);
+                               break;
                }
        }
 
        /* Public methods */
        public boolean isRunning()
        {
-               return this.thread != null;
-       }
-
-       /* Runnable methods */
-       @Override
-       public void run()
-       {
-               Util.debug("Task: thread run");
-
-               // Run nav algorithm
-               while (this.active) {
-                       // Read sensor data
-                       this.tellMain(POSITION, 0);
-               }
-
-               Util.debug("Task: thread exit");
+               return this.ibeacon != null;
        }
 
        /* Service Methods */
@@ -116,13 +121,14 @@ public class Task extends Service implements Runnable
        {
                Util.debug("Task: onCreate");
                super.onCreate();
+               IBeaconManager.LOG_DEBUG = true;
        }
 
        @Override
        public void onDestroy()
        {
                Util.debug("Task: onDestroy");
-               this.handle(DISCONNECT, null);
+               super.onDestroy();
        }
 
        @Override
@@ -130,7 +136,7 @@ public class Task extends Service implements Runnable
        {
                Util.debug("Task: onStartCommand");
                int       rval = super.onStartCommand(intent, flags, startId);
-               int       cmd  = intent.getExtras().getInt("Command");
+               Command   cmd  =   (Command)intent.getExtras().get("Command");
                Messenger mgr  = (Messenger)intent.getExtras().get("Messenger");
                this.handle(cmd, mgr);
                return rval;
@@ -142,4 +148,43 @@ public class Task extends Service implements Runnable
                Util.debug("Task: onBind");
                return null;
        }
+
+       /* IBeaconConsumer Methods */
+       @Override
+       public void onIBeaconServiceConnect() {
+               Util.debug("Task: onIBeaconServiceConnect");
+
+               this.ibeacon.setRangeNotifier(this);
+               this.ibeacon.setMonitorNotifier(this);
+
+               try {
+                       Region region = new Region("iBeaconNavMonitoringId", null, null, null);
+                       this.ibeacon.startRangingBeaconsInRegion(region);
+                       this.ibeacon.startMonitoringBeaconsInRegion(region);
+               } catch (RemoteException e) {
+                       Util.debug("Task: onIBeaconServiceConnect - error");
+               }
+       }
+
+       /* RangeNotifier Methods */
+        @Override
+        public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
+               Util.debug("Task: didRangeBeaconsInRegion");
+        }
+
+       /* MonitorNotifier Methods */
+       @Override
+       public void didEnterRegion(Region region) {
+               Util.debug("Task: didEnterRegion");
+       }
+
+       @Override
+       public void didExitRegion(Region region) {
+               Util.debug("Task: didExitRegion");
+       }
+
+       @Override
+       public void didDetermineStateForRegion(int state, Region region) {
+               Util.debug("Task: didDetermineStateForRegion");
+       }
 }