Android Wifi扫描-无法调用SCAN_RESULTS_AVAILABLE_ACTION的BroadcastReceiver [英] Android Wifi Scan - BroadcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION not getting called

查看:1121
本文介绍了Android Wifi扫描-无法调用SCAN_RESULTS_AVAILABLE_ACTION的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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.

I registered the BroadcastReceiver exactly like I saw in all the examples, and did startScan.

发生的事情是, wifi列表正在更改(可以肯定地说,我已经测试过),但是如果我只留在应用程序中,则不会调用onReceive.

What happens is, the wifi list is changing (for sure, I tested), but onReceive is not called if I just stay in the app.

使onReceive最终被调用的原因是-启动该应用,使其保持运行状态,然后在android手机中进入设置-> Wifi设置.到那里时,列表突然更新,并调用onReceive.

What makes onReceive finally to be called - is to launch the app, leave it running, and going in the android phone to Settings -> Wifi settings. when going there, all of the sudden the List is updating and onReceive is called.

这是什么问题?

  1. wifiManager.startScan();仅运行一次扫描吗?还是一直在监听传入的扫描结果"的功能?

  1. Does wifiManager.startScan(); runs the scan only once? or it is a function that keeps listening to incoming "Scan Results"?

很显然,为什么没有收到接收方电话?

And obviously, why does the receiver doesn't get called?

推荐答案

是的,startScan()仅请求一次扫描.

Yes, startScan() requests only one single scan.

您可以摆脱if (intent.getAction().equals(..))条件.似乎没有其他事情了.

You can get rid of the if (intent.getAction().equals(..)) condition. Anything else seems to be ok.

为了明确起见-我的目标是拥有一个能够 每次Wifi网络列表更改时调用,而无需 单击开始扫描"按钮.

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.

AFAIK,无论何时任何wifi网络发生变化,都无法获得通知.您只能使用startScan请求扫描-当然,您可以使用线程或处理程序重复调用startScan.

AFAIK it is not possible to get notified whenever any of the wifi networks change. You can only request a scan with startScan - and of course you can call startScan repeatedly using a Thread or Handler.

文档SCAN_RESULTS_AVAILABLE_ACTION访问点扫描已完成,并且请求者可以得到结果" 时调用.进行扫描的方式和时间取决于请求方的执行方式. Elenkov 写道,"Android设备很少包含原始的wpa_supplicant代码;通常对包含的实现进行修改,以更好地与基础SoC兼容..

The 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 writes, that "Android devices rarely include the original wpa_supplicant code; the included implementation is often modified for better compatibility with the underlying SoC".

扫描接入点

此示例扫描可用的接入点和ad hoc网络. btnScan激活由WifiManager.startScan()方法启动的扫描.扫描后,WifiManager调用SCAN_RESULTS_AVAILABLE_ACTION意图,并且WifiScanReceiver类处理扫描结果.结果显示在TextView中.

This example scans for available access points and ad hoc networks. btnScan activates a scan initiated by the WifiManager.startScan() method. After the scan, WifiManager calls the SCAN_RESULTS_AVAILABLE_ACTION intent and the WifiScanReceiver class processes the scan result. The results are displayed in a 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 中定义以下权限:

The following permissions need to be defined in 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()发起扫描.

android.permission.ACCESS_WIFI_STATE is necessary for calling WifiManager.getScanResults(). Without android.permission.CHANGE_WIFI_STATE you cannot initiate a scan with WifiManager.startScan().

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

When compiling the project for api level 23 or greater (Android 6.0 and up), either android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_COARSE_LOCATION must be inserted. Furthermore that permission needs to be requested, e.g. in the onCreate method of your main activity:

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

这篇关于Android Wifi扫描-无法调用SCAN_RESULTS_AVAILABLE_ACTION的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