注意:使用 計步器傳感器 Sensor.TYPE_STEP_COUNTER 獲取步數前需要手機支持該傳感器
1、學習資料
1.1 SENSOR.TYPE_STEP_COUNTER
翻譯:
描述步數計數器傳感器的常數。
這種類型的傳感器返回自上次激活時重新啟動以來用戶所采取的步驟數。該值作為一個浮點數返回(小數部分設置為零),只有在系統重啟時才會被重置為零。事件的時間戳設置為采取該事件的最后一步的時間。該傳感器采用硬件實現,預計功耗較低。如果您想在長時間內持續跟蹤步數,請不要取消該傳感器的注冊,這樣即使AP處於掛起模式,它也會在后台計算步數,並在AP喚醒時報告累計計數。應用程序需要為該傳感器保持注冊,因為如果未激活,步數計數器將不計算步數。這種傳感器是理想的健身跟蹤應用。它被定義為Sensor#REPORTING_MODE_ON_CHANGE傳感器。
該傳感器需要權限android.permission.ACTIVITY_RECOGNITION。
看 SensorEvent.values 以獲取更多詳細信息。
常量:19 (0x00000013)
1.2 傳感器返回值
說明:Sensor.TYPE_STEP_COUNTER的 event 中只含有一個float,其代表了已激活傳感器最后一次重新啟動以來用戶邁出的步數
2、Android代碼
主要是:MainActivity.java、activity_main.xml、AndroidManifest.xml
2.1 MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Environment;
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.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SensorEventListener,View.OnClickListener {
private SensorManager mSensorMgr;
private TextView tvx;
private TextView tvy;
private TextView tvz;
private TextView step;
private List<String> LS;
private boolean s; // 記錄是否開始
private int s1; // 開始后,記錄第一次計步器返回的值
private int s2; // 記錄最后一次計步器返回的值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LS = new ArrayList<String>();
Button bt=findViewById(R.id.bt_dsp); // 開始顯示加速度
bt.setOnClickListener(this); // 設置監聽
Button bt_stop=findViewById(R.id.bt_stop); // 停止顯示加速度
bt_stop.setOnClickListener(this); // 設置監聽
tvx=findViewById(R.id.tvx); // x軸
tvy=findViewById(R.id.tvy); // y軸
tvz=findViewById(R.id.tvz); // z軸
step=findViewById(R.id.step); // 步數統計
mSensorMgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE); // 獲取服務
}
protected void onPause()
{
super.onPause();
mSensorMgr.unregisterListener(this); // 取消監聽
}
protected void onResume()
{
super.onResume();
}
protected void onStop()
{
super.onStop();
mSensorMgr.unregisterListener(this); // 取消監聽
}
@SuppressLint("SetTextI18n")
public void onSensorChanged(SensorEvent event) // 監聽數據變化
{
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
tvx.setText("ACC_X: "+Float.toString(values[0]));
tvy.setText("ACC_Y: "+Float.toString(values[1]));
tvz.setText("ACC_Z: "+Float.toString(values[2]));
String s = ""; // 保存數據到字符串中
s = System.currentTimeMillis()+","+Float.toString(values[0])+","+Float.toString(values[1])+","+Float.toString(values[2]);
LS.add(s);
}
if(event.sensor.getType() == Sensor.TYPE_STEP_COUNTER){
float[] values = event.values;
step.setText(Float.toString(values[0]));
if(s){ // 點擊開始
s1 = (int) values[0];
s = false;
}
s2 = (int) values[0];
int st = s2 - s1;
step.setText(Integer.toString(st));
// Log.d("步數計數器",Float.toString(values[0]));
}
}
public void onAccuracyChanged(Sensor sensor,int accuracy)
{//不用處理,空着就行
return ;
}
private static final String TAG = "ACCCollection:";
public static void writeLS(List<String> LS) {
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/pdr_ZL/";
File folde = new File(path);
Log.i(TAG, "write: -------1");
if (!folde.exists() || !folde.isDirectory())
{
Log.i(TAG, "write: --------2");
folde.mkdirs();
}
Date date = new Date();
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
File file=new File(path,time+"_pixel2_B.csv");
if(!file.exists())
{
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
int i;
for(i=0;i<LS.size();i++)
{
bw.write(LS.get(i));
bw.newLine(); // 行換行
}
bw.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void onClick(View v) //監聽函數
{
if(v.getId() == R.id.bt_dsp) // 開始顯示加速度
{
s = true; // 點擊開始記錄
mSensorMgr.unregisterListener(this,mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
mSensorMgr.unregisterListener(this,mSensorMgr.getDefaultSensor(Sensor.TYPE_STEP_COUNTER));
//注冊加速度傳感器
mSensorMgr.registerListener(this,
mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
//注冊步數統計傳感器
mSensorMgr.registerListener(this,
mSensorMgr.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),SensorManager.SENSOR_DELAY_NORMAL);
LS.clear();
return;
}
if(v.getId() == R.id.bt_stop) // 停止監聽
{
mSensorMgr.unregisterListener(this);
writeLS(LS);
return;
}
}
}
2.2 activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tvx"
android:layout_width="214dp"
android:layout_height="69dp"
android:text="TextView"
tools:layout_editor_absoluteX="117dp"
tools:layout_editor_absoluteY="100dp" />
<TextView
android:id="@+id/tvy"
android:layout_width="214dp"
android:layout_height="53dp"
android:text="TextView"
tools:layout_editor_absoluteX="126dp"
tools:layout_editor_absoluteY="158dp" />
<TextView
android:id="@+id/tvz"
android:layout_width="214dp"
android:layout_height="53dp"
android:text="TextView"
tools:layout_editor_absoluteX="130dp"
tools:layout_editor_absoluteY="234dp" />
<TextView
android:id="@+id/step"
android:layout_width="214dp"
android:layout_height="53dp"
android:text="TextView"
tools:layout_editor_absoluteX="130dp"
tools:layout_editor_absoluteY="234dp" />
<Button
android:id="@+id/bt_dsp"
android:layout_width="131dp"
android:layout_height="79dp"
android:text="開始顯示加速度"
tools:layout_editor_absoluteX="115dp"
tools:layout_editor_absoluteY="444dp" />
<Button
android:id="@+id/bt_stop"
android:layout_width="217dp"
android:layout_height="81dp"
android:text="停止顯示加速度" />
</LinearLayout>
2.3 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pdr_save_data">
<!--申請權限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3、APP使用方法
使用方法:
1、點擊“開始顯示加速度”按鈕,注冊 加速度傳感器 和 計步器傳感器,采集模式為SensorManager.SENSOR_DELAY_NORMAL
,以回調的形式監聽這兩個傳感器的 event(若獲取了存取權限,還會保存加速度傳感器xyz三軸的數據到csv文件中,保存在“手機存儲根目錄/pdr_ZL/”中);
2、行走一定步數;
3、點擊“停止顯示加速度”按鈕,取消傳感器的注冊,根據計步器傳感器的取消注冊時的步數和注冊時的步數相減,行走步數顯示到如圖“25”的位置
說明:根據實驗結果,使用Google Pixel2手機的計步器傳感器計算行走步數具有一定的准確性,可供參考