Baseflow / Baseflow/flutter-geolocator

[Bug]: Geolocation services kill Webscocket connections on Android

Open
#1,616 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.3k
Forks
803
Avg merge
8h 16m
Merged PRs (30d)
1

Description

### Please check the following before submitting a new issue.

- [X] I have searched the [existing issues](https://github.com/baseflow/flutter-geolocator/issues).
- [X] I have carefully [read the documentation](https://github.com/Baseflow/flutter-geolocator/blob/main/geolocator/README.md) and verified I have added the required platform specific configuration.

### Please select affected platform(s)

- [X] Android
- [ ] iOS
- [ ] Linux
- [ ] macOS
- [ ] Web
- [ ] Windows

### Steps to reproduce

1. Prepare/find available server with web-socket support
2. Open the example app on Android (the issue persist only on Android)
3. The websocket channel will be connected automatically
4. Everything will work as expected
5. Click "Enable geolocator"
6. Pings (and actual data) from websockets will throttle more and more until completelly muted

### Expected results

I expect ping messages each 3 seconds from websocket channel.
```
[FE][socket] data: {"type":"welcome"}
[FE][socket] data: {"type":"ping","message":1733363850}
[FE][timer] second passed 1
[FE][timer] second passed 2
[FE][timer] second passed 3
[FE][socket] data: {"type":"ping","message":1733363853}
[FE][timer] second passed 4
[FE][timer] second passed 5
[FE][timer] second passed 6
[FE][socket] data: {"type":"ping","message":1733363853}
...
```

### Actual results

As soon as I enable Geolocator (or Google Maps) with accuracy set to "best," it breaks the connection more and more until it is finally completely muted.
```
[FE][socket] data: {"type":"welcome"}
[FE][timer] second passed 1
[FE][socket] data: {"type":"ping","message":1733363850}
[FE][timer] second passed 2
[FE][timer] second passed 3
[FE][timer] second passed 4
[FE][socket] data: {"type":"ping","message":1733363853}
[FE][timer] second passed 5
[FE][timer] second passed 6
[FE][timer] second passed 7
[FE][socket] data: {"type":"ping","message":1733363856}
[FE][timer] second passed 8
[FE][timer] second passed 9
[FE][timer] second passed 10
[FE][socket] data: {"type":"ping","message":1733363859}
[FE][timer] second passed 11
[FE][timer] second passed 12
[FE][timer] second passed 13
[FE][socket] data: {"type":"ping","message":1733363862}
[FE][timer] second passed 14
[FE][timer] second passed 15
[FE][timer] second passed 16
[FE][socket] data: {"type":"ping","message":1733363865}
[FE][timer] second passed 17
[FE][timer] second passed 18
[FE][timer] second passed 19
[FE][socket] data: {"type":"ping","message":1733363868}
[FE][timer] second passed 20
[FE][timer] second passed 21
[FE][timer] second passed 22
[FE][socket] data: {"type":"ping","message":1733363871}
[FE][timer] second passed 23

E/FlutterGeolocator( 9327): Geolocator position updates started
[FE][timer] second passed 24
[FE][timer] second passed 25
[FE][socket] data: {"type":"ping","message":1733363874}
[FE][timer] second passed 26
[FE][timer] second passed 27
[FE][timer] second passed 28
[FE][geolocator] data: Latitude: 43.658825, Longitude: -79.72306
[FE][socket] data: {"type":"ping","message":1733363877}
[FE][timer] second passed 29
[FE][timer] second passed 30
[FE][timer] second passed 31
[FE][timer] second passed 32
[FE][timer] second passed 33
[FE][geolocator] data: Latitude: 43.658821, Longitude: -79.7230634
[FE][socket] data: {"type":"ping","message":1733363880}
[FE][timer] second passed 34
[FE][timer] second passed 35
[FE][timer] second passed 36
[FE][timer] second passed 37
[FE][socket] data: {"type":"ping","message":1733363883}
[FE][socket] data: {"type":"ping","message":1733363886}
[FE][timer] second passed 38
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 39
[FE][timer] second passed 40
[FE][timer] second passed 41
[FE][timer] second passed 42
[FE][timer] second passed 43
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 44
[FE][timer] second passed 45
[FE][timer] second passed 46
[FE][timer] second passed 47
[FE][timer] second passed 48
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 49
[FE][timer] second passed 50
[FE][timer] second passed 51
[FE][timer] second passed 52
[FE][timer] second passed 53
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 54
[FE][timer] second passed 55
[FE][timer] second passed 56
[FE][timer] second passed 57
```

### Code sample

Code sample

```dart
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
runApp(TestSocketsApp());
}

class TestSocketsApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'WebSocket App',
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
@override
void initState() {
super.initState();
subscribeToFEwebsocket(context);
}

Future subscribeToFEwebsocket(BuildContext context) async {
try {
final headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'en-CA,en-US;q=0.7,en;q=0.3',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive, Upgrade',
'Host': '*',
'Origin': 'https://*',
'Pragma': 'no-cache',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'websocket',
'Sec-Fetch-Site': 'same-site',
'Sec-WebSocket-Extensions': 'permessage-deflate',
'Sec-WebSocket-Key': '*',
'Sec-WebSocket-Protocol': 'actioncable-v1-json, actioncable-unsupported',
'Sec-WebSocket-Version': '13',
};
const sid = '*'; // Placeholder for sid
const authToken = '*'; // Placeholder for authToken
const socketUrl =
'wss://*.org/cable?sid=$sid&auth_token=$authToken';

final socket = await WebSocket.connect(
socketUrl,
headers: headers,
);
Timer.periodic(const Duration(seconds: 1), (timer) {
print('[FE][timer] second passed ${timer.tick}');
});
socket.listen(
(data) {
print('[FE][socket] data: $data');
},
onError: (error) {
print('[FE][socket] error: $error');
},
onDone: () {
print('[FE][socket] connection closed');
},
);
} catch (e) {
print('[FE][socket] error: $e');
}
}

StreamSubscription? listener;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
FilledButton(
onPressed: () async {
await enablePermissions();
listener?.cancel();
listener = Geolocator.getPositionStream(
locationSettings: const LocationSettings(
// if I use LocationAccuracy.best, the ping messages will throttle
accuracy: LocationAccuracy.best,
),
).listen((data) {
print('[FE][geolocator] data: $data');
});
},
child: const Text('Enable geolocator (ping messages will throttle)'),
),
],
),
),
);
}

Future enablePermissions() async {
final permission = await Geolocator.checkPermission();
if (permission != LocationPermission.whileInUse) {
await Geolocator.requestPermission();
}
}
}

```

### Screenshots or video

Screenshots or video demonstration

Not needed

### Version

All versions

### Flutter Doctor output

Doctor output

```console
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.5, on macOS 15.1.1 24B91 darwin-arm64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.95.3)
[✓] Connected device (5 available)
! Error: Browsing on the local area network for Alex’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!
```

Contributor guide

Open the contributing guide

Research direction

Start with the sample's WebSocket.connect and Geolocator.getPositionStream calls on Android, reproducing the issue with LocationAccuracy.best after enabling permissions. Compare socket timing before and after location updates; done means websocket ping messages continue arriving at the expected three-second interval while geolocation remains active.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, dart, flutter
Domain
mobile, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.