private static void EnsureDifferentNetworksNotActive(WifiManager wifiManager, WifiConfiguration network)
 {
     var differentActiveNetwork = wifiManager.ConfiguredNetworks
         .Where(x => x.Ssid != network.Ssid)
         .FirstOrDefault(x => x.StatusField == WifiStatus.Current);
     if (differentActiveNetwork != null)
         throw new InvalidOperationException($"Different network {differentActiveNetwork.Ssid} is active");
 }
Example #2
0
        /// <summary>
        /// Sets the WiFi AP configuration.
        /// </summary>
        /// <returns><c>true</c>, if wifi ap configuration was set, <c>false</c> otherwise.</returns>
        /// <param name="wifiConfig">Wifi config.</param>
        public bool SetWifiApConfiguration(WifiConfiguration wifiConfig)
        {
            try
            {
                foreach (var m in WifiManager.Class.GetMethods())
                {
                    Log.Info(m.Name, m.Accessible.ToString());
                }

                var method = WifiManager.Class.GetMethods().FirstOrDefault(a => a.Name == SetWifiApConfigurationMethod);

//              var b = new Java.Lang.Boolean(true);
//              var method = WifiManager.Class.GetMethod (SetWifiApConfigurationMethod, b.Class);

                return method != null && (bool)method.Invoke (WifiManager, wifiConfig);
            }
            catch (Exception ex)
            {
                Log.Error (this.ToString (), ex.Message);
                return false;
            }
        }
Example #3
0
        /// <summary>
        /// Sets the WiFi AP state.
        /// </summary>
        /// <returns><c>true</c>, if WiFi state was set, <c>false</c> otherwise.</returns>
        /// <param name="wifiConfig">Wifi config.</param>
        /// <param name="enabled">State enabled.</param>
        public bool SetWifiApEnabled(WifiConfiguration wifiConfig, Java.Lang.Boolean enabled) 
        {
            try
            {
                if (enabled == Java.Lang.Boolean.True)
                { // disable WiFi in any case
                    WifiManager.SetWifiEnabled(false);
                }

                var method = WifiManager.Class.GetMethod("setWifiApEnabled", wifiConfig.Class, enabled.Class);
                return (Boolean) method.Invoke(WifiManager, wifiConfig, enabled);
            } 
            catch (Exception e) 
            {
                Log.Error(this.ToString(), e.Message);
                return false;
            }
        }
        private static async Task<ConnectionResult> WaitUntilConnectedAsync(WifiManager wifiManager, WifiConfiguration network, TimeSpan checkInterval, TimeSpan timeout)
        {
            // TODO: Get notified when network is active instead of looping and sleeping

            if (timeout != TimeSpan.Zero)
            {
                if (checkInterval == TimeSpan.Zero)
                {
                    if (timeout.TotalMilliseconds > 0)
                        await Task.Delay(timeout);
                }
                else
                {
                    var startTime = DateTime.Now;
                    while (DateTime.Now - startTime < timeout)
                    {
                        if (IsCompletelyConnected(wifiManager, network))
                            return ConnectionResult.Connected;
                        await Task.Delay(TimeSpan.FromMilliseconds(100));
                    }
                }
            }
            if (IsCompletelyConnected(wifiManager, network))
                return ConnectionResult.Connected;
            return ConnectionResult.NotYetConnected;
        }
 private static void ActivateNetwork(WifiManager wifiManager, WifiConfiguration network)
 {
     if (!wifiManager.EnableNetwork(network.NetworkId, true))
         throw new InvalidOperationException($"Can't enable network {network.Ssid}");
 }
 private static bool IsCompletelyConnected(WifiManager wifiManager, WifiConfiguration network)
 {
     return wifiManager.WifiState == WifiState.Enabled
            && wifiManager.ConnectionInfo.SSID == network.Ssid
            && wifiManager.ConnectionInfo.SupplicantState == SupplicantState.Completed
            && wifiManager.DhcpInfo.IpAddress > 0;
 }