WifiManager startScan()仅在第一次工作

时间:2018-09-18 03:14:11

标签: android raspberry-pi wifi wifimanager

我的问题很简单。我使用wifiManager扫描网络,在SCAN_RESULTS_AVAILABLE_ACTION广播接收器中获取结果,然后使用该信息在设备选择页面上连接到我要连接的网络。我目前正在连接到Raspberry Pi上的WAP并在其上ping Web服务器。

我遇到的问题是再次扫描。我有一个按钮,可以按此按钮刷新我可以连接的区域中的设备。我第二次运行该广播时,没有收到任何广播。

这是我的广播接收者:

public class WifiScanReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            devicesInRange.clear();
            Log.i("WifiRunner", "Scan received!");
            List<ScanResult> scanResults = wifiManager.getScanResults();

            for(ScanResult s : scanResults){
                Log.i("WifiRunner", s.toString());
                if(s.BSSID != null && s.BSSID.startsWith("b8:27:eb")){
                    devicesInRange.add(new Device(s.BSSID, s.SSID, ""));
                    Log.i("WifiRunner", "Adding RaspberryPi mac: " + s.BSSID + " with hostName: " + s.SSID);
                }
            }
            needsScan = false;
            isScanning = false;
        }
    }
}

在这里,我根据结果连接到wifiConnection:

 /**
 * Connect to a specific network
 */
private void connectToWifi(){

    if(lastDevice == null){
        Log.i("WifiRunner", "LastDevice is null");
        connectStatus = ConnectStatus.WAITING_FOR_USER;
        sendIntent("status");
        return;
    }
    String networkSSID = lastDevice.getHostName();
    String networkPass = lastDevice.getPassWord();
    String networkMac = lastDevice.getMacAddress();
    Boolean exists = false;

    Log.i("WifiRunner", "connectToWifi: hostName: " + networkSSID + ", password: " + networkPass + ", macAddress: " + networkMac);
    WifiInfo info = wifiManager.getConnectionInfo();
    Log.i("WifiRunner", "connectToWifi: myCurrentHostName: " + info.getSSID() + ", myBSSID: " + info.getBSSID() + ", myMACADDY: " + info.getMacAddress());
    if(info.getBSSID().equals(networkMac) && info.getSSID().split("\"")[1].equals(networkSSID)){
        Log.i("WifiRunner", "Already connected to device!");
        return;
    }

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
            Log.i("WifiRunner", "In configured networks!!  " + i.SSID);
            wifiManager.disconnect();
            wifiManager.enableNetwork(i.networkId, true);
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            exists = true;
            break;
        }
    }

    if(!exists) {
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"" + networkSSID + "\"";
        wc.preSharedKey = "\"" + networkPass + "\"";
        wc.hiddenSSID = true;
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        int res = wifiManager.addNetwork(wc);

        wifiManager.disconnect();
        Log.i("WifiRunner", "add Network returned " + res);
        boolean b = wifiManager.enableNetwork(res, true);
        Log.i("WifiRunner", "enableNetwork returned " + b);

        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我目前有一个状态机,它将在特定状态下调用这些状态机。因此,在这种情况下,我的状态是SEARCHING,这将运行:

if (connectStatus == ConnectStatus.SEARCHING) {
                    Log.i("WifiRunner", "SEARCHING!");
                    if(needsScan && !isScanning) {
                        if(!wifiManager.isWifiEnabled()){
                            Log.i("WifiRunner", "Enabling wifi for searching");
                            wifiManager.setWifiEnabled(true);
                        }
                        Log.i("WifiRunner", "Starting Scan!");
                        Log.i("WifiRunner", "Scan result: " + wifiManager.startScan());
                        isScanning = true;
                    } else if(!isScanning && !needsScan){
                        if (firstConnect) {
                            Log.i("WifiRunner", "CONNECT_TO_LAST");
                            connectStatus = ConnectStatus.CONNECT_TO_LAST;
                            sendIntent("status");
                        } else {
                            connectStatus = ConnectStatus.WAITING_FOR_USER;
                            sendIntent("data");
                            sendIntent("status");
                        }
                    }
                }

但是它陷入了SEARCHING状态,等待来自startScan()调用的响应。为了第二个startScan()工作,我需要做些什么吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的方法,但我认为可能会有更好的方法。我最终在其自己的receive()中注销了接收器。然后在调用startScan()之前,我再次注册了接收者。

如果有人有更好的解决方案,请告诉我。我不太清楚这是什么问题。