Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

ConnectivityChanged fired only once #143

Closed
vladimir-shirmanov opened this issue Feb 28, 2018 · 17 comments
Closed

ConnectivityChanged fired only once #143

vladimir-shirmanov opened this issue Feb 28, 2018 · 17 comments

Comments

@vladimir-shirmanov
Copy link

ConnectivityChanged fired only once on android device version 7.0

Bug Information

Version Number of Plugin: 3.1.1
Device Tested android version 7.0
Version of VS: 2017 15.5.7

Steps to reproduce the Behavior

just create a simple xamarin.forms solution with android, then write a code below. all permissions were setted up in manifest.

Code snippet

##App.xaml.cs

    public App()
    {
        var firstPage = new NavigationPage(new Page1());

        InitializeComponent();

        MainPage = firstPage;
    }

##Page 1.xaml.cs

    public Page1()
    {
        InitializeComponent();

        CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
        {
            Debug.WriteLine($"Connection is {args.IsConnected}");
        };
    }
@mmierzwa
Copy link

It works (or rather doesn't work...) exactly the same way on latest Oreo 8.1.0

@jamesmontemagno
Copy link
Owner

How are you testing it? Works perfect on about 10 of my different devices.

@alexshikov
Copy link

alexshikov commented Mar 17, 2018

I've faced a similar issue that might be related to the current one.

In short:

  • when I turn on/off WiFi - ConnectivtyChanged mostly arrive
  • when I turn on/off Cellular - ConnectivtyChanged mostly doesn't arrive

I've investigated the code deeper and it appeared, that ConnectivityTypeChange always arrives as expected but there are cases when IsConnected property is false.

The reason why this happens is ConnectivityChangeBroadcastReceiver.OnRecieve method fired as soon as connectivity type changed, not the state.
As far as I understand, there is a delay 500ms to let connection status to be determined. But seems this is not enough in some cases (especially Cellular).

To workaround the issue in my app, I've decided to subscribe for ConnectivityTypeChange in addition to ConnectivtyChanged with the next logic:

async void ConnectivityTypeChanged (object sender, ConnectivityTypeChangedEventArgs args)
{
	if (args.ConnectionTypes.Any() && !args.IsConnected)
	{
		var connected = await connectivity.AwaitConnected (TimeSpan.FromSeconds (10));
		// do your stuff here
	}
}

// And extension method
public static async Task<bool> AwaitConnected (this IConnectivity connectivity, TimeSpan timeout)
{
	var end = DateTime.Now + timeout;
	do
	{
		if (connectivity.IsConnected)
			return true;

		await Task.Delay (1000);

	} while (DateTime.Now < end);

	return false;
}

This is not a perfect solution, as the case when internet connection appears after 20 seconds or more is real (for example device connected to the WiFi but there is no internet for some time).

@mmierzwa
Copy link

@jamesmontemagno I test it on Pixel 2 with VS for Mac debugger. I see a breakpoint hit in event handler when I turn-on airplane mode but when I turn it off (and in subsequent toggles) the handler doesn't execute. It works well on iOS.

@gidgol
Copy link

gidgol commented Mar 20, 2018

I've similar issue. For some reason when i turn on my WIFI it doesn't trigger ConnectivityChanged event.
This problem exist on ios and android.If i found out that there is something wrong with my code i will write here.
When i turn on cellular it works normally.

@alexshikov
Copy link

@mmierzwa @gidgol could you please try my solution and give a feedback if it works for you?

@stephenhauck
Copy link

Anything on this?

If I run the app and put it in airplane mode and do some stuff then take it out of airplane mode the event fires and my local data goes upstream.

If I put it in airplane mode again and do some work and then take it back out of airplane mode it does not notify.

I am setting up the handler in my .NET Standard Library 2.0 in App.xaml.cs like so

CrossConnectivity.Current.ConnectivityChanged += Current_ConnectivityChanged;

Am I missing something ?

@jamesmontemagno
Copy link
Owner

Have you tried: 3.1.1.185-beta

Also if you want "all" changes then do connectivitytypechanged

@garie
Copy link

garie commented Apr 20, 2018

I'm also seeing this behavior.

Steps to reproduce:

  1. Turn off wifi on my mac
  2. Start my app in the simulator and open the page with the connectivity listeners
  3. Turn on wifi

Environment:

  • macOS High Sierra 10.13.4 (17E199)
  • iOS Simulator iPhone X 11.3
  • Visual Studio for Mac Community 7.4.3 (build 10)
  • Xam.Plugin.Connectivity 3.1.1

Code:

            CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
            {
                Logger.Debug($"Conn changed 1 {args.IsConnected}");
            };
            CrossConnectivity.Current.ConnectivityTypeChanged += (sender, args) =>
            {
                Logger.Debug($"Conn changed 2 {args.IsConnected}");
            };

Entire output, even after waiting several minutes:

Conn changed 2 False

3.1.1.185-beta appears to resolve the issue partially but toggling connectivity several times does see the same symptoms.

@garie
Copy link

garie commented May 22, 2018

Apologies, I just noticed this additional behavior:

On Android on 3.1.1.185-beta, ConnectivityChanged never fires and IsConnected is always true.

Environment:

  • macOS High Sierra 10.13.4 (17E199)
  • Android emulator Nexus 5X API 26
  • Visual Studio for Mac Community 7.4.3 (build 10)
  • Xam.Plugin.Connectivity 3.1.1.185-beta

@hauteur
Copy link

hauteur commented Jun 12, 2018

@jamesmontemagno I am seeing the same issue - On Android (testing on a Samsung S8), ConnectivityChanged fires the first time, but then does not fire again. I am attempting to trigger it using airplane mode on my physical device.

@sergey-vts
Copy link

Try commenting out [BroadcastReceiver(Enabled = true, Label = "Connectivity Plugin Broadcast Receiver")] in
ConnectivityChangeBroadcastReceiver.cs

developer.android.com
docs.microsoft.com

@jamesmontemagno
Copy link
Owner

Can you try Xamarin.Essentials out and let me know if you see it there: https://docs.microsoft.com/en-us/xamarin/essentials/connectivity?context=xamarin%2Fandroid&tabs=ios

This is a newer updated API and I will be re-working mine to use it soon.

@sergey-vts
Copy link

sergey-vts commented Jun 20, 2018

@jamesmontemagno
Tested in a vanilla Xamarin Android VS template

Xamarin.Essentials
API 23 - OK
API 24 - OK

Xam.Plugin.Connectivity (v4.0.0.186-beta, v3.1.1)
API 23 - OK
API 24 - FAIL (fires only once)

I rebuilt Xam.Plugin.Connectivity v3.1.1 without the aforementioned [BroadcastReceiver(Enabled = true, Label = "Connectivity Plugin Broadcast Receiver")] - now API 24 is OK.

Android wants you to only context-register broadcast receiver, statically registering it causes problems.

Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare the broadcast receiver in their manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

Source

jamesmontemagno added a commit that referenced this issue Jun 21, 2018
@jamesmontemagno
Copy link
Owner

Please try latest 3.1.1.189-beta

@sergey-vts
Copy link

Confirmed: 3.1.1.189-beta API 24 - OK

@jamesmontemagno
Copy link
Owner

yay!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants