Android wifi 属性简介 及 wifi 信息获取(wifi 列表、配置信息
4.? Frequency 当前 WIFI 设备附近热点的频率(MHz)
5.? Level 所发现的 WIFI 网络信号强度
四、连接 wifi 热点
==============
通过? WifiManager.getConfiguredNetworks()? 方法会返回 WifiConfiguration 对象的列表,然后再调用 WifiManager.enableNetwork(); 方法就可以连接上指定的热点。
五、查看已经连接上的 wifi 信息
================
WifiInfo 是专门用来表示连接的对象,这个对象可以通过 WifiManager.getConnectionInfo() 来获取。
WifiInfo 中包含了当前连接中的相关信息。
getBSSID() 获取 BSSID 属性
getDetailedStateOf() 获取客户端的连通性
getHiddenSSID() 获取 SSID 是否被隐藏
getIpAddress() 获取 IP 地址
getLinkSpeed() 获取连接的速度
getMacAddress() 获取 Mac 地址
getRssi() 获取 802.11n 网络的信号
getSSID() 获取 SSID
getSupplicanState() 获取具体客户端状态的信息
六、示例
====
1)获得 WifiManager
WifiManager wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);
2)获取 Wifi 信息
WifiInfo wifiInfo=wifiManager.getConnectionInfo(); //当前 wifi 连接信息
List<ScanResult> scanResults=wifiManager.getScanResults();//搜索到的设备列表
for (ScanResult scanResult : scanResults) {
tv.append("\n 设备名:"+scanResult.SSID
+" 信号强度:"+scanResult.level+"/n :"+wifiManager.calculateSignalLevel(scanResult.level,4));
}
附 WifiManager 中计算级别的代码:
/**
Calculates the level of the signal. This should be used any time a signal
is being shown.
@param rssi The power of the signal measured in RSSI.
@param numLevels The number of levels to consider in the calculated
@return A level of the signal, given in the range of 0 to numLevels-1
*/
public static int calculateSignalLevel(int rssi, int numLevels) {
if (rssi <= MIN_RSSI) {
return 0;
} else if (rssi >= MAX_RSSI) {
return numLevels - 1;
} else {
int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
return (rssi - MIN_RSSI) / partitionSize;
}
}
3)网络权限
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
如:方法封装(返回返回当前连接的 wifi 的名字)
private String getConnectWifiSsid(){
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
Log.d("wifiInfo", wifiInfo.toString());
Log.d("SSID",wifiInfo.getSSID());
return wifiInfo.getSSID();
}
如果不是想获取当前连接,而是想获取 WIFI 设置中的连接, 可以使用? WifiManager.getConfiguredNetworks
七、获取当前已连接的 wifi 配置信息
==================
public class MyWifi extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView infoText = (TextView) this.findViewById(R.id.info);
infoText.setText(getInfo());
}
private String getInfo()
{
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String maxText = info.getMacAddress();
String ipText = intToIp(info.getIpAddress());
String status = "";
if (wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLED)
{
status = "WIFI_STATE_ENABLED";
}
String ssid = info.getSSID();
int networkID = info.getNetworkId();
int speed = info.getLinkSpeed();
return "mac:" + maxText + "\n\r"
"ip:" + ipText + "\n\r"
"wifi status :" + status + "\n\r"
"ssid :" + ssid + "\n\r"
"net work id :" + networkID + "\n\r"
"connection speed:" + speed + "\n\r"
;
}
private String intToIp(int ip)
{
return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "."
((ip >> 24) & 0xFF);
}
}
八、Android 开发周围 wifi 热点和属性的获取
=========================
一、获取 wifi 属性
获得 Android 系统相关的属性,通过 Manager 来获得对对象的操作服务。WiFi 相关的操作中需要用到两个 Manager:WifiManager 和 ConnectivityManage:
[java]? view plain copy
public?WifiManager?wifiManager;?????????????//管理 wifi??
public?ConnectivityManager?connectManager;?//管理网络连接??
[java]? view plain copy
wifiManager?=?(WifiManager)?getSystemService(WIFI_SERVICE);????????//获得系统 wifi 服务??
connectManager?=?(ConnectivityManager)?getSystemService(CONNECTIVITY_SERVICE); ?
获得服务后,即可得到对象进而获得相关属性:
[java]? view plain copy
public?NetworkInfo?netInfo;?????????????????//网络连接??
public?WifiInfo?wifiInfo;???????????????????//wifi??
public?DhcpInfo?dhcpInfo;? ? ? ? ? ? ? ? ? ?//动态主机配置协议信息的对象,获得 IP 等网关信息??
网络信息的获取:
[java]? view plain copy
netInfo?=?connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);??
dhcpInfo?=?wifiManager.getDhcpInfo();??
wifiInfo?=?wifiManager.getConnectionInfo();??
要获得 wifi 相关的网络信息,必须设定网络类型参数:ConnectivityManager.TYPE_WIFI.
下面就可以获得需要的信息,例如当前连接 wifi 的名称等等属性,即 SSID:
[java]? [vie
w plain]( ) copy
wifiInfo.getSSID();??
还要获得当前连接 wifi 网络的? ip、mask、netgate、dns? 等信息,则需要如下实现:
[java]? view plain copy
String?wifiProperty?=?"当前连接 Wifi 信息如下:"+wifiInfo.getSSID()+'\n'+??
????????????????"ip:"?????+?????FormatString(dhcpInfo.ipAddress)???+'\n'+??
????????????????"mask:"???+?????FormatString(dhcpInfo.netmask)?????+'\n'+??
????????????????"netgate:"+?????FormatString(dhcpInfo.gateway)?????+'\n'+??
????????????????"dns:"????+?????FormatString(dhcpInfo.dns1)??;??
其中 dhcpInfo 属性的值为 int 型,要转换成通常见到的 32 位地址则需要转换方法:
[java]? view plain copy
public?String?FormatString(int?value){??
????????String?strValue="";??
????????byte[]?ary?=?intToByteArray(value);??
????????for(int?i=ary.length-1;i>=0;i--){??
????????????????strValue?+=?(ary[i]?&?0xFF);??
????????????????if(i>0){??
????????????????????????strValue+=".";??
????????????????}??
????????}??
????????return?strValue;??
????}??
public??byte[]?intToByteArray(int?value){??
????????byte[]?b?=?new?byte[4];??
?????????for?(int?i?=?0;?i?<?4;?i++){??
????????????????int?offset?=?(b.length?-?1?-?i)?*?8;??
?????????????????b[i]?=?(byte)?((value?>>>?offset)?&?0xFF);??
??????????}??
??????????return?b;??
????}??
上述方法即可获得 wifi 网络相关的信息,根据需要来选择方法。
评论