startMagnetometer method

  1. @override
Future<Stream<SensorEvent>> startMagnetometer(
  1. double samplingRate
)
override

Starts streaming magnetometer data from the smartphone at the given samplingRate (Hz). Returns a stream of SensorEvents.

Implementation

@override
Future<Stream<SensorEvent>> startMagnetometer(double samplingRate) async {
  try {
    final samplingPeriod = Duration(
      microseconds: (1000000 / samplingRate).round(),
    );
    _magStream ??= magnetometerEventStream(
      samplingPeriod: samplingPeriod,
    ).map((e) => SensorEvent(e.x, e.y, e.z, e.timestamp));
    return _magStream!;
  } catch (e) {
    if (kDebugMode) {
      print('Error starting magnetometer: $e');
    }
    rethrow;
  }
}