public Task <IEnumerable <WifiInfo> > GetAllWifiInfoAsync(
            CancellationToken token = new CancellationToken())
        {
            var tcs = new TaskCompletionSource <IEnumerable <WifiInfo> >();

            _semaphore.Wait(token);

            Task.Run(() =>
            {
                var wifiInfo = new List <WifiInfo>();

                var status = CaptiveNetwork.TryGetSupportedInterfaces(out string[] supportedInterfaces);
                if (status == StatusCode.OK && supportedInterfaces != null)
                {
                    foreach (var @interface in supportedInterfaces)
                    {
                        CaptiveNetwork.TryCopyCurrentNetworkInfo(@interface, out NSDictionary dict);

                        var bssid = dict[CaptiveNetwork.NetworkInfoKeyBSSID];
                        var ssid  = dict[CaptiveNetwork.NetworkInfoKeySSID];

                        wifiInfo.Add(new WifiInfo
                        {
                            Bssid = bssid.ToString(),
                            Ssid  = ssid.ToString()
                        });
                    }
                }

                tcs.TrySetResult(wifiInfo);
                _semaphore.Release();
            }, token);

            return(tcs.Task);
        }
Exemple #2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            lblHeader.Font   = UIFont.FromName("SourceSansPro-Light", 48f);
            lblSsid.Font     = UIFont.FromName("SourceSansPro-Light", 24f);
            lblMac.Font      = UIFont.FromName("SourceSansPro-Light", 24f);
            btnConnect.Font  = UIFont.FromName("SourceSansPro-Light", 24f);
            btnLogFiles.Font = UIFont.FromName("SourceSansPro-Light", 24f);
            btnHelp.Font     = UIFont.FromName("SourceSansPro-Light", 24f);
            lblFooter.Font   = UIFont.FromName("SourceSansPro-Light", 14f);

            String[] interfaces;
            CaptiveNetwork.TryGetSupportedInterfaces(out interfaces);
            if (interfaces != null && interfaces.Length >= 1)
            {
                NSDictionary dict;
                CaptiveNetwork.TryCopyCurrentNetworkInfo(interfaces[0], out dict);

                if (dict != null)
                {
                    var bssid = (NSString)dict[CaptiveNetwork.NetworkInfoKeyBSSID];
                    var ssid  = (NSString)dict[CaptiveNetwork.NetworkInfoKeySSID];
                    if (ssid.ToString().ToLower().Contains("rasp") || ssid.ToString().ToLower().Contains("rasp"))
                    {
                        lblSsid.Text = "SSID: " + ssid.ToString();
                        lblMac.Text  = "MAC: " + bssid.ToString();
                        btnConnect.SetTitle("Connect", UIControlState.Normal);
                        btnConnect.Enabled = true;
                    }
                }
            }
        }
Exemple #3
0
        public async Task ShowCurrentWifiInformation()
        {
            await Geolocation.GetLocationAsync();

            //return null pour des simulateurs
            var result = CaptiveNetwork.TryGetSupportedInterfaces(out var supportedInterfaces);

            if (result == StatusCode.OK && supportedInterfaces != null)
            {
                foreach (var r in supportedInterfaces)
                {
                    Console.WriteLine(r);

                    var netResult = CaptiveNetwork.TryCopyCurrentNetworkInfo(r, out var networkInfo);
                    if (netResult == StatusCode.OK && networkInfo != null)
                    {
                        foreach (var kvp in networkInfo)
                        {
                            Console.WriteLine($"{kvp.Key} {kvp.Value}");
                        }
                    }
                    else
                    {
                        Console.WriteLine(netResult);
                    }
                }
            }
            else
            {
                Console.WriteLine(result);
            }
        }
        public WifiInfo GetCurrentWifiInfo()
        {
            _semaphore.Wait();

            var wifiInfo = new WifiInfo();

            try
            {
                NSDictionary dict;
                var          status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);
                if (status == StatusCode.OK)
                {
                    var bssid = dict[CaptiveNetwork.NetworkInfoKeyBSSID];
                    var ssid  = dict[CaptiveNetwork.NetworkInfoKeySSID];

                    wifiInfo.Ssid  = ssid.ToString();
                    wifiInfo.Bssid = bssid.ToString();
                }
            }
            catch (EntryPointNotFoundException)
            {
                // running on sim...

                wifiInfo.Ssid  = "Simulator";
                wifiInfo.Bssid = "Simulator";
            }
            finally
            {
                _semaphore.Release();
            }

            return(wifiInfo);
        }
