使用WifiManager连接到无线网络(wpa2)

时间:2017-11-24 04:04:16

标签: java android wifi nfc wifimanager

尝试创建一个NFC标签应用程序,在NFC标签上存储Wifi凭据,然后将其读出。从那里我拿起字符串并使用它来通过wifimanager将凭证连接到无线网络。我很确定我的代码是正确的,但它仍然没有连接。我错过了什么吗?我尝试连接的频道是WPA2。

我如何提取该功能的数据:

 private void connectToWifi(final String networkSSID, final String networkPassword){
    if (!wifiManager.isWifiEnabled()){
        wifiManager.setWifiEnabled(true);
    }
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = String.format("\"%s\"", networkSSID);
    conf.preSharedKey = "\""+ networkPassword +"\"";

    WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
    wifiManager.addNetwork(conf);

    List<WifiConfiguration> wifiList = wifiManager.getConfiguredNetworks();
    for( WifiConfiguration i : wifiList ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
            wifiManager.disconnect();
            wifiManager.enableNetwork(i.networkId, true);
            wifiManager.reconnect();

            break;
        }
    }
}

更新:连接功能

{{1}}

更新2:

我将我的网络切换到WPA并且它起作用,似乎不适用于WPA2,甚至可能吗?

2 个答案:

答案 0 :(得分:1)

要连接WPA网络,您需要添加如下密码:

conf.preSharedKey = "\""+ networkPass +"\"";

对于Open network,您需要这样做:

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

然后,您需要将其添加到Android wifi管理器设置:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);

之后你必须启用它。

List<WifiConfiguration> wifiList = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : wifiList ) {
            if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                 wifiManager.disconnect();
                 wifiManager.enableNetwork(i.networkId, true);
                wifiManager.status = WifiConfiguration.Status.ENABLED;                                         
wifiManager.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);                                           
wifiManager.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);                                           
wifiManager.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);                                            
wifiManager.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);                                           wifiManager.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);                                        wifiManager.allowedProtocols.set(WifiConfiguration.Protocol.RSN);                                          wifiManager.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                 wifiManager.reconnect();               

                 break;
            }           
         }

快乐的编码!!

答案 1 :(得分:0)

感谢@HemantParmar

在他的帮助和谷歌搜索中,我得到了与WPA2合作的作品:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    mAuth = FirebaseAuth.getInstance();
    logoutBtn = (Button)findViewById(R.id.logoutBtn);
    profileName = (TextView)findViewById(R.id.profileName);

    logoutBtn.setOnClickListener(this);

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                profileName.setText("Welcome, "+ user.getDisplayName()+"!");
            } else {
                finish();
                Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        }
    };
    FirebaseAuth.getInstance().addAuthStateListener(mAuthListener);

}