I am trying to get a list of Wifi Networks. I am trying to isolate the code into its own class (see below.) The Intent Filter WifiReceiver never seems to be called. Once it is working, since I don't necessarily know when the WiFiNetworks List will be completely populated, is it proper programming, in Android, to set a flag letting me know once the WifiReciever class has completed populating the List? I want to populate a Spinner with the results from this List.
using Android.Content;
using Android.Net.Wifi;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace RingerLocation.Utility
{
public class Wifi
{
private Context context = null;
private static WifiManager wifi;
private WifiReceiver wifiReceiver;
public static List<string> WiFiNetworks;
public Wifi(Context ctx)
{
this.context = ctx;
}
public void GetWifiNetworks()
{
WiFiNetworks = new List<string>();
// Get a handle to the Wifi
wifi = (WifiManager)context.GetSystemService(Context.WifiService);
// Start a scan and register the Broadcast receiver to get the list of Wifi Networks
wifiReceiver = new WifiReceiver();
context.RegisterReceiver(wifiReceiver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
wifi.StartScan();
}
class WifiReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
IList<ScanResult> scanwifinetworks = wifi.ScanResults;
foreach(ScanResult wifinetwork in scanwifinetworks)
{
WiFiNetworks.Add(wifinetwork.Ssid);
}
}
}
}
}