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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---