X-Git-Url: http://pileus.org/git/?p=~andy%2FiBeaconNav;a=blobdiff_plain;f=src%2Fedu%2Fucla%2FiBeaconNav%2FBeacon.java;h=859a3e7918874ecb554875e94aa76239e326ed72;hp=d0ee4a80dff052302636d91ca983c895bc8bba80;hb=HEAD;hpb=001336b1cc1d0cf8c9aa4ce4a18fdc1cf8c4876b diff --git a/src/edu/ucla/iBeaconNav/Beacon.java b/src/edu/ucla/iBeaconNav/Beacon.java index d0ee4a8..859a3e7 100644 --- a/src/edu/ucla/iBeaconNav/Beacon.java +++ b/src/edu/ucla/iBeaconNav/Beacon.java @@ -2,6 +2,7 @@ package edu.ucla.iBeaconNav; import java.util.List; import java.util.LinkedList; +import java.util.HashMap; import java.util.Collection; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -29,11 +30,24 @@ public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier private Task task; private IBeaconManager ibeacon; + private String uuid; + private HashMap locations; /* Public methods */ public Beacon(Task task) { - this.task = task; IBeaconManager.LOG_DEBUG = true; + + // Setup object + this.uuid = "iBeaconNav"; + this.task = task; + + // Initialize iBeacons + this.locations = new HashMap(); + this.addIBeacon(1, 1, 34.0722, -118.4441); + this.addIBeacon(1, 2, 34.0722, -118.4441); + this.addIBeacon(1, 3, 34.0722, -118.4441); + this.addIBeacon(1, 5, 34.0722, -118.4441); + this.addIBeacon(1, 5, 34.0722, -118.4441); } public void connect() { @@ -50,6 +64,13 @@ public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier //this.ibeacon.setBackgroundMode(this, true); } + /* Private methods */ + private void addIBeacon(int major, int minor, double lat, double lon) { + IBeacon ib = new IBeacon(this.uuid, major, minor); + LatLon loc = new LatLon(lat, lon); + this.locations.put(ib.getMinor(), loc); + } + /* IBeaconConsumer Methods * Pass most of these off to Task, * I don't even know why they're here */ @@ -87,7 +108,24 @@ public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier /* RangeNotifier Methods */ @Override public void didRangeBeaconsInRegion(Collection iBeacons, Region region) { - Util.debug("Task: didRangeBeaconsInRegion"); + Util.debug("Task: didRangeBeaconsInRegion - " + iBeacons.size() + " beacons"); + for (IBeacon ib : iBeacons) { + if (this.locations.containsKey(ib.getMinor())) { + LatLon loc = this.locations.get(ib.getMinor()); + float[] data = new float[4]; + data[0] = CMD.Data.BEACON.ordinal(); + data[1] = (float)loc.lat; + data[2] = (float)loc.lon; + data[3] = (float)ib.getAccuracy(); + this.task.tellMain(CMD.Response.SHOWDATA, data); + } else { + Util.debug("Unknown beacon:" + + " " + ib.getProximityUuid() + + ":" + ib.getMajor() + + ":" + ib.getMinor() + + " @ " + ib.getAccuracy()); + } + } } /* MonitorNotifier Methods */ @@ -105,4 +143,15 @@ public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier public void didDetermineStateForRegion(int state, Region region) { Util.debug("Task: didDetermineStateForRegion"); } + + /* Location class */ + class LatLon { + public double lat = 0; + public double lon = 0; + + public LatLon(double lat, double lon) { + this.lat = lat; + this.lon = lon; + } + } }