I have problems with the wifi broadcast receiver. It doesn't receive anything, onReceive is never called. Here's my code:
public final class WifiChangeReceiver extends BroadcastReceiver {
boolean portableHotspot = true;
@Override
public void onReceive(final Context context, Intent intent) {
boolean alreadyPresent = false;
String action = intent.getAction();
if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
WifiManager w = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> l = w.getScanResults();
myApp myApp = (myApp)context.getApplicationContext();
List<ScanResult> previousScan = new ArrayList<ScanResult>();
previousScan = myApp.getPreviousScan();
System.out.print("previousScan " + previousScan + "\n");
for (ScanResult r : l) {
if(r.SSID.equals("\""+"TinyBox"+"\"")) {
if(previousScan!=null) {
for (ScanResult previousScanElement : previousScan) {
if(previousScanElement.SSID.contains("\""+"TinyBox"+"\"")) {
alreadyPresent = true;
break;
}
}
}
if (!alreadyPresent) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Methods.connectToNetwork(context);
}
}, 5000);
alreadyPresent = false;
}
}
}
myApp.setPreviousScan(l);
ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();
//Se non connesso a nessuna rete, allora tiro su TinyBox.
if (i == null) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i = conMgr.getActiveNetworkInfo();
if (i == null) {
boolean portableHotspot = false;
final SQLiteDatabase db = DatabaseManager.getInstance(context).getWritableDatabase();
Cursor c = HotSpotActivation.getHotSpotState(db);
if (c.getCount() > 0) {
String activation = c.getString(c.getColumnIndex(HotSpotActivation.CHECK_STATE));
if (activation.equals("ON")) {
portableHotspot = true;
}
else {
portableHotspot = false;
}
}
if (portableHotspot) {
Methods.setWirelessHotSpot(context.getApplicationContext());
}
}
}
else {
}
}
else if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
int iTemp = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
checkState(iTemp);
}
else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
DetailedState state=
((NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
changeState(state, context);
}
}
private void changeState(DetailedState aState, Context context) {
if (aState == DetailedState.SCANNING) {
Log.d("wifiSupplicanState", "SCANNING");
}
else if (aState == DetailedState.CONNECTING) {
Log.d("wifiSupplicanState", "CONNECTING");
}
else if(aState == DetailedState.OBTAINING_IPADDR) {
Log.d("wifiSupplicanState", "OBTAINING_IPADDR");
}
else if (aState == DetailedState.CONNECTED) {
Log.d("wifiSupplicanState", "CONNECTED");
}
else if (aState == DetailedState.DISCONNECTING) {
Log.d("wifiSupplicanState", "DISCONNECTING");
}
else if (aState == DetailedState.DISCONNECTED) {
myApp myApp = (myApp)context.getApplicationContext();
portableHotspot = myApp.getPortableHotspot();
if (portableHotspot) {
Methods.setWirelessHotSpot(context.getApplicationContext());
}
}
else if (aState == DetailedState.FAILED) {
}
}
public void checkState(int aInt) {
if (aInt == WifiManager.WIFI_STATE_ENABLING) {
Log.d("WifiManager", "WIFI_STATE_ENABLING");
}
else if (aInt== WifiManager.WIFI_STATE_ENABLED) {
Log.d("WifiManager", "WIFI_STATE_ENABLED");
}
else if (aInt == WifiManager.WIFI_STATE_DISABLING) {
Log.d("WifiManager", "WIFI_STATE_DISABLING");
}
else if (aInt == WifiManager.WIFI_STATE_DISABLED) {
Log.d("WifiManager", "WIFI_STATE_DISABLED");
}
}
}
and the manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<receiver android:name="sample.broadcastreceivers.WifiChangeReceiver">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"></action>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE"></action>
<action android:name="android.net.wifi.STATE_CHANGE"></action>
<action android:name="android.net.wifi.SCAN_RESULTS"></action>
</intent-filter>
</receiver>
It works (on Receive is called) only if I go to the wifi settings menu, the one in which networks are listed, without pushing anything, just opening it. Immediatly after I enter that menu I start to receive from the broadcast receiver and all works fine, without any reason.
Where is the problem? It's like an Android bug. I'm quite sure the same code worked sometimes ago. Thanks
Check your AndroidManifest: you declare
?