在Xamarina中是否可以获取附近所有可用wifi的信号强度信息?我正在开发一个收集和处理有关附近wifi网络信息的应用程序。我也想获得此信息而不必连接到给定的网络。
编辑: 这是代码,带有wifi信息的容器始终为空。
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Net.Wifi;
using Android.Support.V4.Content;
using Java.Util.Jar;
using Android.Support.V4.App;
using System.Collections.Generic;
namespace AppTut
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
WifiManager wifimanager;
WifiReceiver wifireceiver;
IList<ScanResult> resultList;
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);
wifimanager = (WifiManager)GetSystemService(WifiService);
wifireceiver = new WifiReceiver();
RegisterReceiver(wifireceiver, new Android.Content.IntentFilter(WifiManager.ScanResultsAvailableAction));
//if (ContextCompat.CheckSelfPermission(ApplicationContext, "Man") != PackageManager.permis)
if (ApplicationContext.CheckCallingOrSelfPermission(Android.Manifest.Permission.AccessFineLocation) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.AccessFineLocation }, 0);
}
else
{
scanWifiList();
}
}
private void scanWifiList()
{
wifimanager.StartScan();
resultList = wifimanager.ScanResults;
foreach(var spot in resultList)
{
var wifiLevel = WifiManager.CalculateSignalLevel(spot.Level, 100);
System.Diagnostics.Debug.WriteLine($"SSID: {spot.Ssid} - Signal: {wifiLevel}");
}
}
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);
}
}
}
using Android.Content;
namespace AppTut
{
class WifiReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
}
}
}
答案 0 :(得分:3)
在Xamarin.Android中,您可以使用以下代码获取Wifi连接及其强度(从0到100)。
using Android.Net.Wifi;
...
var wifiMgr = (WifiManager)GetSystemService(WifiService);
var wifiList = wifiMgr.ScanResults;
foreach (var item in wifiList)
{
var wifiLevel = WifiManager.CalculateSignalLevel(item.Level, 100);
Console.WriteLine($"Wifi SSID: {item.Ssid} - Strengh: {wifiLevel}");
}
您需要将以下两个权限添加到清单中:
ACCESS_FINE_LOCATION
和ACCESS_WIFI_STATE
并确保您请求ACCESS_FINE_LOCATION
的授权。
希望这会有所帮助。-