Reputation: 61
My question is fairly simple. Using a wifiManager, I scan the network, get the results in SCAN_RESULTS_AVAILABLE_ACTION broadcast receiver, and connect to the network I want to connect to on a device selection page with that information. I am currently connecting to a WAP on a Raspberry Pi and pinging a web server on it.
The problem I'm having is scanning again. I have a button that i push to refresh the devices in the area that I'm able to connect to. I receive no broadcast at all the second time I run it.
Here is my broadcast receiver:
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;
}
}
}
And here I connect to a wifiConnection given the results:
/**
* 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();
}
}
}
I currently have a state machine that will call these during a certain state. So in this case my state is SEARCHING and this is what will run:
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");
}
}
}
But it gets stuck in SEARCHING waiting for a response from the startScan() call. Is there something I have to do for a second startScan() to work??
Upvotes: 1
Views: 363
Reputation: 61
I found a way to fix the issue, but I feel there could be a better one. I ended up unregistering the receiver in its own receive(). Then right before a calling startScan() i registered the receiver again.
If anyone has a better solution please let me know. I'm not quite sure what the issue is.
Upvotes: 1