Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Last active April 4, 2023 11:15
Show Gist options
  • Save gabrielemariotti/d23bfe583e900a4f9276 to your computer and use it in GitHub Desktop.
Save gabrielemariotti/d23bfe583e900a4f9276 to your computer and use it in GitHub Desktop.
Android Wear: Heart Rate and Samsung Gear Live. (basic example)
<!-- Declare the permission for body sensor -->
<uses-permission android:name="android.permission.BODY_SENSORS" />
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
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:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect_heart"
app:roundLayout="@layout/round_heart"
tools:context=".MyActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
public class MyActivity extends Activity implements SensorEventListener {
//UI Elements
private CircledImageView mCircledImageView;
private TextView mTextView;
//Sensor and SensorManager
Sensor mHeartRateSensor;
SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.heart_layout);
//Sensor and sensor manager
mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
//View
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mCircledImageView = (CircledImageView) stub.findViewById(R.id.circle);
mTextView = (TextView) stub.findViewById(R.id.value);
}
});
}
@Override
protected void onResume() {
super.onResume();
//Register the listener
if (mSensorManager != null){
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
//Unregister the listener
if (mSensorManager!=null)
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
//Update your data. This check is very raw. You should improve it when the sensor is unable to calculate the heart rate
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
if ((int)event.values[0]>0) {
mCircledImageView.setCircleColor(getResources().getColor(R.color.green));
mTextView.setText("" + (int) event.values[0]);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
tools:deviceIds="wear_round">
<android.support.wearable.view.CircledImageView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
app:circle_radius="50dp"
android:id="@+id/circle"
app:circle_color="@color/light_grey"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center"
android:text="wait...."
android:textColor="@color/black"
android:textSize="20sp"
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.wearable.view.CircledImageView>
@tajchert
Copy link

tajchert commented Nov 1, 2014

It doesn't work anymore - probably some update killed it.

@nanofaroque
Copy link

Can you please tell why my samsung gear is not connecting with android studio since you worked on samsung gear. Please help me on that case. I turned on usb debugging in watch. Watch is connected with Samsung S4. Thanks

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