转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49385761 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货!
关于Android WiFi,我写了5篇文章,讲解了在配置WiFi中用到的知识
WifiManager、ScanResult(一)
WifiInfo(二)
ConnectivityManager(三)
NetworkInfo和NetworkInfo.State(四)
WifiManager、WifiInfo、WifiConfiguration集成类(五)
类 WifiManager
该类主要用于操作、判断、获取WiFi连接的信息,以下是常用的函数
(WifiManager) context.getSystemService(Context.WIFI_SERVICE)
获得WifiManager的对象
boolean isWifiEnabled()
判断无线网络是否开启
boolean setWifiEnabled(boolean enabled)
关闭或开启无线网络
boolean startScan()
Request a scan for access points.
List< ScanResult > getScanResults()
Return the results of the latest access point scan.
一般先使用startScan()扫描周围WiFi,再使用getScanResults()获得WiFi列表里的热点信息。
List< WifiConfiguration > getConfiguredNetworks()
Return a list of all the networks configured in the supplicant.
- 该函数返回的结果是曾经连接成功过的WiFi,如果手机已经root过,查看连接过 的wifi在RE管理器中路径:data/misc/wifi/wpa_supplicant.conf。
- 如果想要连接的网络不再这个列表中,说明未连接过,可以创建一个WifiConfiguration对象,并使用addNetwork添加新网络
- WifiConfiguration示例代码
int addNetwork(WifiConfiguration config)
Add a new network description to the set of configured networks.
参数config的配置,请参见 WifiConfiguration示例代码
boolean enableNetwork(int netId, boolean disableOthers)
Allow a previously configured network to be associated with.
连接到addNetwork()添加的网络,这两个方法联合使用,连接到指定的一个网络上
WifiInfo getConnectionInfo()
Return dynamic information about the current Wi-Fi connection, if any is active. 获取当前连接的WiFi信息
boolean removeNetwork(int netId)
Remove the specified network from the list of configured networks.
移除一个指定的网络从网络配置列表中
DhcpInfo getDhcpInfo()
Return the DHCP-assigned addresses from the last successful DHCP request, if any.
ScanResult
成员变量
public String | BSSID | The address of the access point. |
---|---|---|
public String | SSID | The network name. |
public String | capabilities | Describes the authentication, key management, and encryption schemes supported by the access point. |
public int | frequency | The frequency in MHz of the channel over which the client is communicating with the access point. |
public int | level | The detected signal level in dBm, also known as the RSSI. |
public long | timestamp | timestamp in microseconds (since boot) when this result was last seen. |
关注我的公众号,轻松了解和学习更多技术