Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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>
Expand Down Expand Up @@ -151,15 +169,34 @@ 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();
}
}

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.

Expand Down
3 changes: 2 additions & 1 deletion com.cobyplain.augmentreality.AugmentRealityStep1/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,38 @@ 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>
LocationProvider high = locMgr.getProvider(locMgr.getBestProvider(LocationUtils.createFineCriteria(), true));

// 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");
Expand Down Expand Up @@ -102,4 +108,10 @@ protected void onStop() {
mSensorManager.unregisterListener(mListener);
super.onStop();
}

@Override
protected void onDestroy() {
locMgr.removeUpdates(locListener);
super.onDestroy();
}
}
3 changes: 2 additions & 1 deletion com.cobyplain.augmentreality.AugmentRealityStep2/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion com.cobyplain.augmentreality.AugmentRealityStep3/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-16
target=android-18
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -110,4 +117,10 @@ protected void onStop() {
mSensorManager.unregisterListener(mListener);
super.onStop();
}

@Override
protected void onDestroy() {
locMgr.removeUpdates(locListener);
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion com.cobyplain.augmentreality.AugmentRealityStep4/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading