Results 1 to 2 of 2
  1. #1
    koz28 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Check what ssid is currently connected?

    Hi,
    I am using the code below to connect an adroid phone to a wireless network. Is there a way I can have it check to see if it is already connected to it before it attempts to connect?
    package koz.koz.imobile;

    import android.app.Activity;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.EditText;
    import android.widget.TextView;

    public class IVMOBILEActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    //////////////////////////////////
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    TextView status = new TextView(this);

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"test\""; //IMP! This should be in Quotes!!

    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.ENABLED;
    wc.priority = 10;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyM gmt.WPA_PSK);
    wc.allowedProtocols.set(WifiConfiguration.Protocol .RSN);
    wc.allowedProtocols.set(WifiConfiguration.Protocol .WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.Aut hAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.Aut hAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.Pa irwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.Pa irwiseCipher.TKIP);
    //.allowedGroupCiphers.set(WifiConfiguration.GroupCi pher.WEP40);
    //.allowedGroupCiphers.set(WifiConfiguration.GroupCi pher.WEP104);





    //This is the WEP Password
    //wc.wepTxKeyIndex = 0;
    wc.preSharedKey = "\"test\"";
    WifiManager wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE);
    boolean res1 = wifiManag.setWifiEnabled(true);
    int res = wifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean es = wifi.saveConfiguration();
    Log.d("WifiPreference", "saveConfiguration returned " + es );
    boolean b = wifi.enableNetwork(res, true);
    Log.d("WifiPreference", "enableNetwork returned " + b );

    tv.setText("You are now connected! " +
    "Version 1.0");

    status.setText("The was an error connecting , please try again.");

    try {

    Thread.sleep(5000);

    ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONN ECTIVITY_SERVICE);

    if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){
    //You are connected, do something online.
    setContentView(tv);

    }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
    //Not connected.
    setContentView(status);
    }

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }






    }


    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Check what ssid is currently connected?

    Moved from New to Java. In future, please find and post in the most appropriate section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. 8*8 network of nodes connected with each other
    By satish61 in forum New To Java
    Replies: 1
    Last Post: 03-08-2012, 10:39 PM
  2. How to know if 3 machines in LAN are connected
    By Yogesh_P in forum Networking
    Replies: 1
    Last Post: 03-30-2011, 09:10 AM
  3. pc is connected to internet ???
    By mahdi-farzami in forum Networking
    Replies: 1
    Last Post: 03-26-2010, 03:28 PM
  4. How to send folder through a connected socket
    By Chris555 in forum Networking
    Replies: 0
    Last Post: 07-18-2009, 11:41 AM
  5. Replies: 2
    Last Post: 08-08-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •