SSID为wifi的名称
BSSID为wifi的mac地址
level为wifi的信号强度
main_activity.java:
package com.example.myapplication;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import java.util.List;
import android.net.wifi.ScanResult;
public class MainActivity extends AppCompatActivity {
private WifiInfo wifiInfo = null; //获得的Wifi信息
private WifiManager wifiManager = null; //Wifi管理器
private Handler handler;
private int level; //信号强度值
private String macAddress; //wifi信号BSSID值
List listb;
@SuppressLint("HandlerLeak")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得WifiManager
wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
// 使用定时器,每隔5秒获得一次信号强度值
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
listb = wifiManager.getScanResults();
//数组初始化要注意
String[] listSSID = new String[listb.size()];
String[] listBSSID = new String[listb.size()];
int[] listLevel = new int[listb.size()];
if (listb != null) {
for (int i = 0; i < listb.size(); i++) {
ScanResult scanResult = listb.get(i);
listSSID[i] = scanResult.SSID;
listBSSID[i] = scanResult.BSSID;
listLevel[i] = scanResult.level;
}
}
String[] listSSID0 = new String[listb.size()];
String[] listBSSID0 = new String[listb.size()];
int[] listLevel0 = new int[listb.size()];
if (listb == null) {
listSSID0[0] = "NoWiFi";
listBSSID0[0] = "NoWiFi";
listLevel[0] = -200; //-200默认没有wifi
} else {
listSSID0 = listSSID;
listBSSID0 = listBSSID;
listLevel0 = listLevel;
}
for (int i = 0; i < listb.size(); i++) {
Log.d("wifi", listSSID0[i] + "//" + listBSSID0[i] + "//" + listLevel0[i]);
}
Log.d("wifi", "=======================");
wifiInfo = wifiManager.getConnectionInfo();
//获得信号强度值
level = wifiInfo.getRssi();
macAddress = wifiInfo.getBSSID();
Message msg = new Message();
handler.sendMessage(msg);
}
}, 1000, 5000);
// 使用Handler实现UI线程与Timer线程之间的信息传递,每5秒告诉UI线程获得wifiInto
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Toast.makeText(MainActivity.this,
"信号强度:" + level + " BSSID: " + macAddress, Toast.LENGTH_SHORT)
.show();
}
};
}
}
AndroidManifest.xml中获取wifi的权限:
参考:https://blog.csdn.net/zoukai1587/article/details/89639169
对于Android6.0以上版本,需要添加兼容性:
MainActivity.java代码:
package com.example.zlf_empty;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import java.util.List;
import android.net.wifi.ScanResult;
import java.io.IOException;
import android.os.Build;
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
public class MainActivity extends AppCompatActivity {
private WifiInfo wifiInfo = null; //获得的Wifi信息
private WifiManager wifiManager = null; //Wifi管理器
private Handler handler;
private int level; //信号强度值
private String macAddress; //wifi信号BSSID值
List listb;
private static final int REQUEST_CODE_ACCESS_COARSE_LOCATION = 1;
@SuppressLint("HandlerLeak")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得WifiManager
wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
// 判断wifi是否开启
if(! wifiManager.isWifiEnabled()){
if(wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING)
wifiManager.setWifiEnabled(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//如果 API level 是大于等于 23(Android 6.0) 时
//判断是否具有权限
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//判断是否需要向用户解释为什么需要申请该权限
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
//showToast("自Android 6.0开始需要打开位置权限");
}
//请求权限
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_CODE_ACCESS_COARSE_LOCATION);
}
}
// 使用定时器,每隔5秒获得一次信号强度值
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
listb = wifiManager.getScanResults();
Log.d("wifi","listb"+listb);
Log.d("wifi","listb.size()"+listb.size());
//数组初始化要注意
String[] listSSID = new String[listb.size()];
String[] listBSSID = new String[listb.size()];
int[] listLevel = new int[listb.size()];
if (listb != null) {
for (int i = 0; i < listb.size(); i++) {
ScanResult scanResult = listb.get(i);
listSSID[i] = scanResult.SSID;
listBSSID[i] = scanResult.BSSID;
listLevel[i] = scanResult.level;
}
}
String[] listSSID0 = new String[listb.size()];
String[] listBSSID0 = new String[listb.size()];
int[] listLevel0 = new int[listb.size()];
if (listb == null) {
listSSID0[0] = "NoWiFi";
listBSSID0[0] = "NoWiFi";
listLevel[0] = -200; //-200默认没有wifi
} else {
listSSID0 = listSSID;
listBSSID0 = listBSSID;
listLevel0 = listLevel;
}
for (int i = 0; i < listb.size(); i++) {
Log.d("wifi", listSSID0[i] + "//" + listBSSID0[i] + "//" + listLevel0[i]);
}
Log.d("wifi", "=======================");
wifiInfo = wifiManager.getConnectionInfo();
//获得信号强度值
level = wifiInfo.getRssi();
macAddress = wifiInfo.getBSSID();
Message msg = new Message();
handler.sendMessage(msg);
}
}, 1000, 5000);
// 使用Handler实现UI线程与Timer线程之间的信息传递,每5秒告诉UI线程获得wifiInto
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Toast.makeText(MainActivity.this,
"信号强度:" + level + " BSSID: " + macAddress, Toast.LENGTH_SHORT)
.show();
}
};
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_ACCESS_COARSE_LOCATION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//用户允许改权限,0表示允许,-1表示拒绝 PERMISSION_GRANTED = 0, PERMISSION_DENIED = -1
//permission was granted, yay! Do the contacts-related task you need to do.
//这里进行授权被允许的处理
} else {
//permission denied, boo! Disable the functionality that depends on this permission.
//这里进行权限被拒绝的处理
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
AndroidManifest.xml增加的权限:
参考链接:http://www.pianshen.com/article/2568263532/