2017-02-18 47 views
-1

我有一個三星Galaxy S7,我想要觸摸心臟傳感器(靠近相機的手機),應用程序讀取心率並將其顯示到TextView中。Java Android - 創建一個簡單的心率監測器

我試過了,但沒有運行。

我的活動

import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.hardware.SensorEventListener; 
import android.util.Log; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity implements SensorEventListener{ 
    public TextView testoLabel; 
    private static final String TAG = "MyActivity"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.e(TAG, "onCreate called"); 

     testoLabel = (TextView) findViewById(R.id.testoLabel); 

     SensorManager sMgr; 
     sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE); 

     Sensor battito = null; 
     battito = sMgr.getDefaultSensor(Sensor.TYPE_HEART_RATE); 
     if(battito != null) 
      testoLabel.setText("load sensor"); 
     else 
      testoLabel.setText("no load sensor"); 

     sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_NORMAL); 

    } 
    @Override 
    public void onSensorChanged(SensorEvent event) { 
     if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) { 
      String msg = " Value sensor: " + (int)event.values[0]; 
      testoLabel.setText(msg); 
     } 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     System.out.println("onAccuracyChanged - accuracy: " + accuracy); 
    } 
} 

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.giuseppeaccardo.prova" > 
    <uses-permission android:name="android.permission.BODY_SENSORS" /> 

    <application 

     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity android:name=".MainActivity" > 
      <uses-permission android:name="android.permission.BODY_SENSORS" /> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

這裏沒有足夠的信息繼續下去。你至少需要調試它併發布logcat跟蹤 –

+0

好的...但是代碼運行,但手機的心臟傳感器沒有檢測到觸摸 –

回答

0

你可以嘗試改變這一點:

sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_NORMAL); 

這樣:

sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_FASTEST); 

這將增加讀取手指的時間。

您需要做的最後一件事是確保您已正確授予權限。我看到你已經允許它在清單中,但你也需要在運行時允許它。看到這個link尋求幫助。

否則如先前建議,請添加您的問題

0

一些細節,我認爲你的情況的問題是對人體感應的權限。我建議您檢查用戶是否授予身體傳感器權限,然後初始化心率傳感器,如果是,請繼續請求用戶授予權限。即使您已在清單中添加了使用人體感應器權限,這也適用。

if (checkSelfPermission(Manifest.permission.BODY_SENSORS) 
      != PackageManager.PERMISSION_GRANTED) { 
     requestPermissions(new String[]{Manifest.permission.BODY_SENSORS}, 
       PERMISSIONS_REQUEST_BODY_SENSORS); 
} else { 
     //Your code for declaring and initializing Sensor manager and Heartrate sensor 
}; 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, 
             int[] grantResults) { 
    if (requestCode == PERMISSIONS_REQUEST_BODY_SENSORS 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     //Your code for declaring and initializing Sensor manager and Heartrate sensor 
    } 
}