The differences between the code for the emulator and the code for a real device

The Android developers can use Sensor Simulator to test their applications, in particular when they use sensors such as accelerometer or compass.
In this post I explain the differences in the code for an activity that runs on the emulator or a real device, and I don’t discuss how to install or launch the emulator, for those topics you can refer to Sensor Simulator.
Switching from the emulator to the real device and vice versa you need to make some code changes and these changes can be divided into 4 points.

  1. different imports
  2. connection to the simulator in the onCreate method (this part is missing in the current code for the device)
  3. different listeners in the onResume method
  4. different ways to get the type of sensor in the onSensorChanged method


Here below I write the code with which you can connect to the emulator, to switch to the code for the real device comment the lines between
/ / SENSOR SIMULATOR
and
/ / **********
and uncomment the lines between
/ / REAL DEVICE
and
/ / **********.

package eu.lucazanini.sensorsimulator;

// SENSOR SIMULATOR
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
// **********

// REAL DEVICE
//import android.hardware.Sensor;
//import android.hardware.SensorEvent;
//import android.hardware.SensorEventListener;
//import android.hardware.SensorManager; // required for getRotationMatrix and getOrientation
//**********

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class SensorSimulatorActivity extends Activity implements
	SensorEventListener {

    // SENSOR SIMULATOR
    private SensorManagerSimulator mSensorManager;
    private ConnectionToSensorSimulator conn;

    // **********

    // REAL DEVICE
    // private SensorManager mSensorManager;
    // **********

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	// SENSOR SIMULATOR
	mSensorManager = SensorManagerSimulator.getSystemService(this,
		SENSOR_SERVICE);
	conn = new ConnectionToSensorSimulator();
	conn.execute();
	// **********

	// REAL DEVICE
	// mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
	// **********

    }

    @Override
    protected void onResume() {
	super.onResume();
	// SENSOR SIMULATOR
	mSensorManager.registerListener(this,
		mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
		SensorManagerSimulator.SENSOR_DELAY_NORMAL);

	mSensorManager.registerListener(this,
		mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
		SensorManagerSimulator.SENSOR_DELAY_NORMAL);

	mSensorManager.registerListener(this,
		mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
		SensorManagerSimulator.SENSOR_DELAY_NORMAL);
	// **********

	// REAL DEVICE
	// mSensorManager.registerListener(this,
	// mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
	// SensorManager.SENSOR_DELAY_NORMAL);
	//
	// mSensorManager.registerListener(this,
	// mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
	// SensorManager.SENSOR_DELAY_FASTEST);
	//
	// mSensorManager.registerListener(this,
	// mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
	// SensorManager.SENSOR_DELAY_FASTEST);
	// **********
    }

    @Override
    protected void onPause() {
	super.onPause();
	mSensorManager.unregisterListener(this);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {

	synchronized (this) {

	    // SENSOR SIMULATOR
	    int sensor = event.type;
	    // **********

	    // REAL DEVICE
	    // int sensor = event.sensor.getType();
	    // **********

	    float[] values = event.values;

	    switch (sensor) {
	    case Sensor.TYPE_ACCELEROMETER:

		break;
	    case Sensor.TYPE_MAGNETIC_FIELD:

		break;
	    case Sensor.TYPE_GYROSCOPE:

		break;
	    default:
		break;
	    }
	}
    }

    private class ConnectionToSensorSimulator extends
	    AsyncTask<Void, Void, Boolean> {

	@Override
	protected Boolean doInBackground(Void... params) {
	    Log.d("SENSOR", "CONNECTING TO SENSOR SIMULATOR");
	    mSensorManager.connectSimulator();
	    return true;
	}

	@Override
	protected void onPostExecute(Boolean result) {
	    super.onPostExecute(result);
	    if (result) {
		Log.d("SENSOR", "CONNECTED TO SENSOR SIMULATOR");
	    } else {
		Log.d("SENSOR", "NOT CONNECTED TO SENSOR SIMULATOR");
	    }
	}

    }

}

If you don’t want to use AsyncTask to connect to emulator, you can replace

	mSensorManager = SensorManagerSimulator.getSystemService(this,
		SENSOR_SERVICE);
	conn = new ConnectionToSensorSimulator();
	conn.execute();

with

	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
		.permitAll().build();
	StrictMode.setThreadPolicy(policy);
	mSensorManager = SensorManagerSimulator.getSystemService(this,
		SENSOR_SERVICE);
	mSensorManager.connectSimulator();

To get other info you can see this post.

The line

import android.hardware.SensorManager

may also be necessary for the emulator, for example because you have to get the rotation matrix (getOrientationMatrix method) or spatial orientation (method getOrientation).


Comments

One response to “The differences between the code for the emulator and the code for a real device”

  1. […] a previous post I explained the differences between the code for the emulator and the code for the real device, but […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.