我看到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!
例如:
想法是:
- 执行 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.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 (WifiManagerpareSignalLevel(info.getRssi() + 5, scanResult.level) < 0) { stronger = scanResult; } } else if (WifiManagerpareSignalLevel(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漫游
发布评论