如何为Android构建一个传感器模拟器?

时间:2022-03-10 20:49:08

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.

我正在为Android平台开发一个应用程序,我想使用加速计。现在,我发现了一个非常好的传感器模拟应用程序(OpenIntents’s SensorSimulator),但是,对于我想做的,a想创建我自己的传感器模拟器应用程序。

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.

我还没有找到如何实现这一点的信息(我不知道拆分模拟器的jar是否正确),正如我所说,我想构建一个更小更简单的传感器模拟器版本,更适合我的意图。

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.

基本上,我所要求的只是方向。

1 个解决方案

#1


8  

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:

看起来你想做的是一个应用程序,它会在模拟器上测试时模拟Android设备上的传感器。也许在你的申请中,你有这样一行:

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

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

为什么不创建一个具有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:

然后为SensorManager创建一个包装器,在一个真正的SensorManager对象上调用这些方法:

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:

然后创建另一个MySensorManager,这次通过套接字与桌面应用程序通信,您将在输入传感器值的地方创建:

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.

现在,您可以使用这个对象,而不是以前使用的SensorManager。

#1


8  

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:

看起来你想做的是一个应用程序,它会在模拟器上测试时模拟Android设备上的传感器。也许在你的申请中,你有这样一行:

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);

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

为什么不创建一个具有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:

然后为SensorManager创建一个包装器,在一个真正的SensorManager对象上调用这些方法:

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:

然后创建另一个MySensorManager,这次通过套接字与桌面应用程序通信,您将在输入传感器值的地方创建:

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.

现在,您可以使用这个对象,而不是以前使用的SensorManager。