// lib/services/gnss/phone_gps_connection.dart import 'dart:async'; import 'dart:typed_data'; import 'package:geolocator/geolocator.dart'; import 'gnss_connection.dart'; import 'gnss_device_service.dart'; class PhoneGpsConnection implements GnssConnection { @override GnssConnectionType get type => GnssConnectionType.phoneGps; final _stateController = StreamController.broadcast(); final _positionController = StreamController.broadcast(); StreamSubscription? _positionSub; int _retryCount = 0; static const _maxRetries = 5; Timer? _retryTimer; @override Stream get nmeaLines => const Stream.empty(); // Nincs NMEA @override Stream get positionStream => _positionController.stream; @override Stream get connectionState => _stateController.stream; @override Future connect(String address) async { _stateController.add(GnssConnectionState.connecting); bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { _stateController.add(GnssConnectionState.error); return; } LocationPermission permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { _stateController.add(GnssConnectionState.error); return; } } _stateController.add(GnssConnectionState.connected); _retryCount = 0; await _startPositionStream(); } Future _startPositionStream() async { await _positionSub?.cancel(); _positionSub = Geolocator.getPositionStream( locationSettings: const LocationSettings( accuracy: LocationAccuracy.high, distanceFilter: 0, // Folyamatos frissítés ), ).listen( (Position pos) { _retryCount = 0; _positionController.add(pos); }, onError: _handleStreamError, onDone: () => _handleStreamError(Exception('A GPS-stream váratlanul lezárult.')), ); } /// A Geolocator stream NEM öngyógyuló — lásd a PhoneGpsSource-nál már /// javított, ugyanilyen hibát. Ez a kapcsolat idáig SEMMILYEN hiba /// esetén nem próbálkozott újra, csendben, véglegesen elhallgatott. void _handleStreamError(Object e) { _retryCount++; if (_retryCount > _maxRetries) { _stateController.add(GnssConnectionState.error); return; } _retryTimer?.cancel(); _retryTimer = Timer(const Duration(seconds: 1), _startPositionStream); } @override Future disconnect() async { _retryTimer?.cancel(); await _positionSub?.cancel(); _stateController.add(GnssConnectionState.disconnected); } @override void sendData(Uint8List data) { // A telefon beépített GPS-e nem fogad RTCM adatokat } @override void dispose() { _retryTimer?.cancel(); _positionSub?.cancel(); _positionController.close(); _stateController.close(); } }