赞
踩
1:Android m中获取wifi列表的方法
public static final List<ScanResult> getWifiList(Context context){
WifiManager wifiManager = (WifiManager)context
.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
wifiManager.startScan();
List<ScanResult> wifiList = wifiManager.getScanResults();
return wifiList.size()>0? wifiList:new ArrayList();
}
2:申请位置权限,wifi状态权限,你没有看错,Android m中 获取wifi是需要位置权限的
if (ContextCompat.checkSelfPermission(this,Manifest.permission_group.LOCATION)!=PackageManager.PERMISSION_GRANTED){
// 获取wifi连接需要定位权限,没有获取权限
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_WIFI_STATE,
},WIFI_SCAN_PERMISSION_CODE);
return;
}
3:在权限返回出做操作初始化列表的adapter的数据
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case WIFI_SCAN_PERMISSION_CODE:
if (grantResults.length>0&&grantResults[0] == PackageManager.PERMISSION_GRANTED){
// 允许
initData();
}else{
// 不允许
Toast.makeText(this,getString(R.string.permisstion_deny),Toast.LENGTH_SHORT).show();
}
break;
}
}
ps:此外蓝牙列表获取也是需要获取位置权限
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。