hemanthrajv / flutter_compass

MIT License
102 stars 188 forks source link

How to detect compass accuracy #97

Open abdimussa87 opened 1 year ago

abdimussa87 commented 1 year ago

I wanted to show a calibration required gif if the compass requires calibration. How can I detect that?

SantiFiebke commented 8 months ago

Same here

xihuny commented 5 months ago

Listen compass events for accuracy. If accuracy is null, compass needs to be calibrated.

StreamSubscription<CompassEvent>? _compassSubscription;
bool _isCompassCalibrated = true;

@override
void initState() {
  super.initState();
  _compassSubscription = FlutterCompass.events?.listen((event) {
    if (!mounted) return;
    setState(() {
      _currentHeading = event.heading;
      _isCompassCalibrated = event.accuracy != null;
    });
  });
}
if (!_isCompassCalibrated)
  Padding(
    padding: const EdgeInsets.all(8.0),
    child: Text(
      'Compass is not calibrated. Please wave your device in a figure-8 motion.',
      style: TextStyle(
        color: Colors.red,
      ),
      textAlign: TextAlign.center,
    ),
  ),