1

I am trying to display specific Wi-Fi SSID currently in range, displayed in my Activity, but nothing is being displayed. With the current state I am getting the connected SSID but I want to get whether SSID is in the range also it is availabe and not connected. Thereafter, this SSID will be displayed in my xml file.

 public  String getCurrentSsid(Context context) {
  String ssid = null;
  // Check wheather the WIFI is available.
  ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connManager
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

  if (networkInfo.isAvailable()) {
   // get the wifi name
   final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
   final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
   if(connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())){
    ssid = connectionInfo.getSSID();
    textWifi.setText(ssid);
    
   }
  }
  return ssid;
 }
Mr Asker
  • 2,300
  • 11
  • 31
  • 56
  • You want the list of WiFi networks? – Fahim Apr 02 '15 at 01:31
  • yes and then I want to parse it to find wether specific wifi which begins with ""sv-" plus number between 400-500 is availabe like "sv-450". – Mr Asker Apr 02 '15 at 07:37
  • 1
    refer this http://stackoverflow.com/questions/5452940/how-can-i-get-android-wifi-scan-results-into-a-list – Fahim Apr 02 '15 at 07:43

1 Answers1

0

I just tested your code and it works for me. It shows the current connected SSID.

Edit: Your code shows the the current connected SSID (if connected), and it now looks like you want to get the list of all SSIDs currently in range.

Make sure you have your AndroidManifest.xml set up correctly:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Edit 2: Now that it is clear that you would like to get Scan Results and process them, I just worked out this simple example:

import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = (TextView) this.findViewById(R.id.ScanResults);

        WifiManager wifiManager = (WifiManager) this.getSystemService(this.WIFI_SERVICE);

        WifiInfo info = wifiManager.getConnectionInfo();

        final List<ScanResult> results = wifiManager.getScanResults();

        if (results != null) {
            StringBuffer buf = new StringBuffer();

            for (int i = 0; i < results.size(); i++) {
                String ssid = results.get(i).SSID;
                if (ssid.startsWith("sv-")) {
                    buf.append(ssid + "\n");
                }
            }

            text.setText(buf.toString());
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

In activity_main.xml:

<TextView
        android:id="@+id/ScanResults"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • I have already added these permisions in my mainifest.xml before. – Mr Asker Apr 02 '15 at 07:41
  • It wasn't clear in your original question, but I see from the comments that you want to display a list of the available SSIDs. As your code is now, it successfully displays the one SSID that the device is connected to (if connected). The link @Fahim posted looks like it should get you on the right track for what you need. – Daniel Nugent Apr 02 '15 at 07:50
  • I want to check wether specific SSID is avialable (and not connected) in my wifi list and display it in my xml file. As I commeted before, the SSID begins with the "vs-" chracters and number between 400-500 as "sv-456". I do not need to display all the available wifi list on my device just the specific one for example "sv-456" – Mr Asker Apr 02 '15 at 10:03
  • @MrAsker I just updated the answer with a simple example, this should be enough to get you started. – Daniel Nugent Apr 02 '15 at 17:28