Exemple #5
0
        public void TryCopyCurrentNetworkInfo()
        {
            NSDictionary dict;
            StatusCode   status;

#if __TVOS__
            Assert.Throws <NotSupportedException> (() => { status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict); });
#else
            status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);

            // No network, ignore test
            if (status == StatusCode.NoKey)
            {
                return;
            }

            Assert.AreEqual(StatusCode.OK, status, "Status");
            // To get a non-null dictionary back, starting in iOS 12, we must (https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo)
            // * Use core location, and request (and get) authorization to use location information
            // * Add the 'com.apple.developer.networking.wifi-info' entitlement
            // We're not using custom entitlements when building for device, which means that we can't make this work at the moment.
            // So just assert that we get null if running on iOS 12+.
            if (TestRuntime.CheckXcodeVersion(10, 0))
            {
                Assert.IsNull(dict, "Dictionary");
            }
            else
            {
                Assert.IsNotNull(dict, "Dictionary");
            }
#endif
        }
        public void TryCopyCurrentNetworkInfo()
        {
            NSDictionary dict;
            StatusCode   status;

#if __TVOS__
            Assert.Throws <NotSupportedException> (() => { status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict); });
#else
            status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);

            // No network, ignore test
            if (status == StatusCode.NoKey)
            {
                return;
            }

            Assert.AreEqual(StatusCode.OK, status, "Status");
            // It's quite complex to figure out whether we should get a dictionary back or not.
            // References:
            // * https://github.com/xamarin/xamarin-macios/commit/24331f35dd67d19f3ed9aca7b8b21827ce0823c0
            // * https://github.com/xamarin/xamarin-macios/issues/11504
            // * https://github.com/xamarin/xamarin-macios/issues/12278
            // * https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo
            // So don't assert anything about the dictionary.
#endif
        }
Exemple #7
0
        public string GetNetworkName()
        {
            String[] interfaces;
            CaptiveNetwork.TryGetSupportedInterfaces(out interfaces);

            if (interfaces != null && interfaces.Length >= 1)
            {
                NSDictionary dict;
                CaptiveNetwork.TryCopyCurrentNetworkInfo(interfaces[0], out dict);

                if (dict != null)
                {
                    var bssid = (NSString)dict[CaptiveNetwork.NetworkInfoKeyBSSID];
                    var ssid  = (NSString)dict[CaptiveNetwork.NetworkInfoKeySSID];


                    if (!String.IsNullOrEmpty(ssid))
                    {
                        return(ssid);
                    }

                    if (!String.IsNullOrEmpty(bssid))
                    {
                        return(bssid);
                    }
                }
            }

            return("Not Connected");
        }
        public void TryCopyCurrentNetworkInfo_Null()
        {
            NSDictionary dict;

#if __TVOS__
            Assert.Throws <NotSupportedException> (() => CaptiveNetwork.TryCopyCurrentNetworkInfo(null, out dict));
#else
            Assert.Throws <ArgumentNullException> (() => CaptiveNetwork.TryCopyCurrentNetworkInfo(null, out dict));
#endif
        }
Exemple #9
0
        protected override Task <List <Datum> > PollAsync(CancellationToken cancellationToken)
        {
            NSDictionary networkInfo;

            string bssid = null;

            if (CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out networkInfo) != StatusCode.NoKey && networkInfo != null)
            {
                bssid = networkInfo[CaptiveNetwork.NetworkInfoKeyBSSID].ToString();
            }

            return(Task.FromResult(new Datum[] { new WlanDatum(DateTimeOffset.UtcNow, bssid) }.ToList()));
        }
        protected override IEnumerable <Datum> Poll(CancellationToken cancellationToken)
        {
            NSDictionary networkInfo;

            string bssid = null;

            if (CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out networkInfo) != StatusCode.NoKey && networkInfo != null)
            {
                bssid = networkInfo[CaptiveNetwork.NetworkInfoKeyBSSID].ToString();
            }

            return(new Datum[] { new WlanDatum(DateTimeOffset.UtcNow, bssid) });
        }
