14 марта 2012 г. 20:37 пользователь Robert Greenwalt <rgreenw...@google.com> написал: > I think that addNetwork adds to a persistent database of networks.
That's true. > Calling > it again with the same information may generate an error as it can't add > twice. Not sure if that is the case. I believe the network will be added twice (or more). This can (and should be) avoided by calling WifiManager.getConfiguredNetworks and checking the list for a match. Back to the original code: the configuration object, I believe, is missing: wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); After the network's been added and "focused on" by calling enableNetwork(, true), calling WifiManager.startScan will initiate the {re}connection process. -- Kostya If you then call enableNetwork using -1 as the networkID it will of > course give an error. > > > On Tue, Mar 13, 2012 at 11:57 AM, Endy Silveira > <endy.silve...@traceback.com.br> wrote: >> >> Hello all, >> >> I googled it by days and got no success, I'm trying to connect to my >> WPA/WPA2 network when I put the Wifi On, it all over codes... >> >> If I was already connected to the network before I set the wifi off on the >> program, when I set the wifi on it connect, but I want to enter with the >> name of the network and the password... >> >> The code that I got error is this: >> >> wifiManager.setWifiEnabled(true); >> >> Toast.makeText(context, "Off to On", >> Toast.LENGTH_LONG).show(); >> >> WifiManager wifi = (WifiManager) >> getSystemService(Context.WIFI_SERVICE); >> >> WifiConfiguration wc = new WifiConfiguration(); >> >> wc.SSID = "\"My Network\""; >> >> wc.preSharedKey = "\"password\""; >> >> >> wc.hiddenSSID = false; >> >> wc.status = WifiConfiguration.Status.ENABLED; >> >> >> wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); >> >> >> wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); >> >> >> wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); >> >> >> wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); >> >> >> wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); >> >> wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); >> >> int res = wifi.addNetwork(wc); >> >> Toast.makeText(context, "add Network returned " + res, >> Toast.LENGTH_LONG).show(); >> >> boolean b = wifi.enableNetwork(res, true); >> >> Toast.makeText(context, "enableNetwork returned " + b, >> Toast.LENGTH_LONG).show(); >> >> wifiManager.setWifiEnabled(true); >> >> >> I got -1 in the add Network and false in enableNetwork... >> >> Someone know why? >> >> Thanks in advance! >> >> Endy >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Android Developers" group. >> To post to this group, send email to android-developers@googlegroups.com >> To unsubscribe from this group, send email to >> android-developers+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en > > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en