Skip to content

Commit

Permalink
Android | Use geomagnetic rotation sensor as fallback
Browse files Browse the repository at this point in the history
If the TYPE_ROTATION_VECTOR is not available (e.g. on cheaper or older Android devices)
use TYPE_GEOMAGNETIC_ROTATION_VECTOR as fallback. That's increases the support for
more Android devices.
  • Loading branch information
Xennis authored and hemanthrajv committed Jul 27, 2020
1 parent c114bc1 commit 4f10681
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public final class FlutterCompassPlugin implements StreamHandler {

public static void registerWith(Registrar registrar) {
EventChannel channel = new EventChannel(registrar.messenger(), "hemanthraj/flutter_compass");
channel.setStreamHandler(new FlutterCompassPlugin(registrar.context(), Sensor.TYPE_ROTATION_VECTOR));
channel.setStreamHandler(new FlutterCompassPlugin(registrar.context(), Sensor.TYPE_ROTATION_VECTOR, Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR));
}


public void onListen(Object arguments, EventSink events) {
// Added check for the sensor, if null then the device does not have the TYPE_ROTATION_VECTOR sensor
// Added check for the sensor, if null then the device does not have the TYPE_ROTATION_VECTOR or TYPE_GEOMAGNETIC_ROTATION_VECTOR sensor
if(sensor != null) {
sensorEventListener = createSensorEventListener(events);
sensorManager.registerListener(sensorEventListener, this.sensor, SensorManager.SENSOR_DELAY_UI);
Expand Down Expand Up @@ -67,13 +67,18 @@ public void onSensorChanged(SensorEvent event) {
};
}

private FlutterCompassPlugin(Context context, int sensorType) {
private FlutterCompassPlugin(Context context, int sensorType, int fallbackSensorType) {
filter = 1.0F;

sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
orientation = new float[3];
rMat = new float[9];
sensor = this.sensorManager.getDefaultSensor(sensorType);
Sensor defaultSensor = this.sensorManager.getDefaultSensor(sensorType);
if (defaultSensor != null) {
sensor = defaultSensor;
} else {
sensor = this.sensorManager.getDefaultSensor(fallbackSensorType);
}
}

}

0 comments on commit 4f10681

Please sign in to comment.