Exemple #11
0
        public string GetCurrentWifiSSID()
        {
            NSDictionary nsDictionary;

            CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out nsDictionary);

            if (nsDictionary?[CaptiveNetwork.NetworkInfoKeySSID] == null)
            {
                return(string.Empty);
            }

            return(nsDictionary[CaptiveNetwork.NetworkInfoKeySSID].ToString());
        }
        protected override IEnumerable <Datum> Poll(CancellationToken cancellationToken)
        {
            NSDictionary networkInfo;

            if (CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out networkInfo) == StatusCode.NoKey || networkInfo == null)
            {
                return new Datum[] { }
            }
            ;

            return(new Datum[] { new WlanDatum(DateTimeOffset.UtcNow, networkInfo[CaptiveNetwork.NetworkInfoKeyBSSID].ToString()) });
        }
    }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            Task.Run(() => {
                string[] supportedInterfaces;
                if (CaptiveNetwork.TryGetSupportedInterfaces(out supportedInterfaces) == StatusCode.OK && supportedInterfaces != null && supportedInterfaces.Length > 0)
                {
                    foreach (var si in supportedInterfaces)
                    {
                        NSDictionary networkInfo;
                        if (CaptiveNetwork.TryCopyCurrentNetworkInfo(si, out networkInfo) == StatusCode.OK)
                        {
                            if (networkInfo.Count > 0)
                            {
                                NSObject value;
                                if (networkInfo.TryGetValue(CaptiveNetwork.NetworkInfoKeySSID, out value))
                                {
                                    BeginInvokeOnMainThread(() => {
                                        lblSsid.Text = string.Format("SSID: {0}", value);
                                    });
                                }
                                if (networkInfo.TryGetValue(CaptiveNetwork.NetworkInfoKeyBSSID, out value))
                                {
                                    BeginInvokeOnMainThread(() => {
                                        lblBssid.Text = string.Format("BSSID: {0}", value);
                                    });
                                }
                                if (networkInfo.TryGetValue(CaptiveNetwork.NetworkInfoKeySSIDData, out value))
                                {
                                    BeginInvokeOnMainThread(() => {
                                        lblSsidData.Text = string.Format("SSID Data: {0}", value);
                                    });
                                }
                            }
                        }
                    }
                }
                else
                {
                    BeginInvokeOnMainThread(() => {
                        var alert = new UIAlertView("Error", "Cannot get network info!", null, "Ok");
                        alert.Show();
                    });
                }
            });
        }
