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

Building Contextual Apps With Huawei Awareness Kit: Barrier API

DZone 's Guide to

Building Contextual Apps With Huawei Awareness Kit: Barrier API

In this article, we’re going to take a look at the Awareness Kit Barrier API features as the last section so we can easily implement these features in our apps.

· Integration Zone ·
Free Resource

Hello everyone, we looked at the Awareness Kit Capture API features in the previous article.

You can read this article from here:

https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201288940018380104&fid=0101246461018590361

In this article, we’re going to take a look at the Awareness Kit Barrier API features as the last section so we can easily implement these features in our apps.

I created a sample app that I’ve open-sourced on GitHub for enthusiasts who want to use this kit.

https://github.com/tualdev/hms-awareness-kit-demo

Document

developer.huawei.com

Barrier API

The Barrier API allows your app to set a combination of contextual conditions. When the preset contextual conditions are met, your app will receive a notification. One of the great aspects of the barrier API is that we can watch the contextual changes. For example, while the user is running, she/he goes into the walking mode or listening to the headphone connection or we can watch a certain level of ambient light and start an action.

Let’s examine what type of barrier properties we could create:

  • A barrier can be created for the user for a specified time interval.

  • A barrier can be created with latitude, longitude, and radius information.

  • A barrier can be created that matches user behavior.

  • A barrier can be created that matches labeled beacons.

  • A barrier can be created that listens to connection condition for audio devices.

  • A barrier can be created that watches the above condition for ambient light.

Setting up the Awareness Kit

You’ll need to do set up before you can use the Awareness Kit. You can follow the official documentation on how to prepare an app on App Gallery Connect.

Add the required dependencies to the build.Gradle file under the app folder. We need awareness and location kit dependencies.

implementation 'com.huawei.hms:awareness:1.0.3.300'   

implementation 'com.huawei.hms:location:4.0.4.300'   

Add the required permissions to the AndroidManifest.xml file under app/src/main folder.

Java
 




x
13


1
<!-- Location permission, which is sensitive. After the permission is declared, you need to dynamically apply for it in the code. -->   
2
 
          
3
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Location permission. If only the IP address-based time capture function is used, you can declare only this permission. -->   
4
 
          
5
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- If your app uses the barrier capability and runs on Android 10 or later, you need to add the background location access permission, which is also a sensitive permission. -->   
6
 
          
7
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />   
8
 
          
9
<uses-permission android:name="android.permission.BLUETOOTH" /> <!-- Behavior detection permission (Android 10 or later). This permission is sensitive and needs to be dynamically applied for in the code after being declared. -->   
10
 
          
11
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" /> <!-- Behavior detection permission (Android 9). This permission is sensitive and needs to be dynamically applied for in the code after being declared. -->   
12
 
          
13
<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION" />  



Don’t forget to check permissions in the app.

Checking location permission:

Java
 




xxxxxxxxxx
1


 
1
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {   
2
 
          
3
  ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_ACCESS_FINE_LOCATION)   
4
 
          
5
  return   
6
 
          
7
}    



Let’s take a deep look at these different barrier capabilities that we can listen to changes from the Barrier API.

Listening for Barrier changes

Let’s talk about which steps we will process in general. For the first step, each barrier should use BroadcastReceive to listen for changes. As an example, you can examine the code example:

Java
 




xxxxxxxxxx
1
39


1
class BarrierReceiver : BroadcastReceiver() {   
2
 
          
3
       override fun onReceive(   
4
 
          
5
           context: Context,   
6
 
          
7
           intent: Intent   
8
 
          
9
       ) {   
10
 
          
11
           val barrierStatus = BarrierStatus.extract(intent)   
12
 
          
13
           val label = barrierStatus.barrierLabel   
14
 
          
15
           when (barrierStatus.presentStatus) {   
16
 
          
17
               BarrierStatus.TRUE -> {   
18
 
          
19
                   // We are in the barrier   
20
 
          
21
               }   
22
 
          
23
               BarrierStatus.FALSE -> {   
24
 
          
25
                   // We are NOT in the barrier   
26
 
          
27
               }   
28
 
          
29
               BarrierStatus.UNKNOWN -> {   
30
 
          
31
                   // There is a problem with barrier   
32
 
          
33
               }   
34
 
          
35
           }   
36
 
          
37
       }   
38
 
          
39
   }   



The receiver will get notifications on the barrier that we have registered. Let’s examine what it means to barrier status from the receiver:

  • If the status is TRUE: this means the barrier has been entered.

  • If the status is FALSE: this means the barrier has not yet been done.

  • If the status is UNKNOWN: this means there is a problem with the barrier.

Let’s see how we will use this broadcast receiver:

Java
 




xxxxxxxxxx
1


 
1
val BARRIER_RECEIVER_ACTION = application.packageName + "BARRIER_RECEIVER_ACTION"   
2
 
          
3
val intent = Intent(BARRIER_RECEIVER_ACTION)   
4
 
          
5
mPendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)   
6
 
          
7
mBarrierReceiver = BarrierReceiver()   
8
 
          
9
registerReceiver(mBarrierReceiver, IntentFilter(BARRIER_RECEIVER_ACTION))  



