I am trying to use the geofencing api from the google play services but I can't manage to get my broadcast receiver trigger its onReceive()
method.
It looks like I respect everything that is needed to make it work but I get no transition event.
- I include the google play services in my dependencies
- I request the
ACCESS_FINE_LOCATION
permission - I register my
BroadcastReceiver
with the exported value set to "true" (some say that it is needed) in my Manifest - I add a big geofence at the exact location where I am (I validated it with Google Maps on my cellphone) with both transition types and no expiration
- I tried with a precise mock location in the middle of the geofence, and another out of the geofence 10 seconds later (and I made sure the
ACCESS_MOCK_LOCATION
persmission was present) - I verified that my cellphone's location was on and its mode was set to high accuracy
- I verified the internet connection
- I tried with and without disconnecting the LocationClient after adding the geofence
But still, I get a successful onAddGeofencesResult()
callback, but no onReceive()
callback. I tried it with a Nexus 5 and a Nexus 7 and both fails to trigger the geofence transitions.
I also tried the sample on https://developer.android.com/training/location/geofencing.html, but its behavior was identical.
Here is my BroadcastReceiver
class :
public class GeofencesReceiver extends BroadcastReceiver implements ConnectionCallbacks, OnConnectionFailedListener, OnAddGeofencesResultListener {
private final static String TAG = "GeofencesReceiver";
private Context context = null;
private LocationClient locationClient = null;
/**
* An empty constructor is needed by the manifest.
*/
@SuppressWarnings("unused")
public GeofencesReceiver(){}
public GeofencesReceiver(Context context){
this.context = context;
locationClient = new LocationClient(context, this, this);
locationClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
new Thread(new Runnable() {
@Override
public void run() {
List<Geofence> geofences = getGeofences();
if(geofences.size() > 0){
Intent geofenceBroadcastReceiverIntent = new Intent(context, GeofencesReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceBroadcastReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.addGeofences(geofences, pi, GeofencesReceiver.this);
}else
Log.w(TAG, "No GPS zone found");
}
}).start();
}
private List<Geofence> getGeofences(){
Vector<Geofence> geofences = new Vector<Geofence>();
Geofence geofence = new Geofence.Builder()
.setRequestId("1")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(45.2676018, -72.1572185, 100)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
geofences.add(geofence);
return geofences;
}
@Override
public void onDisconnected() {
Log.d(TAG, "onDisconnected");
}
@Override
public void onAddGeofencesResult(int i, String[] strings) {
if(i != LocationStatusCodes.SUCCESS)
Log.e(TAG, "Failed to add geofences");
else
Log.d(TAG, "Geofences successfully added");
locationClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed");
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
if(LocationClient.hasError(intent)){
int errorCode = LocationClient.getErrorCode(intent);
Log.e(TAG, "Location Services error: " + Integer.toString(errorCode));
return;
}
int transitionType = LocationClient.getGeofenceTransition(intent);
if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
for(Geofence geofence : triggerList){
Log.d(TAG, (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ? "Entering" : "Exiting") + " GPS zone " + geofence.getRequestId());
}
}else{
Log.e(TAG, "Geofence transition error: " + transitionType);
}
}
}
In my Manifest, I have the following lines (not in that structure)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name="novom.anyware.anywaresdk.GeofencesReceiver" android:exported="true" />
<meta-data android:name="com.google.android.gms.version" android:value="4323000" />
And in my build.gradle file, I include the services like this
dependencies {
compile 'com.google.android.gms:play-services:4.3.23'
}
I spent the whole day trying to figure out why it doesn't work... From Logcat I've found those lines, but I am not sure if they are related to the problem, since I find no post on Google about those errors with Geofencing.
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Not calling LocationReportingService, hasMoved: true, elapsed millis: 580166, request: Phone
04-07 09:02:16.481 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:02:16.501 1160-1319/? I/GCoreUlr﹕ Starting service, intent=Intent { cmp=com.google.android.gms/com.google.android.location.reporting.LocationReportingService }, extras=null
04-07 09:02:16.571 1210-1252/? W/GLSUser﹕ GoogleAccountDataService.getToken()
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Failed write_ctrl(u 69) res=-1 errno=22
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Untagging socket 69 failed errno=-22
04-07 09:02:16.601 1160-3780/? W/NetworkManagementSocketTagger﹕ untagSocket(69) failed with errno -22
04-07 09:02:16.601 1160-3780/? I/imp﹕ I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
04-07 09:02:16.601 1160-3780/? I/imp﹕ Retrying request
04-07 09:02:17.501 1160-6156/? I/GCoreUlr﹕ Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_ACTIVE_STATE cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService }, extras=null
04-07 09:02:17.511 1160-6156/? I/GCoreUlr﹕ Batch Location Update succeeded for account account#7#
04-07 09:02:17.571 1160-1319/? I/GCoreUlr﹕ Ensuring that reporting is active for [account#7#]
If nobody can help me, I fear I'm going to implement my own geofencing algorithm with the LocationManager registered with the GPS_PROVIDER, and this is going to drain my users' battery...