0

I'm trying to have a background service which reads the azimuth, pitch and roll of a phone and sends them over the network. I have already created a IntentService and the calculation code to calculate the orientation however I'm not quite sure how I am meant to register the background service as a event listener.

public class SocketService extends IntentService implements SensorEventListener {
Socket my_socket;
PrintWriter out;
float[] mGravity;
float[] mGeomagnetic;
float azimuth,pitch,roll;

public void onAccuracyChanged(Sensor s, int accuracy){

}
public void onSensorChanged(SensorEvent event){
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
        if (success) {
            float orientationData[] = new float[3];
            SensorManager.getOrientation(R, orientationData);
            azimuth = orientationData[0];
            pitch = orientationData[1];
            roll = orientationData[2];
            out.print("Hello");
            out.flush();

        }
    }
}
public SocketService(){
    super("Socket Service");
}


protected void onHandleIntent(Intent workIntent) {
    try {
        my_socket = new Socket(ip, 5000);
        out = new PrintWriter(my_socket.getOutputStream());
    }catch(Exception e){
        Log.v("ERROR",e.getMessage());
    }
}

}

I have tried inside the onHandleIntent to put

        Sensor mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        Sensor mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
        mSensorManager.registerListener(this,mAccelerometer,SensorManager.SENSOR_DELAY_FASTEST);
        mSensorManager.registerListener(this,mGravity,SensorManager.SENSOR_DELAY_FASTEST);

Which compiles however no data is detected at the other end of the socket (no data is detected even if I try to log it so there must be a problem with the actual SensorEventListener

Bula
  • 2,398
  • 5
  • 28
  • 54
  • It works for me, what do you put at the place of the dots exactly? mSensorManager.registerListener(this,...) – jbarat Feb 17 '16 at 14:07
  • @jbarat Yes sorry. I updated the question – Bula Feb 17 '16 at 16:53
  • Just to make sure if the code is the problem not something else, if you put this code into an activity does it work? – jbarat Feb 17 '16 at 16:57
  • Are you trying in on an emulator or on a real phone? And What is compilation target? – jbarat Feb 17 '16 at 17:35
  • On a real phone and it's lollipop – Bula Feb 17 '16 at 17:36
  • Just found out if i put a log.v at the beginning of the `onSensorChanged` method my logcat does continuously print it. So I assume there is a problem somewhere in the middle of the function – Bula Feb 17 '16 at 17:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103754/discussion-between-jbarat-and-bula). – jbarat Feb 17 '16 at 17:45

1 Answers1

1

Seems the problem was that

Sensor mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

is not the same as

 Sensor mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

Once I changed this, everything worked as a charm.

Bula
  • 2,398
  • 5
  • 28
  • 54