I need to find all available wifi networks. Here are my code to get networks

    public IList<ScanResult> getWifiNetworks()
    {
        _wifi = (WifiManager) _context.GetSystemService(Context.WifiService);
        return  _wifi.ScanResults;
    }

But in _wifi.ScanResults I cannot find all wifi network. Android manifest

<uses-sdk android:minSdkVersion="24" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

1 Answers

0
Leon Lu - MSFT On

I wrote a demo about your needs.

This is running gif of my demo

enter image description here

First of all, you should add these permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

And request these permissions with link, I do not have enough time to acheve that, so i add it manually(in android setting).If you did not know how to achieve that, you can refer to following link, or I can help you to achieve it, if you need. https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

There is a broadcastreceiver.

    [BroadcastReceiver]
public class WifiScanReceiver : BroadcastReceiver
{
    private MainActivity mainActivity;

    public WifiScanReceiver() { }
    public WifiScanReceiver(MainActivity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    public override async void OnReceive(Context context, Intent intent)
    {
        var mainActivity = (MainActivity)context;

        var wifiManager = (WifiManager)mainActivity.GetSystemService(Context.WifiService);
        var message = string.Join("\r\n", wifiManager.ScanResults
            .Select(r => $"{r.Ssid} - {r.Bssid}"));

        mainActivity.DisplayText(message);
        await Task.Delay(TimeSpan.FromSeconds(1));
        wifiManager.StartScan();

      //  Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();

    }
}

MainActivity.cs

     [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    TextView txtScanResults;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        RegisterReceiver(new WifiScanReceiver(this), new IntentFilter(WifiManager.ScanResultsAvailableAction));
        ((Android.Net.Wifi.WifiManager)GetSystemService(WifiService)).StartScan();
        txtScanResults = FindViewById<TextView>(Resource.Id.textView1);
    }
    public void DisplayText(string text)
    {
        txtScanResults.Text = "Wifi networks: \r\n" + text;
    }
    protected override void OnResume()
    {
        base.OnResume();


    }
 }

activity_main.axml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:minWidth="25px"
   android:minHeight="25px">

 <TextView
  android:text="Scanning for networks..."
  android:textAppearance="?android:attr/textAppearanceMedium"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="10dp"
  android:id="@+id/textView1"
  android:typeface="monospace" />
 </LinearLayout>

There is my demo.

https://github.com/851265601/ReceiverWifiDemo