【发布时间】:2018-09-18 03:14:10
【问题描述】:
我的问题很简单。使用 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() 工作吗?
【问题讨论】:
标签: android raspberry-pi wifi wifimanager