Problem when scanning Wifi networks in Android Studio, it does not show me the SSID

1

I am using the following code but it does not show me the BSSID in the list of Wifi networks,

public class MainActivity extends AppCompatActivity {
ListView lv;
WifiManager wifi;
String wifis[];
WifiScanReceiver wifiReciever;

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

    lv=(ListView) findViewById(R.id.listview);
    wifi=(WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiReciever = new WifiScanReceiver();
    wifi.startScan();

    Button btn=(Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            wifi.startScan();
            Toast notificacion=Toast.makeText(MainActivity.this,"Actualizacion de parametros",Toast.LENGTH_SHORT);
            notificacion.show();
        }
    });    
}


protected void onPause(){
    unregisterReceiver(wifiReciever);
    super.onPause();
}

protected void onResume(){
    registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}


private class WifiScanReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent){

        WifiInfo nombre1 = wifi.getConnectionInfo();
        TextView name = (TextView) findViewById(R.id.textView);
        name.setText(nombre1.getSSID());

        List<ScanResult> wifiScanList = wifi.getScanResults();
        wifis = new String[wifiScanList.size()];

             for (int i=0; i<wifiScanList.size(); i++){            
            wifis[i]=((wifiScanList.get(i)).toString());
        }

        lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, wifis));
    }
}

It only shows me the list from 'capabilities' but the name of the network does not show it. Only using:

WifiInfo nombre1 = wifi.getConnectionInfo();
TextView name = (TextView) findViewById(R.id.textView);
name.setText(nombre1.getSSID());

Is that I can show in a TextView the name of the Wi-Fi Network but if I'm not connected to any network it does not show me the name.

On the other hand, the permissions that you declare in 'Android Manifest' are:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    
asked by Heidi Medina 27.07.2017 в 08:31
source

1 answer

1

What you want to get are the SSIDs in the WLAN and you can determine them in this way:

 WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> wifilist = wifiManager.getScanResults();
        for(int i=0;i<wifilist.size();i++) {
            Log.d("detectWIFI", "SSID: " +  wifilist.get(i).SSID );
        }

Regarding what you are saying:

  

Is that I can show in a TextView the name of the Wifi Network but if   I'm not connected to any network, it does not show me the name.

This is obvious, for example how you could get the properties of a WLAN where you are not connected!

In this question you can see the ways to get the SSID:

Problem when scanning Wi-Fi networks on Android

    
answered by 27.07.2017 в 12:07