Home > Mobile >  Which sensor is responsible for compass functionality in Kotlin?
Which sensor is responsible for compass functionality in Kotlin?

Time:10-04

i have a compass page in my app but as far as i know there are some devices that doesn't support compass (no built-in sensor)

what i am trying to do here is navigate the user to another page in case there is no sensor in their device as follow:

val msensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
    if (msensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size > 0) {

    } else {

    }

but i am not sure which Sensor type is responsible for compass functionality

is the above code correct?

CodePudding user response:

For the compass functionality you have to check Sensor.TYPE_MAGNETIC_FIELD:

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
    // Success! There's a magnetometer.
} else {
    // Failure! No magnetometer.
}
  • Related