Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Build a Sensor Simulator for Android?

I am building a application for the Android platform and I would like to use the accelerometer. Now, I have found a very nice application for sensor simulation (OpenIntents' SensorSimulator) but, for what I want to do, a would like to create my own sensor simulator application.

I have not found information on how to do this (I do not know if disassembly the jar of the Simulator is correct) and, as I said, I would like to build a smaller and simpler version of a sensor simulator, more suitable for my intents.

Do you know where could I start? where can I see what are the pieces of code that I need to build?

Basically, all my asking just for some direction.

like image 949
Hugo Avatar asked Jun 29 '09 18:06

Hugo


1 Answers

Well, it seems what you want to make is a application that will emulate the sensors on a Android device for your application while testing on the emulator.
Probably in your application, you have a line like this:

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

Why not create a interface that has the methods you use from SensorManager:

interface MySensorManager {
    List<Sensor> getSensorList(int type);

    ... // You will need to add all the methods you use from SensorManager here
}

And then create a wrapper for SensorManager that just calls those methods on a real SensorManager object:

class MySensorManagerWrapper implements MySensorManager {
    SensorManager mSensorManager;

    MySensorManagerWrapper(SensorManager sensorManager) {
        super();
        mSensorManager = sensorManager;
    }

    List<Sensor> getSensorList(int type) {
         return mSensorManager.getSensorList(type_;
    }

    ... // All the methods you have in your MySensorManager interface will need to be defined here - just call the mSensorManager object like in getSensorList()
}

And then create another MySensorManager, that this time communicates over a socket to a desktop application you will create where you enter the sensor values or something:

class MyFakeSensorManager implements MySensorManager {
    Socket mSocket;

    MyFakeSensorManager() throws UnknownHostException, IOException {
        super();
        // Connect to the desktop over a socket
        mSocket =  = new Socket("(IP address of your local machine - localhost won't work, that points to localhost of the emulator)", SOME_PORT_NUMBER);
    }

    List<Sensor> getSensorList(int type) {
        // Use the socket you created earlier to communicate to a desktop app
    }

    ... // Again, add all the methods from MySensorManager
}

And finally, replace your first line:

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

With a new line:

MySensorManager mSensorManager;
if(YOU_WANT_TO_EMULATE_THE_SENSOR_VALUES) {
    mSensorManager = new MyFakeSensorManager();
else {
    mSensorManager = new MySensorManagerWrapper((SensorManager)getSystemService(SENSOR_SERVICE));
}

Now you can just use that object instead of the SensorManager you used before.

like image 135
Isaac Waller Avatar answered Sep 28 '22 03:09

Isaac Waller