在使用Cyanogenmod(6.0+)的设备中以编程方式创建热点的正确方法

时间:2017-03-01 11:06:08

标签: java android android-studio cyanogenmod hotspot

public static boolean isApOn(Context context) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    try {
        Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
        method.setAccessible(true);
        return (Boolean) method.invoke(wifimanager);
    }
    catch (Throwable ignored) {}
    return false;
}
public static boolean configApState(Context context) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiConfiguration wificonfiguration = null;
    try {
        // if WiFi is on, turn it off
        if(isApOn(context)) {
            wifimanager.setWifiEnabled(false);
        }
        //Change Name of the Created Hotspot
        try {
            Method getConfigMethod = wifimanager.getClass().getMethod("getWifiApConfiguration");
            WifiConfiguration wifiConfig = (WifiConfiguration) getConfigMethod.invoke(wifimanager);                
            wifiConfig.SSID = "abcde123";
            wifiConfig.preSharedKey = "password123";                wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

            Method setWifiApMethod = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            boolean apstatus = (Boolean) setWifiApMethod.invoke(wifimanager, wifiConfig, true);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method.invoke(wifimanager, wificonfiguration, !isApOn(context));
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
    }

但是当我运行此代码时,会创建热点,但是当客户端转向连接它时,它会停留在"获取IP地址"环。针对许多设备在CM13,CM14上进行了测试。 请帮我解决这个问题。

0 个答案:

没有答案