0

I am currently working on a simple Wi-fi scanner android application with min API level 26 and target API level 28.

I want real time update in scan results so i have created a broadcast receiver but it is not working as intended.

Note: I have already tried Wifi scan results broadcast receiver not working, Broadcast receiver with wifi scan not working

PLEASE NOTE THAT I WANT EXPLICIT BROADCAST RECEIVER NOT VIA MANIFEST FILE

I will be grateful to you.

Below is my java code:

package com.example.quickshare;
import androidx.appcompat.app.AppCompatActivity;
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.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

public class ActivitySend extends AppCompatActivity {

    WifiManager wifiManager;
    ListView ScanList;
    List<ScanResult> results;
    ListAdapter listAdapter;
    WifiReceiver wifiReceiver;
    IntentFilter intentFilter;
    TextView msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        CheckWifiStatus();
        msg = findViewById(R.id.wifiStatus);
        intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

        intentFilter.addAction(WifiManager.EXTRA_RESULTS_UPDATED);

        try {
            getApplicationContext().registerReceiver(wifiReceiver, intentFilter);
        }
        catch(Exception e){
            System.out.println(e);
        }
        boolean success = wifiManager.startScan();
        if(success)
            Toast.makeText(ActivitySend.this, "Scanning", Toast.LENGTH_SHORT).show();

    }

    @Override
    protected void onResume() {
        super.onResume();
        CheckWifiStatus();
        registerReceiver(wifiReceiver, intentFilter);

        wifiManager.startScan();
        results = wifiManager.getScanResults();
        if (results.size() > 0)
             Toast.makeText(ActivitySend.this, "Scan Successful", Toast.LENGTH_LONG).show();
        else
             Toast.makeText(ActivitySend.this, "No Device Available", Toast.LENGTH_LONG).show();
             ScanList = findViewById(R.id.ScanList);
             listAdapter = new ListAdapter(getApplicationContext(), results);
             ScanList.setAdapter(listAdapter);
             ScanList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   Toast.makeText(ActivitySend.this, "Selected" + results.get(position).SSID, Toast.LENGTH_LONG).show();
                   //TODO: Establish Connection with selected SSID
                }
             });
        }

    class WifiReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(ActivitySend.this,"Available Device list changed",Toast.LENGTH_LONG).show();
            //TODO: Append SSID of new Available APs in ListView and arrange a callback to onResume().
        }
    }

    public void CheckWifiStatus(){
        if (!wifiManager.isWifiEnabled()){
            wifiManager.setWifiEnabled(true);
            Toast.makeText(ActivitySend.this, "Wifi turned 'On' Successfully", Toast.LENGTH_SHORT).show();
            msg.setText("Wifi Status : ON");
        }
    }

    @Override
    protected void onPause() {
        unregisterReceiver(wifiReceiver);
        super.onPause();
    }
}

Using Above java code i can scan available APs if they are available before launching the activity. After Launching this activity nothing changes in scan result and it keep showing previously fetched results even if i turn off that AP.

1 Answers1

0

In order to detect your AP being disconnected, your intentFilter is lacking the ConnectivityManager.CONNECTIVITY_ACTION.

You can listen to these action with the following line:

intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

Also, you need to add brackets to your else code blocks, i.e.

if {
    // ...
} else {

    Toast.makeText(ActivitySend.this, "No Device Available", Toast.LENGTH_LONG).show();
    ScanList = findViewById(R.id.ScanList);
    // ...
}
matdev
  • 4,115
  • 6
  • 35
  • 56
  • Thank you for your reply but i have not yet reached the connection phase i just want to list available APs. I'll be grateful if you can help with that and once i reach the connection phase i'll surely try your suggestion. – LakshaySingh Sep 01 '20 at 15:10
  • well in your onReceive() method, you should update the results attribute, or refresh it using wifiManager.getScanResults(); – matdev Sep 01 '20 at 15:20
  • This is where the actual problem lies, onReceive() function is not working. or if i rephrase that BroadcastReceiver is not working at all. – LakshaySingh Sep 01 '20 at 16:58