1

I'm trying to search for available wifi networks but nothing happens

this is my code

After Executing my code no error occurs.

I'm new to using BroadcastReceiver and I think that the problem in registering the Receiver.

public class MainActivity extends AppCompatActivity {

public class WifiReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        wifilist = wifi.getScanResults();

        txt.setText(wifilist.get(0).toString());

    }
}

WifiManager wifi;
WifiReceiver receiver;
List<ScanResult> wifilist;

String result = "";

TextView txt;


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

    txt = (TextView) findViewById(R.id.txt) ;

    wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    if(!wifi.isWifiEnabled())
    {
        Toast.makeText(getApplicationContext(), "Wifi is Disabaled .. enabled it", Toast.LENGTH_LONG ).show();
        boolean b = wifi.setWifiEnabled(true);
    }



    receiver = new WifiReceiver();
    registerReceiver(receiver, new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi.startScan();


}

@Override
protected void onResume() {

    registerReceiver(receiver, new 
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

    super.onResume();
}

@Override
protected void onPause() {

    unregisterReceiver(receiver);

    super.onPause();
}


}

i made the TextView named txt show just the first network but it's name doesn't change

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Lakhdar
  • 377
  • 2
  • 8
  • Make sure that your app has Location permission, and also that Location setting is enabled: https://stackoverflow.com/questions/40051204/android-studio-getscanresults-returns-empty-but-permission-is-given – Daniel Nugent Oct 06 '17 at 20:35
  • I've add every location permission there is and enabled location in my phone, still no change – Lakhdar Oct 06 '17 at 20:59
  • I just tried your code, it works fine for me. make sure that you're requesting Location permission at runtime: https://stackoverflow.com/questions/40142331/how-to-request-location-permission-on-android-6 – Daniel Nugent Oct 06 '17 at 21:08
  • I don't Know how to request Location permission at runtime – Lakhdar Oct 06 '17 at 21:09
  • See here: https://stackoverflow.com/questions/40142331/how-to-request-location-permission-on-android-6 – Daniel Nugent Oct 06 '17 at 21:09

0 Answers0