]> Pileus Git - ~andy/iBeaconNav/commitdiff
Add connect/disconnect and iBeacon data master
authorAndy Spencer <andy753421@gmail.com>
Tue, 18 Mar 2014 04:27:56 +0000 (04:27 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 18 Mar 2014 04:27:56 +0000 (04:27 +0000)
res/layout/main.xml
src/edu/ucla/iBeaconNav/Beacon.java
src/edu/ucla/iBeaconNav/CMD.java
src/edu/ucla/iBeaconNav/Main.java
src/edu/ucla/iBeaconNav/Sensors.java
src/edu/ucla/iBeaconNav/Task.java

index ba0827eed4c66c95a848e068f27f22cdf431b4cf..37ce27197b7239874f96b7271f596c254513a237 100644 (file)
                                                android:text="One Second Gyro Sums"
                                                android:textAppearance="?android:attr/textAppearanceMedium" />
                                </GridLayout>
+                               <GridLayout
+                                       android:layout_width="match_parent"
+                                       android:layout_height="wrap_content"
+                                       android:columnCount="1" >
+                                       <TextView
+                                               android:id="@+id/ibLatText"
+                                               android:layout_column="0"
+                                               android:layout_gravity="left|top"
+                                               android:layout_row="1"
+                                               android:text="Latitude"
+                                               android:textAppearance="?android:attr/textAppearanceMedium" />
+                                       <TextView
+                                               android:id="@+id/ibLonText"
+                                               android:layout_column="0"
+                                               android:layout_gravity="center_horizontal|top"
+                                               android:layout_row="1"
+                                               android:text="Longitude"
+                                               android:textAppearance="?android:attr/textAppearanceMedium" />
+                                       <TextView
+                                               android:id="@+id/ibDistText"
+                                               android:layout_column="0"
+                                               android:layout_gravity="right|top"
+                                               android:layout_row="1"
+                                               android:text="Distance"
+                                               android:textAppearance="?android:attr/textAppearanceMedium" />
+                                       <TextView
+                                               android:id="@+id/beaconLabel"
+                                               android:layout_width="wrap_content"
+                                               android:layout_height="wrap_content"
+                                               android:layout_column="0"
+                                               android:layout_gravity="left|center_vertical"
+                                               android:layout_row="0"
+                                               android:text="IBeacon Distance"
+                                               android:textAppearance="?android:attr/textAppearanceMedium" />
+                               </GridLayout>
                                <GridLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
index d0ee4a80dff052302636d91ca983c895bc8bba80..859a3e7918874ecb554875e94aa76239e326ed72 100644 (file)
@@ -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<Integer,LatLon> 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<Integer,LatLon>();
+               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<IBeacon> 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;
+               }
+       }
 }
index 70e8e4feca17d974f5716d803a38719805b911eb..f363966d5401f544c15eaf5bc3421f52a4f465d3 100644 (file)
@@ -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
        }
 }
index 9082279c9640887711cf34454391eb5ce17b396c..e8d25c1c012d1f6f536526960b198c73f8a1c5ab 100644 (file)
@@ -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) {
index 5d6fb2dd1e94fc4302a868b8bc9c19500ec7e593..e52d1c306c335fc50574db50482e2d7a73a8b673 100644 (file)
@@ -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();
index c3def77fb4084f7bf983312466f94ab97626fe79..ac248378863d7c010d8e16b72060554ce6d521a9 100644 (file)
@@ -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;