My application gets GPS updates once a second. It also gets the RSSI values of the Wifi connection once a second. The problem I am facing is that, the BroadcastReceiver never gets called. Can someone point out what I am doing wrong. I looked at lots of stackoverflow questions as well as other sites, but I am not able to figure out what I am doing wrong. My code is below:
final String locAction = "com.android.ping.STARTGPS";
PendingIntent pIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(locAction);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled) {
Log.i(TAG,"PING: GPS not enabled");
} else {
Log.i(TAG,"PING: GPS enabled");
pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_time_BW_Updates, MIN_Distance_Change_For_Updates, pIntent);
registerReceiver(myWifiReceiver, new IntentFilter(locAction));
Log.i(TAG,"PING: adding GPS status listener");
}
}
BroadcastReceiver:
protected BroadcastReceiver myWifiReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent){
Log.i(TAG, "PING: inside onReceive");
if (intent.getAction().equals(locAction)){
String locationKey = LocationManager.KEY_LOCATION_CHANGED;
Location location = (Location) intent.getExtras().get(locationKey);
Log.i(TAG, "PING: location is " + location.getLatitude() + ", " + location.getLongitude());
setLocation(location.getLatitude(), location.getLongitude());
try {
WifiInfo(); //Calls the function that gets the RSSI
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
The intent is unregistered here:
@Override
protected void onStop() {
super.onStop();
locationManager.removeUpdates(pIntent);
unregisterReceiver(myWifiReceiver);
}
My manifest file, where I register the BroadcastReceiver:
<receiver android:name="PingActivity" >
<intent-filter>
<action android:name="com.android.ping.STARTGPS" />
</intent-filter>
</receiver>
EDIT 1: I must also add that I hard-code IP addresses(for now) in the app and deploy it on 2 different devices. Depending on the IP address, the devices start either a server or a client thread. This portion of the code follows right after I register the broadcastReceiver