System.Net.Sockets.SocketException (0x80004005): Network is unreachable

0

I have commands that work properly on lower version Android, but since WiFi Manager changed the config command in Android 10 and above, my command does not respond and gives an error, please help me!

connectivityManager Android 10 :

WifiNetworkSuggestion guestUsers = new WifiNetworkSuggestion.Builder()
               .SetSsid("########")
               .SetWpa2Passphrase("#######")
               .Build();
            var suggestions = new[] { guestUsers };
            var status = wifiManager.AddNetworkSuggestions(suggestions);
var specifier = new WifiNetworkSpecifier.Builder()
                            .SetSsid("##########")
                            .SetWpa2Passphrase("###########")
                            .Build();
                    var request = new NetworkRequest.Builder()
                                        .AddTransportType(TransportType.Wifi) // we want WiFi
                                        .RemoveCapability(NetCapability.Internet) // Internet not required
                                        .SetNetworkSpecifier(specifier) // we want _our_ network
                                        .Build();
                    var connectivityManager = Android.App.Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
                    var callback = new NetworkCallback
                    {
                        NetworkAvailable = network =>
                        {
                            re = true;
                        }
                    };
                    connectivityManager.RequestNetwork(request, callback);

UdpClient Defult:

using (var client = new UdpClient())
                        {
                            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), sendPort);
                            client.Connect(ep);
                            client.Send(test, test.Length);
                        }

Error Catch:

{System.Net.Sockets.SocketException (0x80004005): Network is unreachable at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b0] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net.Sockets/Socket.cs:892 at System.Net.Sockets.UdpClient.Connect (System.Net.IPEndPoint endPoint) [0x00033] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/System/net/System/Net/Sockets/UDPClient.cs:477 at SmartCube.DeviceFolder.Connection.SocketUdp () [0x00071] in }

note :

Let me remind you again, this command works for Android 10 and below, and there is no problem, it does not work due to the Wi-Fi Manager update of Android 10 and above.

sockets
xamarin.forms
wifimanager
udpclient
asked on Stack Overflow Feb 6, 2021 by Amiroid

1 Answer

0

If your app targets Android 10 or higher, and it isn't a system app or a DPC, then the following methods don't return useful data:

  • The getConfiguredNetworks() method always returns an empty list.

  • Each network operation method that returns an integer value—addNetwork() and updateNetwork()—always returns -1.

  • Each network operation that returns a boolean value—removeNetwork(), reassociate(), enableNetwork(), disableNetwork(), reconnect(), and disconnect()—always returns false.

If your app needs to connect to Wi-Fi networks, use the following alternative methods:

  • To trigger an instant local connection to a Wi-Fi network, use WifiNetworkSpecifier in a standard NetworkRequest object.

  • To add Wi-Fi networks for consideration for providing internet access to the user, work with WifiNetworkSuggestion objects. You can add and remove networks that appear in the auto-connect network selection dialog by calling addNetworkSuggestions() and removeNetworkSuggestions(), respectively. These methods don't require any location permissions.


User contributions licensed under CC BY-SA 3.0