2.wifi开发

1.打开wif

2.搜索wifi,得到wifi列表

3.连接wifi

4.连接成功,连接失败

5.忘记wifi密码


1.

// 取得WifiManager对象

mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

mWifiManager.setWifiEnabled(true);

2.

mWifiManager.startScan();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

  扫描结果监听

mWifiManager.getScanResults();

3.

public int addWiFiNetwork(String SSID, String password, Data type) {


    // 创建WiFi配置
    WifiConfiguration configuration = createWifiConfig(SSID, password, type);

    // 添加WIFI网络
    int networkId = mWifiManager.addNetwork(configuration);

    if (networkId == -1) {
        return -1;
    }

    // 使WIFI网络有效
    mWifiManager.enableNetwork(networkId, true);
    mWifiManager.saveConfiguration();
    return networkId;
}
//类型我看了一下源码,一般都只有3种  1 没密码  2 wep 3 wpa ,下面的创建config方式是网络上别人写的

private WifiConfiguration createWifiConfig(String SSID, String password, Data type) {
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\"";

    if (type == Data.WIFI_CIPHER_NOPASS) {
        config.wepKeys[0] = "";
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;
    } else if (type == Data.WIFI_CIPHER_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;
    } else if (type == Data.WIFI_CIPHER_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.status = WifiConfiguration.Status.ENABLED;
    } else if (type == Data.WIFI_CIPHER_WPA2) {
        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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        config.status = WifiConfiguration.Status.ENABLED;
    }

    return config;
}

4.

wifi连接成功的广播

WifiManager.NETWORK_STATE_CHANGED_ACTION


NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (null != info && info.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
   //成功连接
}

wifi连接失败的广播

WifiManager.SUPPLICANT_STATE_CHANGED_ACTION
int error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0);
if (WifiManager.ERROR_AUTHENTICATING == error) {
    //密码错误,认证失败
  
}

5.

mWifiManager.removeNetwork(networkId);
mWifiManager.saveConfiguration();



相关文章:

  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
猜你喜欢
  • 2022-01-09
  • 2022-01-12
  • 2022-02-09
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2021-12-07
相关资源
相似解决方案