gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:18:59 26 4
gpt4 key购买 nike

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

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

  1. 如何在 App.xaml 中创建一个 TextView,然后用 MainActivity 中的数据填充它
  2. 我们可以在此处设置什么样的内容 View 才能使应用程序正常运行。如何像 Resources->Layout->Get Main Activity 一样设置为 View(然后创建 Content View )。像 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 中创建接口(interface):

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

}

在Android平台上实现接口(interface)

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


}

主页.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>

主页.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扫描,请允许。

enter image description here

关于android - 如何在 Android Xamarin.Forms 中获取附近的 WiFi 网络列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65062467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com