public WifiHandler() { this.context = Android.App.Application.Context; _connectivityManager = Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager; _callback = new NetworkCallback { NetworkAvailable = network => { // we are connected! _callbackStatus = $"Requested network connected"; _connectivityManager.BindProcessToNetwork(network); Static.RuntimeSettings.IsWifiRequestingFinished = true; }, NetworkUnavailable = () => { _callbackStatus = $"Requested network unavailable"; Static.RuntimeSettings.IsWifiRequestingFinished = true; }, NetworkLost = network => { _callbackStatus = $"Requested network lost"; _connectivityManager.BindProcessToNetwork(null); _connectivityManager.UnregisterNetworkCallback(_callback); } }; }
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 void Disconnect() { this.msgPump.Disconnect(); if (this.connectCallback != null) { // Some note that sometimes you need to force un-bind ConnectivityManager cm = this.GetConnectivityManager(); cm.BindProcessToNetwork(null); cm.UnregisterNetworkCallback(this.connectCallback); this.connectCallback.Dispose(); this.connectCallback = null; } if (this.network != null) { this.network.Dispose(); this.network = null; // Bug in the connect callback returns immediately if socket // is still detected. Then a second time. Looks like time is // required to shut down but no synchronisation is provided Thread.Sleep(100); } }
protected override void OnCreate(Bundle savedInstanceState) { AppCenter.Start("b17f9c9d-e90c-488f-8c4b-92ef3e305c0d", typeof(Analytics), typeof(Distribute)); var versionInfo = Application.Context.ApplicationContext?.PackageManager?.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0); //var username = System.Security.Principal.WindowsIdentity.GetCurrent(); SentryXamarin.Init(o => { o.AddXamarinFormsIntegration(); o.Dsn = "https://[email protected]/5390642"; o.Release = $"TacControl@{versionInfo?.VersionName}:{versionInfo?.LongVersionCode}"; o.Environment = //username == "Dedmen-PC\\dedmen" ? "Dev" : "Alpha"; }); WifiManager wifiMgr = (WifiManager)ApplicationContext.GetSystemService(Context.WifiService); wifiLock = wifiMgr.CreateWifiLock(WifiMode.Full, "TacControl-udp"); wifiLock.SetReferenceCounted(true); wifiLock.Acquire(); castLock = wifiMgr.CreateMulticastLock("TacControl-udp"); castLock.SetReferenceCounted(true); castLock.Acquire(); ConnectivityManager conMgr = (ConnectivityManager)ApplicationContext.GetSystemService(Context.ConnectivityService); var stuff = conMgr.GetAllNetworks(); var wifiNet = stuff.FirstOrDefault(x => conMgr.GetNetworkInfo(x).Type == ConnectivityType.Wifi); if (wifiNet != null) { var res = conMgr.BindProcessToNetwork(wifiNet); var info = conMgr.GetNetworkInfo(wifiNet); var connInfo = wifiMgr.ConnectionInfo; } //Networking.ConnectionInfo TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); Android.Views.Window window = Window; window.AddFlags(WindowManagerFlags.KeepScreenOn); window.AddFlags(WindowManagerFlags.Fullscreen); LoadApplication(new App((action) => { TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); bool isMain = MainThread.IsMainThread; RunOnUiThread(() => { try { action(); tcs.SetResult(null); } catch (Exception ex) { tcs.SetException(ex); } }); return(tcs.Task); })); }
public override void OnAvailable(Network network) { //ConnectivityManager.SetProcessDefaultNetwork(network); //deprecated (but works even in Android P) connection_manager.BindProcessToNetwork(network); //this works in Android P }
public override void OnAvailable(Network network) { base.OnAvailable(network); connectivityManager.BindProcessToNetwork(network); NetworkAvailable?.Invoke(network); }