如何获取 Android Xamarin.Forms 附近的 WiFi 网络列表?

How to get list of nearby WiFi Networks in Android Xamarin.Forms?

这部分我搜索了很多方法,关于如何在获取附近的WiFi网络时获得自刷新应用程序。但没有什么对我有用。所以我只是试图检查这个资源> How to get available wifi networks and display them in a list in android

我试图将此代码“转换”为 Xamarin.Forms 代码,但存在一些问题。

  1. 如何在 App.xaml 中创建一个 TextView,然后用 MainActivity
  2. 中的数据填充它
  3. 我们可以在这里设置什么样的内容视图,应用才能正常工作。如何像 Resources->Layout->Get Main Activity 一样设置为 View(然后创建 Content 视图)。喜欢SetContentView(Resource.Layout.MainActivity);
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content;
using System.Collections.Generic;
using Android.Net.Wifi;
using System.Text;
using Java.Lang;
using Xamarin.Forms;
using StringBuilder = System.Text.StringBuilder;
using Menu = Android.Views.Menu;

namespace MyApp.Droid
{
    [Activity(Label = "MyApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        static TextView mainText;
        readonly Context context = Android.App.Application.Context;
        static WifiManager mainWifi;
        WifiReceiver receiverWifi;
        static List<ScanResult> wifiList;
        readonly System.Text.StringBuilder sb = new System.Text.StringBuilder();

        protected override void OnCreate(Bundle savedInstanceState)
        {
            /* Default MainActivity Data--------------------------*/
            base.OnCreate(savedInstanceState);
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            global::Xamarin.Forms.Forms.Init(context, savedInstanceState);

            /*-------------------------------------------------------*/

            SetContentView();//Here must be set the main content view ???????
            mainText = (TextView)FindViewById(Resource.Id.); // Here must get TextView Id  ???????
            mainWifi = (WifiManager)GetSystemService(Context.WifiService);

            if (mainWifi.IsWifiEnabled == false)
            {
                // If wifi disabled then enable it
                Toast.MakeText(context, "wifi is disabled..making it enabled", ToastLength.Long).Show();
                mainWifi.SetWifiEnabled(true);
            }
            receiverWifi = new WifiReceiver();
            RegisterReceiver(receiverWifi, new IntentFilter(WifiManager.ScanResultsAvailableAction));
            mainWifi.StartScan();
            char[] mine = "Starting Scan...".ToCharArray();
            int length = mine.Length;
            mainText.SetText(mine, 0, length);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            LoadApplication(new App());
        }


        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            menu.Add(0, 0, 0, "Refresh");
            return base.OnCreateOptionsMenu(menu);

        }

        public override bool OnContextItemSelected(IMenuItem item)
        {
            mainWifi.StartScan();
            char[] mine = "Starting Scan...".ToCharArray();
            int length = mine.Length;
            mainText.SetText(mine, 0, length);
            return base.OnContextItemSelected(item);

        }

        protected override void OnPause()
        {
            RegisterReceiver(receiverWifi, new IntentFilter(WifiManager.ScanResultsAvailableAction));
            base.OnPause();
        }

        protected override void OnResume()
        {
            RegisterReceiver(receiverWifi, new IntentFilter(WifiManager.ScanResultsAvailableAction));
            base.OnResume();
        }


        // Broadcast receiver class called its receive method
        // when number of wifi connections changed

        private class WifiReceiver : BroadcastReceiver
        {

            // This method call when number of wifi connections changed
            public override void OnReceive(Context context, Intent intent)
            {
                if (intent.Action.CompareTo(WifiManager.ScanResultsAvailableAction) == 0)
                {
                    var sb = new StringBuilder();
                    wifiList = (List<ScanResult>)mainWifi.ScanResults;
                    sb.Append("\n        Number Of Wifi connections :" + wifiList.Count + "\n\n");

                    for (int i = 0; i < wifiList.Count; i++)
                    {

                        sb.Append(new Integer(i + 1).ToString() + ". ");
                        sb.Append(wifiList[i].ToString());
                        sb.Append("\n\n");
                    }
                    char[] sb_t = (sb.ToString()).ToCharArray();
                    
                    mainText.SetText(sb_t,0,sb_t.Length);
                }
            }
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}


关于在 Xamarin.forms 中获取 Wifi 列表,我使用 DependencyService 做了一个示例。

正在 PCL 中创建界面:

 public interface IWifi
{
    Task<IEnumerable<string>> GetAvailableNetworksAsync();

}

在Android平台上实现接口

[assembly: Xamarin.Forms.Dependency(typeof(ListWifi.Droid.Wifi))]
namespace ListWifi.Droid
{
[Activity(Label = "ListWifi", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

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;
    }

 
    [BroadcastReceiver(Enabled = true, Exported = false)]
    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()
        {

        }
        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();
        }
    }
}


}

MainPage.xaml:

 <StackLayout>
    <ListView x:Name="wifilist">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding .}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <Button
        x:Name="btn1"
        Clicked="btn1_Clicked"
        Text="get wifi list" />
</StackLayout>

Mainpage.cs:

private async void btn1_Clicked(object sender, EventArgs e)
    {
        _wifiService = await DependencyService.Get<IWifi>().GetAvailableNetworksAsync();
        wifilist.ItemsSource = _wifiService;

      
    }

如果你想在某个时间刷新wifi列表,你可以使用Timer来做到这一点:

 private async void btn1_Clicked(object sender, EventArgs e)
    {
        //_wifiService = await DependencyService.Get<IWifi>().GetAvailableNetworksAsync();
        //wifilist.ItemsSource = _wifiService;

        Device.StartTimer(new TimeSpan(0, 0, 60), () =>
        {
            // do something every 60 seconds
            Device.BeginInvokeOnMainThread(async() =>
            {
                _wifiService = null;
                _wifiService = await DependencyService.Get<IWifi>().GetAvailableNetworksAsync();
                wifilist.ItemsSource = _wifiService;
            });
            return true; // runs again, or false to stop
        });

    }

注意:WiFi扫描,请允许。