Reputation: 39
I am using WifiManager to connect to a network or actually a device. It was working perfectly and then all of sudden stopped, with no code changes. I did start using a different phone. I've checked and I believe the phone is setup properly. When I call addNetwork I get a network ID back that is different from the current network ID. When I run enableNetwork, passing it the network ID returned by addNetwork, I do not get an error but when I check the connection info I am still connected to the original network. The device I'm connecting to does not require a user or password. Can anyone tell me what I'm doing wrong.
Thank you in advance for any help you can give me.
I've tried doing a reconnect right after the enable network and I've tried disconnecting from the current network before connecting to the new one.
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.util.Log;
public class DeviceConnect extends AsyncTask<String, String, String> {
public interface AsyncResponse {
void processFinish(String output);
}
public AsyncResponse delegate = null;
public DeviceConnect(AsyncResponse delegate){
this.delegate = delegate;
}
private Context _context = null;
private String _result = null;
private WiFiHandler _wifiHandler = null;
public void setContext(Context value) {
this._context = value;
}
public String getResult() {
return this._result;
}
@Override
protected String doInBackground(String... params) {
String message = null;
boolean ok = true;
String device = params[0];
this._result = null;
this._wifiHandler = new WiFiHandler();
if (!device.isEmpty()) {
int netID = 0;
WifiManager wifi = (WifiManager) this._context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config;
WifiInfo connInfo;
config = new WifiConfiguration();
config.SSID = device;
wifi.disconnect();
netID = wifi.addNetwork(config);
Log.e("doInBackground", "netID: " + netID);
if (netID > 0) {
try {
wifi.enableNetwork(netID, true);
} catch (Exception ex) {
ok = false;
this._result = device + " - Add network failed (2)";
}
if(ok) {
connInfo = wifi.getConnectionInfo();
Log.e("doInBackground", connInfo.getSSID() + " - " + connInfo.getNetworkId());
if (connInfo != null && connInfo.getSSID().equalsIgnoreCase(device)) { // || (netID = connInfo.getNetworkId()) <= 0) {
this._result = "SUCCESS";
} else {
this._result = device + " - Add device failed (3)";
}
}
} else {
this._result = device + " - Add device failed (1)";
}
} else {
this._result = "No device to configure";
}
return this._result;
}
@Override
public void onPostExecute(String result) {
delegate.processFinish(result);
}
}
It should connect to the device and return "SUCCESS". For what I need to do I will only be connected to the device for a matter of a second or so.
Upvotes: 0
Views: 932
Reputation: 39
After hours of trying and failing I finally found the answer. It was apparently a timing issue. The code below solved my problem.
Object lock = new Object();
lock = wifi.enableNetwork(netID, true);
synchronized(lock) {
lock.wait(500);
}
Upvotes: 1