public bool CBCMGWiFiConnected() { bool wc = false; bool gc = false; String WiFiIP = null; if (WiFiStatus() == NetworkState.ConnectedWifi) { wc = true; PlayWiFiConnected(); Java.Util.IEnumeration networkInterfaces = Java.Net.NetworkInterface.NetworkInterfaces; while (networkInterfaces.HasMoreElements) { Java.Net.NetworkInterface netInterface = (Java.Net.NetworkInterface)networkInterfaces.NextElement(); Java.Util.IEnumeration a = netInterface.InetAddresses; while (a.HasMoreElements) { Java.Net.InetAddress b = (Java.Net.InetAddress)a.NextElement(); String IP = b.GetAddress()[0] + "." + b.GetAddress()[1] + "." + b.GetAddress()[2] + "." + b.GetAddress()[3]; if (IP.StartsWith(IPRange, StringComparison.Ordinal)) { WiFiIP = IP; gc = true; PlayCBCMGWiFiConnected(); } } } } LOGMSG("IP " + WiFiIP + " WiFi connected: " + wc + " CBCMG connected: " + gc + " " + sd.GetEnabled() + ":" + WiFiConnectDateTime + " " + WiFiDisconnectWarning + ":" + WiFiDisconnectDateTime + " " + sd.GetMovingDateTime()); return(gc); }
public static void ExecuteNetworkCommand(ExecuteNetworkDelegate command, IPAddress ipAddr, object networkDataObject) { #if Android if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop) { command(); return; } Android.Net.Network bindNetwork = null; NetworkData networkData = networkDataObject as NetworkData; Android.Net.ConnectivityManager connectivityManager = networkData?.ConnectivityManager; Java.Net.InetAddress inetAddr = Java.Net.InetAddress.GetByName(ipAddr.ToString()); // ReSharper disable once UseNullPropagation if (connectivityManager != null && inetAddr is Java.Net.Inet4Address inet4Addr) { System.Collections.Generic.List <Android.Net.Network> networkList = new System.Collections.Generic.List <Android.Net.Network>(); lock (networkData.LockObject) { networkList.AddRange(networkData.ActiveWifiNetworks); networkList.AddRange(networkData.ActiveEthernetNetworks); } foreach (Android.Net.Network network in networkList) { Android.Net.NetworkCapabilities networkCapabilities = connectivityManager.GetNetworkCapabilities(network); // HasTransport support started also with Lollipop if (networkCapabilities != null) { bool linkValid = false; Android.Net.LinkProperties linkProperties = connectivityManager.GetLinkProperties(network); if (linkProperties != null) { foreach (Android.Net.LinkAddress linkAddress in linkProperties.LinkAddresses) { if (linkAddress.Address is Java.Net.Inet4Address linkInet4Address) { Java.Net.NetworkInterface networkInterface = Java.Net.NetworkInterface.GetByInetAddress(linkInet4Address); if (networkInterface != null && networkInterface.IsUp && (linkInet4Address.IsSiteLocalAddress || linkInet4Address.IsLinkLocalAddress)) { foreach (Java.Net.InterfaceAddress interfaceAddress in networkInterface.InterfaceAddresses) { if (interfaceAddress.Address is Java.Net.Inet4Address) { byte[] linkAddrBytes = interfaceAddress.Address.GetAddress(); byte[] inet4AddrBytes = inet4Addr.GetAddress(); if (linkAddrBytes.Length == inet4AddrBytes.Length) { for (int bit = interfaceAddress.NetworkPrefixLength; bit < linkAddrBytes.Length * 8; bit++) { int index = bit >> 3; byte mask = (byte)(0x80 >> (bit & 0x07)); linkAddrBytes[index] |= mask; inet4AddrBytes[index] |= mask; } } if (linkAddrBytes.SequenceEqual(inet4AddrBytes)) { linkValid = true; break; } } } if (linkValid) { break; } } } } } if (linkValid) { bindNetwork = network; break; } } } } if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M) { #pragma warning disable 618 Android.Net.Network defaultNetwork = Android.Net.ConnectivityManager.ProcessDefaultNetwork; try { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(bindNetwork); command(); } finally { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(defaultNetwork); } #pragma warning restore 618 return; } Android.Net.Network boundNetwork = connectivityManager?.BoundNetworkForProcess; try { connectivityManager?.BindProcessToNetwork(bindNetwork); command(); } finally { connectivityManager?.BindProcessToNetwork(boundNetwork); } #else command(); #endif }