jamesmontemagno / Xamarin.Plugins

Cross-platform Native API Access from Shared Code!
MIT License
1.3k stars 380 forks source link

Bug in Android ConnectivityManager property #310

Closed markdou812 closed 8 years ago

markdou812 commented 8 years ago

Please take a moment to fill out the following (change to preview to check or place x in []):

This is a

Which plugin does this impact:

Version Number of Plugin: 2.1.2 Device Tested On: OnePlus One, LG G4 Simulator Tested On:

Expected Behavior

Connect to wifi network in your Android Wifi settings. Run app and exercise the IsConnected property. Change to a different wifi network and repeat. The IsConnected status should be correct.

Actual Behavior

Sometimes when you switch hosts in your wifi settings while running an app, your ConnectivityManager property in ConnectivityImplementation.cs will return an instance that has already been disposed (Handle == IntPtr.Zero). Then you get a false negative when you call IsConnected since it goes into the Exception handler.

Changing the ConnectivityManager property to this fixes the bug: ConnectivityManager ConnectivityManager { get { if (_connectivityManager == null || _connectivityManager.Handle == IntPtr.Zero) _connectivityManager = (ConnectivityManager)(Application.Context.GetSystemService(Context.ConnectivityService));

            return _connectivityManager;
        }
    }

Steps to reproduce the Behavior

jamesmontemagno commented 8 years ago

I recommend sending down a pull request so I can attempt to repo.

Thanks!

jamesmontemagno commented 8 years ago

Moved to: https://github.com/jamesmontemagno/ConnectivityPlugin/issues/3