プログラミング勉強日記
2021年1月22日
加速度センサーの制度や種類、バージョンなどいろいろな情報を表示する方法を示す。
Sensorクラス(センサーAPI)
Androidでは、センサーによって変換された電気信号を取得するためのセンサーAPIが提供されている。そのうちのSensorでは、各センサーに関する情報を管理する。Sensorクラスのメソッドを使って、各センサーに関する情報を取り出すことができる。
Sensorクラスのメソッド
// センサーが検出できる最大値を取得するメソッド
getMaximumRange()
// センサーの名前を取得するメソッド
getName()
// センサーからの電気信号をmAで取得するメソッド
getPower()
// センサーの精度を取得するメソッド
getResolution()
// センサーの種類を取得するメソッド
getType()
// センサーのベンダー名を取得するメソッド
getVendor()
// センサーのバージョンを取得するメソッド
getVersion()
サンプルプログラム
MainActivity.java
package sample;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity implements SensorEventListener {
private TextView myText;
private TextView textInfo;
// SensorManagerインスタンス
private SensorManager sma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = findViewById(R.id.my_text);
textInfo = findViewById(R.id.text_info);
// SensorManagerのインスタンスを取得する
sma = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sma.registerListener(this, sma.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
}
// センサーの値が変化すると呼ばれる
public void onSensorChanged(SensorEvent event) {
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
String str = " X= " + x + "\n"
+ " Y= " + y + "\n"
+ " Z= " + z;
myText.setText(str);
// センサー名
StringBuffer info = new StringBuffer("Name: ");
info.append(event.sensor.getName());
info.append("\n");
// ベンダー名
info.append("Vendor: ");
info.append(event.sensor.getVendor());
info.append("\n");
// 型番
info.append("Type: ");
info.append(event.sensor.getType());
info.append("\n");
// 最小遅れ
int data = event.sensor.getMinDelay();
info.append("Mindelay: ");
info.append(String.valueOf(data));
info.append(" usec\n");
// 最大遅れ
data = event.sensor.getMaxDelay();
info.append("Maxdelay: ");
info.append(String.valueOf(data));
info.append(" usec\n");
// レポートモード
data = event.sensor.getReportingMode();
String stinfo = "unknown";
if(data == 0){
stinfo = "REPORTING_MODE_CONTINUOUS";
}else if(data == 1){
stinfo = "REPORTING_MODE_ON_CHANGE";
}else if(data == 2){
stinfo = "REPORTING_MODE_ONE_SHOT";
}
info.append("ReportingMode: ");
info.append(stinfo);
info.append("\n");
// 最大レンジ
info.append("MaxRange: ");
float fData = event.sensor.getMaximumRange();
info.append(String.valueOf(fData));
info.append("\n");
// 分解能
info.append("Resolution: ");
fData = event.sensor.getResolution();
info.append(String.valueOf(fData));
info.append(" m/s^2\n");
// 消費電流
info.append("Power: ");
fData = event.sensor.getPower();
info.append(String.valueOf(fData));
info.append(" mA\n");
textInfo.setText(info);
}
// センサーの精度が変更されると呼ばれる
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/my_text"
android:layout_width="343dp"
android:layout_height="119dp"
android:paddingLeft="30dp"
android:paddingTop="25dp" />
<TextView
android:id="@+id/text_info"
android:layout_width="343dp"
android:layout_height="119dp"
android:paddingLeft="30dp"
android:paddingTop="25dp" />
</androidx.constraintlayout.widget.ConstraintLayout>