We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core GuidesAwareness KitApp DevelopmentClient DevelopmentAmbient Light Awareness

Ambient Light Awareness

Importing API Classes

To use the ambient light awareness capability, you need to import the public capability classes of HUAWEI Awareness Kit, and also the ambient light-related classes.

Collapse
Word wrap
Dark theme
Copy code
  1. import com.huawei.hmf.tasks.OnFailureListener;
  2. import com.huawei.hmf.tasks.OnSuccessListener;
  3. import com.huawei.hms.kit.awareness.Awareness;
  4. // Import the ambient light capture-related classes.
  5. import com.huawei.hms.kit.awareness.capture.AmbientLightResponse;
  6. import com.huawei.hms.kit.awareness.status.AmbientLightStatus;
  7. // Import the ambient light barrier-related classes.
  8. import com.huawei.hms.kit.awareness.barrier.AmbientLightBarrier;
  9. import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
  10. import com.huawei.hms.kit.awareness.barrier.BarrierStatus;
  11. import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;

Developing Capabilities

Developing capture capabilities

1. Obtain the Capture Client object of HUAWEI Awareness Kit.

2. Call the ambient light intensity 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.

Collapse
Word wrap
Dark theme
Copy code
  1. Awareness.getCaptureClient(this).getLightIntensity()
  2.        .addOnSuccessListener(new OnSuccessListener<AmbientLightResponse>() {
  3.            @Override
  4.            public void onSuccess(AmbientLightResponse ambientLightResponse) {
  5.                AmbientLightStatus ambientLightStatus = ambientLightResponse.getAmbientLightStatus();
  6.                Log.i(TAG, "Light intensity is " + ambientLightStatus.getLightIntensity() + " lux");
  7.            }
  8.        })
  9.        .addOnFailureListener(new OnFailureListener() {
  10.            @Override
  11.            public void onFailure(Exception e) {
  12.                Log.e(TAG, "get light intensity failed", e);
  13.            }
  14.        });

Developing barrier capabilities

The following example illustrates how to develop a barrier triggered by the above condition. That is, the barrier will be triggered when the ambient light intensity exceeds the preset value.

1. Define the barrier.

Collapse
Word wrap
Dark theme
Copy code
  1. final float luxValue = 2500.0f;
  2. AwarenessBarrier lightAboveBarrier = AmbientLightBarrier.above(luxValue);

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.

Collapse
Word wrap
Dark theme
Copy code
  1. final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "LIGHT_BARRIER_RECEIVER_ACTION";
  2. Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
  3. PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  4. LightBarrierReceiver barrierReceiver = new LightBarrierReceiver();
  5. registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));

3. Define the label for the barrier and add the barrier.

Collapse
Word wrap
Dark theme
Copy code
  1. String lightBarrierLabel = "light above barrier";
  2. BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
  3. BarrierUpdateRequest request = builder.addBarrier(lightBarrierLabel, lightAboveBarrier,pendingIntent).build();
  4. Awareness.getBarrierClient(context).updateBarriers(request)
  5.        .addOnSuccessListener(new OnSuccessListener<Void>() {
  6.            @Override
  7.            public void onSuccess(Void aVoid) {
  8.                Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show();
  9.            }
  10.        })
  11.        .addOnFailureListener(new OnFailureListener() {
  12.            @Override
  13.            public void onFailure(Exception e) {
  14.                Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show();
  15.                Log.e(TAG, "add barrier failed", e);
  16.            }
  17.        });

4. Define the broadcast receiver to listen for the barrier event for further processing.

Collapse
Word wrap
Dark theme
Copy code
  1. class LightBarrierReceiver extends BroadcastReceiver {
  2.    @Override
  3.    public void onReceive(Context context, Intent intent) {
  4.        BarrierStatus barrierStatus = BarrierStatus.extract(intent);
  5.        String label = barrierStatus.getBarrierLabel();
  6.        switch(barrierStatus.getPresentStatus()) {
  7.            case BarrierStatus.TRUE:
  8.                Log.i(TAG, label + " status:true");
  9.                break;
  10.            case BarrierStatus.FALSE:
  11.                Log.i(TAG, label + " status:false");
  12.                break;
  13.            case BarrierStatus.UNKNOWN:
  14.                Log.i(TAG, label + " status:unknown");
  15.                break;
  16.        }
  17.    }
  18. }
This page may contain third-party content. For details, click here.