Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Populating list in dialog fragment with available wifi networks

ArpanMehetre.8020ArpanMehetre.8020 USMember
edited January 2017 in Xamarin.Android

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.

Answers

  • TIHORO123456TIHORO123456 Member

    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.

  • TIHORO123456TIHORO123456 Member

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

Sign In or Register to comment.