diff --git a/README.md b/README.md
index caac83a..10693c1 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,21 @@ To use the compass we will need to use the sensor manager. We will need to decla
}
};
+**Update**
+
+The code now uses the new ROTATION_VECTOR sense which providers values as Radians and should be converted to Degrees.
+
+ @Override
+ public void onSensorChanged(SensorEvent event) {
+ SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
+ SensorManager.getOrientation(mRotationMatrix, mValues);
+ if (DEBUG) {
+ Log.d(TAG, "sensorChanged (" + Math.toDegrees(mValues[0]) + ", " + mValues[1] + ", " + mValues[2] + ")");
+ }
+
+ }
+
+
Right now it doesn't do much. The method `onSensorChanged()` is where we are going to be getting all our compass data, it will be called every time the compass detects a change. All Android devices have super sensitive compasses so you may want to add a little something to make it limit itself but for now we will leave it the way it is.
The other thing we will need to declare is a `LocationManager`, this will be handling all of our GPS data.
@@ -55,7 +70,10 @@ The other thing we will need to declare is a `LocationManager`, this will be han
Now that we have them both declared we initialise them in `onCreate()`.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ //old method of getting compass
+ // mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ //new method uses rotation vector
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
setContentView(R.layout.activity_main);
locMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE); // <2>
@@ -151,8 +169,8 @@ now we have access to the `DrawSurfaceView` we can start passing data to it! Rem
private final SensorEventListener mListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
- if (mDrawView != null) {
- mDrawView.setOffset(event.values[0]);
+ if (mDrawView != null) {
+ //mDrawView.setOffset(event.values[0]);
mDrawView.invalidate();
}
}
@@ -160,6 +178,25 @@ now we have access to the `DrawSurfaceView` we can start passing data to it! Rem
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
+
+**UPDATE**
+
+Using the new VOTATION_VECTOR sensor this code becomes:
+
+ private float[] mRotationMatrix = new float[16];
+ private float[] mValues = new float[3];
+
+ @Override
+ public void onSensorChanged(SensorEvent event) {
+ SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
+ SensorManager.getOrientation(mRotationMatrix, mValues);
+ if (mDrawView != null) {
+ mDrawView.setOffset((float)Math.toDegrees(mValues[0]));
+ mDrawView.invalidate();
+ }
+ }
+
+
You can see here that re have replaced the debug output with a `setOffset()` method. Don't worry about any errors, we will make `setOffset` soon. you can also see a call to invalidate `DrawSurfaceView`, what this will do is force `DrawSurfaceView` to redraw its canvas every time the compass reports a change. If you remember how the logcat looked this is very often so I suggest if your using this somewhere else you may want to do a check to see if it has moved more than a degree or something similar. For our purposes it doesn't matter.
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep1/.classpath b/com.cobyplain.augmentreality.AugmentRealityStep1/.classpath
index 3f9691c..d57ec02 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep1/.classpath
+++ b/com.cobyplain.augmentreality.AugmentRealityStep1/.classpath
@@ -1,8 +1,9 @@
-
+
+
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep1/src/com/cobyplain/augmentreality/Compass.java b/com.cobyplain.augmentreality.AugmentRealityStep1/src/com/cobyplain/augmentreality/Compass.java
index 35d4d1c..dbd76b6 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep1/src/com/cobyplain/augmentreality/Compass.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep1/src/com/cobyplain/augmentreality/Compass.java
@@ -39,24 +39,30 @@ public class Compass extends Activity {
private SensorManager mSensorManager;
private Sensor mSensor;
LocationManager locMgr;
+ LocationListener locListener;
+
private final SensorEventListener mListener = new SensorEventListener() {
+ private float[] mRotationMatrix = new float[16];
+ private float[] mValues = new float[3];
+
public void onSensorChanged(SensorEvent event) {
- if (DEBUG)
- Log.d(TAG, "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");
-
+ SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
+ SensorManager.getOrientation(mRotationMatrix, mValues);
+ if (DEBUG) {
+ Log.d(TAG, "sensorChanged (" + Math.toDegrees(mValues[0]) + ", " + Math.toDegrees(mValues[1]) + ", " + Math.toDegrees(mValues[2]) + ")");
+ }
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
- @SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
setContentView(R.layout.activity_main);
locMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE); // <2>
@@ -64,7 +70,7 @@ protected void onCreate(Bundle icicle) {
// using high accuracy provider... to listen for updates
locMgr.requestLocationUpdates(high.getName(), 0, 0f,
- new LocationListener() {
+ locListener = new LocationListener() {
public void onLocationChanged(Location location) {
// do something here to save this new location
Log.d(TAG, "Location Changed");
@@ -102,4 +108,10 @@ protected void onStop() {
mSensorManager.unregisterListener(mListener);
super.onStop();
}
+
+ @Override
+ protected void onDestroy() {
+ locMgr.removeUpdates(locListener);
+ super.onDestroy();
+ }
}
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep2/.classpath b/com.cobyplain.augmentreality.AugmentRealityStep2/.classpath
index 3f9691c..d57ec02 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep2/.classpath
+++ b/com.cobyplain.augmentreality.AugmentRealityStep2/.classpath
@@ -1,8 +1,9 @@
-
+
+
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/CameraSurfaceView.java b/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/CameraSurfaceView.java
index 09e0211..98d37a8 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/CameraSurfaceView.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/CameraSurfaceView.java
@@ -6,10 +6,8 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
-import android.graphics.Bitmap;
import android.graphics.Point;
import android.hardware.Camera;
-import android.hardware.Camera.PictureCallback;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/Compass.java b/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/Compass.java
index 42fcfaa..a59dac4 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/Compass.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/Compass.java
@@ -42,14 +42,21 @@ public class Compass extends Activity {
private Sensor mSensor;
private DrawSurfaceView mDrawView;
LocationManager locMgr;
+ LocationListener locListener;
private final SensorEventListener mListener = new SensorEventListener() {
+ private float[] mRotationMatrix = new float[16];
+ private float[] mValues = new float[3];
+
public void onSensorChanged(SensorEvent event) {
+ SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
+ SensorManager.getOrientation(mRotationMatrix, mValues);
+
if (DEBUG)
- Log.d(TAG, "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");
+ Log.d(TAG, "sensorChanged (" + Math.toDegrees(mValues[0]) + ", " + Math.toDegrees(mValues[1]) + ", " + Math.toDegrees(mValues[2]) + ")");
if (mDrawView != null) {
- mDrawView.setOffset(event.values[0]);
+ mDrawView.setOffset((float) Math.toDegrees(mValues[0]));
mDrawView.invalidate();
}
}
@@ -58,12 +65,11 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
- @SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
setContentView(R.layout.activity_main);
mDrawView = (DrawSurfaceView) findViewById(R.id.drawSurfaceView);
@@ -73,7 +79,7 @@ protected void onCreate(Bundle icicle) {
LocationUtils.createFineCriteria(), true));
// using high accuracy provider... to listen for updates
- locMgr.requestLocationUpdates(high.getName(), 0, 0f, new LocationListener() {
+ locMgr.requestLocationUpdates(high.getName(), 0, 0f, locListener = new LocationListener() {
public void onLocationChanged(Location location) {
// do something here to save this new location
Log.d(TAG, "Location Changed");
@@ -110,6 +116,13 @@ protected void onStop() {
if (DEBUG)
Log.d(TAG, "onStop");
mSensorManager.unregisterListener(mListener);
+
super.onStop();
}
+
+ @Override
+ protected void onDestroy() {
+ locMgr.removeUpdates(locListener);
+ super.onDestroy();
+ }
}
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/DrawSurfaceView.java b/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/DrawSurfaceView.java
index 5bb1263..f68ccfd 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/DrawSurfaceView.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep2/src/com/cobyplain/augmentreality/DrawSurfaceView.java
@@ -1,11 +1,8 @@
package com.cobyplain.augmentreality;
-import java.io.IOException;
-import java.io.InputStream;
import java.util.ArrayList;
import android.content.Context;
-import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep3/.classpath b/com.cobyplain.augmentreality.AugmentRealityStep3/.classpath
index 3f9691c..d57ec02 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep3/.classpath
+++ b/com.cobyplain.augmentreality.AugmentRealityStep3/.classpath
@@ -1,8 +1,9 @@
-
+
+
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep3/project.properties b/com.cobyplain.augmentreality.AugmentRealityStep3/project.properties
index 9b84a6b..ce39f2d 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep3/project.properties
+++ b/com.cobyplain.augmentreality.AugmentRealityStep3/project.properties
@@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
-target=android-16
+target=android-18
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/CameraSurfaceView.java b/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/CameraSurfaceView.java
index 0ba9d4d..5cfd001 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/CameraSurfaceView.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/CameraSurfaceView.java
@@ -6,7 +6,6 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
-import android.graphics.Bitmap;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/Compass.java b/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/Compass.java
index 9f6942a..6e1c04f 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/Compass.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/Compass.java
@@ -34,18 +34,26 @@
public class Compass extends Activity {
private static final String TAG = "Compass";
- private static boolean DEBUG = false;
+ private static boolean DEBUG = true;
private SensorManager mSensorManager;
private Sensor mSensor;
private DrawSurfaceView mDrawView;
LocationManager locMgr;
+ LocationListener locListener;
private final SensorEventListener mListener = new SensorEventListener() {
+ private float[] mRotationMatrix = new float[16];
+ private float[] mValues = new float[3];
+
public void onSensorChanged(SensorEvent event) {
+ SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
+ SensorManager.getOrientation(mRotationMatrix, mValues);
+
if (DEBUG)
- Log.d(TAG, "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");
+ Log.d(TAG, "sensorChanged (" + Math.toDegrees(mValues[0]) + ", " + Math.toDegrees(mValues[1]) + ", " + Math.toDegrees(mValues[2]) + ")");
+
if (mDrawView != null) {
- mDrawView.setOffset(event.values[0]);
+ mDrawView.setOffset((float) Math.toDegrees(mValues[0]));
mDrawView.invalidate();
}
}
@@ -54,12 +62,11 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
- @SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
setContentView(R.layout.activity_main);
mDrawView = (DrawSurfaceView) findViewById(R.id.drawSurfaceView);
@@ -70,7 +77,7 @@ protected void onCreate(Bundle icicle) {
// using high accuracy provider... to listen for updates
locMgr.requestLocationUpdates(high.getName(), 0, 0f,
- new LocationListener() {
+ locListener = new LocationListener() {
public void onLocationChanged(Location location) {
// do something here to save this new location
Log.d(TAG, "Location Changed");
@@ -110,4 +117,10 @@ protected void onStop() {
mSensorManager.unregisterListener(mListener);
super.onStop();
}
+
+ @Override
+ protected void onDestroy() {
+ locMgr.removeUpdates(locListener);
+ super.onDestroy();
+ }
}
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/DrawSurfaceView.java b/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/DrawSurfaceView.java
index 2f85a9d..44e8bb3 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/DrawSurfaceView.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep3/src/com/cobyplain/augmentreality/DrawSurfaceView.java
@@ -7,10 +7,8 @@
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
-import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
-import android.util.FloatMath;
import android.util.Log;
import android.view.View;
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep4/.classpath b/com.cobyplain.augmentreality.AugmentRealityStep4/.classpath
index 3f9691c..d57ec02 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep4/.classpath
+++ b/com.cobyplain.augmentreality.AugmentRealityStep4/.classpath
@@ -1,8 +1,9 @@
-
+
+
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/CameraSurfaceView.java b/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/CameraSurfaceView.java
index 0ba9d4d..5cfd001 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/CameraSurfaceView.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/CameraSurfaceView.java
@@ -6,7 +6,6 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
-import android.graphics.Bitmap;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
diff --git a/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/Compass.java b/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/Compass.java
index 9f6942a..666120f 100644
--- a/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/Compass.java
+++ b/com.cobyplain.augmentreality.AugmentRealityStep4/src/com/cobyplain/augmentreality/Compass.java
@@ -31,35 +31,23 @@
* @author Coby Plain coby.plain@gmail.com, Ali Muzaffar ali@muzaffar.me
*/
-public class Compass extends Activity {
+public class Compass extends Activity implements SensorEventListener, LocationListener {
private static final String TAG = "Compass";
- private static boolean DEBUG = false;
+ private static boolean DEBUG = true;
private SensorManager mSensorManager;
private Sensor mSensor;
private DrawSurfaceView mDrawView;
LocationManager locMgr;
- private final SensorEventListener mListener = new SensorEventListener() {
- public void onSensorChanged(SensorEvent event) {
- if (DEBUG)
- Log.d(TAG, "sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");
- if (mDrawView != null) {
- mDrawView.setOffset(event.values[0]);
- mDrawView.invalidate();
- }
- }
-
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- };
-
- @SuppressWarnings("deprecation")
+ private float[] mRotationMatrix = new float[16];
+ private float[] mValues = new float[3];
+
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
setContentView(R.layout.activity_main);
mDrawView = (DrawSurfaceView) findViewById(R.id.drawSurfaceView);
@@ -69,27 +57,7 @@ protected void onCreate(Bundle icicle) {
LocationUtils.createFineCriteria(), true));
// using high accuracy provider... to listen for updates
- locMgr.requestLocationUpdates(high.getName(), 0, 0f,
- new LocationListener() {
- public void onLocationChanged(Location location) {
- // do something here to save this new location
- Log.d(TAG, "Location Changed");
- mDrawView.setMyLocation(location.getLatitude(), location.getLongitude());
- mDrawView.invalidate();
- }
-
- public void onStatusChanged(String s, int i, Bundle bundle) {
-
- }
-
- public void onProviderEnabled(String s) {
- // try switching to a different provider
- }
-
- public void onProviderDisabled(String s) {
- // try switching to a different provider
- }
- });
+ locMgr.requestLocationUpdates(high.getName(), 0, 0f, this);
}
@@ -99,15 +67,65 @@ protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
- mSensorManager.registerListener(mListener, mSensor,
- SensorManager.SENSOR_DELAY_GAME);
+ mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
+ }
+
+ /***
+ * START SensorEventListener
+ */
+ @Override
+ public void onSensorChanged(SensorEvent event) {
+ SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
+ SensorManager.getOrientation(mRotationMatrix, mValues);
+ if (DEBUG) {
+ Log.d(TAG, "sensorChanged (" + Math.toDegrees(mValues[0]) + ", " + Math.toDegrees(mValues[1]) + ", " + Math.toDegrees(mValues[2]) + ")");
+ }
+ if (mDrawView != null) {
+ mDrawView.setOffset((float) Math.toDegrees(mValues[0]));
+ mDrawView.invalidate();
+ }
+ }
+
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {
+
+ }
+ /***
+ * END SensorEventListener
+ */
+
+ /***
+ * START LocationListener
+ */
+ public void onLocationChanged(Location location) {
+ // do something here to save this new location
+ Log.d(TAG, "Location Changed");
+ mDrawView.setMyLocation(location.getLatitude(), location.getLongitude());
+ mDrawView.invalidate();
+ }
+
+ public void onStatusChanged(String s, int i, Bundle bundle) {
+
+ }
+
+ public void onProviderEnabled(String s) {
}
+ public void onProviderDisabled(String s) {
+ }
+ /***
+ * END LocationListener
+ */
@Override
protected void onStop() {
if (DEBUG)
Log.d(TAG, "onStop");
- mSensorManager.unregisterListener(mListener);
+ mSensorManager.unregisterListener(this);
super.onStop();
}
+
+ @Override
+ protected void onDestroy() {
+ locMgr.removeUpdates(this);
+ super.onDestroy();
+ }
}