From 9eaab2e256d4eacf8bbb8bb5c1b5462976c4dd70 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Tue, 18 Mar 2014 04:27:56 +0000 Subject: [PATCH] Add connect/disconnect and iBeacon data --- res/layout/main.xml | 35 ++++++++++++++++++ src/edu/ucla/iBeaconNav/Beacon.java | 53 ++++++++++++++++++++++++++-- src/edu/ucla/iBeaconNav/CMD.java | 25 ++++++------- src/edu/ucla/iBeaconNav/Main.java | 28 ++++++++------- src/edu/ucla/iBeaconNav/Sensors.java | 20 ++++++++--- src/edu/ucla/iBeaconNav/Task.java | 6 ++-- 6 files changed, 134 insertions(+), 33 deletions(-) diff --git a/res/layout/main.xml b/res/layout/main.xml index ba0827e..37ce271 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -91,6 +91,41 @@ android:text="One Second Gyro Sums" android:textAppearance="?android:attr/textAppearanceMedium" /> + + + + + + 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; + } + } } diff --git a/src/edu/ucla/iBeaconNav/CMD.java b/src/edu/ucla/iBeaconNav/CMD.java index 70e8e4f..f363966 100644 --- a/src/edu/ucla/iBeaconNav/CMD.java +++ b/src/edu/ucla/iBeaconNav/CMD.java @@ -26,17 +26,18 @@ public class CMD { /* Show Data Commands */ static enum Data { - ACC, - MAG, - GYR, - GYR1HZ, - ORIENT, - WRDACC, - WRDGYR, - STPCNT, - POSITION, - ROTATION, - HEADING, - STABLE, + ACC, // x, y, z + MAG, // x, y, z + GYR, // roll, pitch, yaw (delta radians?) + GYR1HZ, // roll, pitch, yaw (delta radians?) + ORIENT, // roll, pitch, yaw (??? + WRDACC, // x, y, z (world frame) + WRDGYR, // roll, pitch, yaw (worldf rame?) + STPCNT, // count, gravity + POSITION, // cur x, cur y + ROTATION, // roll, pitch, yaw (degrees) + HEADING, // heading + STABLE, // stable (true/false) + BEACON, // lat, lon, dist } } diff --git a/src/edu/ucla/iBeaconNav/Main.java b/src/edu/ucla/iBeaconNav/Main.java index 9082279..e8d25c1 100644 --- a/src/edu/ucla/iBeaconNav/Main.java +++ b/src/edu/ucla/iBeaconNav/Main.java @@ -30,7 +30,7 @@ import android.widget.TextView; import android.widget.Toast; import android.os.Bundle; - + import com.google.android.gms.maps.*; import com.google.android.gms.maps.model.*; @@ -67,7 +67,7 @@ public class Main extends Activity Util.debug("Main: onRegister_task"); this.task = task; } - + private void onPosition() { Util.debug("Main: onPosition"); @@ -147,19 +147,23 @@ public class Main extends Activity textView1 = (TextView)findViewById(R.id.stableText); displayNum = 1; break; + case BEACON: + textView1 = (TextView)findViewById(R.id.ibLatText); + textView2 = (TextView)findViewById(R.id.ibLonText); + textView3 = (TextView)findViewById(R.id.ibDistText); + displayNum = 3; + break; default: Util.debug("Main: Nothing Matches"); } - - textView1.setText(Float.toString(data[1])); - if (displayNum >1){ - textView2.setText(Float.toString(data[2])); - } - if (displayNum >2){ - textView3.setText(Float.toString(data[3])); + + switch (displayNum) { + case 3: textView3.setText(Float.toString(data[3])); + case 2: textView2.setText(Float.toString(data[2])); + case 1: textView1.setText(Float.toString(data[1])); } } - + /* Private service methods */ private void register() { @@ -220,7 +224,7 @@ public class Main extends Activity this.scroll = (ScrollView) findViewById(R.id.debug_scroll); this.rstHdBttn = (Button) findViewById(R.id.rstHdBttn); this.rstDstBttn= (Button) findViewById(R.id.rstDstBttn); - + // TODO - remove these rstHdBttn.setOnClickListener(new View.OnClickListener() { @Override @@ -230,7 +234,7 @@ public class Main extends Activity .putExtra("Command", CMD.Command.RSTHEAD)); } }); - + rstDstBttn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/src/edu/ucla/iBeaconNav/Sensors.java b/src/edu/ucla/iBeaconNav/Sensors.java index 5d6fb2d..e52d1c3 100644 --- a/src/edu/ucla/iBeaconNav/Sensors.java +++ b/src/edu/ucla/iBeaconNav/Sensors.java @@ -120,11 +120,13 @@ public class Sensors implements SensorEventListener this.sensorManager = (SensorManager)task.getSystemService(Context.SENSOR_SERVICE); // Get sensors - accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); - grvSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); - magSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); - gyrSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); - + this.accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + this.grvSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); + this.magSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); + this.gyrSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); + } + + public void connect() { // Connect sensor listeners sensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_FASTEST); sensorManager.registerListener(this, grvSensor, SensorManager.SENSOR_DELAY_FASTEST); @@ -132,6 +134,14 @@ public class Sensors implements SensorEventListener sensorManager.registerListener(this, gyrSensor, SensorManager.SENSOR_DELAY_FASTEST); } + public void disconnect() { + // Remove sensor listeners + sensorManager.unregisterListener(this, accSensor); + sensorManager.unregisterListener(this, grvSensor); + sensorManager.unregisterListener(this, magSensor); + sensorManager.unregisterListener(this, gyrSensor); + } + public void reset_heading() { Util.debug("Sensors: handle - reset heading"); this.rotation = new Quat(); diff --git a/src/edu/ucla/iBeaconNav/Task.java b/src/edu/ucla/iBeaconNav/Task.java index c3def77..ac24837 100644 --- a/src/edu/ucla/iBeaconNav/Task.java +++ b/src/edu/ucla/iBeaconNav/Task.java @@ -79,15 +79,17 @@ public class Task extends Service case CONNECT: Util.debug("Task: handle - connect"); this.running = true; + this.beacon.connect(); + this.sensors.connect(); this.notify("Connected", android.R.drawable.presence_online); - //this.ibeacon.setBackgroundMode(this, false); break; // Stop client thread case DISCONNECT: Util.debug("Task: handle - register"); - //this.ibeacon.setBackgroundMode(this, true); this.running = false; + this.beacon.disconnect(); + this.sensors.disconnect(); this.stopForeground(true); break; -- 2.43.2