Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin Android Wifi on API29/Q/Andriod 10

$
0
0

I have the following code that works where I connect to my wifi IOT device (to do setting on the device). The thing is that I can get it working, that is i can access the REST Api of the wifi device and once i am done I call the disconnect and it does that and I am back to my regular wifi but now my app has lost internet access. How to I get that back ?

public void ConnectWifi()
{

    _wifiManager = Android.App.Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
        _networkCallback = new NetworkCallback(_wifiManager)
            {
                NetworkAvailable = network =>
                {
                    // we are connected!


                    WifiEventArgs args = new WifiEventArgs { IsConnected = true };
                    OnWifiConnected(args);

                },
                NetworkUnavailable = () =>
                {
                    WifiEventArgs args = new WifiEventArgs {  IsConnected = false };
                    OnWifiConnected(args);

                }
            };

            var wifiSpecifier = new WifiNetworkSpecifier.Builder()
               .SetSsid(ssid)
               .SetWpa2Passphrase(preSharedKey)
               .Build();

            var request = new NetworkRequest.Builder()
                .AddTransportType(TransportType.Wifi) // we want WiFi
                .RemoveCapability(NetCapability.Internet) // Internet not required
                .SetNetworkSpecifier(wifiSpecifier) // we want _our_ network
                .Build();

            _wifiManager.RequestNetwork(request, _networkCallback);

}

  private class NetworkCallback : ConnectivityManager.NetworkCallback
        {
            private ConnectivityManager _conn;
            public Action<Network> NetworkAvailable { get; set; }
            public Action NetworkUnavailable { get; set; }

            public NetworkCallback(ConnectivityManager connectivityManager) 
            {
                _conn = connectivityManager;
            }

            public override void OnAvailable(Network network)
            {
                base.OnAvailable(network);
        // Need this to bind to network otherwise it is connected to wifi 
        // but traffic is not routed to the wifi specified
                _conn.BindProcessToNetwork(network);
                NetworkAvailable?.Invoke(network);
            }

            public override void OnUnavailable()
            {
                base.OnUnavailable();

                NetworkUnavailable?.Invoke();
            }
        }

public void DisconnectWifi()
 {
   if (_networkCallback is null || _wifiManager is null)
        return;
   if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Q)
   {
      _wifiManager.UnregisterNetworkCallback(_networkCallback);
   }


}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>