# 2021.11.19(金)④⑤ ###### tags: `AN25` `授業ノート` #### センサーを利用する 実機に内蔵されているセンサーにより、外部の環境の情報を数値として得る 温度、方位、角度など20以上ある #### センサーの利用手順 1. sensorManagerの取得 各センサーの種類に応じた制御方法を提供する 1. イベントリスナーの実装 イベントが発生した時に呼ばれるメソッドが用意されている 1. センサー値の取得 1. イベントリスナーの開放 #### 加速度センサー(アクセロメーター) ものが動くときに働く加速度(重力)を測定する x軸:実機の右から受ける加速度 y軸:実機の上から受ける加速度 z軸:実機の正面から受ける加速度 onAccuracyChanged()メソッド センサーの精度が変化した時に呼ばれる onSensorChanged()メソッド センサーの値が変化した時に呼ばれる #### センサー精度 SENSOR_DELAY_ FASTEST…最高速 GAME…ゲーム向き NORMAL…通常モード UI…低速 #### 加速度センサを万歩計に応用する x軸y軸のそれぞれが10から1、または逆へ上下運動とみなす変化した時に一歩とする ```xml= <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textSize="16sp" android:id="@+id/TextView01"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="歩数" android:textSize="48sp" android:id="@+id/TextView02" /> <Button android:text="リセット" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:onClick="buttonMethod" /> </LinearLayout> ``` ```java= package nhs00650.hal.ac.pedometer; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.util.List; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements SensorEventListener { private TextView TextView1; private TextView TextView2; int flg_x = 0; int flg_y = 0; int flg_z = 0; int count_flg = 0; int count = 0; public static float acceler_y, acceler_x, acceler_z; private SensorManager sensorManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView1 = (TextView)findViewById(R.id.TextView01); TextView2 = (TextView)findViewById(R.id.TextView02); sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); } public void onAccuracyChanged(Sensor arg0, int arg1){ } public void onSensorChanged(SensorEvent event) { acceler_x = event.values[0]; acceler_y = event.values[1]; acceler_z = event.values[2]; TextView1.setText(acceler_x + "," + acceler_y + "," + acceler_z); count_flg = 0; if(acceler_x < 0) { if (flg_x == 1) { count_flg = 1; flg_x = 0; } }else{ if(flg_x == 0){ count_flg = 1; flg_x = 1; } } if(acceler_y < 0) { if (flg_y == 1) { count_flg = 1; flg_y = 0; } }else{ if(flg_y == 0) { count_flg = 1; flg_y = 1; } } if(acceler_z < 0) { if (flg_z == 1) { count_flg = 1; flg_z = 0; } }else{ if(flg_z == 0){ count_flg = 1; flg_z = 1; } } if(count_flg == 1){ TextView2.setText(String.format("歩数 %d", count++)); } } @Override protected void onResume(){ super.onResume(); List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); if(sensors.size() > 0){ Sensor sensor = sensors.get(0); sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL); } } @Override protected void onStop(){ super.onStop(); sensorManager.unregisterListener(this); } public void buttonMethod(View myButton){ count = 0; TextView2.setText(String.format("歩数 %d", count++)); } } ``` ## 雑談 (0→やまぴ 1→みや 2→やすい 3→りょうくん 4→アミカ 5→やの 6→山田) --- <span style="color: #ff3333">aaa</span> <span style="text-decoration: underline">aaa</span>