Skip to content

Instantly share code, notes, and snippets.

@mjohnsullivan
Last active March 1, 2024 01:28
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mjohnsullivan/557c2f19ba177312b1d7 to your computer and use it in GitHub Desktop.
Save mjohnsullivan/557c2f19ba177312b1d7 to your computer and use it in GitHub Desktop.
Android Wear activity that reads and displays sensor data from the device
package com.example.wear;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends Activity implements SensorEventListener {
private static final String TAG = "MainActivity";
private TextView mTextViewStepCount;
private TextView mTextViewStepDetect;
private TextView mTextViewHeart;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Keep the Wear screen always on (for testing only!)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextViewStepCount = (TextView) stub.findViewById(R.id.step_count);
mTextViewStepDetect = (TextView) stub.findViewById(R.id.step_detect);
mTextViewHeart = (TextView) stub.findViewById(R.id.heart);
getStepCount();
}
});
}
private void getStepCount() {
SensorManager mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
Sensor mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
Sensor mStepCountSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
Sensor mStepDetectSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mStepCountSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mStepDetectSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
private String currentTimeStr() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(c.getTime());
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(TAG, "onAccuracyChanged - accuracy: " + accuracy);
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
String msg = "" + (int)event.values[0];
mTextViewHeart.setText(msg);
Log.d(TAG, msg);
}
else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
String msg = "Count: " + (int)event.values[0];
mTextViewStepCount.setText(msg);
Log.d(TAG, msg);
}
else if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
String msg = "Detected at " + currentTimeStr();
mTextViewStepDetect.setText(msg);
Log.d(TAG, msg);
}
else
Log.d(TAG, "Unknown sensor type");
}
}
@mjohnsullivan
Copy link
Author

@leonlihkust
Copy link

Hi,
Just want to ask if the code works for Android wear gear or Samsung gear. Because Samsung gear runs on TIZEN OS while Android wear runs on Android wear OS.
Thank you.
Leon Li

@leonlihkust
Copy link

Hi,
One more question. Could we collect data from Android wear like heart rate to mobile phone or to server?

@mjohnsullivan
Copy link
Author

This code is for Android Wear.

You can easily pass the collected data directly over the network to a server on Android Wear 2.0; for 1.0 you'll need to use the Data Layer API to pass data to an Android companion app on the phone.

@MujasSam
Copy link

Hi Folks,
I tried this code and observed that in my Moto 360 Sport watch the heart rate is keep triggers onSensorChanged() even I am not wearing the watch. This should give me the heart rate only when i tied up the watch.

@cengm
Copy link

cengm commented Jun 9, 2018

Dont forget to ask permission before read Sensor value.
`public void permissionRequest(){

    if (checkSelfPermission(Manifest.permission.BODY_SENSORS)
            != PackageManager.PERMISSION_GRANTED) {

        requestPermissions(
                new String[]{Manifest.permission.BODY_SENSORS},
                MY_PERMISSIONS_REQUEST_BODY_SENSOR);
    }
    else{
        Log.d(TAG,"ALREADY GRANTED");
    }
}`

OR you can enable it from Settings>Apps>YOUR_APP>Permissions>Sensor

@lailaalqam
Copy link

Hi mjohnsullivan
What kind of android watch using?
You can use any Android watch for this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment