views:

2570

answers:

3

Hello,

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 =)

Thanks in advance for your answers.

+4  A: 

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.

Isaac Waller
Hi Isaac!! Thanks for your answer. That is more or less what I want to build, I'll give it a try and let you all know how it turned out. =)
Hugo
A: 

Hello Hugo, if I can ask you have you managed to do something on android 2.1 version or newer ones. I could really use some points or guides on how to make sensor simulator. Ty in advance for any help.

To be completely honest, I must say that no, I haven't tried this on 2.1 Android. I think you will need to try it and let us know ;)
Hugo
A: 

Hello all,

I'm interested if anyone's actually built a dummy SensorManager that implements the new API (the one making use of Sensor objects)? The aforementioned OpenIntents simulator has an open issue about this. I would like to see this fixed, but I'm stuck figuring out how to implement some parts of the API, like getSensorList. Basically, the problem is that there are no public constructors for the Sensor class. Any ideas are appreciated.

dep
This is not answer to the question. You should considerer posting this in another place.
Hugo
I know; however, I cannot comment before I reach rep. 15.Sorry.
dep