Get Gimbal up and running in your Android App.
Important This guide assumes you have a Android project setup with Gimbal. Please follow Gimbal Quick Start guide if you have not yet setup an Android project with Gimbal.
Now, we will continue adding code to your MainActivity
. This code will allow you to start listening for places.
First, import some more files:
import android.widget.Toast; import com.qualcommlabs.usercontext.ContextPlaceConnector; import com.qualcommlabs.usercontext.ContextPlaceConnectorFactory; import com.qualcommlabs.usercontext.PlaceEventListener; import com.qualcommlabs.usercontext.protocol.PlaceEvent;
Then, add some fields to hold an instance of the ContextPlaceConnector
and a PlaceEventListener
. Initialize your PlaceEventListener
and tell it what to do when a place event occurs (e.g. when you break a geofence). The one we have made will log and toast the name and id of any geofences that are triggered:
private ContextPlaceConnector contextPlaceConnector; private PlaceEventListener placeEventListener = new PlaceEventListener() { @Override public void placeEvent(PlaceEvent event) { String placeNameAndId = "id: " + event.getPlace().getId() + " name: " + event.getPlace().getPlaceName(); Toast toast = Toast.makeText(getApplicationContext(), placeNameAndId, Toast.LENGTH_LONG); toast.show(); Log.i("found place", placeNameAndId); } };
In your onCreate()
method add a line to obtain the instance of the ContextPlaceConnector
. Your onCreate()
method should end up like this:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); contextCoreConnector = ContextCoreConnectorFactory.get(this); contextPlaceConnector = ContextPlaceConnectorFactory.get(this); checkContextConnectorStatus(); } private void checkContextConnectorStatus() { if (contextCoreConnector.isPermissionEnabled()) { startListeningForGeofences(); } else { contextCoreConnector.enable(this, new Callback<Void>() { @Override public void success(Void arg0) { startListeningForGeofences(); } @Override public void failure(int arg0, String arg1) { Log.e("failed to enable", arg1); } }); } }
Finally, implement the ForGeofences()
method that we call from checkContextConnectorStatus()
. In this case, it is really simple and just adds the PlaceEventListener
we made earlier to the ContextPlaceConnector
:
private void startListeningForGeofences() { contextPlaceConnector.addPlaceEventListener(placeEventListener); }
Before you run your application, you want to create a geofence for your current location so you will see a place event trigger when your application starts.
Note Make sure the geofence you create is at your current location if you want it to trigger when your app launches.
Now, everything should be ready for you to find some geofences. Make sure your phone is plugged in and in debug mode, then in Eclipse right click on your project and click Run as -> Android Application
. Upon successful launch, you will be prompted to accept Gimbal terms of service. Once you do that you will see a toast and log entry with the name and id of the geofence you just made assuming it is around your location.
Note You will only get the geofence entered event when you install the application for the first time. All subsequent times you will have to leave the geofence and re-enter to get another event.
For additional details on how to leverage the SDK functionality, refer to the sample applications and documentation included in the zip file provided.