Exemple #14
0
        protected override void Refresh()
        {
            var profiles = Connectivity.ConnectionProfiles;

            if (profiles.Contains(ConnectionProfile.Cellular))
            {
                Status = NetStatus.Wwan;
            }
            else if (profiles.Contains(ConnectionProfile.WiFi))
            {
                Status = NetStatus.Wlan;
                try
                {
                    CheckStatus(CaptiveNetwork.TryGetSupportedInterfaces(out string[] interfaces));
                    if (interfaces.Length == 0)
                    {
                        Ssid = string.Empty;
                    }
                    else
                    {
                        CheckStatus(CaptiveNetwork.TryCopyCurrentNetworkInfo(interfaces[0], out NSDictionary dict));
                        using (dict)
                        {
                            if (dict.TryGetValue(CaptiveNetwork.NetworkInfoKeySSID, out NSObject ssid))
                            {
                                Status = NetStatus.Wlan;
                                Ssid   = ssid.ToString();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    Ssid = string.Empty;
                }
            }
            else if (profiles.Contains(ConnectionProfile.Ethernet))
            {
                Status = NetStatus.Lan;
            }
            else
            {
                Status = NetStatus.Unknown;
            }
        }
Exemple #15
0
        public void TryCopyCurrentNetworkInfo()
        {
            if (Runtime.Arch == Arch.SIMULATOR)
            {
#if __IOS__
                if (TestRuntime.CheckSystemVersion(PlatformName.iOS, 6, 0))
                {
                    Assert.Inconclusive("This test throws EntryPointNotFoundException on the iOS 6 simulator with Lion");
                }
#endif
            }

            NSDictionary dict;
            var          status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);

            // No network, ignore test
            if (status == StatusCode.NoKey)
            {
                return;
            }

            Assert.AreEqual(StatusCode.OK, status, "Status");

#if __TVOS__
            Assert.IsNull(dict, "Dictionary");              // null?
            return;
#endif

            if ((dict == null) && (Runtime.Arch == Arch.DEVICE) && TestRuntime.CheckSystemVersion(PlatformName.iOS, 9, 0))
            {
                Assert.Ignore("null on iOS9 devices - CaptiveNetwork is being deprecated ?!?");
            }

            if (dict.Count == 3)
            {
                Assert.NotNull(dict [CaptiveNetwork.NetworkInfoKeyBSSID], "NetworkInfoKeyBSSID");
                Assert.NotNull(dict [CaptiveNetwork.NetworkInfoKeySSID], "NetworkInfoKeySSID");
                Assert.NotNull(dict [CaptiveNetwork.NetworkInfoKeySSIDData], "NetworkInfoKeySSIDData");
            }
            else
            {
                Assert.Fail("Unexpected dictionary result with {0} items", dict.Count);
            }
        }
Exemple #16
0
        Task <WifiNetwork> IWifiService.GetCurrentlyConnectedWifiNetwork()
        {
            WifiNetwork wifiNetwork = null;

            CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out var info);

            foreach (var pair in info)
            {
                if (pair.Key.ToString() == "SSID")
                {
                    wifiNetwork = new WifiNetwork
                    {
                        SSID = pair.Value.ToString(),
                    };
                }
            }

            return(Task.FromResult(wifiNetwork));
        }
Exemple #17
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            String[] interfaces;
            CaptiveNetwork.TryGetSupportedInterfaces(out interfaces);
            if (interfaces != null && interfaces.Length >= 1)
            {
                NSDictionary dict;
                CaptiveNetwork.TryCopyCurrentNetworkInfo(interfaces[0], out dict);

                if (dict != null)
                {
                    var bssid = (NSString)dict[CaptiveNetwork.NetworkInfoKeyBSSID];
                    var ssid  = (NSString)dict[CaptiveNetwork.NetworkInfoKeySSID];
                }
            }

            StartConnecting();
        }
        protected override void Refresh()
        {
            var profiles = Connectivity.ConnectionProfiles;

            if (profiles.Contains(ConnectionProfile.Cellular))
            {
                Status = NetStatus.Wwan;
            }
            else if (profiles.Contains(ConnectionProfile.WiFi))
            {
                try
                {
                    CheckStatus(CaptiveNetwork.TryGetSupportedInterfaces(out string[] interfaces));
                    foreach (string name in interfaces)
                    {
                        CheckStatus(CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out NSDictionary dict));
                        using (dict)
                        {
                            if (dict.TryGetValue(CaptiveNetwork.NetworkInfoKeySSID, out NSObject ssid))
                            {
                                Status = NetStatus.Wlan;
                                Ssid   = ssid.ToString();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    Status = NetStatus.Unknown;
                }
            }
            else if (profiles.Contains(ConnectionProfile.Ethernet))
            {
                Status = NetStatus.Lan;
            }
            else
            {
                Status = NetStatus.Unknown;
            }
        }
        public async Task <WifiNetwork> GetCurrentlyConnectedWifiNetwork()
        {
            WifiNetwork wifiNetwork = null;

            CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out var info);

            if (info != null)
            {
                foreach (var pair in info)
                {
                    if (pair.Key.ToString() == "SSID")
                    {
                        wifiNetwork = new WifiNetwork
                        {
                            SSID = pair.Value.ToString(),
                        };
                    }
                }
            }

            return(await Task.FromResult(wifiNetwork));
        }
        public string GetConnectedAp()
        {
            //foreach (var network in NEHotspotHelper.SupportedNetworkInterfaces)
            //{
            //    if (!string.IsNullOrEmpty(network.Ssid))
            //        return network.Ssid;
            //}
            //return string.Empty; //check for iOS 11+
            NSDictionary dict;
            var          status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);

            if (status == StatusCode.NoKey)
            {
                return(string.Empty);
            }

            var bssid    = dict[CaptiveNetwork.NetworkInfoKeyBSSID];
            var ssid     = dict[CaptiveNetwork.NetworkInfoKeySSID];
            var ssiddata = dict[CaptiveNetwork.NetworkInfoKeySSIDData];

            return(ssid.ToString());
        }
