Android: Scanning Wifi Network + Selectable list

前端 未结 2 1080
青春惊慌失措
青春惊慌失措 2021-01-02 17:33

I\'m trying to creat an application that let you scan the available wifi networks, and then display them in a selectable list. To do that I tried with this code:

<         


        
相关标签:
2条回答
  • 2021-01-02 18:16

    Please try below one. Working well, but not tested properly. Please optimise the code yourself.

    public class MainActivity extends Activity {
    
    
        WifiManager mainWifi;
        WifiReceiver receiverWifi;
    
        StringBuilder sb = new StringBuilder();
    
        private final Handler handler = new Handler();
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    
            receiverWifi = new WifiReceiver();
            registerReceiver(receiverWifi, new IntentFilter(
                    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            if (mainWifi.isWifiEnabled() == false) {
                mainWifi.setWifiEnabled(true);
            }
    
    
            doInback();
        }
    
        public void doInback() {
            handler.postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    
                    if (receiveWifi == null)
                        receiverWifi = new WifiReceiver();
                    registerReceiver(receiverWifi, new IntentFilter(
                            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                    mainWifi.startScan();
                    doInback();
                }
            }, 1000);
    
        }
    
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 0, 0, "Refresh");
            return super.onCreateOptionsMenu(menu);
        }
    
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
            mainWifi.startScan();
    
            return super.onMenuItemSelected(featureId, item);
        }
    
    
        @Override
        protected void onPause() {
            unregisterReceiver(receiverWifi);
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            registerReceiver(receiverWifi, new IntentFilter(
                    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            super.onResume();
        }
    
        class WifiReceiver extends BroadcastReceiver {
            public void onReceive(Context c, Intent intent) {
    
                ArrayList<String> connections = new ArrayList<String>();
                ArrayList<Float> Signal_Strenth = new ArrayList<Float>();
    
                sb = new StringBuilder();
                List<ScanResult> wifiList;
                wifiList = mainWifi.getScanResults();
                for (int i = 0; i < wifiList.size(); i++) {
    
                    connections.add(wifiList.get(i).SSID);
                }
    
    
            }
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-02 18:28

    Doesn't doInback() create multiple WifiReceivers which then will end up in an Exception as they might not all get unregistered?

    Maybe s.th. like this could fix that:

    if (receiveWifi==null) {
       receiveWifi = new WifiReceiver();
    }
    
    0 讨论(0)
提交回复
热议问题