WiFi的显示及WPA2_PSK加密

  public void createWifiHotspot() {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiConfiguration config = new WifiConfiguration();
        config.SSID =WIFI_ssid;
        config.preSharedKey = WIFI_PASSWORK;
        config.hiddenSSID = false;  //是否隐藏热点名称  false 不掩藏
        config.allowedAuthAlgorithms
                .set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedKeyManagement.set(4); //WifiConfiguration.KeyMgmt.WPA2_PSK,不能直接用,用代号4即可
//        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;
        //通过反射调用设置热点
        try {
            Method method = wifiManager.getClass().getMethod(
                    "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            boolean enable = (Boolean) method.invoke(wifiManager, config, true);
            if (enable) {
                textview.setText("热点已开启 SSID:" + WIFI_HOTSPOT_SSID + " password:123456789");
            } else {
                textview.setText("创建热点失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            textview.setText("创建热点失败");
        }
    }
 
  
  遇到的两个问题以解决
 config.hiddenSSID = false;  //是否隐藏热点名称  false 不掩藏
 config.allowedKeyManagement.set(4); //WifiConfiguration.KeyMgmt.WPA2_PSK,不能直接用,用代号4即可

 源码下载链接 http://download.csdn.net/detail/m0_37039192/9858469


 
 

你可能感兴趣的:(Android问题)