2009-06-29 83 views
10

我正在构建Android平台的应用程序,我想使用加速度计。现在,我发现了一个非常好的传感器仿真应用程序(OpenIntents' SensorSimulator),但是为了我想要做的事情,我想创建自己的传感器仿真器应用程序。如何构建适用于Android的传感器模拟器?

我还没有找到关于如何做到这一点的信息(我不知道如果反汇编模拟器的jar是正确的),正如我所说,我想建立一个更小和更简单的传感器模拟器版本,更适合我的意图。

你知道我从哪里开始?我在哪里可以看到我需要构建的代码片段?

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

回答

8

好吧,看起来你想做的是一个应用程序,它将在模拟器上进行测试时,在您的应用程序上模拟Android设备上的传感器。
可能在你的应用程序,你有这样一行:

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 

为什么不创建一个具有您的SensorManager使用的方法的接口:

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

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

然后创建的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() 
} 

然后创建另一个MySensorManager,即此次通讯通过套接字到桌面应用程序nicates您将创建在其中输入传感器值或东西:

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 
} 

最后,替换您的第一行:

SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 

随着新线:

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

现在您可以使用该对象而不是之前使用的SensorManager。

+1

嗨艾萨克!感谢您的回答。这或多或少是我想要建立的,我会试一试,让大家知道它是如何发生的。 =) – Hugo 2009-06-30 16:20:13