alvinashcraft
shared this story
from Xamarin Blog.
Google Play services offers a plethora of amazing APIs for developers to integrate into their iOS and Android applications. For Android developers, specifically, there are several more APIs available that can give you even more power and integration with the Android OS when developing apps for the platform. One of the newest APIs, Awareness, brings together seven different location and context signals into a single easy to use API, including time, location, places, beacons, headphones, activity, and even weather. The NuGet package for the Awareness API launches alongside our release of Google Play services 9.6.1 (32.961.0) for Xamarin.
The Awareness API offers two different ways to get insight to these signals, the Fence and the Snapshot APIs. The Fence API allows you to react to changes in the user’s environment and the signals. You can create multiple conditions that need to be met and when they occur, you receive a callback to react to them. The Snapshot API on the other hand, which we’ll look at today, gives you direct access to the signals from one simple asynchronous API. In addition to being a simple API, the Awareness API delivers better battery performance and memory usage due to its intelligent caching and cross-app optimizations.
Install the Awareness NuGet
The first step to accessing the Awareness APIs is to add the Google Play services – Awareness NuGet to your Xamarin.Android application. As of this blog post, the current version is 32.961.0. The easiest way to find the NuGet is to directly enter the package name Xamarin.GooglePlayServices.Awareness. The additional dependencies of Google Play services Places, Location, Base packages, and required Support Packages will also come along when the NuGet is added.
Register for API Keys
Google Play services often require a unique API key for each application using them. These are created and enabled in the Google API Developer Console. First create a new project:
Once it is created, you’ll want to enable three different APIs:
Awareness API
Google Places API for Android (for Places API)
Nearby Messages API (for Beacons API)
You’ll need to enter your application’s package name and the keystore’s SHA1 you’re using for debug and release when ready to deploy to the app store to register these. You can read through Obtaining your Signing Key Fingerprint in our Android documentation.
Simply create new credentials using these keys.
Add Keys to the Android Manifest
Once you have registered for the APIs, you’ll receive a unique API Key that needs to be added to your Android Manifest file. It’s the same API Key for each of the meta-data tags that we’ll add to the application node, which will look like this:
Create GoogleAPIClient
With all of the API Key registration out of the way, it’s now time to actually start using the Awareness API. To use any Google Play service API, we’ll need to create a GoogleApiClient. First let’s bring in a few namespaces:
Now, in our Activity, we can create and connect to the GoogleApiClient:
Query Signals
We can now start using the API to query signals, such as headphones.
Other signals besides headphones require additional permissions such as location and activity recognition. We must add two specific permissions to the Android Manifest:
To check and request permissions, we can use the Permissions Plugin that helps abstract the functionality into a simple call. If we want to query the location and weather, we can use the Permissions Plugin along side the Awareness API:
We can even take it a step further and detect the user’s current activity, such as running or cycling, by requesting the Detected Activity:
Learn More
The Awareness API is extremely powerful and at the same time extremely energy efficient when querying signals on the device. You can download a full source code example from my GitHub page that walks through using each of the Snapshot APIs, and you can also learn more about the Awareness API from the Google Developer portal.
The post Google Awareness API for Android: Query and React to Signals appeared first on Xamarin Blog.