Accessing Sensors and Gps

Tutorials with advanced 'difficulty' and more Lines of Code.

Accessing Sensors and Gps

Postby hamorabi » Fri Sep 10, 2010 1:14 pm

Hi,

I am trying to access the sensors and gps using the code bellow, and idea why the code doesn't work (Forced Stop).
The following permissions were add to the Manifest file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.INTERNET"/>
Thanks,


Code: Select all
package com.example.SensorsAndGps;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;



@SuppressWarnings("deprecation")
public class SensorsAndGps extends Activity {
    /** Called when the activity is first created. */
    final String tag = "SensorsAndGps";
    SensorManager sm = null;
    TextView xViewA = null;
    TextView yViewA = null;
    TextView zViewA = null;
    TextView xViewO = null;
    TextView yViewO = null;
    TextView zViewO = null;
    TextView gpsView = null;
    private LocationManager lm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lm = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
       lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1l,1l, (LocationListener) this);
       // get reference to SensorManager
       sm = (SensorManager) getSystemService(SENSOR_SERVICE);
       sm.registerListener((SensorListener) this, SensorManager.SENSOR_ORIENTATION |SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_NORMAL);
        setContentView(R.layout.main);
        xViewA = (TextView) findViewById(R.id.xbox);
        yViewA = (TextView) findViewById(R.id.ybox);
        zViewA = (TextView) findViewById(R.id.zbox);
        xViewO = (TextView) findViewById(R.id.xboxo);
        yViewO = (TextView) findViewById(R.id.yboxo);
        zViewO = (TextView) findViewById(R.id.zboxo);
        gpsView = (TextView)findViewById(R.id.gpsbox);
    }
    public void onLocationChanged(Location arg0) {
        String lat = String.valueOf(arg0.getLatitude());
        String lon = String.valueOf(arg0.getLongitude());
        gpsView.setText("GPS Location: lat="+lat+"  lon="+lon);
        Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
    }
    public void onProviderDisabled(String arg0) {
        Log.e("GPS", "provider disabled " + arg0);
    }
    public void onProviderEnabled(String arg0) {
        Log.e("GPS", "provider enabled " + arg0);
    }
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        Log.e("GPS", "status changed to  " + arg0 + " [" + arg1 + "]");
    }
    public void onSensorChanged(int sensor, float[] values) {
        synchronized (this.sm) {
            Log.d(tag, "onSensorChanged: " + sensor + ", x: " +
                  values[0] + ", y: " + values[1] + ", z: " + values[2]);
            if (sensor == SensorManager.SENSOR_ORIENTATION) {
                xViewO.setText("Orientation X: " + values[0]);
                yViewO.setText("Orientation Y: " + values[1]);
                zViewO.setText("Orientation Z: " + values[2]);
            }
            if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
                xViewA.setText("Accel X: " + values[0]);
                yViewA.setText("Accel Y: " + values[1]);
                zViewA.setText("Accel Z: " + values[2]);
            }           
        }
    }
   
    public void onAccuracyChanged(int sensor, int accuracy) {
       Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
    }
 
    protected void onResume() {
        super.onResume();
      // register this class as a listener for the orientation and accelerometer sensors
        sm.registerListener((SensorListener) this,
                SensorManager.SENSOR_ORIENTATION |SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
   
    @Override
    protected void onStop() {
        // unregister listener
        sm.unregisterListener((SensorListener) this);
        super.onStop();
    }

}
hamorabi
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 06, 2010 6:28 pm

Top

Re: Accessing Sensors and Gps

Postby hamorabi » Sat Sep 11, 2010 1:55 pm

i have managed to activate read the sensors but i am still having a problem with the GPS:

package com.example.SensorsAndGps;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;




public class SensorsAndGps extends Activity implements SensorEventListener {

/** Called when the activity is first created. */
final String tag = "SensorsAndGps";
SensorManager sm = null;

TextView xViewA = null;
TextView yViewA = null;
TextView zViewA = null;
TextView xViewO = null;
TextView yViewO = null;
TextView zViewO = null;
TextView gpsView = null;
LocationManager lm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
lm = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000,30, (LocationListener) this);
} catch (Exception e){
Toast.makeText(SensorsAndGps.this, "There was an error in Intialzing the GPS", Toast.LENGTH_LONG).show();
}
// get reference to SensorManager
try{
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
} catch (Exception e){
Toast.makeText(SensorsAndGps.this, "There was an error in Intialzing the Sensors", Toast.LENGTH_LONG).show();
}

setContentView(R.layout.main);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
gpsView = (TextView)findViewById(R.id.gpsbox);
}
public void onLocationChanged(Location arg0) {
String lat = String.valueOf(arg0.getLatitude());
String lon = String.valueOf(arg0.getLongitude());
gpsView.setText("GPS Location: lat="+lat+" lon="+lon);
Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
}
public void onProviderDisabled(String arg0) {
Toast.makeText(SensorsAndGps.this, "Provider is disabled", Toast.LENGTH_LONG).show();
Log.e("GPS", "provider disabled " + arg0);
}
public void onProviderEnabled(String arg0) {
Toast.makeText(SensorsAndGps.this, "Provider is enabled", Toast.LENGTH_LONG).show();
Log.e("GPS", "provider enabled " + arg0);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
}



public void onAccuracyChanged(int sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}

protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and accelerometer sensors
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sm.SENSOR_DELAY_GAME);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), sm.SENSOR_DELAY_GAME);

}


protected void onStop() {
// unregister listener
super.onStop();
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION));

}

public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent event) {
synchronized (this.sm) {
switch (event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
xViewA.setText("Accel X: " + Float.toString(event.values[0]));
yViewA.setText("Accel Y: " + Float.toString(event.values[1]));
zViewA.setText("Accel Z: " + Float.toString(event.values[2]));
break;
case Sensor.TYPE_ORIENTATION:
xViewO.setText("Accel X: " + Float.toString(event.values[0]));
yViewO.setText("Accel Y: " + Float.toString(event.values[1]));
zViewO.setText("Accel Z: " + Float.toString(event.values[2]));
break;
}
}
}

}
hamorabi
Freshman
Freshman
 
Posts: 9
Joined: Mon Sep 06, 2010 6:28 pm

Re: Accessing Sensors and Gps

Postby edreamz » Wed Sep 15, 2010 11:32 pm

Are you testing it on the emulator or a test phone.
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests