wifiManager.getScanResults()返回0

问题描述 投票:4回答:1

我想开发一个软件,用处理程序线程在后台扫描可用的wifi网络。问题是当我使用handler.postDelayed()时,WifiManager.getScanResults()返回0同时在正常情况下它的工作。

这些是运行时权限:

@Override
protected void onResume() {
    super.onResume();
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 87);
        }
    }
}

我的经纪人:

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                counter++;
                scanWifi();
                scannWifiInterval();
                if(counter==5){
                    sum/=5;
                    textView2.setText(Integer.toString(sum));
                }
            }
        },timeoutSecondGenerator());

和scanWifi方法:

private void scanWifi() {
        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (!wifiManager.isWifiEnabled()) {
            Toast.makeText(this, "WiFi is disabled ... We need to enable it", Toast.LENGTH_LONG).show();
            wifiManager.setWifiEnabled(true);

        }
        arrayList.clear();
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();

        Toast.makeText(this, "Scanning WiFi ...", Toast.LENGTH_SHORT).show();
    }

BroadCastReciever

BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {


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



                for(int i=0;i<results.size();i++){

                }
                for (ScanResult scanResult : results) {
                    if (scanResult.SSID.equals(networkSSID)) {
                        sum+=scanResult.level;
                        textView.setText(Integer.toString(scanResult.level));

                    }


                }

            }
        }
    };

logcat的:

09-16 16:36:47.384 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.454 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.454 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.454 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.454 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.454 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.454 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.474 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.474 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:36:47.474 2536-2536/? E/ViewRootImpl: mStopped=false mHasWindowFocus=false mPausedForTransition=false
09-16 16:37:02.169 1478-1849/? E/WifiStateMachine: result: -1266999764, Moved BSSID: 80:2a:a8:9a:ea:da
09-16 16:37:02.269 2681-2692/? E/Auth: [GoogleAccountDataServiceImpl] getToken() -> NETWORK_ERROR. Account: <ELLIDED:905778253>, App: com.google.android.googlequicksearchbox, Service: oauth2:https://www.googleapis.com/auth/googlenow
    pez: Error when calling server.
        at gnx.c(:com.google.android.gms@[email protected] (040304-211705629):279)
        at gnx.a(:com.google.android.gms@[email protected] (040304-211705629):24)
        at gnx.b(:com.google.android.gms@[email protected] (040304-211705629))
        at gmi.a(:com.google.android.gms@[email protected] (040304-211705629):34)
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):13)
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):148)
        at iet.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):16)
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):6)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):33)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):69)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):254)
        at wwt.a(:com.google.android.gms@[email protected] (040304-211705629):3)
        at cgv.onTransact(:com.google.android.gms@[email protected] (040304-211705629):5)
        at android.os.Binder.transact(Binder.java:387)
        at djf.onTransact(:com.google.android.gms@[email protected] (040304-211705629):3)
        at android.os.Binder.execTransact(Binder.java:453)
     Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer
        at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
        at com.google.android.gms.org.conscrypt.NativeSsl.doHandshake(:com.google.android.gms@[email protected] (040304-211705629):8)
        at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@[email protected] (040304-211705629):15)
        at com.google.android.gms.common.net.SSLCertificateSocketFactory.verifyHostname(:com.google.android.gms@[email protected] (040304-211705629):2)
        at com.google.android.gms.common.net.SSLCertificateSocketFactory.createSocket(:com.google.android.gms@[email protected] (040304-211705629):8)
        at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:188)
        at com.android.okhttp.Connection.connect(Connection.java:1324)
        at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1412)
        at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:131)
        at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:481)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:462)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:368)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
        at yod.a(:com.google.android.gms@[email protected] (040304-211705629):32)
        at imu.a(:com.google.android.gms@[email protected] (040304-211705629):1)
        at gnj.a(:com.google.android.gms@[email protected] (040304-211705629):6)
        at gnj.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at gnx.c(:com.google.android.gms@[email protected] (040304-211705629):109)
        at gnx.a(:com.google.android.gms@[email protected] (040304-211705629):24) 
        at gnx.b(:com.google.android.gms@[email protected] (040304-211705629)) 
        at gmi.a(:com.google.android.gms@[email protected] (040304-211705629):34) 
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):13) 
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):148) 
        at iet.a(:com.google.android.gms@[email protected] (040304-211705629):2) 
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):16) 
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):6) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):33) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):69) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):254) 
        at wwt.a(:com.google.android.gms@[email protected] (040304-211705629):3) 
        at cgv.onTransact(:com.google.android.gms@[email protected] (040304-211705629):5) 
        at android.os.Binder.transact(Binder.java:387) 
        at djf.onTransact(:com.google.android.gms@[email protected] (040304-211705629):3) 
        at android.os.Binder.execTransact(Binder.java:453) 
