目录

Before using beacon capabilities, you need to register beacon devices with your project. For details, please refer to Beacon Management.

Assigning Permissions in the Manifest File

Before calling the beacon awareness capability, assign required permissions in the manifest file.

  1. <!-- Location permission. This permission is sensitive and needs to be dynamically applied for in the code after being declared. -->
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  3. <!-- Bluetooth permission -->
  4. <uses-permission android:name="android.permission.BLUETOOTH" />

Importing API Classes

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

  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 beacon capture-related classes.
  5. import com.huawei.hms.kit.awareness.capture.BeaconStatusResponse;
  6. import com.huawei.hms.kit.awareness.status.BeaconStatus;
  7. // Import the beacon barrier-related classes.
  8. import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
  9. import com.huawei.hms.kit.awareness.barrier.BarrierStatus;
  10. import com.huawei.hms.kit.awareness.barrier.BeaconBarrier;
  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 beacon 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.

  1. String namespace = "sample namespace";
  2. String type = "sample type";
  3. byte[] content = new byte[]{'s', 'a', 'm', 'p', 'l', 'e'};
  4. BeaconStatus.Filter filter = BeaconStatus.Filter.match(namespace, type, content);
  5. Awareness.getCaptureClient(this).getBeaconStatus(filter)
  6.        .addOnSuccessListener(new OnSuccessListener<BeaconStatusResponse>() {
  7.            @Override
  8.            public void onSuccess(BeaconStatusResponse beaconStatusResponse) {
  9.                List<BeaconStatus.BeaconData> beaconDataList = beaconStatusResponse.
  10.                        getBeaconStatus().getBeaconData();
  11.                if (beaconDataList != null && beaconDataList.size() != 0) {
  12.                    int i = 1;
  13.                    StringBuilder builder = new StringBuilder();
  14.                    for (BeaconStatus.BeaconData beaconData : beaconDataList) {
  15.                        builder.append("Beacon Data ").append(i);
  16.                        builder.append(" namespace:").append(beaconData.getNamespace());
  17.                        builder.append(",type:").append(beaconData.getType());
  18.                        builder.append(",content:").append(Arrays.toString(beaconData.getContent()));
  19.                        builder.append("; ");
  20.                        i++;
  21.                    }
  22.                    Log.i(TAG, builder.toString());
  23.                } else {
  24.                    Log.i(TAG, "no beacons match filter nearby");
  25.                }
  26.            }
  27.        })
  28.        .addOnFailureListener(new OnFailureListener() {
  29.            @Override
  30.            public void onFailure(Exception e) {
  31.                Log.e(TAG, "get beacon status failed", e);
  32.            }
  33.        });

Developing barrier capabilities

The following example illustrates how to develop a barrier triggered by the discover condition. That is, the barrier will be triggered when the filter parameters passed by a beacon match those defined here.

1. Define the barrier.

  1. String namespace = "sample namespace";
  2. String type = "sample type";
  3. byte[] content = new byte[]{'s', 'a', 'm', 'p', 'l', 'e'};
  4. BeaconStatus.Filter filter = BeaconStatus.Filter.match(namespace, type, content);
  5. AwarenessBarrier discoverBeaconBarrier = BeaconBarrier.discover(filter);

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.

  1. final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "BEACON_BARRIER_RECEIVER_ACTION";
  2. Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
  3. PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  4. BeaconBarrierReceiver barrierReceiver = new BeaconBarrierReceiver();
  5. registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));

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

  1. String beaconBarrierLabel = "discover beacon barrier";
  2. BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
  3. BarrierUpdateRequest request = builder.addBarrier(beaconBarrierLabel, discoverBeaconBarrier,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.

  1. class BeaconBarrierReceiver 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. }