More articles like this, you can visit HUAWEI Developer Forum and Medium.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Introduction:
Behavior Awareness is being used to obtain user current behaviour or detect the behavior change.
For calling Behavior Awareness capability we have to assign the given permissions in the manifest file.
Code:
<!-- Behavior detection permission (Android 10 or later). This permission is sensitive and needs to be dynamically applied for in the code after being declared. -->
<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. -->
<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION" />
Capture API
We can use the Capture API to detect user behaviour such as walking, running,cycling,driving etc.
To get current behavior status of a user, we need to call the getBehavior() method - this will return an instance of the BehaviorResponse class that if successful, will contain information about the current user behavior.
Code:
private void getBehavorInfo() {
Awareness.getCaptureClient(this).getBehavior()
.addOnSuccessListener(new OnSuccessListener<BehaviorResponse>() {
@Override
public void onSuccess(BehaviorResponse behaviorResponse) {
BehaviorStatus behaviorStatus = behaviorResponse.getBehaviorStatus();
DetectedBehavior mostLikelyBehavior = behaviorStatus.getMostLikelyBehavior();
String str1 = "Most likely behavior type is " + mostLikelyBehavior.getType() +
",the confidence is " + mostLikelyBehavior.getConfidence();
behavior_info_capture.setTextColor(getColor(R.color.green));
if(mostLikelyBehavior.getType()==0){
behavior_info_capture.setText("You are in Vehicle");
behavior_image.setImageDrawable(getDrawable(R.drawable.vehicle));
}else if(mostLikelyBehavior.getType()==1){
behavior_info_capture.setText("You are in Bicycle");
behavior_image.setImageDrawable(getDrawable(R.drawable.bicycle));
}else if(mostLikelyBehavior.getType()==2){
behavior_info_capture.setText("You are on Foot");
behavior_image.setImageDrawable(getDrawable(R.drawable.foot));
} else if(mostLikelyBehavior.getType()==3){
behavior_info_capture.setText("You are still now");
behavior_image.setImageDrawable(getDrawable(R.drawable.still));
}else if(mostLikelyBehavior.getType()==4){
behavior_info_capture.setText("Unknown behavior");
}else if(mostLikelyBehavior.getType()==7){
behavior_info_capture.setText("You are Walking");
behavior_image.setImageDrawable(getDrawable(R.drawable.walking));
}else if(mostLikelyBehavior.getType()==8){
behavior_info_capture.setText("You are Running");
behavior_image.setImageDrawable(getDrawable(R.drawable.running));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "get behavior failed", e);
behavior_info_capture.setTextColor(getColor(R.color.red));
behavior_info_capture.setText("get behavior failed: "+e);
}
});
}
Barrier API
We can use the Barrier API to detect the behavior change such as from walking to running.
Public Methods:
Behavior barriers, including the beginning, ending, and keeping barriers of behaviors. Given three mathods which we use in behavior barrier.
public static AwarenessBarrier beginning(int… behaviorTypes)
When a user is in the behavior status, the barrier status is TRUE and a barrier event is reported. After 5s, the barrier status changes to FALSE.
Code:
AwarenessBarrier awarenessBarrier = BehaviorBarrier.beginning(BehaviorBarrier.BEHAVIOR_WALKING);
public static AwarenessBarrier keeping(int… behaviorTypes)
When a user is in the behavior status , the barrier status is TRUE and a barrier event is reported.
Code:
AwarenessBarrier awarenessBarrier = BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING);
public static AwarenessBarrier ending(int… behaviorTypes)
When a user stops the behavior, the barrier status is TRUE and a barrier event is reported. After 5s, the barrier status changes to FALSE.
Code:
AwarenessBarrier awarenessBarrier = BehaviorBarrier.ending(BehaviorBarrier.BEHAVIOR_WALKING);
Sample Code
Given an example in which a barrier is triggered by the keeping condition. That is, the barrier will be triggered when a user is in stand still.
Code:
public class BehaviorBarrierActivity extends AppCompatActivity {
private static final String TAG ="BehaviorBarrierActivity" ;
private TextView behavior_info_barrier;
private ImageView behavior_info_barrier_image;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.behavior_barrier_activity);
behavior_info_barrier=findViewById(R.id.behavior_info_barrier);
behavior_info_barrier_image=findViewById(R.id.behavior_info_barrier_image);
// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "BEHAVIOR_BARRIER_RECEIVER_ACTION";
Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
BehaviorBarrierReceiver barrierReceiver = new BehaviorBarrierReceiver();
registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
addbarrier(this);
}
private void addbarrier(Context context) {
//define the barrier
AwarenessBarrier keepStillBarrier = BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_STILL);
//define the label for the barrier and add the barrier
String behaviorBarrierLabel = "behavior keeping barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(behaviorBarrierLabel, keepStillBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
Log.e(TAG, "add barrier failed", e);
}
});
}
// define the broadcast receiver to listen for the barrier event.
class BehaviorBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
switch(barrierStatus.getPresentStatus()) {
case BarrierStatus.TRUE:
Log.i(TAG, label + " status:true");
behavior_info_barrier.setText("You are standing now");
behavior_info_barrier_image.setImageDrawable(getDrawable(R.drawable.still));
break;
case BarrierStatus.FALSE:
Log.i(TAG, label + " status:false");
behavior_info_barrier.setText("Looks like you are not standing");
behavior_info_barrier_image.setImageDrawable(getDrawable(R.drawable.foot));
break;
case BarrierStatus.UNKNOWN:
Log.i(TAG, label + " status:unknown");
break;
}
}
}
}
References:
https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/awareness-behavior-dev
Related
This article is originally from HUAWEI Developer Foum
Forum link: https://forums.developer.huawei.com/forumPortal/en/home
About GymOut:
GymOut is a simple work out App which uses some of the features of HMS awareness Kit. In this, we have to set Gym location (here it is taking current location) and work out time. Once we enter into Gym it prompts us to plug in the head set to listen the work out music. The music will stop playing after the completion of work out /on leaving the gym.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
An Introduction for Awareness Kit:
HUAWEI 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. Through this features the app can gain insight into a user's current situation more efficiently, making it possible to deliver a smarter, more considerate user experience
· Capture API
The Capture API allows the app to request the current user status, such as time, location, behaviour, and whether a headset is connected. For example, it can request location for getting latitude and longitude.
· BarrierAPI
The Barrier API allows the app to set a combination of contextual conditions. When the preset contextual conditions are met, the app will receive a notification. We can even accommodate our app with different combinations of contextual conditions to support different use cases. For example, we will get notification once we enter into a specified location.
Software Requirements
Android Studio
Java JDK 1.8 or later
Huawei Mobile Services (APK) 4.0.0.300 or later
Integration
1. Create a project in android studio and Huawei AGC.
2. Provide the SHA-256 Key in App Information Section.
3. Provide storage location. (Selected Singapore here)
4. Download the agconnect-services.json from AGCand save into app directory.
5. In root build.gradle
Go to allprojects->repositories and buildscript->repositories and the given line.
Code:
maven { url 'http://developer.huawei.com/repo/' }
In dependency add class path
Code:
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
6. In app build.gradle
We have to add signingConfigs and buildTypes, otherwise we have to create signed apk for awareness testing.
Code:
signingConfigs {
release {
storeFile file('store.jks')
keyAlias 'mykey'
keyPassword 'hmsapp'
storePassword 'hmsapp'
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.release
debuggable true
}
}
Add Implementation
Code:
implementation 'com.huawei.hms:awareness:1.0.4.301'
Apply plugin
Code:
apply plugin: 'com.huawei.agconnect'
7. The app need the following permissions
Code:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Code Implementation:
In this application we are using the following HMS Features:
Capture API
Location Capture: For getting gym Latitude and Longitude.
Barrier API
Location Barrier: To get notification when entering into Gym and exit from Gym .
Headset Barrier:To get alert to connect Head set for workout music
Time Barrier: To alert, once the time elapses the specified workout time.
To capture Location API using getLocation () function
Code:
private void getLocation() {
Awareness.getCaptureClient(this).getLocation()
.addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
@Override
public void onSuccess(LocationResponse locationResponse) {
Location location = locationResponse.getLocation();
latitude=location.getLatitude();
longitude=location.getLongitude();
Toast.makeText(getApplicationContext(),"Longitude:" + longitude
+ ",Latitude:" + latitude,Toast.LENGTH_SHORT).show();
mLogView.printLog("Longitude:" + longitude
+ ",Latitude:" + latitude);
mp = MediaPlayer.create(getApplicationContext(), R.raw.audio);
mp.setLooping(true);
alert_stayTimeDialog();
//add_locationBarrier_enter
AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);
Utils.addBarrier(getApplicationContext(), ENTER_BARRIER_LABEL, enterBarrier, mPendingIntent);
AwarenessBarrier exitBarrier = LocationBarrier.exit(latitude, longitude, radius);
Utils.addBarrier(getApplicationContext(), EXIT_BARRIER_LABEL, exitBarrier, mPendingIntent);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(),"Failed to get the location.",Toast.LENGTH_SHORT).show();
mLogView.printLog("Failed to get the location.");
Log.e(TAG, "Failed to get the location.", e);
}
});
mScrollView.postDelayed(()-> mScrollView.smoothScrollTo(0,mScrollView.getBottom()),200);
}
Using GymBarrierReceiver to receive the broadcast sent by Awareness Kit when the barrier status changes.
Code:
final class GymBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
int barrierPresentStatus = barrierStatus.getPresentStatus();
switch (label) {
case ENTER_BARRIER_LABEL:
if (barrierPresentStatus == BarrierStatus.TRUE) {
mLogView.printLog("You are in Gym");
alert_startWorkOutDialog();
} else if (barrierPresentStatus == BarrierStatus.FALSE) {
// mLogView.printLog("You are away from Gym");
} else {
mLogView.printLog("The location status is unknown.");
}
break;
case STAY_BARRIER_LABEL:
if (barrierPresentStatus == BarrierStatus.TRUE) {
mLogView.printLog("You have to spent "
+ workOutTime + " minutes in Gym");
} else if (barrierPresentStatus == BarrierStatus.FALSE) {
mLogView.printLog("Your time of duration in Gym is Over.");
} else {
mLogView.printLog("The location status is unknown.");
}
break;
case EXIT_BARRIER_LABEL:
if (barrierPresentStatus == BarrierStatus.TRUE) {
mLogView.printLog("You are exiting Gym");
mp.pause();
play_pause.setBackground(getDrawable(android.R.drawable.ic_media_play));
} else if (barrierPresentStatus == BarrierStatus.FALSE) {
// mLogView.printLog("You are in Gym");
} else {
mLogView.printLog("The location status is unknown.");
}
break;
case KEEPING_BARRIER_LABEL:
if (barrierPresentStatus == BarrierStatus.TRUE) {
mLogView.printLog("Audio is playing...");
play_pause.setVisibility(View.VISIBLE);
mp.start();
play_pause.setBackground(getDrawable(android.R.drawable.ic_media_pause));
} else if (barrierPresentStatus == BarrierStatus.FALSE) {
mLogView.printLog("Connect headset to play audio");
mp.pause();
play_pause.setBackground(getDrawable(android.R.drawable.ic_media_play));
} else {
mLogView.printLog("The headset status is unknown.");
}
break;
case CONNECTING_BARRIER_LABEL:
if (barrierPresentStatus == BarrierStatus.TRUE) {
mLogView.printLog("The headset is connecting. Audio is playing");
play_pause.setVisibility(View.VISIBLE);
mp.start();
play_pause.setBackground(getDrawable(android.R.drawable.ic_media_pause));
} else if (barrierPresentStatus == BarrierStatus.FALSE) {
} else {
mLogView.printLog("The headset status is unknown.");
}
break;
case DURING_TIME_PERIOD_BARRIER_LABEL:
if (barrierPresentStatus == BarrierStatus.TRUE) {
mLogView.printLog("Your Work out is "+workOutTime+" minutes.");
} else if (barrierPresentStatus == BarrierStatus.FALSE) {
mLogView.printLog("Work out time reached "+workOutTime+" minutes.");
} else {
mLogView.printLog("The time status is unknown.");
}
break;
default:
break;
}
mScrollView.postDelayed(()-> mScrollView.smoothScrollTo(0,mScrollView.getBottom()*3),200);
}
}
MediaPlayer is added as given :
Code:
MediaPlayer mp = MediaPlayer.create(this, R.raw.audio);
mp.setLooping(true);
CountDownTimer to notify remaining workout time:
Code:
public void countDown(long workOutInMs){
new CountDownTimer(workOutInMs,1000) {
@Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
workOut_txt.setText(String.format("%d:%02d", minutes, seconds));
// counter++;
}
@Override
public void onFinish() {
workOut_txt.setText("Your work out is over");
mp.pause();
play_pause.setBackground(getDrawable(android.R.drawable.ic_media_play));
play_pause.setVisibility(View.GONE);
}
}.start();
}
Added different barriers as given:
Code:
AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);
Utils.addBarrier(getApplicationContext(), ENTER_BARRIER_LABEL, enterBarrier, mPendingIntent);
AwarenessBarrier exitBarrier = LocationBarrier.exit(latitude, longitude, radius);
Utils.addBarrier(getApplicationContext(), EXIT_BARRIER_LABEL, exitBarrier, mPendingIntent);
AwarenessBarrier stayBarrier = LocationBarrier.stay(latitude, longitude, radius, workOutInMs);
Utils.addBarrier(getApplicationContext(), STAY_BARRIER_LABEL, stayBarrier, mPendingIntent);
AwarenessBarrier keepingConnectedBarrier = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);
Utils.addBarrier(getApplicationContext(), KEEPING_BARRIER_LABEL, keepingConnectedBarrier, mPendingIntent);
AwarenessBarrier connectingBarrier = HeadsetBarrier.connecting();
Utils.addBarrier(getApplicationContext(), CONNECTING_BARRIER_LABEL, connectingBarrier, mPendingIntent);
AwarenessBarrier timePeriodBarrier = TimeBarrier.duringTimePeriod(currentTimeStamp,
currentTimeStamp + workOutInMs);
Utils.addBarrier(getApplicationContext(), DURING_TIME_PERIOD_BARRIER_LABEL,
timePeriodBarrier, mPendingIntent);
The function alert_stayTimeDialog() is used to capture user workout time.
Code:
void alert_stayTimeDialog(){
final EditText edittext = new EditText(this);
edittext.setText("0");
edittext.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder alert = new AlertDialog.Builder(
this);
alert.setMessage("Your Workout Time");
alert.setTitle("Enter time in minutes ");
alert.setView(edittext);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//What ever you want to do with the value
Editable YouEditTextValue = edittext.getText();
//OR
int stayTime = Integer.parseInt(edittext.getText().toString());
//Add the code after success
SharedPreferences.Editor editor = gymOutPref.edit();
editor.putBoolean("SET_WORKOUT", true);
editor.putInt("SET_WORKOUT_TIME", stayTime);
editor.commit();
workOutTime=stayTime;
set_workout.setEnabled(false);
set_workout.setBackground(getDrawable(R.drawable.button_style_disable));
delete_workout.setEnabled(true);
delete_workout.setBackground(getDrawable(R.drawable.button_style));
workOut_txt.setText("WorkOut Time:"+stayTime+" Mnts");
Toast.makeText(getApplicationContext(), "WorkOut Time: " + stayTime, Toast.LENGTH_LONG).show();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
}
HUAWEI 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 API 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.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
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.
Code:
<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.
Code:
private void getHeadsetStatus() {
// Use the getHeadsetStatus API to get headset connection status.
Awareness.getCaptureClient(this)
.getHeadsetStatus()
.addOnSuccessListener(new OnSuccessListener<HeadsetStatusResponse>() {
@Override
public void onSuccess(HeadsetStatusResponse headsetStatusResponse) {
HeadsetStatus headsetStatus = headsetStatusResponse.getHeadsetStatus();
int status = headsetStatus.getStatus();
String statusStr = "Headset is " +
(status == HeadsetStatus.CONNECTED ? "connected" : "disconnected");
headset_status_capture.setText(statusStr);
if(status==HeadsetStatus.CONNECTED) {
headset_status_capture.setTextColor(getColor(R.color.green));
headset_status_image.setImageDrawable(getDrawable(R.drawable.ic_headset_connected));
}else{
headset_status_capture.setTextColor(getColor(R.color.red));
headset_status_image.setImageDrawable(getDrawable(R.drawable.ic_volume_up));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
headset_status_capture.setTextColor(getColor(R.color.red));
headset_status_capture.setText("Failed to get the headset capture.");
}
});
}
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.
Code:
public class HeadsetBarrierActivity extends AppCompatActivity {
private TextView headset_status_barrier;
private ImageView headset_status_barrier_image;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.headset_barrier_activity);
headset_status_barrier=findViewById(R.id.headset_status_barrier);
headset_status_barrier_image=findViewById(R.id.headset_status_barrier_image);
// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "HEADSET_BARRIER_RECEIVER_ACTION";
Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
HeadsetBarrierReceiver barrierReceiver = new HeadsetBarrierReceiver();
registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
addbarrier();
}
private void addbarrier() {
//define the barrier
AwarenessBarrier headsetBarrier = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);
//define the label for the barrier and add the barrier
String headsetBarrierLabel = "headset keeping connected barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(headsetBarrierLabel, headsetBarrier,pendingIntent).build();
Awareness.getBarrierClient(this).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add headset keeping barrier connected success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add headset keeping barrier connected failed", Toast.LENGTH_SHORT).show();
}
});
}
// define the broadcast receiver to listen for the barrier event.
private class HeadsetBarrierReceiver extends BroadcastReceiver {
private static final String TAG ="Headset Barrier" ;
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
switch(barrierStatus.getPresentStatus()) {
case BarrierStatus.TRUE:
headset_status_barrier.setTextColor(getColor(R.color.green));
headset_status_barrier.setText("Headset is connected");
headset_status_barrier_image.setImageDrawable(getDrawable(R.drawable.ic_headset_connected));
break;
case BarrierStatus.FALSE:
headset_status_barrier.setTextColor(getColor(R.color.red));
headset_status_barrier.setText("Headset is disconnected");
headset_status_barrier_image.setImageDrawable(getDrawable(R.drawable.ic_volume_up));
break;
case BarrierStatus.UNKNOWN:
headset_status_barrier.setTextColor(getColor(R.color.red));
headset_status_barrier.setText("unknown");
break;
}
}
}
}
Location Awareness
For calling Location Awareness capability we have to assign the given permissions in the manifest file.
Code:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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.
Code:
private void getLocation() {
Awareness.getCaptureClient(this).getLocation()
.addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
@Override
public void onSuccess(LocationResponse locationResponse) {
Location location = locationResponse.getLocation();
Utils.setHomeLatitude(location.getLatitude());
Utils.setHomeLongitude(location.getLongitude());
location_details_capture.setText("Longitude:" + location.getLongitude()
+ ",Latitude:" + location.getLatitude());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
location_details_capture.setText("Failed to get the location.");
}
});
}
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.
Code:
public class LocationBarrierActivity extends AppCompatActivity {
private TextView location_details_barrier;
private ImageView location_image_barrier;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_barrier_activity);
location_details_barrier=findViewById(R.id.location_details_barrier);
location_image_barrier=findViewById(R.id.location_image_barrier);
// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "LOCATION_BARRIER_RECEIVER_ACTION";
Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
LocationBarrierReceiver barrierReceiver = new LocationBarrierReceiver();
registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
addbarrier(this);
}
private void addbarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 200;
//define the barrier
AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);
//define the label for the barrier and add the barrier
String locationBarrierLabel = "Home enter barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, enterBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add Home enter barrier barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add Home enter barrier barrier failed", Toast.LENGTH_SHORT).show();
}
});
}
// define the broadcast receiver to listen for the barrier event
class LocationBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
switch(barrierStatus.getPresentStatus()) {
case BarrierStatus.TRUE:
location_details_barrier.setText("You are at Home");
break;
case BarrierStatus.FALSE:
location_details_barrier.setText("You are away from home");
break;
case BarrierStatus.UNKNOWN:
location_details_barrier.setText("unknown");
break;
}
}
}
}
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 AmbientLightResponse class that if successful, will contain information about the users light intensity.
Code:
private void getLightIntensity() {
Awareness.getCaptureClient(this).getLightIntensity()
.addOnSuccessListener(new OnSuccessListener<AmbientLightResponse>() {
@Override
public void onSuccess(AmbientLightResponse ambientLightResponse) {
AmbientLightStatus ambientLightStatus = ambientLightResponse.getAmbientLightStatus();
ambientLight_capture.setTextColor(getColor(R.color.green));
ambientLight_capture.setText("Light intensity is " + ambientLightStatus.getLightIntensity() + " lux");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
ambientLight_capture.setTextColor(getColor(R.color.red));
ambientLight_capture.setText("Failed to get the light intensity.");
}
});
}
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.
Code:
public class AmbientLightBarrierActivity extends AppCompatActivity {
private TextView ambientLight_barrier;
private ImageView ambientLight_barrier_image;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ambient_light_barrier_activity);
ambientLight_barrier=findViewById(R.id.ambient_light_barrier);
ambientLight_barrier_image=findViewById(R.id.ambient_light_barrier_image);
// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "LIGHT_BARRIER_RECEIVER_ACTION";
Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
LightBarrierReceiver barrierReceiver = new LightBarrierReceiver();
registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
addbarrier(this);
}
private void addbarrier(Context context) {
//define lux here
final float luxValue = 60.0f;
//define the barrier
AwarenessBarrier lightAboveBarrier = AmbientLightBarrier.above(luxValue);
//define the label for the barrier and add the barrier
String lightBarrierLabel = "light above barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(lightBarrierLabel, lightAboveBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add light abov barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add light above barrier failed", Toast.LENGTH_SHORT).show();
}
});
}
// define the broadcast receiver to listen for the barrier event.
class LightBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
switch(barrierStatus.getPresentStatus()) {
case BarrierStatus.TRUE:
ambientLight_barrier.setTextColor(getColor(R.color.green));
ambientLight_barrier.setText("Room light is sufficient");
break;
case BarrierStatus.FALSE:
ambientLight_barrier.setTextColor(getColor(R.color.red));
ambientLight_barrier.setText("Room light is minimal");
break;
case BarrierStatus.UNKNOWN:
ambientLight_barrier.setTextColor(getColor(R.color.red));
ambientLight_barrier.setText("Unknown");
break;
}
}
}
}
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.
Code:
<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.
Code:
private void getBluetoothStatus() {
int deviceType = 0; // Value 0 indicates a Bluetooth car stereo.
Awareness.getCaptureClient(this).getBluetoothStatus(deviceType)
.addOnSuccessListener(new OnSuccessListener<BluetoothStatusResponse>() {
@Override
public void onSuccess(BluetoothStatusResponse bluetoothStatusResponse) {
BluetoothStatus bluetoothStatus = bluetoothStatusResponse.getBluetoothStatus();
int status = bluetoothStatus.getStatus();
String statusStr = "The Bluetooth car stereo is " +
(status == BluetoothStatus.CONNECTED ? "connected" : "disconnected");
bluetooth_status_capture.setText(statusStr);
if(status== BluetoothStatus.CONNECTED) {
bluetooth_status_capture.setTextColor(getColor(R.color.green));
bluetooth_status_image.setImageDrawable(getDrawable(R.drawable.bluetooth_connected));
}else{
bluetooth_status_capture.setTextColor(getColor(R.color.red));
bluetooth_status_image.setImageDrawable(getDrawable(R.drawable.bluetooth_disconnected));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
bluetooth_status_capture.setText("Failed to get Bluetooth status.");
}
});
}
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.
Code:
public class BluetoothBarrierActivity extends AppCompatActivity {
private TextView bluetooth_status_barrier;
private ImageView bluetooth_status_barrier_image;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth_barrier_activity);
bluetooth_status_barrier=findViewById(R.id.bluetooth_status_barrier);
bluetooth_status_barrier_image=findViewById(R.id.bluetooth_status_barrier_image);
// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "BLUETOOTH_BARRIER_RECEIVER_ACTION";
Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
BluetoothBarrierReceiver barrierReceiver = new BluetoothBarrierReceiver();
registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
addbarrier(this);
}
private void addbarrier(Context context) {
final int deviceType = 0; // Value 0 indicates a Bluetooth car stereo.
//define the barrier
AwarenessBarrier connectingBarrier = BluetoothBarrier.connecting(deviceType);
String bluetoothBarrierLabel = "bluetooth connecting barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(bluetoothBarrierLabel, connectingBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add bluetooth connecting barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add bluetooth connecting barrier failed", Toast.LENGTH_SHORT).show();
}
});
}
// define the broadcast receiver to listen for the barrier event.
class BluetoothBarrierReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String label = barrierStatus.getBarrierLabel();
switch(barrierStatus.getPresentStatus()) {
case BarrierStatus.TRUE:
bluetooth_status_barrier.setTextColor(getColor(R.color.green));
bluetooth_status_barrier.setText("The Bluetooth car stereo is connected");
bluetooth_status_barrier_image.setImageDrawable(getDrawable(R.drawable.bluetooth_connected));
break;
case BarrierStatus.FALSE:
bluetooth_status_barrier.setTextColor(getColor(R.color.red));
bluetooth_status_barrier.setText("The Bluetooth car stereo is not connected");
bluetooth_status_barrier_image.setImageDrawable(getDrawable(R.drawable.bluetooth_disconnected));
break;
case BarrierStatus.UNKNOWN:
bluetooth_status_barrier.setText("Unknown");
break;
}
}
}
}
References:
https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/awareness-introduction
More information like this, you can visit HUAWEI Developer Forum
Introduction:
Location awareness is used to get the location details and to set barriers based on the location.
Barrier API
This API is used to set a location barrier based on the target location, radius (not smaller than 200 meters), and stay duration.
For example, if you set a circular region with a 300 meter radius in the target location as a barrier, a notification will be triggered if the user has stayed in the area for the specified period of time.
Capture API
We can use the Capture API to obtain the latitude and longitude of the current location.
For calling Location Awareness capability we have to assign the given permissions in the manifest file.
Code:
<!-- Location permission, which is sensitive. After the permission is declared, you need to dynamically apply for it in the code. -->
<uses-permission android:name="android.permission.ACCESS_FINE_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. -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
To understand more about Barrier API and Capture API here we can discuss about the following two classes:
LocationBarrier
LocationResponse
Let's go to the details.
LocationBarrier
This class features, barriers to be triggered while enter, exit and stay in a specified location.
Following are the methods in LocationBarrier class.
enter
exit
stay
1) enter
After this barrier is added, when a user enters a specified area, the barrier status is TRUE and a barrier event is reported. After 5 seconds, the barrier status changes to FALSE.
Syntax:
public static AwarenessBarrier enter(double latitude, double longitude, double radius)
Parameters
Code:
private void add_enterBarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 300;
//define the barrier
AwarenessBarrier enterBarrier = LocationBarrier.enter(latitude, longitude, radius);
//define the label for the barrier and add the barrier
String locationBarrierLabel = "enter barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, enterBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add enter barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add enter barrier failed", Toast.LENGTH_SHORT).show();
}
});
}
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
2) exit
After this barrier is added, when a user exits from a specified area, the barrier status is TRUE and a barrier event is reported. After 5 seconds, the barrier status changes to FALSE.
Syntax:
public static AwarenessBarrier exit(double latitude, double longitude, double radius)
Code:
private void add_exitBarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 300;
//define the barrier
AwarenessBarrier exitBarrier = LocationBarrier.exit(latitude, longitude, radius);
//define the label for the barrier and add the barrier
String locationBarrierLabel = "exit barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, exitBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add exit barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add exit barrier failed", Toast.LENGTH_SHORT).show();
}
});
}
3) stay
After this barrier is added, if a user is in the area, the barrier status is TRUE; if a user enters the area and stays in the area for a specified time period, the barrier status is TRUE and a barrier event is reported.
Syntax:
public static AwarenessBarrier stay(double latitude, double longitude, double radius, long timeOfDuration)
Code:
private void add_stayBarrier(Context context) {
//Home latitude and longitude
double latitude = Utils.getHomeLatitude();
double longitude = Utils.getHomeLongitude();
double radius = 300;
//define the barrier
AwarenessBarrier stayBarrier = LocationBarrier.stay(latitude, longitude, radius,2000);
//define the label for the barrier and add the barrier
String locationBarrierLabel = "stay barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(locationBarrierLabel, stayBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add stay barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add stay barrier failed", Toast.LENGTH_SHORT).show();
}
});
}
LocationResponse:
This class provides response to the location request. The app can call the getLocation method provided by CaptureClient to obtain the last geographical location response.
Public Methods:
There is one method in LocationResponse class.
getLocation
1)getLocation
This method is used to gets the location information, including the current longitude and latitude of the device.
Syntax:
public Location getLocation()
Code:
private void getLocation() {
Awareness.getCaptureClient(this).getLocation()
.addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
@Override
public void onSuccess(LocationResponse locationResponse) {
Location location = locationResponse.getLocation();
Utils.setHomeLatitude(location.getLatitude());
Utils.setHomeLongitude(location.getLongitude());
location_details_capture.setText("Capture Your Location\nLongitude:" + location.getLongitude()
+ "\nLatitude:" + location.getLatitude());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
location_details_capture.setText("Failed to get the location.");
}
});
}
References:
https://developer.huawei.com/consumer/en/doc/awareness-barrier-locationbarrier
https://developer.huawei.com/consumer/en/doc/awareness-capture-locationresponse
Interesting, thanks.
Can we use location kit also right for geofence, then what is the usage of this awareness kit.
More information like this, you can visit HUAWEI Developer Forum
Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202331460267570004&fid=0101188387844930001
Introduction
HMS Ambient Light Awareness is used to obtain the illuminance and to set barriers based on the illuminance. We can use this features in photography applications, auto flash light applications etc.
This article provides information about main classes and methods used for HMS Ambient Light Awareness.
AmbientLightBarrier
This class features, barriers to be triggered while light intensity is below, above or in the specified range.
Following are the methods in AmbientLightBarrier class.
range
below
above
All these methods will return Awareness barrier object.
range
When the illuminance is within the range specified by [minLightIntensity, maxLightIntensity), the barrier status is TRUE and a barrier event is reported. When the illuminance is not within the range, the barrier status is FALSE and a barrier event is reported.
Syntax:
public static AwarenessBarrier range(float minLightIntensity, float maxLightIntensity)
Code:
//define lux range here
final float minLightIntensity = 40.0f;
final float maxLightIntensity =60.0f;
//define the barrier
AwarenessBarrier lightRangeBarrier = AmbientLightBarrier.range(minLightIntensity,maxLightIntensity);
//define the label for the barrier and add the barrier
String lightBarrierLabel = "light range barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(lightBarrierLabel, lightRangeBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add light range barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add light range barrier failed", Toast.LENGTH_SHORT).show();
}
});
It will Throws IllegalArgumentException when the value of minLightIntensity is less than 0, or the value of MinLightIntensity is greater than that of maxLightIntensity, or the value of maxLightIntensity is greater than that of Float.MAX_VALUE.
below
When the illuminance is within the range specified by [0, maxLightIntensity), the barrier status is TRUE and a barrier event is reported. When the illuminance is not within the range, the barrier status is FALSE and a barrier event is reported.
Syntax:
public static AwarenessBarrier below(float maxLightIntensity)
Code:
//define lux here
final float maxLightIntensity = 50.0f;
//define the barrier
AwarenessBarrier lightBelowBarrier = AmbientLightBarrier.below(maxLightIntensity);
//define the label for the barrier and add the barrier
String lightBarrierLabel = "light below barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(lightBarrierLabel, lightBelowBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add light below barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add light below barrier failed", Toast.LENGTH_SHORT).show();
}
});
It will throw an IllegalArgumentException when the value of maxLightIntensityis less than 0 or the value of maxLightIntensityis greater than that of Float.MAX_VALUE.
above
When the illuminance is within the range specified by [minLightIntensity, Float.MAX_VALUE), the barrier status is TRUE and a barrier event is reported. When the illuminance is not within the range, the barrier status is FALSE and a barrier event is reported.
Syntax:
public static AwarenessBarrier above(float minLightIntensity)
Code:
//define lux here
final float minLightIntensity = 40.0f;
//define the barrier
AwarenessBarrier lightAboveBarrier = AmbientLightBarrier.above(minLightIntensity);
//define the label for the barrier and add the barrier
String lightBarrierLabel = "light above barrier";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(lightBarrierLabel, lightAboveBarrier,pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(), "add light above barrier success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), "add light above barrier failed", Toast.LENGTH_SHORT).show();
}
});
It will throws IllegalArgumentException when the value of minLightIntensity is less than 0, or the value of minLightIntensity is greater than that of Float.MAX_VALUE.
AmbientLightResponse
This class provide the response to the request for obtaining the the illuminance status. We can use the getAmbientLightStatus method provided by CaptureClient to obtain illuminance status.
getAmbientLightStatus
This method is used to obtains the illuminance status. The value of AmbientLightStauts contains the illuminance in lux.
Syntax:
public AmbientLightStatus getAmbientLightStatus()
Code:
private void getLightIntensity() {
Awareness.getCaptureClient(this).getLightIntensity()
.addOnSuccessListener(new OnSuccessListener<AmbientLightResponse>() {
@Override
public void onSuccess(AmbientLightResponse ambientLightResponse) {
AmbientLightStatus ambientLightStatus = ambientLightResponse.getAmbientLightStatus();
ambientLight_capture.setTextColor(getColor(R.color.green));
ambientLight_capture.setText("Light intensity is " + ambientLightStatus.getLightIntensity() + " lux");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
ambientLight_capture.setTextColor(getColor(R.color.red));
ambientLight_capture.setText("Failed to get the light intensity.");
}
});
}
Output
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Reference
https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/awareness-ambientlight-dev
Hello everyone, in this article we will learn how to use Huawei Awareness Kit with foreground service to send notification when certain condition is met.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
Huawei Awareness Kit enables us to observe some environmental factors such as time, location, behavior, audio device status, ambient light, weather and nearby beacons. So, why don’t we create our own conditions to be met and observe them even when the application is not running?
First of all, we need to do HMS Core integration to be able to use Awareness Kit. I will not go into the details of that because it is already covered here.
If you are done with the integration, let’s start coding.
Activity Class
We will keep our activity class pretty simple to prevent any confusion. It will only be responsible for starting the service:
Java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceStartIntent = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceStartIntent);
}
else {
startService(serviceStartIntent);
}
}
}
However, we need to add the following permission to start a service correctly:
XML:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Service Class
Now, let’s talk about the service class. We are about to create a service which will run even when the application is killed. However, this comes with some restrictions. Since the Android Oreo, if an application wants to start a foreground service, it must inform the user by a notification which needs to be visible during the lifetime of the foreground service. Also, this notification needs to be used to start foreground. Therefore, our first job in the service class is to create this notification and call the startForeground() method with it:
Java:
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
notification = createCustomNotification();
else
notification = new Notification();
startForeground(1234, notification);
And here is how we create the information notification we need for SDK versions later than 25:
Java:
@RequiresApi(api = Build.VERSION_CODES.O)
private Notification createCustomNotification() {
NotificationChannel notificationChannel = new NotificationChannel("1234", "name", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "com.awarenesskit.demo");
return notificationBuilder
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Observing headset status")
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.build();
}
Note: You should replace the application id above with the application id of your application.
Now, it is time to prepare the parameters to create a condition to be met called barrier:
Java:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
headsetBarrierReceiver = new HeadsetBarrierReceiver();
registerReceiver(headsetBarrierReceiver, new IntentFilter(barrierReceiverAction));
AwarenessBarrier headsetBarrier = HeadsetBarrier.connecting();
createBarrier(this, HEADSET_BARRIER_LABEL, headsetBarrier, pendingIntent);
Here we have sent the required parameters to a method which will create a barrier for observing headset status. If you want, you can use other awareness barriers too.
Creating a barrier is a simple and standard process which will be taken care of the following method:
Java:
private void createBarrier(Context context, String barrierLabel, AwarenessBarrier barrier, PendingIntent pendingIntent) {
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, barrier, pendingIntent).build();
Awareness.getBarrierClient(context).updateBarriers(request)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void void1) {
System.out.println("Barrier Create Success");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
System.out.println("Barrier Create Fail");
}
});
}
We will be done with the service class after adding the following methods:
Java:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
unregisterReceiver(headsetBarrierReceiver);
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
And of course, we shouldn’t forget to add our service to the manifest file:
XML:
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
Broadcast Receiver Class
Lastly, we need to create a broadcast receiver where we will observe and handle the changes in the headset status:
Java:
public class HeadsetBarrierReceiver extends BroadcastReceiver {
public static final String HEADSET_BARRIER_LABEL = "HEADSET_BARRIER_LABEL";
@Override
public void onReceive(Context context, Intent intent) {
BarrierStatus barrierStatus = BarrierStatus.extract(intent);
String barrierLabel = barrierStatus.getBarrierLabel();
int barrierPresentStatus = barrierStatus.getPresentStatus();
if (HEADSET_BARRIER_LABEL.equals(barrierLabel)) {
if (barrierPresentStatus == BarrierStatus.TRUE) {
System.out.println("The headset is connected.");
createNotification(context);
}
else if (barrierPresentStatus == BarrierStatus.FALSE) {
System.out.println("The headset is disconnected.");
}
}
}
When a change occurs in the headset status, this method will receive the information. Here, the value of barrierPresentStatus will determine if headset is connected or disconnected.
At this point, we can detect that headset is just connected, so it is time to send a notification. The following method will take care of that:
Java:
private void createNotification(Context context) {
// Create PendingIntent to make user open the application when clicking on the notification
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1234, new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "channelId")
.setSmallIcon(R.drawable.ic_headset)
.setContentTitle("Cool Headset!")
.setContentText("Want to listen to some music ?")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("channelId", "ChannelName", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(1234, notificationBuilder.build());
}
Output
When headset is connected, the following notification will be created even if the application is not running:
Final Thoughts
We have learned how to use Barrier API of Huawei Awareness Kit with a foreground service to observe the changes in environmental factors even when the application is not running.
As you may notice, the permanent notification indicating that the application is running in the background is not dismissible by the user which can be annoying. Even though these notifications can be dismissed by some third party applications, not every user has those applications, so you should be careful when deciding to build such services.
In this case, observing the headset status was just an example, so it might not be the best scenario for this use case, but Huawei Awareness Kit has many other great features that you can use with foreground services in your projects.
References
You can check the complete project on GitHub.
Note that, you will not be able to run this project because you don’t have the agconnect-services.json file for it. Therefore, you should only take it as a reference to create your own project.
Does Awareness Kit support wearable devices such as smart watches?