[android-developers] Re: Establishing WiFi connection to a chosen hotspot

2009-10-19 Thread Farproc

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)"  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  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 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("\"");
> >  

[android-developers] Re: Establishing WiFi connection to a chosen hotspot

2009-08-21 Thread Roman ( T-Mobile USA)

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 the
addNetwork(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  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 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 networks =
> wifiManager.getConfiguredNetworks();
>                         System.out.println("=> Number of configured
> networks: "+networks.size());
>                         Toast.makeText(context,"NUmber of confiured
>

[android-developers] Re: Establishing WiFi connection to a chosen hotspot

2009-08-21 Thread R Ravichandran
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 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 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 = 10;

// 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;
}

[android-developers] Re: Establishing WiFi connection to a chosen hotspot

2009-08-20 Thread Roman ( T-Mobile USA)

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  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) <
>
> roman.baumgaert...@t-mobile.com> wrote:
>
> > Are you using the following permissions?
>
> >     > android:name="android.permission.ACCESS_NETWORK_STATE"/>
> >     > android:name="android.permission.CHANGE_NETWORK_STATE"/>
> >     > android:name="android.permission.ACCESS_WIFI_STATE"/>
> >     > 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  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 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 = 10;
>
> > >                         // 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_

[android-developers] Re: Establishing WiFi connection to a chosen hotspot

2009-08-19 Thread R Ravichandran
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?
>
> android:name="android.permission.ACCESS_NETWORK_STATE"/>
> android:name="android.permission.CHANGE_NETWORK_STATE"/>
> android:name="android.permission.ACCESS_WIFI_STATE"/>
> 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  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 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 = 10;
> >
> > // 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

[android-developers] Re: Establishing WiFi connection to a chosen hotspot

2009-08-19 Thread Roman ( T-Mobile USA)

Are you using the following permissions?







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  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 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 = 10;
>
>                         // 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
-~--~~~~--~~--~--~---