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