安卓Wifi开发笔记

用WifiManager来开启扫描

wifiManager.startScan()

开始扫描来扫描附近的wifi,这个接口在最新的安卓版本弃用了。暂时没去看替代的方法

打开和关闭wifi的开关,关闭设为false

mWifiManager.setWifiEnabled(true);

获取当前连接wifi的信息

WifiInfo currentInfo = mWifiManager.getConnectionInfo();

获取系统已保存的wifi信息

List mWifiConfiguration = mWifiManager.getConfiguredNetworks();

取消保存的wifi

mWifiManager.removeNetwork(mWifiConfiguration.get(i).networkId);

连接指定wifi,需要先获取wifi的ID 然后连接

mWifiManager.disconnect();
int networkId = mWifiManager.addNetwork(createWifiInfo(SSID,pwd,3));
boolean enable = mWifiManager.enableNetwork(networkId, true);
mWifiManager.saveConfiguration();
mWifiManager.reconnect();

用下面的方法可以获取要连接的wifi的配置,然后获取netid

/*创建要连接的Wifi的配置*/
public WifiConfiguration createWifiInfo(String SSID, String Password, int Type)
{
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\"";

    WifiConfiguration tempConfig = this.IsExsits(SSID);
    if(tempConfig != null) {
        mWifiManager.removeNetwork(tempConfig.networkId);
    }

    if(Type == 1) //WIFICIPHER_NOPASS
    {
        config.wepKeys[0] = "";
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;
    }
    if(Type == 2) //WIFICIPHER_WEP
    {
        config.hiddenSSID = true;
        config.wepKeys[0]= "\""+Password+"\"";
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;
    }
    if(Type == 3) //WIFICIPHER_WPA
    {
        config.preSharedKey = "\""+Password+"\"";
        config.hiddenSSID = true;
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        //config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        config.status = WifiConfiguration.Status.ENABLED;
    }
    return config;
}

上面的enable不是指连接指定wifi的成功与否
而是返回的这个wifi是否可以连接。
如果要去判断wifi是否连接成功。可以接收广播

mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION); //信号强度变化
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); //网络状态变化
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); //wifi状态,是否连上,密码
mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);  //是不是正在获得IP地址
mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);//连上与否
//扫描结果
mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

通过上面的wifi广播,我们就可以接收到想要的信息。

你可能感兴趣的:(安卓)