[Bug]: onConnectivityChanged listener not detecting when Mobile and Wi-Fi are disabled
Platform
Android 12
Plugin
connectivity_plus
Version
2.3.6+1
Flutter SDK
3.3.0
Steps to reproduce
- Call onConnectivityChanged listener and include code to print the event.
- Run the app and turn off Wi-Fi and Mobile from the device settings.
Code Sample
StreamSubscription listenForConnectivityChanges() {
return Connectivity().onConnectivityChanged.listen((event) {
print('Connectivity status = $event');
});
}
Logs
No backticks or errors detected.
Flutter Doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.3.0, on Microsoft Windows [Version 10.0.19044.1889], locale en-CA)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.10)
[√] Android Studio (version 2021.2)
[√] VS Code (version 1.70.2)
[√] Connected device (4 available)
[√] HTTP Host Availability
• No issues found!
I wonder if this is a limitation on the Android OS API itself.
It would be good to check if this is actually possible natively, and if so, port the solution back to the plugin
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days
Is it really appropriate to close an issue because it's not being commented on regularly? I haven't tried using this listener since but shouldn't this issue exist on some sort of to-do list? Closing it without a solution just feels like I wasted my time reporting it. :/
@AdamBridges I've just encountered the same behavior while testing only Wi-Fi. But after you enable Wi-Fi on the device, I've got an event from onConnectivityChanged with value ConnectivityResult.wifi. After disabling the Wi-Fi once more I did receive event with ConnectivityResult.none
Testing it on Android 10 with connectivity_plus - 2.3.6+1
Issue still occurs.. Package version : 3.0.5 Android version : 11 Flutter version : 3.7.7
I am also having the same issue. When you test on the iOS simulator, start with WiFi disabled, then enable WiFi and it's all detecting find. Turn off WiFi and onConnectivityChanged shows ConnectivityResult.wifi instead of ConnectivityResult.none
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days
Is it really appropriate to close an issue because it's not being commented on regularly? I haven't tried using this listener since but shouldn't this issue exist on some sort of to-do list? Closing it without a solution just feels like I wasted my time reporting it. :/
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days
.
Hello, Can we please figure something out here? This sucks, even if i have wifi but there is no connectivity it does not trigger anything. It should do a ping in the background.
There was a recent PR on this: https://github.com/fluttercommunity/plus_plugins/pull/2673
If anyone can give it a try and confirm it fixes this issue, we can close this one.
After some research and thinking your package is not responsible and should not be. Just add a timer every 15s and do a ping check. That is the for sure way to handle all use cases. Ill submit a code sample soon.
Ok as promised, this is my solution and it works like a charm:
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'dart:io';
import 'package:dart_ping/dart_ping.dart';
class App extends StatefulWidget {
const App({
super.key,
});
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
late StreamSubscription _connectionChangeStream;
Timer? _connectivityCheckTimer;
@override
void initState() {
super.initState();
const interval = Duration(seconds: 15);
_connectivityCheckTimer = Timer.periodic(interval, (Timer t) => _checkConnection() );
_connectionChangeStream = Connectivity().onConnectivityChanged.listen((ConnectivityResult connectivityResult) {
_processConnectivityResults(connectivityResult);
});
}
@override
void dispose() {
_connectionChangeStream.cancel();
_connectivityCheckTimer?.cancel();
super.dispose();
}
void _processConnectivityResults(ConnectivityResult connectivityResult) async {
if (connectivityResult == ConnectivityResult.mobile) {
print('there is mobile connectivity');
}
if (connectivityResult == ConnectivityResult.wifi) {
print('there is wifi connectivity');
}
if (connectivityResult == ConnectivityResult.vpn) {
print('there is vpn connectivity');
}
if (connectivityResult == ConnectivityResult.ethernet) {
print('there is ethernet connectivity');
}
if (connectivityResult == ConnectivityResult.none) {
print('there is NO connectivity');
_connectionChanged(false);
} else {
try {
PingData result = await Ping(connectivityEndpoint, count: 1).stream.first;
print('lookup results:');
print(result);
if (result.response == null) {
_connectionChanged(false);
} else {
_connectionChanged(true);
}
} on SocketException catch(_) {
_connectionChanged(false);
}
}
}
void _connectionChanged(bool hasConnection) {
/* trace */ if (showFunctionPrintStatements) { print('_connectionChanged function inside $_fileName'); }
if (hasConnection == true) {
//update the state management
} else {
//update the state management
}
}
void _checkConnection() async {
/* trace */ if (showFunctionPrintStatements) { print('_checkConnection function inside $_fileName'); }
ConnectivityResult connectivityResult = await Connectivity().checkConnectivity();
_processConnectivityResults(connectivityResult);
}
@override
Widget build(BuildContext context) {
return Observer(
builder: (_) => _buildWithTheme(context, _appStore!.appState, _appStore!.themeData),
);
}
}
I have issue with Simulator iphone15. Step 1: Check internet connectivity with Enable wifi. => result: ConnectivityResult.wifi Step 2: Check internet connectivity with Disable wifi. => result: ConnectivityResult.none. Step 3: Check internet connectivity with Enable wifi. => result: ConnectivityResult.none is forever.
The issues with the simulator are known, that is documented in the readme file.
Closing as the infornation in the original ticket are outdated.