如何以编程方式创建隐藏的WiFi热点?

时间:2018-07-19 01:56:47

标签: android wificonfiguration

我想用Android设备以编程方式创建一个隐藏的WiFi热点,但是我找不到[developer.android.google.cn] [1]的答案

[1]:http:///https://developer.android.google.cn/reference/android/net/wifi/WifiConfiguration。刚才我看到了一个名为 hiddenSSID 的属性,我试图更改它的值(true / false),但是它不起作用,有人可以帮我吗?

 private void stratWifiAp(String mSSID, String mPasswd) {
    Method method1 = null;
    try {

        method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        WifiConfiguration netConfig = new WifiConfiguration();

        netConfig.SSID = mSSID;
        netConfig.preSharedKey = mPasswd;
        netConfig.hiddenSSID = true;
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(4);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        method1.invoke(mWifiManager, netConfig, true);

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将您的代码修改为如下形式:

private void stratWifiAp(String mSSID, String mPasswd) {
    Method method1 = null;
    try {
        //通过反射机制打开热点
        method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        WifiConfiguration netConfig = new WifiConfiguration();

        netConfig.SSID = mSSID;
        netConfig.preSharedKey = mPasswd;
        netConfig.hiddenSSID = true;
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(4);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        method1.invoke(mWifiManager, netConfig, true);

        return netConfig;

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    //return netConfig; or try to add here IF ONLY YOU ADDED THE ABOVE ONE!
}