|
|||||
搭載されているセンサを調べるAndroid端末にどのようなセンサが搭載されているかは、メーカーのサイトを見ても意外と正確に情報を得ることができない。確かに、世間一般の人には自分の持っている端末にどのようなセンサが搭載されているか等は、さして関心のない事かも知れない。 しかし、Androidのプログラムを書いてセンサを利用しようという場合は別だ。そのようなとき、Android端末の実機があればAPIを使って簡単にセンサの一覧を取得できる。 まず、ContextのgetSystemServiceメソッドでSENSOR_SERVICEを指定してSensorManagerオブジェクトを取得する。取得したSensorManagerのgetSensorListメソッドを使えば、その端末に搭載されているすべてのセンサを取得できる。getSensorListメソッドが成功するとSensorオブジェクトがListで取得できるので、後は順にgetTypeメソッドでセンサ種別を調べていけば良い。
package biz.office_matsunaga.android;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class SensorListActivity extends Activity {
SensorManager sensorManager;
TextView textView2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView2 = (TextView)findViewById(R.id.textView2);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL); // センサのリスト取得
textView2.setText("");
for(Sensor sensor: sensors) {
switch(sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
textView2.append("加速度センサ\n");
break;
case Sensor.TYPE_GRAVITY:
textView2.append("重力センサ\n");
break;
case Sensor.TYPE_GYROSCOPE:
textView2.append("ジャイロスコープ\n");
break;
case Sensor.TYPE_LIGHT:
textView2.append("光センサ\n");
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
textView2.append("直線加速度センサ\n");
break;
case Sensor.TYPE_MAGNETIC_FIELD:
textView2.append("磁界センサ\n");
break;
case Sensor.TYPE_ORIENTATION:
textView2.append("方位センサ\n");
break;
case Sensor.TYPE_PRESSURE:
textView2.append("気圧センサ\n");
break;
case Sensor.TYPE_PROXIMITY:
textView2.append("近接センサ\n");
break;
case Sensor.TYPE_ROTATION_VECTOR:
textView2.append("回転ベクトル\n");
break;
case Sensor.TYPE_TEMPERATURE:
textView2.append("温度センサ\n");
break;
default:
textView2.append("不明なセンサ " + sensor.getType() + "\n");
break;
}
}
}
}
右はGalaxy SII LTEで上記のコードを実行したときのものである。 (2012/01/28)
Copyright(C) 2004-2013 モバイル開発系(K) All rights reserved.
[Home]
|
|||||