android.net.wifi.scan_results
时间: 2023-07-28 17:03:22 浏览: 319
android.net.wifi.scan_results是Android操作系统中的一个类,用于获取Wi-Fi扫描结果列表。
在Android设备中,我们可以通过使用Wi-Fi功能来连接无线网络。而在连接无线网络之前,我们通常需要先扫描周围可用的Wi-Fi网络。这时,就可以使用android.net.wifi.scan_results类来获取扫描结果。
android.net.wifi.scan_results类提供了一组方法和属性,用于获取和处理Wi-Fi扫描结果。通过调用相关的方法,我们可以获取到扫描到的Wi-Fi网络的详细信息,包括网络的名称、信号强度、安全性、频率等等。
使用android.net.wifi.scan_results类时,我们需要先获取一个扫描结果列表的实例。一般情况下,我们可以通过WifiManager类中的getScanResults()方法来获取该实例。得到实例后,我们可以使用get()方法来获取某个位置的扫描结果,并通过相关的方法来获取相应的信息。
通过android.net.wifi.scan_results类,我们可以很方便地获取到周围的Wi-Fi网络的信息,并据此来进行一系列的操作,比如显示可用网络列表给用户选择、自动连接到最强信号的网络等。
总之,android.net.wifi.scan_results类在Android中起到了非常重要的作用,为我们提供了便捷的方式来获取Wi-Fi网络的扫描结果,使得我们能够更好地管理和利用无线网络。
相关问题
12-21 15:41:33.636 I/WifiService( 1081): startScan uid=10098 12-21 15:41:33.640 I/WifiScanRequestProxy( 1081): Scan request from com.autonavi.amapauto throttled 12-21 15:41:33.640 I/ActivityManager( 1081): caller:android, send broadcast:android.net.wifi.SCAN_RESULTS 12-21 15:41:33.642 E/WifiService( 1081): Failed to start scan 12-21 15:41:33.775 I/ActivityManager( 1081): caller:android, send broadcast:android.intent.action.SIG_STR 12-21 15:41:33.971 I/hostapd ( 3893): ap0: STA 28:c7:09:63:e0:a3 IEEE 802.11: disassociated 12-21 15:41:33.971 I/wificond( 631): Station 28:c7:09:63:e0:a3 disassociated from hotspot 12-21 15:41:33.972 I/hostapd ( 3893): ap0: STA 28:c7:09:63:e0:a3 IEEE 802.11: disassociated 12-21 15:41:33.972 I/hostapd ( 3893): ap0: AP-STA-DISCONNECTED 28:c7:09:63:e0:a3 12-21 15:41:33.973 D/SoftApManager( 1081): Setting num stations on CMD_NUM_ASSOCIATED_STATIONS_CHANGED 12-21 15:41:33.973 D/SoftApManager( 1081): Number of associated stations changed: 0 12-21 15:41:33.975 D/AlarmManager( 1081): [set] switch ELAPSED_REALTIME_WAKEUP -> ELAPSED_REALTIME,callingPackage = android,triggerAtTime = 721599,listenerTag = SoftApManager Soft AP Send Message Timeout 12-21 15:41:33.976 D/SoftApManager( 1081): Timeout message scheduled这段log表示什么
这段log的意思是在手机上的Wifi服务试图进行扫描时被拒绝,因为它被限流了。在扫描失败后,系统发送了一个广播,通知相关应用有关Wifi扫描结果的变化。接着,设备上的一个客户端(STA)28:c7:09:63:e0:a3从热点(hotspot)断开连接,并在此之后,SoftApManager处理了与连接的客户端数量有关的变化,并安排了一条消息在将来的某个时间进行超时处理。
写一个Android wifi开发程序,适配android11,包括wifi打开、关闭、扫描、连接、断开
以下是一份简单的Android wifi开发程序,适配android 11,包括wifi打开、关闭、扫描、连接和断开。
Java代码:
```java
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@RequiresApi(30)
public class MainActivity extends AppCompatActivity {
private final int REQUEST_CODE_LOCATION_PERMISSION = 100;
private final int REQUEST_CODE_WIFI_PERMISSION = 101;
private WifiManager wifiManager;
private boolean isWifiEnabled;
private WifiScanReceiver wifiScanReceiver;
private List<WifiConfiguration> wifiConfigurations;
private List<WifiInfo> wifiInfos;
private List<ScanResult> scanResults;
private boolean isConnected;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
isWifiEnabled = wifiManager.isWifiEnabled();
wifiScanReceiver = new WifiScanReceiver();
wifiConfigurations = wifiManager.getConfiguredNetworks();
wifiInfos = new ArrayList<>();
scanResults = new ArrayList<>();
isConnected = false;
handler = new Handler(Looper.getMainLooper());
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION_PERMISSION);
} else {
enableWifi();
}
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(wifiScanReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(wifiScanReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
disconnect();
disableWifi();
}
private void enableWifi() {
if (!isWifiEnabled) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CHANGE_WIFI_STATE}, REQUEST_CODE_WIFI_PERMISSION);
} else {
wifiManager.setWifiEnabled(true);
isWifiEnabled = true;
Toast.makeText(this, "开启Wifi", Toast.LENGTH_SHORT).show();
}
}
}
private void disableWifi() {
if (isWifiEnabled) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CHANGE_WIFI_STATE}, REQUEST_CODE_WIFI_PERMISSION);
} else {
wifiManager.setWifiEnabled(false);
isWifiEnabled = false;
Toast.makeText(this, "关闭Wifi", Toast.LENGTH_SHORT).show();
}
}
}
private void scanWifi() {
if (isWifiEnabled) {
wifiManager.startScan();
Toast.makeText(this, "开始扫描Wifi", Toast.LENGTH_SHORT).show();
}
}
private void connect(WifiConfiguration wifiConfiguration) {
if (isWifiEnabled) {
int networkId = wifiManager.addNetwork(wifiConfiguration);
if (networkId != -1) {
wifiManager.saveConfiguration();
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
isConnected = true;
Toast.makeText(this, "已连接Wifi", Toast.LENGTH_SHORT).show();
} else {
isConnected = false;
Toast.makeText(this, "连接Wifi失败", Toast.LENGTH_SHORT).show();
}
}
}
private void disconnect() {
if (isWifiEnabled && isConnected) {
wifiManager.disconnect();
isConnected = false;
Toast.makeText(this, "已断开Wifi连接", Toast.LENGTH_SHORT).show();
}
}
private void requestLocationPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION_PERMISSION);
}
private void requestWifiPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CHANGE_WIFI_STATE}, REQUEST_CODE_WIFI_PERMISSION);
}
// 单独提取出请求权限的onRequestPermissionsResult方法,后续修改方便
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_LOCATION_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableWifi();
} else {
Toast.makeText(this, "请求位置权限失败,无法开启Wifi", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == REQUEST_CODE_WIFI_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (isWifiEnabled) {
disableWifi();
} else {
enableWifi();
}
} else {
Toast.makeText(this, "请求Wifi权限失败", Toast.LENGTH_SHORT).show();
}
}
}
// Broadcast Receiver
private class WifiScanReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
scanResults = wifiManager.getScanResults();
sortScanResultsByLevel();
Toast.makeText(context, "扫描结束,共找到" + scanResults.size() + "个Wifi", Toast.LENGTH_SHORT).show();
}
}
}
// 排序
private void sortScanResultsByLevel() {
Collections.sort(scanResults, new Comparator<ScanResult>() {
@Override
public int compare(ScanResult o1, ScanResult o2) {
return o2.level - o1.level;
}
});
}
// 获取连接状态
private boolean getConnectivityStatus() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
}
```
阅读全文