android wifi范围变化

时间:2015-08-20 14:22:29

标签: android range wifi

我在android中开发了一个应用程序来检查WiFi连接。我想知道我的设备是否在特定的wifi热点范围内。我可以使用以下代码获取可用的wifi热点列表。

List<android.net.wifi.ScanResult> mScanResults = myWifiManager.getScanResults();

但在我的情况下,我们有一个非常大的办公室,有很多wifi热点。所以我连接到wifi_hp_1并移动,然后当我到达wifi_hp_2时,扫描没有发生。我已经注册了

的广播接收器
ConnectivityManager.CONNECTIVITY_ACTION

但扫描没有发生,如果我在热点1的范围内并且在热点2的范围内到达,但仍然可以获得hotspot1的信号。

对此有何帮助?

1 个答案:

答案 0 :(得分:0)

这就是我们在应用中处理它的方式:

private WifiManager wifi;

private BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (wifi.isWifiEnabled()) {
            ConnectivityManager mgr = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

            NetworkInfo info = mgr.getActiveNetworkInfo();
            if (info == null) {
                scanForWiFiNetworks();
                setScanningEnabled(true);

            } else if (!info.getState().equals(NetworkInfo.State.CONNECTED)) {
                setScanningEnabled(false);

                scanForWiFiNetworks();

            } else {
                scanForWiFiNetworks();
                setScanningEnabled(true);
            }
        }
    }
};

private BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (wifi.isWifiEnabled()) {
            setScanningEnabled(false);

            scan.sendEmptyMessageDelayed(0, 2500);
        }
    }
};

private Handler scan = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        setScanningEnabled(false);
        scanForWiFiNetworks();
    }
};

private Handler reenableScanning = new Handler() {
    public void handleMessage(Message msg) {
        reenableScanning.removeCallbacksAndMessages(null);
        setScanningEnabled(true);
    };
};

private void scanForWiFiNetworks() {
    setScanningEnabled(false);

    mapConnections = new HashMap<String, String>();

    btnConnect.setEnabled(false);
    btnForget.setEnabled(false);
    btnUpdate.setEnabled(false);

    checkWiFiState();

    List<ScanResult> results = wifi.getScanResults();
    WifiInfo connectedWiFi = wifi.getConnectionInfo();

    String connected = "";

    ArrayList<String> listDupeCheck = new ArrayList<String>();

    String[] listResults = new String[0];
    ArrayList<String> list = new ArrayList<String>();

    for (int i = 0; i < results.size(); i++) {
        if (!listDupeCheck.contains(results.get(i).SSID)) {
            if (connectedWiFi.getSSID() != null) {
                if ((connectedWiFi.getSSID().equalsIgnoreCase(results.get(i).SSID) || connectedWiFi.getSSID().equalsIgnoreCase(
                        "\"" + results.get(i).SSID + "\""))
                        && connectedWiFi.getSupplicantState().equals(SupplicantState.COMPLETED)) {
                    connected = "; " + GlobalVars.getMessageText(1159);// Connected

                } else {
                    connected = "";
                }

            } else {
                connected = "";
            }

            String connectionInfo = getMessageText(171) + ": " + results.get(i).SSID + ", " + getMessageText(1160) + ": "
                    + results.get(i).capabilities + connected;

            list.add(connectionInfo);

            mapConnections.put(connectionInfo, results.get(i).SSID);

            listDupeCheck.add(results.get(i).SSID);
        }
    }

    listResults = list.toArray(listResults);

    scan.removeCallbacksAndMessages(null);
    setScanningEnabled(true);
}

然后在onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    IntentFilter wifiState = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);

    this.registerReceiver(wifiReceiver, filter);
    this.registerReceiver(wifiStateReceiver, wifiState);
    wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
}