gyroscope method

Stream<GyroscopeEvent> gyroscope({
  1. Duration samplingPeriod = SensorInterval.normalInterval,
})

Implementation

Stream<GyroscopeEvent> gyroscope({
  Duration samplingPeriod = SensorInterval.normalInterval,
}){
  if (_gyroscopeEventStreamController == null) {
    _gyroscopeEventStreamController = StreamController<GyroscopeEvent>();
    _featureDetected(
      () {
        final gyroscope = Gyroscope(
          SensorOptions(
            frequency: samplingPeriod.frequency,
          ),
        );

        gyroscope.start();

        gyroscope.onreading = (Event _) {
          _gyroscopeEventStreamController!.add(
            GyroscopeEvent(
              gyroscope.x,
              gyroscope.y,
              gyroscope.z,
            ),
          );
        }.toJS;

        gyroscope.onerror = (SensorErrorEvent e) {
          developer.log(
            'The gyroscope API is supported but something is wrong!',
            error: e.error.message,
          );
        }.toJS;
      },
      apiName: 'Gyroscope()',
      permissionName: 'gyroscope',
      onError: () {
        _gyroscopeEventStreamController!.add(GyroscopeEvent(0, 0, 0));
      },
    );
    _gyroscopeEventResultStream =
        _gyroscopeEventStreamController!.stream.asBroadcastStream();

    _gyroscopeEventStreamController!.onCancel = () {
      _gyroscopeEventStreamController!.close();
    };
  }

  return _gyroscopeEventResultStream;
}