A final comment: WifiManager.AddNetwork(conf) will fail if the the AP you're trying to connect is protected by password and the password you set in `conf` is incorrect!!!!
I have been debugging AddNetwork() failure for half a day, MY GOD!!! On 8月22日, 上午7时40分, "Roman ( T-Mobile USA)" <roman.baumgaert...@t- mobile.com> wrote: > Ravi, > > I think your current code looks pretty good. Please be aware that the > constant NETWORK_STATE_CHANGED_ACTION maps to > android.net.wifi.STATE_CHANGE. > > I am running nearly the same code and I receive the > NETWORK_STATE_CHANGED_ACTION event. Please, make sure that > theaddNetwork(configuration) is successful. But based on your post it is. > > Could you post the catlog of the entents which you are receiving? > > -- > Roman Baumgaertner > Sr. SW Engineer-OSDC > ·T· · ·Mobile· stick together > The views, opinions and statements in this email are those of the > author solely in their individual capacity, and do not necessarily > represent those of T-Mobile USA, Inc. > > On Aug 21, 12:34 pm, R Ravichandran <ravirajami...@gmail.com> wrote: > > > > > I am receiving some events and not all I am looking for. Here is the simple > > code that I wrote to experiment with my requirement. > > > All I wanted to accomplish with my code was: > > > 1. get a list of WiFi hot spots. I have a BoadcastReceiver that gets the > > WifiManager.SCAN_RESULTS_AVAILABLE_ACTION intent. I can iterate through the > > list and find my hotspot. > > > 2. Pick one, I am interested in. Once I find the hotspot I am interested in, > > I create a WifiConfiguration using the results from scan, and add that with > > wifiManager.addNetwork. > > 3. connect it. Then I call wifiManager.enableNetwork. HERE is WHRE I HAVE > > PROBLEM. I am expecting this will create a connection, and trigger a > > NETWORK_STATE_CHANGED_INTENT. But it doesn't. > > > Here is the code I am playing with: > > > package com.example.helloandroid; > > > import android.app.Activity; > > import android.os.Bundle; > > import android.widget.Toast; > > import android.content.BroadcastReceiver; > > import android.content.Context; > > import android.content.Intent; > > import android.content.IntentFilter; > > import android.net.wifi.ScanResult; > > import android.net.wifi.WifiManager; > > import android.net.wifi.WifiConfiguration; > > import android.net.wifi.WifiInfo; > > import java.util.List; > > import android.view.View; > > import android.view.View.OnClickListener; > > import android.widget.Button; > > > public class HelloAndroid extends Activity implements OnClickListener { > > /** Called when the activity is first created. */ > > private WifiManager wifiManager; > > private int globalCounter=0; > > private static final String CHOSEN_HOTSPOT = "myhotspot"; > > > @Override > > public void onCreate(Bundle savedInstanceState) { > > super.onCreate(savedInstanceState); > > setContentView(R.layout.main); > > > Button button = (Button)findViewById(R.id.ok); > > button.setOnClickListener(this); > > > wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); > > // register the broadcast receiver for wifi state events > > IntentFilter filter = new IntentFilter(); > > filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); > > filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); > > filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); > > filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); > > filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); > > filter.addAction(WifiManager.EXTRA_SUPPLICANT_ERROR); > > filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); > > filter.addAction(WifiManager.RSSI_CHANGED_ACTION); > > registerReceiver(wifiEventReceiver, filter); > > } > > > public void onClick(View v) { > > wifiManager.startScan(); > > Toast.makeText(this.getApplicationContext(),"Scan Started: "+"Msg > > Count: "+(++globalCounter),Toast.LENGTH_LONG).show(); > > } > > > private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() { > > > private boolean enabled = false; > > > @Override > > public void onReceive(Context context, Intent intent) { > > System.out.println ("WiFi Event BroadReceiver: > > onReceive()======================================="); > > > if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { > > if(enabled) return; > > List<ScanResult> hotSpots = wifiManager.getScanResults(); > > StringBuffer ssids = new StringBuffer(); > > > for(ScanResult hotSpot: hotSpots) { > > String hotSpotSsid = hotSpot.SSID; > > String hotSpotBssid = hotSpot.BSSID; > > System.out.println("SSID: "+hotSpotSsid+" BSSID: > > "+hotSpotBssid); > > StringBuffer sBuf = new StringBuffer("\""); > > sBuf.append(hotSpotSsid+"\""); > > hotSpotSsid = sBuf.toString(); > > ssids.append(hotSpotSsid+".."); > > if(hotSpotSsid.indexOf(CHOSEN_HOTSPOT) > 0) { > > > List<WifiConfiguration> networks = > > wifiManager.getConfiguredNetworks(); > > System.out.println("=====> Number of configured > > networks: "+networks.size()); > > Toast.makeText(context,"NUmber of confiured > > Networks: "+networks.size(),Toast.LENGTH_LONG).show(); > > WifiConfiguration wifiConfiguration = new > > WifiConfiguration(); > > wifiConfiguration.SSID = hotSpotSsid; > > wifiConfiguration.BSSID = hotSpotBssid; > > wifiConfiguration.hiddenSSID = false; > > wifiConfiguration.priority = 100000; > > > // add this to the configured networks > > int inetId = > > wifiManager.addNetwork(wifiConfiguration); > > if(inetId < 0) { > > System.out.println("Unable to add network > > configuration for SSID: "+hotSpotSsid); > > Toast.makeText(context,"Unable to add SSID: > > "+hotSpotSsid,Toast.LENGTH_LONG).show(); > > return; > > } > > else { > > Toast.makeText(context,"Successfully added SSID: > > "+hotSpotSsid+" Inet Id: "+inetId,Toast.LENGTH_LONG).show(); > > > } > > boolean successConnected = > > wifiManager.enableNetwork(inetId, true); > > if(successConnected) { > > Toast.makeText(context,"Connected to SSID: > > "+hotSpotSsid+" Inet Id: "+inetId,Toast.LENGTH_LONG).show(); > > > enabled = true; > > break; > > } > > else { > > System.out.println("====> Connection attempt > > failed "+hotSpotSsid+" ...Returning"); > > return; > > } > > } > > } > > Toast.makeText(context,ssids+"Msg Count: > > "+(++globalCounter),Toast.LENGTH_LONG).show(); > > } > > else > > if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { > > System.out.println("===== NETWORK_STATE_CHANGE_ACTION event > > happened...."); > > WifiInfo wifiInfo = wifiManager.getConnectionInfo(); > > System.out.println("Check for current connection: SSID: > > "+wifiInfo.getSSID()); > > int ipAddr = wifiInfo.getIpAddress(); > > > System.out.println("IP Address for connection: "+ipAddr); > > Intent wifiEstablishedIntent = new Intent("WiFi"); > > wifiEstablishedIntent.putExtra("Connection", "Connection > > established: "+wifiInfo.getSSID()); > > sendBroadcast(wifiEstablishedIntent); > > Toast.makeText(context,"Connected: > > "+(++globalCounter),Toast.LENGTH_LONG).show(); > > } > > else > > if(intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { > > System.out.println("===== SUPPLICANT_STATE_CHANGE_ACTION > > event happened...."); > > > intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false); > > > } > > else > > if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTIO > > N)) > > { > > System.out.println("===== > > SUPPLICANT_CONNECTION_CHANGE_ACTION event happened...."); > > > intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false); > > } > > } > > }; > > > } > > > These are the permissions I am using: > > > <uses-permission > > android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> > > <uses-permission > > android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> > > <uses-permission > > android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> > > <uses-permission > > android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission> > > > On Thu, Aug 20, 2009 at 4:10 PM, Roman ( T-Mobile USA) < > > > roman.baumgaert...@t-mobile.com> wrote: > > > > Ravi, > > > > I would first try to check out whether in general your code is able to > > ... > > 阅读更多 >> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---