From: Andy Spencer Date: Sat, 1 Mar 2014 04:38:20 +0000 (+0000) Subject: Get iBeacon service running X-Git-Url: http://pileus.org/git/?p=~andy%2FiBeaconNav;a=commitdiff_plain;h=328b9fbc030072c9ac37c01e1edb035e60060e3a Get iBeacon service running --- diff --git a/.gitignore b/.gitignore index 0947ddf..72719b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Settings config.mk +# Local files +local/ + # Vim junk *~ *.swp @@ -9,9 +12,3 @@ config.mk bin/ gen/ obj/ - -# Leave out cards for now -opt/drawable/*.svg -opt/drawable/*.xcf -res/drawable/*.png -res/drawable/*.jpg diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5ff7ed0..8f0b15b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5,30 +5,21 @@ android:versionName="1.0"> - - - - - - - - + + + + + + + + @@ -42,6 +33,13 @@ + + 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 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"); + } }