Search code examples
androidandroid-sensorshuawei-mobile-servicessensormanagerwearables

How can I access the heart rate sensor of my Huawei Watch 2 if it is not an original Huawei Sensor?


I bought a Huawei Watch 2 because I'm working on an app that reads steps and heart rate. Before opting for the Google Fitness API, I wanted to try accessing raw data directly from the watch sensors.

The heart rate sensor doesn't seem to be working no matter what i try. I've already put "uses-permission android:name="android.permission.BODY_SENSORS"" in my AndroidManifest file. My main activity extends the WearableActivity class and implements the SensorEventListener interface.

This is what I have in the onCreate() method regarding sensors:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    mSensorSteps = mSensorManager.getDefaultSensor(TYPE_STEP_COUNTER);
    mSensorHeart = mSensorManager.getDefaultSensor(TYPE_HEART_RATE);

    String sensor_error = getResources().getString(R.string.error_no_sensor);

    if (mSensorSteps == null) {
        mTextSensorSteps.setText(sensor_error);
    }
    if (mSensorHeart == null) {
        mTextSensorHeart.setText(sensor_error);
    }

There is a string resource that says "No Sensor" if it can't detect the sensor, and both the step and heart rate sensors have their own textviews, mTextSensorSteps and mTextSensorHeart.

In the onStart() and onStop() methods I've registered and unregistered the listeners for the sensors.

Here's what I have in the onSensorChanged() method (the onAccuracyChanged() method is empty):

@Override
public void onSensorChanged(SensorEvent event) {

    int sensorType = event.sensor.getType();
    float currentValue = event.values[0];

    switch (sensorType) {

        case Sensor.TYPE_STEP_COUNTER:
            // Event came from the steps sensor.
            mTextSensorSteps.setText(getResources().getString(R.string.steps_label, currentValue));
            break;

        case Sensor.TYPE_HEART_RATE:
            // Event came from the heart rate sensor.
            mTextSensorHeart.setText(getResources().getString(R.string.heart_label, currentValue));
            break;

        default:
            // do nothing
    }}

These are my string resources:

<string name="error_no_sensor">No Sensor</string>
<string name="heart_label">Heart: %1$.2f</string>
<string name="steps_label">Steps: %1$.2f</string>

The code works for every sensor except for the heart rate sensor. If it is a sensor not present in the watch, I get a "No Sensor" error. If I try it with the gyroscope it works. If I try it with the accelerometer it works. When I write TYPE_HEART_RATE, the textview just shows "Heart: %1$.2f".

I even tried using the number IDs of the sensors instead of the names, I know that TYPE_STEP_COUNTER is 19 and TYPE_HEART_RATE is 21. Same result: it works with steps, gyroscope (I think its ID is 4 or something like that), accelerometer, but not with the heart rate sensor. I even tried to modify the Switch case in the onSensorChanged() method to work like this:

if (sensorType == 21) {
        mTextSensorHeart.setText(getResources().getString(R.string.heart_label, currentValue));
    }

I removed the switch case and just used some ifs, same thing. The heart rate sensor is the only one not working.

I created another android project that shows a list of the sensor that are in the watch, and I noticed that the heart rate sensor is the only one that hasn't "vendor="Huawei SensorHub"". Here's what I found:

{sensor
name="ADPD153GGRI",
vendor="ANALOG DEVICES",
version=1,
type=21,
maxRange=255.0
...
}

while all the other sensors have "Huawei SensorHub" as vendor.

I can't find the sensor model on the Analog Devices site (this is the closest one I found, but it doesn't really help) and from here I really don't know what else to do. Maybe since it's a non original huawei sensor, the values[0] array of the sensor event doesn't work?

I really don't know what to do.


Solution

  • In API 23 the body sensor moved to a runtime permission. If you go into settings, apps and notifications, app info, select your app, then permissions. You should see the sensor permission disabled. You can enable it there for testing but should implement the runtime permission model outlined here. https://developer.android.com/training/articles/wear-permissions