我需要找到所有可用的wifi网络。这是我获取网络的代码
public IList<ScanResult> getWifiNetworks()
{
_wifi = (WifiManager) _context.GetSystemService(Context.WifiService);
return _wifi.ScanResults;
}
但是在_wifi.ScanResults中,我找不到所有的wifi网络。 Android清单
<uses-sdk android:minSdkVersion="24" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
答案 0 :(得分:0)
我写了一个有关您需求的演示。
这是我的演示的gif
首先,您应该在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" />
并通过链接请求这些权限,我没有足够的时间来实现,所以我手动添加了它(在android设置中)。如果您不知道如何实现,可以参考以下链接,或者我如果需要,可以帮助您实现目标。 https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows
有一个广播接收器。
[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>
有我的演示。