Android Wifi 扫描 - SCAN_RESULTS_AVAILABLE_ACTION 的 BroadcastReceiver 未被调用

Android Wifi Scan - BroadcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION not getting called

这是我的代码:

public class FloatWifiManager implements IWifiManager {

    private WifiManager wifiManager;

    private BroadcastReceiver wifiScanReceiver;

    public FloatWifiManager(Context context) {
        ...
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        // Registering Wifi Receiver
        wifiScanReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent intent) {
                if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                    // not getting called, only after running app and manually going to the wifi settings in android
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        context.registerReceiver(wifiScanReceiver, intentFilter);
        wifiManager.startScan();
    }

我完全按照我在所有示例中看到的方式注册了 BroadcastReceiver,并且做了 startScan

发生的事情是,wifi 列表 正在更改(当然,我测试过),但如果我只是留在应用程序中,则不会调用 onReceive

是什么让 onReceive 最终被调用 - 是启动应用程序,离开它 运行,然后进入 android phone 到 设置 -> Wifi 设置。去那里时,列表突然更新并调用 onReceive

这里有什么问题?

  1. wifiManager.startScan(); 是否只运行一次扫描?或者它是一个不断监听传入的函数 "Scan Results"?

  2. 显然,为什么接收者没有被调用?

是的,startScan() 只请求一次扫描。

您可以摆脱 if (intent.getAction().equals(..)) 条件。其他似乎都还可以。

just to make it clear - my goal to have a receiver that will get called every time the Wifi networks list are changing, without having to click a "start scan" button.

据我所知,任何 wifi 网络发生变化时都无法收到通知。您只能使用 startScan 请求扫描 - 当然您可以使用线程或处理程序重复调用 startScan

docs say that SCAN_RESULTS_AVAILABLE_ACTION is called when "an access point scan has completed, and results are available from the supplicant". How and when a scan is proceeded depends on the implemention of the supplicant. Elenkov 写道,"Android devices rarely include the original wpa_supplicant code; the included implementation is often modified for better compatibility with the underlying SoC".


扫描访问点

此示例扫描可用的接入点和临时网络。 btnScan 激活由 WifiManager.startScan() 方法启动的扫描。扫描后,WifiManager调用SCAN_RESULTS_AVAILABLE_ACTION意图,WifiScanReceiverclass处理扫描结果。结果显示在 TextView.

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "MainActivity";

    TextView txtWifiInfo;
    WifiManager wifi;
    WifiScanReceiver wifiReceiver;

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

        wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        wifiReceiver = new WifiScanReceiver();

        txtWifiInfo = (TextView)findViewById(R.id.txtWifiInfo);
        Button btnScan = (Button)findViewById(R.id.btnScan);
        btnScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "Start scan...");
                wifi.startScan();
            }
        });
    }

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

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

    private class WifiScanReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifi.getScanResults();
            txtWifiInfo.setText("");
            for(int i = 0; i < wifiScanList.size(); i++){
                String info = ((wifiScanList.get(i)).toString());
                txtWifiInfo.append(info+"\n\n");
            }
        }
    }
}

权限

需要在AndroidManifest.xml中定义以下权限:

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

android.permission.ACCESS_WIFI_STATE 是调用 WifiManager.getScanResults() 所必需的。如果没有 android.permission.CHANGE_WIFI_STATE,您将无法使用 WifiManager.startScan() 启动扫描。

为 api 23 级或更高级别(Android 6.0 及更高版本)编译项目时,必须插入 android.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_COARSE_LOCATION。此外,需要请求许可,例如在 onCreate 方法中 activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    String[] PERMS_INITIAL={
            Manifest.permission.ACCESS_FINE_LOCATION,
    };
    ActivityCompat.requestPermissions(this, PERMS_INITIAL, 127);
}