Android Wifi 扫描仪未在列表视图中显示结果

Android Wifi Scanner NOT SHOWING RESULTS IN LISTVIEW

我正在编写一个 wifi 扫描器来扫描可用网络并连接到它们。我的代码编译没有问题,应用程序运行,但扫描结果未显示在列表视图中。请帮忙。即使是触发吐司的按钮点击也会按预期运行。

Main.java

package com.example.wifiscanner;

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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private WifiManager wifiManager;
    private ListView listView;
    private Button button;
    private int size = 0;
    private List<ScanResult> results;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ArrayAdapter arrayAdapter;

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

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                scanWifi();
            }
        });

        listView = findViewById(R.id.listview);
        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (!wifiManager.isWifiEnabled()) {

            Toast.makeText(this,"Wifi is disabled",Toast.LENGTH_SHORT).show();
            wifiManager.setWifiEnabled(true);

        }

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);
        scanWifi();
    }

    private void scanWifi() {

        arrayList.clear();
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();
        Toast.makeText(this,"Scanning for Networks", Toast.LENGTH_SHORT).show();
    }


    BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            results = wifiManager.getScanResults();
            unregisterReceiver(this);

            for (ScanResult scanResult : results){
                arrayList.add(scanResult.SSID + scanResult.capabilities);
                arrayAdapter.notifyDataSetChanged();
            }
        }
    };
}

我正在编写一个 wifi 扫描器来扫描可用网络并连接到它们。我的代码编译没有问题,应用程序运行,但扫描结果未显示在列表视图中。请帮忙。即使是触发吐司的按钮点击也会按预期运行。

@AfterPermissionGranted(123)
    public void scanWifi() {

        String[] perm = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.CHANGE_WIFI_STATE};

        if(EasyPermissions.hasPermissions(this,perm)){

            registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            boolean scanStarted = wifiManager.startScan();

            if (scanStarted) {
                Toast.makeText(this, "Scanning true", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "Scanning false", Toast.LENGTH_SHORT).show();
            }
        Toast.makeText(this,"PERMISSIONS HAVE BEEN GRANTED",Toast.LENGTH_SHORT).show();

        }else{
            EasyPermissions.requestPermissions(this,"WE REQUIRE THESE PERMISSIONS FOR THE APP TO FUNCTION",123,perm);
        }

Gradle 依赖添加

“实现'pub.devrel:easypermissions:2.0.1'”并编译

使用简单的权限库我能够解决问题并引入更易于维护的代码。