如何建立一个传感器模拟器为Android? [英] How to Build a Sensor Simulator for Android?

查看:202
本文介绍了如何建立一个传感器模拟器为Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立针对Android平台的应用程序,我想用加速感应器。现在,我已经找到了一个非常好的应用传感器仿真( OpenIntents软件SensorSimulator ),但是,对于我想做的事情,一个想创造我自己的传感器模拟器应用程序。

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.

你知道我能开始?我在哪里可以看到什么是code的作品,我需要建立?

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.

推荐答案

嗯,看来你想那是什么将模拟一个Android设备上的传感器,为您的应用程序,同时测试在模拟器上的应用程序。
也许在你的应用程序,你有这样一行:

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

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

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
}

然后创建一个包装的SensorManager只是调用一个真正的SensorManager对象的方法:

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()
}

和再创造​​另一个MySensorManager,这一次传达了一个插座到桌面的应用程序,您将创建在其中输入传感器的值或东西:

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

通过一个新行:

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

现在你可以使用你之前使用的的SensorManager中的对象,而不是。

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

这篇关于如何建立一个传感器模拟器为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