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_ACTION))
{
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) <
[email protected]> wrote:
>
> Ravi,
>
> I would first try to check out whether in general your code is able to
> receive the intents you are looking for.
> You can verify this with configuring the Wifi settings on the phone
> first and then by turning on/off your AP. If you are bale to see the
> events, then the next step would be to connect with your own code.
>
> --
> 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, 10:15 pm, R Ravichandran <[email protected]> wrote:
> > 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) <
> >
> > [email protected]> 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 <[email protected]> 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 [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
-~----------~----~----~----~------~----~------~--~---