1

My watch face needs access to heart rate sensor, and I would like to know whether there is a physical sensor present prior to asking for permission. According to documentation, I can do it in two ways:

private SensorManager mSensorManager;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_HEART_RATE);

or

boolean heartRateSensorEnabled = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE) != null;

However, if permission is not granted (which is by default), I get empty sensor list and also no default sensor. That makes me force ask for permission even if it is meaningless (Sony SW3, for example, doesn't have the sensor). Is there a way to differentiate between "permission denied" and "no sensor present"?

fomand
  • 81
  • 3

1 Answers1

0

Try looking into requestPermissions https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#requestPermissions(android.app.Activity,java.lang.String[], int) method. It has callback ActivityCompat.OnRequestPermissionsResultCallback which returns if the permission is denied or not.

To check if sensor is present or not, try to use the code in this SO thread where he listed the sensors and looked for TYPE_HEART_RATE.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Thanks for the feedback. It doesn't help though, solution in the link is exactly the same I described initially. It says "Ensure that your application has defined and granted the Body Sensors permission." before you can even check for sensors presence. That's what I'm trying to avoid. – fomand Jan 30 '17 at 14:03