Roman, I have all the permissions you mentioned, plus the intent filters also in my code. I am trying this code on G1 phone. Still the same problem.
What is the right approach to make a wifi connection to a specific hot spot? Thanks Ravi On Wed, Aug 19, 2009 at 4:18 PM, Roman ( T-Mobile USA) < roman.baumgaert...@t-mobile.com> wrote: > > Are you using the following permissions? > > <uses-permission > android:name="android.permission.ACCESS_NETWORK_STATE"/> > <uses-permission > android:name="android.permission.CHANGE_NETWORK_STATE"/> > <uses-permission > android:name="android.permission.ACCESS_WIFI_STATE"/> > <uses-permission > android:name="android.permission.CHANGE_CHANGE_STATE"/> > > > How do u setup your broadcast receiver? > > Write a method: > > private void registerForWifiBroadcasts() { > IntentFilter intentFilter = new IntentFilter(); > intentFilter.addAction > (WifiManager.NETWORK_STATE_CHANGED_ACTION); > intentFilter.addAction > (WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); > intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); > intentFilter.addAction > (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); > > mContext.registerReceiver(this, intentFilter); > } > > or do this in the Manifest file. > > With doing this you should be able to get the state change intent. > > -- > 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 19, 12:51 pm, R Ravichandran <ravirajami...@gmail.com> wrote: > > Hello, > > > > I have some code using the WifiManager class that needs to do the > following: > > > > 1. scan available open wifi hotspots > > 2. pick one > > 3. establish connection. > > > > I am not able to successfully do steps 2 and 3. Here is the snippet: > > > > private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() > { > > > > @Override > > public void onReceive(Context context, Intent intent) { > > > > if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) > { > > // Asynchronous response from scan request. Hotspot > results > > are returned. > > > > List<ScanResult> hotSpots = wifiManager.getScanResults(); > > > > for(ScanResult hotSpot: hotSpots) { > > String hotSpotSsid = hotSpot.SSID; > > String hotSpotBssid = hotSpot.BSSID; > > StringBuffer sBuf = new StringBuffer("\""); > > sBuf.append(hotSpotSsid+"\""); > > hotSpotSsid = sBuf.toString(); > > > > if(hotSpotSsid.equals("\"myhotspot\"")) { > > > > 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); > > return; > > } > > // connect to this wifi network > > > > boolean successConnected = > > wifiManager.enableNetwork(inetId, true); > > if(successConnected) { > > System.out.println("====> Connected > > successfully to myhotspot.."); > > } > > else { > > System.out.println("====> Connection attempt > to > > myhotspot failed...Returning"); > > return; > > } > > } > > > > } > > > > } > > else > > if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { > > 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); > > > > } > > } > > > > I am getting the message that 'enableNetwork' call is succeeding. But the > > code for handling the NETWORK_STATE_CHANGED_ACTION event never gets > executed > > > > What am I doing wrong? > > > > Thanks > > > > Ravi > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---