通过具有相同SSID的AP进行Android Wifi漫游 [英] Android Wifi Roaming through AP with same SSID

查看:790
本文介绍了通过具有相同SSID的AP进行Android Wifi漫游的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Android系统在Wifi漫游中表现不佳。
我们有一个Wifi集中式网络,其中许多AP带有一个SSID签名。
The Adroid Phones不会无缝漫游。
即使有其他具有良好信号的AP(具有相同的SSID),Android Phone也会尝试保持与AP的连接,直到信号变为零为止!
当信号为零时,最后它会与另一个AP(信号良好)建立关联。但是由于这种行为,电话会丢失所有TCP连接!

I saw that Android system has a bad behavior with Wifi roaming. We have a Wifi centralized network with many AP with a signle SSID. The Adroid Phones wont roams seamlessly. An Android Phone tries to stay connected to an AP until the signal reaches zero even if there are others AP (with the same SSID) with a good signal! When the signal is zero, finally it performs an assosiation to another AP (with a good signal). But with this behavior the phone loses all the TCP Connections!

例如:


  1. 电话已通过WiFi连接到AP1

  2. 电话在建筑物中移动,现在听到了来自AP1和AP2的两个信号。

  3. 何时AP2的信号强度比AP1的信号强,我希望电话对AP2做重新关联(不是关联)。

  1. the phone is connected in WiFi to AP1
  2. the phone moves in the building and now hears two signals from AP1 and from AP2.
  3. When the signal form AP2 is stronger than the signal from AP1, i want that the phone do a reassosiation (not an assosiation) to AP2.

想法是:


  • 执行 WifiManager.startScan()

  • 获取结果 WifiManager.getScanResults()

  • 找到最好的结果结果中的AP

  • 对最佳AP进行重新关联

  • Perform a WifiManager.startScan()
  • Get the results WifiManager.getScanResults()
  • Find the best AP in the results
  • Perform a reassosiation to the best AP

每30秒重复一次。

我说的是重新关联,因为我不希望手机丢失TCP连接。

I talk about reassosiation because i don't want that the phone loses the TCP Connections.

有一种方法可以做到这一点?

There is a way to do this ?

谢谢,
Salvo

Thank you, Salvo

推荐答案

在下面添加权限;

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

注册以下意图;

private WifiBroadcastReceiver wifiBroadcastReceiver = new WifiBroadcastReceiver();

然后按常规;

registerReceiver(wifiBroadcastReceiver, new IntentFilter("android.net.wifi.SCAN_RESULTS"));

使用下面的类更改重新关联;

Use the below class to change the reassociation;

public class WifiBroadcastReceiver extends BroadcastReceiver {

    private WiFiManager manager = null;//set the value in constructor
    private WifiConfiguration connectedConfiguration = null;//set the value in constructor
    private int connectedNetId;

    private void updateConnectedConfiguration(String ssid) {
        configs = manager.getConfiguredNetworks();
        int nid = 0;
        for (WifiConfiguration cnf : configs) {
            if (cnf.SSID.substring(1, cnf.SSID.length() - 1).equals(ssid)) {
                connectedConfiguration = cnf;
                connectedNetId = nid;
            }
            nid++;
        }
    }


    public void onReceive(Context c, Intent intent) {
        List<ScanResult> results = manager.getScanResults();
        WifiInfo info = manager.getConnectionInfo();
        ScanResult stronger = null;
        for (ScanResult scanResult : results) {
            try {
                if (scanResult.SSID.equals(info.getSSID())) {
                    if (stronger == null) {
                        if (WifiManager.compareSignalLevel(info.getRssi() + 5, scanResult.level) < 0) {
                            stronger = scanResult;
                        }
                    } else if (WifiManager.compareSignalLevel(stronger.level, scanResult.level) < 0) {
                        stronger = scanResult;
                    }
                }
            } catch (Exception e) {
            }
        }
        if (stronger != null && !stronger.BSSID.equals(info.getBSSID())) {
            updateConnectedConfiguration(info.getSSID());
            if (connectedConfiguration != null) {
                connectedConfiguration.BSSID = stronger.BSSID;
                manager.updateNetwork(connectedConfiguration);
                manager.saveConfiguration();
                manager.enableNetwork(connectedNetId, true);
                manager.reassociate();
                info = manager.getConnectionInfo();
                //showNotification("\nConnecting " + stronger.SSID, stronger.BSSID + " " + stronger.level + "dBm");
            }
        }
    }
}

这篇关于通过具有相同SSID的AP进行Android Wifi漫游的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