WifiManager、ScanResult(一)

点击查看代码示例

类 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.

你可能感兴趣的:(网络,函数,Wi-Fi)