Exemple #21
0
        public void TryCopyCurrentNetworkInfo()
        {
            NSDictionary dict;
            StatusCode   status;

#if __TVOS__
            Assert.Throws <NotSupportedException> (() => { status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict); });
#else
            status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);

            // No network, ignore test
            if (status == StatusCode.NoKey)
            {
                return;
            }

            Assert.AreEqual(StatusCode.OK, status, "Status");
            // To get a non-null dictionary back, we must (https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo)
            // * Use core location, and request (and get) authorization to use location information
            // * Add the 'com.apple.developer.networking.wifi-info' entitlement
            // I tried this, and still got null back, so just assert that we get null.
            Assert.IsNull(dict, "Dictionary");
#endif
        }
Exemple #22
0
        public void TryCopyCurrentNetworkInfo_Null()
        {
            NSDictionary dict;

            CaptiveNetwork.TryCopyCurrentNetworkInfo(null, out dict);
        }
Exemple #23
0
        /// <summary>
        /// Updates the status.
        /// </summary>
        void UpdateStatus()
        {
            SystemLogger.Log(SystemLogger.Module.PLATFORM, "UpdateStatus");
            var type = NetworkType.Unknown;

            try{
                var remoteHostStatus = Reachability.RemoteHostStatus();
                var internetStatus   = Reachability.InternetConnectionStatus();
                var localWifiStatus  = Reachability.LocalWifiConnectionStatus();

                SystemLogger.Log(SystemLogger.Module.PLATFORM, "remoteHostStatus: " + remoteHostStatus);
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "internetStatus: " + internetStatus);
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "localWifiStatus: " + localWifiStatus);


                if (remoteHostStatus == NetworkStatus.ReachableViaCarrierDataNetwork)
                {
                    type = NetworkType.Carrier_3G;
                }
                if (remoteHostStatus == NetworkStatus.ReachableViaWiFiNetwork && internetStatus == NetworkStatus.ReachableViaWiFiNetwork)
                {
                    type = NetworkType.Wifi;

                    NSDictionary dict;

                    var status = CaptiveNetwork.TryCopyCurrentNetworkInfo("en0", out dict);

                    /*SystemLogger.Log (SystemLogger.Module.PLATFORM, "wifi status "+status);
                     * if (status != null && status != StatusCode.NoKey) {
                     *
                     *      var ssid = dict [CaptiveNetwork.NetworkInfoKeySSID];
                     *
                     *      SystemLogger.Log (SystemLogger.Module.PLATFORM, "SSID: " + ssid.ToString ());
                     * }*/
                }

                SystemLogger.Log(SystemLogger.Module.PLATFORM, "type: " + type);

                /*
                 * switch(type){
                 * case NetworkType.Wifi:
                 *      NetStatus = 6;
                 *      break;
                 * case NetworkType.Carrier_3G:
                 *      NetStatus = 5;
                 *      break;
                 * default:
                 *      NetStatus = 0;
                 *      break;
                 * }
                 *
                 */
                NetStatus = (int)type;

                UIApplication.SharedApplication.InvokeOnMainThread(delegate {
                    if (IPhoneServiceLocator.CurrentDelegate != null)
                    {
                        IPhoneServiceLocator.CurrentDelegate.EvaluateJavascript("if(Appverse&&Appverse.Net){ Appverse.Net.NetworkStatus =" + NetStatus + "; window.localStorage.setItem('_NetworkStatus',Appverse.Net.NetworkStatus);}");
                    }
                    if (IPhoneUtils.GetInstance() != null)
                    {
                        IPhoneUtils.GetInstance().FireUnityJavascriptEvent("Appverse.Net.onConnectivityChange", NetStatus);
                    }
                });
            }catch (Exception ex) { SystemLogger.Log(SystemLogger.Module.PLATFORM, "UpdateStatus Appverse.Net.onConnectivityChange EXCEPTION; " + ex.ToString()); }
        }