java—有没有新的方法或方法来扫描访问点附近而不是wifimanager.startscan?

uyto3xhc  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(233)

api 28中不推荐使用wifimanager.startscanhttps://developer.android.com/reference/android/net/wifi/wifimanager#startscan()
你好!我的应用程序需要请求对附近的访问点进行扫描,wifimanager.startscan()可以正常工作,但现在我将targetsdkversion和compilesdkversion更改为api 29,我就不工作了。有没有其他方法可以做到这一点?应用程序只需要显示他们家中的ssid就可以选择它,并将这些数据发送到它将要尝试与他们的接入点连接的设备(iot)。此代码适用于targetsdkversion 28

final WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        final List<ScanResult> results = new ArrayList<>();

        wiFiScanResultsBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                for (ScanResult temp : wifiManager.getScanResults()){
                    if(!results.contains(temp)){
                        results.add(temp);
                    }
                }
            }
        };

        registerReceiver(wiFiScanResultsBroadcast,
                new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

        wifiManager.startScan();

        if(cdt1 != null) cdt1.cancel();

        ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<>(context,
                android.R.layout.simple_list_item_1);
        arrayAdapter2.add("Buscando Puntos de Acceso...");
        spinner.setAdapter(arrayAdapter2);

        cdt1 = new CountDownTimer(8000, 8000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {

                findViewById(R.id.reloadAPView).clearAnimation();
                final Spinner spinner = (Spinner) findViewById(R.id.spinnerSSID);

                final ArrayAdapter<String> arrayAdapter3 = new ArrayAdapter<>(context,
                        android.R.layout.simple_list_item_1);
                final int[] select = {0};

            /*if(results != null && results.size() == 1 && results.get(0).SSID.isEmpty()){
                results.clear();
            }*/

                if(results != null && results.size() > 0){

                    Collections.sort(results, new Comparator<ScanResult>() {                                   // Se ordenan los resultados de mayor a menor intensidad de señal.
                        @Override
                        public int compare(ScanResult lhs, ScanResult rhs) {
                            if(lhs.level > rhs.level){
                                return -1;
                            }else if(lhs.level < rhs.level){
                                return 1;
                            }else{
                                return 0;
                            }
                        }
                    });

                    for(ScanResult ap : results){

                        if(!ap.SSID.toString().startsWith("SmartHeat_")
                                && !ap.SSID.toString().startsWith("miCalorex_")
                                && !ap.SSID.toString().isEmpty()){
                            Log.d("SSID",":"+ap.SSID);
                            if(ap.frequency > 3000){
                                arrayAdapter3.add(ap.SSID + " (5GHz)");
                            }else{
                                arrayAdapter3.add(ap.SSID);
                            }
                        }
                    }
                }

                if(arrayAdapter3.getCount() <= 0){
                    arrayAdapter3.clear();
                    arrayAdapter3.add("No se encontraron redes");
                }

                spinner.setAdapter(arrayAdapter3);
                spinner.setSelection(0);
                /*if(arrayAdapter2.getCount() > 0){

                    boolean selectedOk = false;
                    for(int i = 0; i < arrayAdapter2.getCount(); i++){
                        if(!arrayAdapter2.getItem(i).contains("5GHz")){
                            spinner.setSelection(i);
                            selectedOk = true;
                            break;
                        }
                    }

                    if(!selectedOk){
                        spinner.setSelection(0);
                    }
                }*/
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        if(!adapterView.getItemAtPosition(i).toString().contains("Buscando Puntos de Acceso") &&
                                !adapterView.getItemAtPosition(i).toString().contains("No se encontraron redes")){
                            selectedAP[0] = adapterView.getItemAtPosition(i).toString();
                            verifiSsid(selectedAP);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

            }
        };
        cdt1.start();

我用api 28得到这个结果,相反,我在scanresult temp:wifimanager.getscanresults()debug中得到空响应

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题