09-16 16:37:02.419 17764-17764/? E/Zygote: no v2
09-16 16:37:02.489 2681-17697/? E/Auth: [GoogleAccountDataServiceImpl] getToken() -> NETWORK_ERROR. Account: <ELLIDED:905778253>, App: com.google.android.googlequicksearchbox, Service: oauth2:https://www.googleapis.com/auth/googlenow
    pez: Error when calling server.
        at gnx.c(:com.google.android.gms@[email protected] (040304-211705629):279)
        at gnx.a(:com.google.android.gms@[email protected] (040304-211705629):24)
        at gnx.b(:com.google.android.gms@[email protected] (040304-211705629))
        at gmi.a(:com.google.android.gms@[email protected] (040304-211705629):34)
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):13)
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):148)
        at iet.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):16)
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):6)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):33)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):69)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):254)
        at wwt.a(:com.google.android.gms@[email protected] (040304-211705629):3)
        at cgv.onTransact(:com.google.android.gms@[email protected] (040304-211705629):5)
        at android.os.Binder.transact(Binder.java:387)
        at djf.onTransact(:com.google.android.gms@[email protected] (040304-211705629):3)
        at android.os.Binder.execTransact(Binder.java:453)
     Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer
        at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
        at com.google.android.gms.org.conscrypt.NativeSsl.doHandshake(:com.google.android.gms@[email protected] (040304-211705629):8)
        at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@[email protected] (040304-211705629):15)
        at com.google.android.gms.common.net.SSLCertificateSocketFactory.verifyHostname(:com.google.android.gms@[email protected] (040304-211705629):2)
        at com.google.android.gms.common.net.SSLCertificateSocketFactory.createSocket(:com.google.android.gms@[email protected] (040304-211705629):8)
        at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:188)
        at com.android.okhttp.Connection.connect(Connection.java:1324)
        at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1412)
        at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:131)
        at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:481)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:462)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:368)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
        at yod.a(:com.google.android.gms@[email protected] (040304-211705629):32)
        at imu.a(:com.google.android.gms@[email protected] (040304-211705629):1)
        at gnj.a(:com.google.android.gms@[email protected] (040304-211705629):6)
        at gnj.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at gnx.c(:com.google.android.gms@[email protected] (040304-211705629):109)
        at gnx.a(:com.google.android.gms@[email protected] (040304-211705629):24) 
        at gnx.b(:com.google.android.gms@[email protected] (040304-211705629)) 
        at gmi.a(:com.google.android.gms@[email protected] (040304-211705629):34) 
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):13) 
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):148) 
        at iet.a(:com.google.android.gms@[email protected] (040304-211705629):2) 
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):16) 
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):6) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):33) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):69) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):254) 
        at wwt.a(:com.google.android.gms@[email protected] (040304-211705629):3) 
        at cgv.onTransact(:com.google.android.gms@[email protected] (040304-211705629):5) 
        at android.os.Binder.transact(Binder.java:387) 
        at djf.onTransact(:com.google.android.gms@[email protected] (040304-211705629):3) 
        at android.os.Binder.execTransact(Binder.java:453) 
