jamesmontemagno/Xamarin.Plugins

Bug in Android ConnectivityManager property

markdou812 opened this issue · 2 comments

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

This is a

  • Bug
  • Feature Request

Which plugin does this impact:

  • Battery
  • Connectivity
  • Contacts
  • DeviceInfo
  • ExternalMaps
  • Geolocator
  • Media
  • Permissions
  • Settings
  • Text To Speech
  • Vibrate
  • Other:

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

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

Thanks!