我目前正在写一个类在Android中,将扫描接入点,计算出哪个接入点具有最佳的信号,然后连接到接入点。
I am currently trying to write a class in Android that will Scan for access points, calculate which access point has the best signal and then connect to that access point.
因此,应用程序将能够扫描的移动和连接到新的接入点在旅途中。
So the application will be able to scan on the move and attach to new access points on the go.
我有最好的信号工作的扫描和计算。
I have the scanning and calculation of the best signal working.
但是,当涉及到连接到最佳接入点我有麻烦了。
But when it comes to attaching to the best access point I am having trouble.
看来,enableNetwork(网络标识符号,othersTrueFalse)是用于连接到接入点的唯一方法,但是这会导致问题,因为从我的扫描结果,我无法获得与信号最强的无线接入点的ID。
It appears that enableNetwork(netid, othersTrueFalse) is the only method for attaching to an Access point but this causes problems as from my Scan Results I am not able to get the id of the access point with the strongest signal.
这是我的code:
public void doWifiScan(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
sResults = wifiManager.scan(getBaseContext());
if(sResults!=null)
Log.d("TIMER", "sResults count" + sResults.size());
ScanResult scan = wifiManager.calculateBestAP(sResults);
wifiManager.addNewAccessPoint(scan);
}
});
}};
t.schedule(scanTask, 3000, 30000);
}
public ScanResult calculateBestAP(List<ScanResult> sResults){
ScanResult bestSignal = null;
for (ScanResult result : sResults) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String message = String.format("%s networks found. %s is the strongest. %s is the bsid",
sResults.size(), bestSignal.SSID, bestSignal.BSSID);
Log.d("sResult", message);
return bestSignal;
}
public void addNewAccessPoint(ScanResult scanResult){
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = '\"' + scanResult.SSID + '\"';
//wc.preSharedKey = "\"password\"";
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.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = mainWifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = mainWifi.enableNetwork(res, false);
Log.d("WifiPreference", "enableNetwork returned " + b );
}
当我尝试使用addNewAccessPoint(ScanResult scanResult),它只是增加了一个AP到列表中具有相同的名称作为一个具有最佳信号的设置应用程序,所以我结束了重复的负载,而不是实际连接到它们。
When I try to use addNewAccessPoint(ScanResult scanResult) it just adds another AP to the list in the settings application with the same name as the one with the best signal, so I end up with loads of duplicates and not actually attaching to them.
任何人都可以点我的一个更好的解决方案的方向?
Can anyone point me in the direction of a better solution?
只需更改
boolean b = mainWifi.enableNetwork(res, false);
到
boolean b = mainWifi.enableNetwork(res, true);
上一篇:是否有可能从一个适配器加载列表preference项目?有可能、适配器、加载、项目
下一篇:创建于Android的一个IntentFilter的匹配所有意图意图、创建于、Android、IntentFilter