We use addBarrier() method to join the barrier. Now we will use the intent we created:

Java
 




xxxxxxxxxx
1
17


1
val builder = BarrierUpdateRequest.Builder()   
2
 
          
3
val request = builder.addBarrier(label, barrier, mPendingIntent).build()   
4
 
          
5
Awareness.getBarrierClient(applicationContext).updateBarriers(request)   
6
 
          
7
   .addOnSuccessListener {   
8
 
          
9
       Toast.makeText(applicationContext, "add barrier success", Toast.LENGTH_SHORT).show()   
10
 
          
11
   }   
12
 
          
13
   .addOnFailureListener { e ->   
14
 
          
15
       Toast.makeText(applicationContext, "add barrier failed", Toast.LENGTH_SHORT).show()   
16
 
          
17
   }  



  • label: This is a string key value that is used in our receiver to react with the correct barrier.

  • barrier: This represents the barrier we registered like TimeBarrier.duringPeriodOfDay (…)

  • mPendingIntent: This is the intent to be triggered when barrier events occur.

We can also remove barriers while using deleteBarrier() method:

Java
 




xxxxxxxxxx
1
23


1
private fun removeBarrier(label: String) {   
2
 
          
3
       val builder = BarrierUpdateRequest.Builder()   
4
 
          
5
       builder.deleteBarrier(label)   
6
 
          
7
       Awareness.getBarrierClient(this)   
8
 
          
9
           .updateBarriers(builder.build())   
10
 
          
11
           .addOnSuccessListener {   
12
 
          
13
               Log.i(TAG, "remove Barrier success")   
14
 
          
15
           }   
16
 
          
17
           .addOnFailureListener {   
18
 
          
19
                   e -> Log.e(TAG, "remove Barrier failed", e)   
20
 
          
21
           }   
22
 
          
23
   }  



Don’t forget to unregister your receiver when you don’t need it.

Java
 




x


1
override fun onStop() {   
2
 super.onStop()   
3
 unregisterReceiver(mBarrierReceiver)   
4
}  



Let’s take a look at the different barriers that we can create:

Time Barrier

We can create a time barrier triggered by the duringPeriodOfDaycondition.That is, the barrier will be triggered when a user is in a specified time frame in a specified time zone.

Java
 




xxxxxxxxxx
1


1
var oneHourMilliSecond = 60 * 60 * 1000Lvar periodOfDayBarrier = TimeBarrier.duringPeriodOfDay(TimeZone.getDefault(), 11 * oneHourMilliSecond, 12 * oneHourMilliSecond)



Location Barrier

We can create a location barrier triggered by the enter condition. That is, the barrier will be triggered when a user enters a specified district.

Java
 




xxxxxxxxxx
1


1
var latitude = 22.4943   
2
var longitude = 113.7436   
3
var radius = 200.0   
4
var enterBarrier = LocationBarrier.enter(latitude, longitude, radius)



Behavior Barrier

We can create a behavior barrier triggered by the keeping condition. That is, the barrier will be triggered when a user’s behavior matches the behavior defined here.

var keepStillBarrier = BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_STILL)

Beacon Barrier

We can create a beacon barrier triggered by the discovery condition. That is, the barrier will be triggered when the filter parameters passed by a beacon match those defined here.

Java
 




xxxxxxxxxx
1
23


1
var namespace = "sample namespace"   
2
var type = "sample type"   
3
var content = byteArrayOf(   
4
   's'.toByte(),   
5
   'a'.toByte(),   
6
   'm'.toByte(),   
7
   'p'.toByte(),   
8
   'l'.toByte(),   
9
   'e'.toByte()   
10
)   
11
var filter = BeaconStatus.Filter.match(namespace, type, content)   
12
var discoverBeaconBarrier = BeaconBarrier.discover(filter)



Bluetooth Car Stereo Barrier

We can create a Bluetooth car stereo barrier triggered by the connecting condition. That is, the barrier will be triggered when the Bluetooth car stereo is connected.

Java
 




xxxxxxxxxx
1


1
val deviceType = 0 // Value 0 indicates a Bluetooth car stereo.   
2
 
          
3
var connectingBarrier = BluetoothBarrier.connecting(deviceType)  



Headset Barrier

We can create a headset barrier triggered by the connecting condition. That is, the barrier will be triggered when the headset is connected.

val barrier = HeadsetBarrier.connecting()

Ambient Light Barrier

We can create an ambient light barrier triggered by the above condition. That is, the barrier will be triggered when the ambient light intensity exceeds the preset value

val luxValue = 2500.0f 

var lightAboveBarrier = AmbientLightBarrier.above(luxValue)

Bonus: Comparing With Google Awareness API

Huawei Awareness Kit supports 8 different awareness types:

  • Time
  • Location
  • Behavior
  • Beacon
  • Bluetooth Car Stereo
  • Headset
  • Ambient Light
  • Weather

Google Awareness API supports 4 different awareness types :

  • Activity
  • Beacon
  • Headset
  • Location

References:

HUAWEI Awareness Kit — About the Service:

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

HUAWEI Developer Forum:

https://forums.developer.huawei.com/forumPortal/en/home

Topics:
android, barrier api, integration, java, mobile, opensource, tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}