Scanning Wifi Network doesn't return values in Xamarin Code

女生的网名这么多〃 提交于 2019-12-11 09:04:26

问题


We've looked into implementing code from this link: https://spin.atomicobject.com/2018/02/15/connecting-wifi-xamarin-forms/ and modified it according to our setup. When we go through debugging the code, we're getting no returns.

public class Wifi : IWifi
{
    private Context context = null;

    public Wifi()
    {
        this.context = Android.App.Application.Context;
    }

    public async Task<IEnumerable<string>> GetAvailableNetworksAsync()
    {
        IEnumerable<string> availableNetworks = null;

        // Get a handle to the Wifi
        var wifiMgr = (WifiManager)context.GetSystemService(Context.WifiService);
        var wifiReceiver = new WifiReceiver(wifiMgr);

        await Task.Run(() =>
        {
            // Start a scan and register the Broadcast receiver to get the list of Wifi Networks
            context.RegisterReceiver(wifiReceiver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
            availableNetworks = wifiReceiver.Scan();
        });

        return availableNetworks;
    }

    class WifiReceiver : BroadcastReceiver
    {
        private WifiManager wifi;
        private List<string> wifiNetworks;
        private AutoResetEvent receiverARE;
        private Timer tmr;
        private const int TIMEOUT_MILLIS = 20000; // 20 seconds timeout

        public WifiReceiver(WifiManager wifi)
        {
            this.wifi = wifi;
            wifiNetworks = new List<string>();
            receiverARE = new AutoResetEvent(false);
        }

        public IEnumerable<string> Scan()
        {
            tmr = new Timer(Timeout, null, TIMEOUT_MILLIS, System.Threading.Timeout.Infinite);
            wifi.StartScan();
            receiverARE.WaitOne();
            return wifiNetworks;
        }

        public override void OnReceive(Context context, Intent intent)
        {
            IList<ScanResult> scanwifinetworks = wifi.ScanResults;
            foreach (ScanResult wifinetwork in scanwifinetworks)
            {
                wifiNetworks.Add(wifinetwork.Ssid);
            }

            receiverARE.Set();
        }

        private void Timeout(object sender)
        {
            // NOTE release scan, which we are using now, or we throw an error?
            receiverARE.Set();
        }
    }
}

We've also tried enabling WIFI_ACCESS_STATE, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, CHANGE_WIFI_STATE on the required permissions on the Android Manifest but had no effect.

We're looking for help to point out what's causing the bug. We've tried it on a oneplus android device and an asus phone.


回答1:


Figured it out. Might as well post the answer here as well for future reference.

I looked into Permissions for Android Xamarin. Although we added permissions via the manifest file, it seems that is not enough as we also need to do run-time permissions where the app asks the user to explicitly grant the permissions.

Also, based on the Android documentation for WIFI scanning, we must meet these criteria in order for it to be successful:

One thing to note though, on Android 8.0 and Android 8.1 it doesn't work if location services is turned off. The documentation states that this is only required on Android 9.0 and later but when I tested it on my Android 8.0 device it doesn't work unless location services is turned on. Also, I had to add Access_WIFI_State permission which is not also mentioned in the doc otherwise it will throw an exception. You may omit either of ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION and it works fine.



来源:https://stackoverflow.com/questions/55059395/scanning-wifi-network-doesnt-return-values-in-xamarin-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!