0

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

Sarvavyapi
  • 810
  • 3
  • 23
  • 35
  • "My application gets GPS updates once a second" -- GPS does not offer any guarantees about the rate in which a GPS receiver can obtain fixes. – CommonsWare Oct 08 '13 at 19:43
  • You don't need to register it in Manifest if are doing it dynamically. BTW Try this technique: http://developer.android.com/guide/topics/location/strategies.html – M-Wajeeh Oct 08 '13 at 19:43
  • @CommonsWare - yes, I already tested a standalone GPS app. I get updates close to a second which is all right. – Sarvavyapi Oct 08 '13 at 19:44
  • @M-WaJeEh - thanks. I have read that guide. I already tried using a LocationListener and it is not working. That is why I started using Intents. LocationListener is not working, because I need to get the Wifi signals as well using the BroadcastReceiver. I read here - http://stackoverflow.com/questions/7895995/android-requestlocationupdates-using-pendingintent-with-broadcastreceiver - that both LocationListener and BroadcastReceiver cannot be used together. – Sarvavyapi Oct 08 '13 at 19:49

1 Answers1

1

Note that you declare a broadcast in your manifest with the name PingActivity and your implementation class is called myWifiReceiver yout must rename it to PingActivity, and also make your broadcast a separate class.

Jans
  • 11,064
  • 3
  • 37
  • 45
  • adding the broadcast to the manifest was one of my attempts to fix the issue, but I have removed it now, since I am registering it inside my app. I also tried making the broadcast a separate class, which did not work. – Sarvavyapi Oct 08 '13 at 20:02
  • Depending on the approach you choose are required steep that you must to do, you can see that article http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html – Jans Oct 08 '13 at 20:08