2009.02.14 追記:この記事の内容は若干古くなっています。こちらの投稿も併せてご覧下さい。
OpenIntentsという、Google Codeのプロジェクトがあります。このプロジェクトでは、役に立つライブラリやアプリ、ツールがいくつか公開されています。8/29頃に概ねver.0.9 betaに対応されているようです。OpenIntentsには、SensorSimulatorというツールが含まれています。このツールは、加速度やコンパス、回転(傾き)のセンサー入力部を、ソフトウェアでシミュレートするものです。SensorSimulatorがサーバーとなり、アプリからソケットで接続する構成です。SensorSimulatorは、デバイスの回転運動の表現にロール・ピッチ・ヨー(roll, pitch, yaw)角を用いています。簡単な解釈は以下の図をご覧下さい。(下図は端末を地面に水平に置いた時の想定です。)

SensorSimulatorを使うと、この動画のようにセンサーからの入力を受け付ける(SensorListenerをインプリしている)アプリを操作することができます。
使い方に関する簡単な説明を書きます。
・はじめに
- Java 6じゃないとダメです
- OpenIntentsはeclipseで開発されているので、eclipseを使った方が楽です
- 現時点でリリースされているOpenIntents 0.1.7は、SDK 0.9 betaに対応していないので、SVNからチェックアウトする必要があります。私は、rev.1065で動作確認しました。
・セットアップ
まず、SensorSimulator(http://openintents.googlecode.com/svn/trunk/tools/SensorSimulator)をチェックアウトし、org.openintents.tools.sensorsimulator.SensorSimulatorを実行します。Simulatorのウィンドウが表示されるはずです。この時点でエミュレータからの接続を待っている状態になっています。

次に、OpenIntentsのコア部をエミュレータにデプロイする必要があります。しかし、現時点でリリースされている0.1.7は、SDK 0.9 betaに対応していないので、SVN(http://openintents.googlecode.com/svn/trunk/openintents)からチェックアウトし、エミュレータにデプロイして下さい。(eclipseの場合、OpenIntentsプロジェクトをAndroid Applicationとして起動)
OpenIntentsが起動したら、“setting”タブを開き“Sensor Simulator”を押します。

ここでエミュレータからSensorSimulatorが待ち受けているIPアドレスやポート番号を設定します。

これで準備は完了です。OpenIntentsのOpenGLのサンプル(http://openintents.googlecode.com/svn/trunk/samples/OpenGLSensors)で動作を確認してみて下さい。
・使用上の注意
SensorSimulatorを使う上で、いくつか気にしておいた方がよいポイントや注意点があります。
- openintentsのjarを含まねばなりません。(OpenGLSensorsに含まれるopenintents-lib.jarを使えばよいと思います。)
- アプリ側のコードがOpenIntentsに依存することになります。
まず、初期化時(onCreateなど)に以下のおまじないコードが必要です。
OpenIntents.requiresOpenIntents(this);
Hardware.mContentResolver = getContentResolver();
SensorManagerのインスタンスは、SensorManagerSimulatorでなければなりません。
SensorManager sensorManager = (SensorManager) new SensorManagerSimulator((SensorManager) getSystemService(SENSOR_SERVICE));
アプリがアクティブになる際(onResumeなど)に、ソケットを開くstaticメソッドをコールする必要があります。
SensorManagerSimulator.connectSimulator();
アプリが非アクティブになる際(onStopなど)に、ソケットを閉じるstaticメソッドをコールする必要があります。
SensorManagerSimulator.disconnectSimulator();
安直に書くとこんな感じです。
public class SensorExample extends Activity implements SensorListener {
SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// --- views
・・・
// --- sensors
OpenIntents.requiresOpenIntents(this);
Hardware.mContentResolver = getContentResolver();
sensorManager = (SensorManager) new SensorManagerSimulator((SensorManager) getSystemService(SENSOR_SERVICE));
}
@Override
protected void onStop() {
sensorManager.unregisterListener(this);
SensorManagerSimulator.disconnectSimulator();
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
SensorManagerSimulator.connectSimulator();
sensorManager.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_MAGNETIC_FIELD |
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_FASTEST);
}
public void onSensorChanged(int sensor, float[] values) {
・・・
}
}
ハードに疎く、エミュレータだけが頼りの開発者(私のような..汗)がテストする用途には充分使えるような気がします。