{{announcement.body}}
{{announcement.title}}

A Journey Through HMS Awareness - Part 1

DZone 's Guide to

A Journey Through HMS Awareness - Part 1

Awareness Ki provides your app with the ability to obtain contextual information, including users' current time, location, behavior, etc.

· Web Dev Zone ·
Free Resource

Awareness Kit provides your app with the ability to obtain contextual information, including users' current time, location, behavior, audio device status, ambient light, weather, and nearby beacons.

There are two types of APIs in Awareness Kit — Capture API and Barrier API. The Capture API allows the app to request the current user status and the Barrier API allows the app to set a combination of contextual conditions. In this article, we are explaining about Location Awareness, Headset Awareness, Ambient Light Awareness and Bluetooth Car Stereo Awareness.

Steps for Developing Capture Capabilities

  1. Obtain the Capture Client object of HUAWEI Awareness Kit.
  2. Call the respective query capability API through the Capture Client object to obtain the user's context.
  3. Enable your app to listen for the result returned by HUAWEI Awareness Kit for further processing.

Steps for developing barrier capabilities

  1. Define the barrier.
  2. Define PendingIntent that will be triggered upon a barrier status change, for example, to send a broadcast, and create a broadcast receiver to receive the broadcast.
  3. Define the label for the barrier and add the barrier.
  4. Define the broadcast receiver to listen for the barrier event for further processing.

In this article, we are explaining about Location Awareness, Headset Awareness, Ambient Light Awareness, and Bluetooth Car Stereo Awareness.

Headset Awareness

For calling Headset Awareness capability we have to assign the given permissions in the manifest file.

XML
 




xxxxxxxxxx
1


 
1
<uses-permission android:name="android.permission.BLUETOOTH" />



Capture API

We can use the  Capture  API  to detect whether the user currently has their headset connected or disconnected. 

To get the headset status from the Capture API, we need to call the getHeadsetStatus() method —this will return an instance of the HeadsetStatusResponse class that, if successful, will contain information about the devices current headphone status.

Java


Barrier API

The given example illustrates how to develop a barrier triggered by the connecting condition. That is, the barrier will be triggered when the headset is connected or plugged in.

Java


Location Awareness

For calling Location Awareness capability we have to assign the given permissions in the manifest file.

XML
 




xxxxxxxxxx
1


 
1
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />



Capture API

We can use the  Capture  API to Obtains the latitude and longitude of the current location.

To get the location from the Capture API we need to call the  getLocation() method - this will return an instance of the LocationResponse class that if successful, will contain information about the location.

Java


Barrier API

Given an example in which a barrier is triggered by the enter condition. That is, the barrier will be triggered when a user enters his house.

Java


Ambient Light Awareness

Capture API

We can use the  Capture  API to obtain the illuminance of the environment where the device is located.

To get the light intensity from the Capture API we need to call the  getLightIntensity () method - this will return us and instance of the AmbientLightResponseclass that if successful, will contain information about the users light intensity.

Java



Barrier API

We can use the Barrier API to set the ambient light barrier. For example, we can set the application to enable the auto flashlight function when the luminance is less than 20 lux.  You can see the article "HMS Ambient Light Awareness for building an Auto Flash Light Application”  for the reference.

Java

Bluetooth Car Stereo Awareness

For calling Bluetooth Car Stereo Awareness capability we have to assign the given permissions in the manifest file.We have to assign the given permissions in the manifest file.

XML
 




xxxxxxxxxx
1


1
<uses-permission android:name="android.permission.BLUETOOTH" />


Capture API

We can use the  Capture  API  to detect whether The Bluetooth car stereo is currently connected or disconnected.

To get the Bluetooth car stereo status from the Capture API we need to call the getBluetoothStatus(0) method - this will return an instance of the BluetoothStatusResponse class that if successful, will contain information about the The Bluetooth car stereo status. 

Java



Barrier API

The following example illustrates how to develop a barrier triggered by the connecting condition. That is, the barrier will be triggered when the Bluetooth car stereo is connected.

Java



References:

https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/awareness-introduction

Topics:
android, application, integration, java, kotlin, mobile, opensource

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}