转载自:https://blog.csdn.net/QQxiaoqiang1573/article/details/54694205
1.首先我们创建一个wifi工具类WifiUtil,代码如下:
private WifiManager mWifiManager;
private static WifiUtil mInstance;
private WifiUtil(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
public static WifiUtil getInstance(Context context) {
if (mInstance == null) {
synchronized (WifiUtil.class) {
if (mInstance == null) {
mInstance = new WifiUtil(context.getApplicationContext());
}
}
}
return mInstance;
}
/***
* 打开wifi
*/
public void openWifi(){
if(!mWifiManager.isWifiEnabled()){
mWifiManager.setWifiEnabled(true);
}
}
/**
* 关闭wifi
*/
public void closeWifi(){
if(mWifiManager.isWifiEnabled()){
mWifiManager.setWifiEnabled(false);
}
}
/**
* 获取wifi扫描结果
*/
public List getWifiScanResult(){
List mScanResultList = new ArrayList<>();
List scanResultList = mWifiManager.getScanResults();
if(scanResultList != null && scanResultList.size() > 0){
for (int i = 0; i < scanResultList.size(); i++) {
ScanResult scanResult = scanResultList.get(i);
if(scanResult != null && !TextUtils.isEmpty(scanResult.SSID)){
mScanResultList.add(scanResult);
}else {
continue;
}
}
}
return mScanResultList;
}
/**
* 获取wifi等级,总共分为四级
* @param rssi
* @return
*/
public int getWifiSignal(int rssi){
if(rssi == Integer.MAX_VALUE){
return -1;
}
return mWifiManager.calculateSignalLevel(rssi,4);
}
/**
* 判断是否2.4Gwifi
* @param frequency
* @return
*/
public boolean is24GHzWifi(int frequency){
return frequency > 2400 && frequency < 2500;
}
/**
* 判断是否5Gwifi
* @param frequency
* @return
*/
public boolean is5GHzWifi(int frequency) {
return frequency > 4900 && frequency < 5900;
}
3、创建一个布局文件,主要是三个按钮,分别是开启wifi,关闭wifi,扫描wifi,和一个listview用于显示wifi列表
4、创建activity
public class ScanWifiActivity extends AppCompatActivity {
public static final String TAG = "ScanWifiActivity";
private WifiUtil wifiUtil;
private List scanResultList;
private ListView listView;
private ScanResultAdapter scanResultAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_wifi);
Button openWifi = (Button) findViewById(R.id.open_wifi);
Button closeWifi = (Button) findViewById(R.id.close_wifi);
Button scanWifi = (Button) findViewById(R.id.get_wifi_info);
listView = (ListView) findViewById(R.id.listview);
wifiUtil = WifiUtil.getInstance(this);
String[] requestPermission = new String[]{//由于我的targetSDK是7.0,所以需要动态申请权限
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_COARSE_LOCATION};
List needRequest = new ArrayList<>();
for (int i = 0; i < requestPermission.length; i++) {
if (ContextCompat.checkSelfPermission(this, requestPermission[i]) == PackageManager.PERMISSION_GRANTED) {
} else {
needRequest.add(requestPermission[i]);
}
}
if (needRequest != null && needRequest.size() > 0) {
ActivityCompat.requestPermissions(this, needRequest.toArray(new String[needRequest.size()]), 0);
}
openWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiUtil.openWifi();
}
});
closeWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiUtil.closeWifi();
}
});
scanWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanResultList = wifiUtil.getWifiScanResult();
if (scanResultAdapter != null) {
scanResultAdapter.notifyDataSetChanged();
}
}
});
initListView();
}
private void initListView() {
scanResultList = new ArrayList<>();
scanResultAdapter = new ScanResultAdapter();
listView.setAdapter(scanResultAdapter);
}
class ScanResultAdapter extends BaseAdapter {
@Override
public int getCount() {
return scanResultList.size();
}
@Override
public Object getItem(int position) {
return scanResultList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = ScanWifiActivity.this.getLayoutInflater().inflate(R.layout.activity_scan_wifi_list_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (scanResultList != null && scanResultList.size() > 0) {
ScanResult scanResult = scanResultList.get(position);
if (scanResult == null) return convertView;
int level = scanResultList.get(position).level;
viewHolder.ivSignal.setImageResource(R.drawable.wifi_signal);
viewHolder.ivSignal.setImageLevel(wifiUtil.getWifiSignal(level));
viewHolder.tvWifiName.setText(scanResultList.get(position).SSID);
int frequency = scanResultList.get(position).frequency;
boolean is5G = wifiUtil.is5GHzWifi(frequency);
boolean is24G = wifiUtil.is24GHzWifi(frequency);
if (is5G) {
viewHolder.tvWifiFrequency.setText("5G");
} else if (is24G) {
viewHolder.tvWifiFrequency.setText("2.4G");
} else {
viewHolder.tvWifiFrequency.setText("");
}
Log.d(TAG, "SSID = " + scanResult.SSID + "--LEVEL = " + level + "--FREQUENCY = " + scanResult.frequency);
}
return convertView;
}
class ViewHolder {
public ImageView ivSignal;
public TextView tvWifiName;
public TextView tvWifiFrequency;
public ViewHolder(View convertView) {
this.ivSignal = convertView.findViewById(R.id.iv_wifi_signal);
this.tvWifiName = convertView.findViewById(R.id.tv_wifi_name);
this.tvWifiFrequency = convertView.findViewById(R.id.tv_wifi_hz);
}
}
}
//动态申请权限的回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
return;
}
}
Log.d(TAG, "onRequestPermissionsResult: OK");
// wifiUtil.openWifi();
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}