locked
Populating list in dialog fragment with available wifi networks RRS feed

  • Question

  • User267437 posted

    Hi friends, I am trying to populate a list inside the DialogFragment. The list will contain the available wifi networks. I have tried below code(referring - https://forums.xamarin.com/discussion/27364/how-to-get-list-of-wifi-networks). But the fragment pops up with an empty list. I have rechecked the list reference and everything seems fine. Also I testing it on real device with wifi enabled. But no luck. Here is the code-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.Net.Wifi;
    namespace Informer
    {
        class WifiDialog : DialogFragment
        {
            public ListView mListView;
            WifiManager mWifiManager;
    
        IntentFilter intentFilter;
    
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = inflater.Inflate(Resource.Layout.dialog_wifi, container, false);
    
    
            //view
            mListView = (ListView)view.FindViewById<ListView>(Resource.Id.lvWifiList);
    
            //intent filter
            intentFilter = new IntentFilter(WifiManager.ScanResultsAvailableAction);
            MyWifiReceiver myWifiReceiver = new MyWifiReceiver(this);
    
            Context.RegisterReceiver(myWifiReceiver, intentFilter);
    
            return view;
        }
    
    
    
    
        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            //sets the title bar to invisible
            Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
            base.OnActivityCreated(savedInstanceState);
            //sets the animation
            Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation;
        }
        public void GetWifiNetworks()
        {
           //Get a handle to the wifi
            mWifiManager = (WifiManager)this.Context.GetSystemService(Context.WifiService);
    
            //Start a scan and register the Broadcast receiver to get the full list of Wifi networks
            MyWifiReceiver wifiReceiver = new MyWifiReceiver(this);
            this.Context.RegisterReceiver(wifiReceiver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
            mWifiManager.StartScan();
        }
    }
    
    internal class MyWifiReceiver : BroadcastReceiver
    {
        private WifiDialog wifiDialog;
        WifiManager wifiManager;
        public List<string> mWifiNetworks = new List<string>();
        public MyWifiReceiver(WifiDialog wifiDialog)
        {
            this.wifiDialog = wifiDialog;
        }
    
        public override void OnReceive(Context context, Intent intent)
        {
             wifiManager = (WifiManager)context.GetSystemService(Context.WifiService); ;
            IList<ScanResult> scanWifiNetworks = wifiManager.ScanResults;
            foreach (ScanResult item in scanWifiNetworks)
            {
                mWifiNetworks.Add(item.Ssid);
            }
            ArrayAdapter<string> adapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleListItem1, mWifiNetworks);
    
            wifiDialog.mListView.Adapter = adapter;
        }
    }
    

    }

    Please help and guide me what I am doing wrong.

    Also, I wanted to show to which Wifi network my device is connected. I'm not able to figure out how to find that.

    Sunday, January 15, 2017 7:09 PM

All replies

  • User368308 posted

    Hi ArpanMehetre.8020, Did you got above code to work. If yes then please tell the solution. I urgently need it for one of my application.

    Monday, May 7, 2018 7:04 PM
  • User368308 posted

    Got it. You need to call GetWifiNetworks() function in onCreate method

    Monday, May 7, 2018 7:16 PM