09-16 16:37:02.499 2400-17412/? E/VelvetNetworkClient: Failed to get auth token
09-16 16:37:02.589 2400-17412/? E/UpdateGcmReg: Network error while trying to register for GCM updates
09-16 16:37:03.320 2681-4954/? E/Auth: [GoogleAccountDataServiceImpl] getToken() -> NETWORK_ERROR. Account: <ELLIDED:905778253>, App: com.google.android.instantapps.supervisor, Service: oauth2:https://www.googleapis.com/auth/playatoms
    pez: Error when calling server.
        at gnx.c(:com.google.android.gms@[email protected] (040304-211705629):279)
        at gnx.a(:com.google.android.gms@[email protected] (040304-211705629):24)
        at gnx.b(:com.google.android.gms@[email protected] (040304-211705629))
        at gmi.a(:com.google.android.gms@[email protected] (040304-211705629):34)
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):13)
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):148)
        at iet.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):16)
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):6)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):33)
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):69)
        at gjc.a(:com.google.android.gms@[email protected] (040304-211705629):4)
        at gjb.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at gjb.c(:com.google.android.gms@[email protected] (040304-211705629):6)
        at gja.a(:com.google.android.gms@[email protected] (040304-211705629):1)
        at gkz.getAuthToken(:com.google.android.gms@[email protected] (040304-211705629):8)
        at android.accounts.AbstractAccountAuthenticator$Transport.getAuthToken(AbstractAccountAuthenticator.java:214)
        at android.accounts.IAccountAuthenticator$Stub.onTransact(IAccountAuthenticator.java:113)
        at android.os.Binder.transact(Binder.java:387)
        at djf.onTransact(:com.google.android.gms@[email protected] (040304-211705629):3)
        at android.os.Binder.execTransact(Binder.java:453)
     Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer
        at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
        at com.google.android.gms.org.conscrypt.NativeSsl.doHandshake(:com.google.android.gms@[email protected] (040304-211705629):8)
        at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@[email protected] (040304-211705629):15)
        at com.google.android.gms.common.net.SSLCertificateSocketFactory.verifyHostname(:com.google.android.gms@[email protected] (040304-211705629):2)
        at com.google.android.gms.common.net.SSLCertificateSocketFactory.createSocket(:com.google.android.gms@[email protected] (040304-211705629):8)
        at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:188)
        at com.android.okhttp.Connection.connect(Connection.java:1324)
        at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1412)
        at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:131)
        at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:481)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:462)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:368)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
        at yod.a(:com.google.android.gms@[email protected] (040304-211705629):32)
        at imu.a(:com.google.android.gms@[email protected] (040304-211705629):1)
        at gnj.a(:com.google.android.gms@[email protected] (040304-211705629):6)
        at gnj.a(:com.google.android.gms@[email protected] (040304-211705629):2)
        at gnx.c(:com.google.android.gms@[email protected] (040304-211705629):109)
        at gnx.a(:com.google.android.gms@[email protected] (040304-211705629):24) 
        at gnx.b(:com.google.android.gms@[email protected] (040304-211705629)) 
        at gmi.a(:com.google.android.gms@[email protected] (040304-211705629):34) 
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):13) 
        at ift.a(:com.google.android.gms@[email protected] (040304-211705629):148) 
        at iet.a(:com.google.android.gms@[email protected] (040304-211705629):2) 
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):16) 
        at ieq.a(:com.google.android.gms@[email protected] (040304-211705629):6) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):33) 
        at dhb.a(:com.google.android.gms@[email protected] (040304-211705629):69) 
        at gjc.a(:com.google.android.gms@[email protected] (040304-211705629):4) 
        at gjb.a(:com.google.android.gms@[email protected] (040304-211705629):2) 
        at gjb.c(:com.google.android.gms@[email protected] (040304-211705629):6) 
        at gja.a(:com.google.android.gms@[email protected] (040304-211705629):1) 
        at gkz.getAuthToken(:com.google.android.gms@[email protected] (040304-211705629):8) 
        at android.accounts.AbstractAccountAuthenticator$Transport.getAuthToken(AbstractAccountAuthenticator.java:214) 
        at android.accounts.IAccountAuthenticator$Stub.onTransact(IAccountAuthenticator.java:113) 
        at android.os.Binder.transact(Binder.java:387) 
        at djf.onTransact(:com.google.android.gms@[email protected] (040304-211705629):3) 
        at android.os.Binder.execTransact(Binder.java:453) 
09-16 16:37:03.350 17542-17787/? E/Isotope: UID: [10104]  PID: [17542] HygieneTask : GetArchiveDownload RPC failed
    eer: UNAUTHENTICATED
        at bai.b(PG:6)
        at baf.call(PG:9)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at bke.run(PG)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.io.IOException: NetworkError
        at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2138)
        at android.accounts.AccountManager.-wrap0(AccountManager.java)
        at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1996)
        at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
        at android.os.Binder.execTransact(Binder.java:453)
09-16 16:37:03.350 17542-17789/? E/Isotope: UID: [10104]  PID: [17542] HygieneTaskService : Archive Prefetching failed
    eer: UNAUTHENTICATED
        at bai.b(PG:6)
        at baf.call(PG:9)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at bke.run(PG)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.io.IOException: NetworkError
        at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2138)
        at android.accounts.AccountManager.-wrap0(AccountManager.java)
        at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1996)
        at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
        at android.os.Binder.execTransact(Binder.java:453)
09-16 16:37:03.370 2681-14277/? E/NetworkScheduler: Invalid component specified.
09-16 16:37:03.390 2681-14277/? E/NetworkScheduler: Invalid component specified.
09-16 16:37:03.420 1478-1632/? E/NativeLibraryHelper: Failed to load assets verifier: 0
09-16 16:37:03.700 1478-1632/? E/NativeLibraryHelper: Failed to load assets verifier: 0

继续扫描,但一无所获

minSdkVersion 17 targetSdkVersion 28

我在三星galaxy s9上运行它

如果有人回答,我将不胜感激。

java android handler android-wifi wifimanager
1个回答
0
投票

最后我找到了问题。我刚刚将targetSdkVersion从28改为22并且有效!我认为在一些Android设备上运行时权限不是答案,你需要在清单文件上设置它,而在sdk 28程序中只设置运行时权限。

© www.soinside.com 2019 - 2024. All rights reserved.