More information like this, you can visit HUAWEI Developer Forum
HMS support different type of awareness like Headset, Location, Time, Behavior etc. In some real-time scenarios we may have to use multiple awareness simultaneously and barrier logic operation is used to integrate these barriers.
For example, we can detect that a person is connecting a headset while running or he is in the beach during sunset.
This article explains the integration of multiple barriers through different logic operations. For that we have to know more about AwarenessBarrier class.
Awareness Barrier
AwarenessBarrier is Barrier basic class, which supports barrier logic operations.
Public Methods:
The following are the public methods in AwarenessBarrier class
public static AwarenessBarrier and(AwarenessBarrier… barriers)
public static AwarenessBarrier and(Collection<AwarenessBarrier> barriers)
public static AwarenessBarrier not(AwarenessBarrier barrier)
public static AwarenessBarrier or(AwarenessBarrier… barriers)
public static AwarenessBarrier or(Collection<AwarenessBarrier> barriers)
Let us go to the details.
public static AwarenessBarrier and(AwarenessBarrier… barriers)
This method contain barrier set as parameter and returns the integrated barriers after the AND logic operation.
Example: Here the integrated barrier will detect whether the user is on the beach during sunrise.
Code:
//Logical AND Operation
//define here the Barrier stay in beach
AwarenessBarrier stayBeachBarrier = LocationBarrier.stay(beachLatitude, beachLongitude, radius,2000);
//define here the Barrier in sunrise period
AwarenessBarrier inSunrisePeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
5* oneHourMilliSecond, 7 * oneHourMilliSecond);
//and operation integrate barriers and detect whether the user is on the beach during sunrise.
AwarenessBarrier beachANDsunrise= AwarenessBarrier.and(stayBeachBarrier,inSunrisePeriodBarrier);
//define the label for the barrier and add the barrier
String barrierLabel = "beach and sunrise";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, beachANDsunrise ,pendingIntent).build();
public static AwarenessBarrier and(Collection<AwarenessBarrier> barriers)
This method contains Collection which consist of barrier set as a parameter and returns the integrated barriers after the AND logic Operation.
Example: Here the integrated barrier will detect whether the user is on the beach during sunrise.
Code:
//Logical AND Operation with collection
//define here the Barrier stay in beach
AwarenessBarrier stayBeachBarrier = LocationBarrier.stay(beachLatitude, beachLongitude, radius,2000);
//define here the Barrier in sunrise period
AwarenessBarrier inSunrisePeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
5* oneHourMilliSecond, 7 * oneHourMilliSecond);
//and operation integrate barriers and detect whether the user is on the beach during sunrise.
Collection<AwarenessBarrier> collection=new ArrayList<AwarenessBarrier>();
collection.add(stayBeachBarrier);
collection.add(inSunrisePeriodBarrier);
// collection and operation
AwarenessBarrier beachANDsunrise=AwarenessBarrier.and(collection);
//define the label for the barrier and add the barrier
String barrierLabel = "beach and sunrise";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, beachANDsunrise ,pendingIntent).build();
public static AwarenessBarrier not(AwarenessBarrier barrier)
This method contains barrier as a parameter and returns the barrier after the NOT logic operation.
Example: Here the logical NOT is using light intensity above 60 lux and will detect the whether the light intensity is below 60 lux or not.
Code:
//Logical NOT Operation
//define lux here
final float luxValue = 60.0f;
//define the barrier
AwarenessBarrier intensityAbove = AmbientLightBarrier.above(luxValue);
//NOT operation above barrier.
AwarenessBarrier NOTintensityAbove= AwarenessBarrier.not(intensityAbove);
//define the label for the barrier and add the barrier
String barrierLabel = "NOT Intensity Above";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, NOTintensityAbove ,pendingIntent).build();
public static AwarenessBarrier or(AwarenessBarrier… barriers)
This method contain barrier set as parameter and returns the integrated barrier after the OR logic operation.
Example: Here the integrated barrier will detect whether the user is walking or connected headset.
Code:
//Logical OR Operation
//define here the Barrier head is is keeping connected
AwarenessBarrier headsetKeepingConnected = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);
//define here the Barrier user is walking
AwarenessBarrier keepingWalking = AwarenessBarrier.and(BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING));
//and operation integrate barriers and detect whether he is walking or connected headset.
AwarenessBarrier headsetConnectedORwalking= AwarenessBarrier.or(headsetKeepingConnected,keepingWalking);
//define the label for the barrier and add the barrier
String barrierLabel = "headset connecting and walking";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, headsetConnectedORwalking ,pendingIntent).build();
public static AwarenessBarrier or(Collection<AwarenessBarrier> barriers)
This method contains Collection which consist of barrier set as a parameter and returns the integrated barrier after the OR logic operation.
Example: Here the integrated barrier will detect whether the user is walking or connected headset.
Code:
//Logical OR Operation with Collection
//define here the Barrier head is is keeping connected
AwarenessBarrier headsetKeepingConnected = HeadsetBarrier.keeping(HeadsetStatus.CONNECTED);
//define here the Barrier user is walking
AwarenessBarrier keepingWalking = AwarenessBarrier.and(BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING));
//and operation integrate barriers and detect whether he is walking or connected headset.
Collection<AwarenessBarrier> collection=new ArrayList<AwarenessBarrier>();
collection.add(headsetKeepingConnected);
collection.add(keepingWalking);
// Collection 'or' operation
AwarenessBarrier headsetConnectedORwalking=AwarenessBarrier.or(collection);
//define the label for the barrier and add the barrier
String barrierLabel = "headset connecting and walking";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, headsetConnectedORwalking ,pendingIntent).build();
Sample:
Let us consider a real-time scenario to detect whether a person is walking on the beach during sunset or sunrise.
Code:
public class BarrierIntegrationActivity extends AppCompatActivity {
private static final String TAG ="BarrierIntegration" ;
private TextView barrier_status,sunrise_sunset_txt,beach_txt,walking_txt;
private ImageView sunrise_sunset_img,beach_img,walking_img;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barrier_integration);
barrier_status=findViewById(R.id.barrier_status);
sunrise_sunset_img=findViewById(R.id.sunrise_sunset_img);
beach_img=findViewById(R.id.beach_img);
walking_img=findViewById(R.id.walking_img);
sunrise_sunset_txt=findViewById(R.id.sunrise_sunset_txt);
beach_txt=findViewById(R.id.beach_txt);
walking_txt=findViewById(R.id.walking_txt);
// define PendingIntent that will be triggered upon a barrier status change.
final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "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) {
double beachLatitude = Utils.getHomeLatitude();
double beachLongitude = Utils.getHomeLongitude();
double radius = 300;
long oneHourMilliSecond = 60 * 60 * 1000L;
//Logical AND Operation.
//define here the Barrier in sunrise period
AwarenessBarrier inSunrisePeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNRISE_CODE,
5* oneHourMilliSecond, 7 * oneHourMilliSecond);
//define here the Barrier in sunrise period
AwarenessBarrier inSunsetPeriodBarrier = TimeBarrier.inSunriseOrSunsetPeriod(TimeBarrier.SUNSET_CODE,
17* oneHourMilliSecond, 19* oneHourMilliSecond);
//and operation integrate barriers and detect whether it is in sunrise or sunset.
AwarenessBarrier sunriseORsunset= AwarenessBarrier.or(inSunrisePeriodBarrier,inSunsetPeriodBarrier);
//define here the Barrier user is walking
AwarenessBarrier keepingWalking = AwarenessBarrier.and(BehaviorBarrier.keeping(BehaviorBarrier.BEHAVIOR_WALKING));
//define here the Barrier stay in beach
AwarenessBarrier stayBeachBarrier = LocationBarrier.stay(beachLatitude, beachLongitude, radius,2000);
//and operation integrate barriers and detect whether the user is walking on the beach during sun rise or sunset.
AwarenessBarrier walkingBeachInSunriseorSunset= AwarenessBarrier.and(keepingWalking,stayBeachBarrier,sunriseORsunset);
//define the label for the barrier and add the barrier
String barrierLabel = "walking on the beach during sunrise or sunset";
//add the barrier
BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
BarrierUpdateRequest request = builder.addBarrier(barrierLabel, walkingBeachInSunriseorSunset ,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");
barrier_status.setText("Status: TRUE" );
sunrise_sunset_img.setImageDrawable(getDrawable(R.drawable.sunrise_sunset));
beach_img.setImageDrawable(getDrawable(R.drawable.beach));
walking_img.setImageDrawable(getDrawable(R.drawable.beach_walk));
sunrise_sunset_txt.setText("Sunset");
beach_txt.setText("Beach");
walking_txt.setText("Walking");
break;
case BarrierStatus.FALSE:
Log.i(TAG, label + " status:false");
// barrier_status.setText("Status: FALSE" );
break;
case BarrierStatus.UNKNOWN:
Log.i(TAG, label + " status:unknown");
break;
}
}
}
}
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-References/awareness-barrier-awarenessbarrier
MOD ACTION:
@sujith.e
Please check your PM inbox.
Related
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 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
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?