wifiManager.getScanResult()返回空值

时间:2019-05-30 13:50:10

标签: java android wifimanager

我正在搜索可用的wifi网络,但是方法getScanResults()返回空列表。

我包括了所需的所有权限:

android.permission.ACCESS_COARSE_LOCATION
android.permission.CHANGE_WIFI_STATE
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_WIFI_STATE

主要活动类别是:

public class Home extends Activity {`

    Context context;
    WifiManager wifiManager = null;
    WiFiReceiver wifiReceiver = null;

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

        context = this;
        wifiManager = (WifiManager) 
        context.getSystemService(Context.WIFI_SERVICE);
        wifiReceiver = new WiFiReceiver(wifiManager);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiReceiver, intentFilter);

        wifiManager.startScan();
        List<ScanResult> results = wifiReceiver.results;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(wifiReceiver);
    }
}

广播接收者是:

public class WiFiReceiver extends BroadcastReceiver {`

    public List<ScanResult> results;
    private WifiManager wifiManager;

    public WiFiReceiver(WifiManager wifiManager) {
        this.wifiManager = wifiManager;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);
        if (success) {
            results = wifiManager.getScanResults();
            Log.e("wiFi Manager", "Done");

        } else {
            Log.e("wiFi Manager", "Scan failure");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

问题是您假设startScan()将立即产生结果,但实际上它只是按照说的去做,开始扫描。您正在访问resultsonReceive之前的WiFiReceiver变量,这就是为什么它始终为空的原因(您的日志记录应该确认)。

您需要做的是像代码here一样,在准备就绪时使用回调获取结果。请注意,onReceive方法如何调用scanSuccess(),并且仅在scanSuccess()中访问结果,而不是在调用startScan()之后立即访问结果。 还要注意他们如何通过检查布尔值startScan()返回

来检查启动扫描是否实际上成功

答案 1 :(得分:0)

从 API 级别 23(Android 6.0 Marshmallow)开始,我们需要向用户端请求运行时许可。特别是 ACCESS_FINE_LOCATION。需要在wifiManager.startScan()之前检查是否授予权限,如果未授予权限,则需要调用requestPermissions()。

这是一个例子:

public void startScanningWifi(){if ( Build.VERSION.SDK_INT >= 23){

    if (ActivityCompat.checkSelfPermission(context, Manifest.
            permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(context,
            Manifest.permission.ACCESS_COARSE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED ){
        
        requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_CODE_ASK_PERMISSIONS);
        Log.i(TAG, "User location NOT ENABLED, waiting for permission");

    }else{
        //Start scanning for wifi
    }}

您还需要在活动中包含此方法

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //start scanning
                    wifiManager.startScan();
                } else {
                    // Permission for location Denied
                    Toast.makeText( this,"Well cant help you then!" ,
                            Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

不要忘记在您的活动中也声明这一点

private final int REQUEST_CODE_ASK_PERMISSIONS = 1;