plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Bug]: onConnectivityChanged listener not detecting when Mobile and Wi-Fi are disabled

Open AdamBridges opened this issue 3 years ago • 10 comments

Platform

Android 12

Plugin

connectivity_plus

Version

2.3.6+1

Flutter SDK

3.3.0

Steps to reproduce

  1. Call onConnectivityChanged listener and include code to print the event.
  2. 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!

AdamBridges avatar Aug 31 '22 23:08 AdamBridges

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

miquelbeltran avatar Sep 08 '22 05:09 miquelbeltran

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

github-actions[bot] avatar Dec 28 '22 00:12 github-actions[bot]

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 avatar Jan 04 '23 22:01 AdamBridges

@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

Ishchik avatar Feb 17 '23 12:02 Ishchik

Issue still occurs.. Package version : 3.0.5 Android version : 11 Flutter version : 3.7.7

fmdogan avatar Apr 27 '23 14:04 fmdogan

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

Sun3 avatar Aug 14 '23 19:08 Sun3

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

github-actions[bot] avatar Nov 13 '23 00:11 github-actions[bot]

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 avatar Nov 14 '23 18:11 AdamBridges

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

github-actions[bot] avatar Feb 13 '24 00:02 github-actions[bot]

.

AdamBridges avatar Feb 13 '24 20:02 AdamBridges

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.

nicolasvahidzein avatar Mar 11 '24 18:03 nicolasvahidzein

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.

miquelbeltran avatar Mar 12 '24 07:03 miquelbeltran

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.

nicolasvahidzein avatar Mar 12 '24 10:03 nicolasvahidzein

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),
		);
		
	}
	
}

nicolasvahidzein avatar Mar 12 '24 21:03 nicolasvahidzein

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.

gj-thaoht avatar Apr 10 '24 04:04 gj-thaoht

The issues with the simulator are known, that is documented in the readme file.

Closing as the infornation in the original ticket are outdated.

miquelbeltran avatar Apr 10 '24 05:04 miquelbeltran