Before calling the Bluetooth car stereo awareness capability, assign required permissions in the manifest file.
- <!-- Bluetooth permission -->
- <uses-permission android:name="android.permission.BLUETOOTH" />
To use the Bluetooth car stereo awareness capability, you need to import the public capability classes of HUAWEI Awareness Kit, and also the Bluetooth car stereo-related classes.
- import com.huawei.hmf.tasks.OnFailureListener;
- import com.huawei.hmf.tasks.OnSuccessListener;
- import com.huawei.hms.kit.awareness.Awareness;
- // Import the Bluetooth car stereo capture-related classes.
- import com.huawei.hms.kit.awareness.capture.BluetoothStatusResponse;
- import com.huawei.hms.kit.awareness.status.BluetoothStatus;
- // Import the Bluetooth car stereo barrier-related classes.
- import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
- import com.huawei.hms.kit.awareness.barrier.BarrierStatus;
- import com.huawei.hms.kit.awareness.barrier.BluetoothBarrier;
- import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;
1. Obtain the Capture Client object of HUAWEI Awareness Kit.
2. Call the Bluetooth car stereo status 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.
- 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 stateStr = "The Bluetooth car stereo is " +
- (status == BluetoothStatus.CONNECTED ? "connected" : "disconnected");
- Log.i(TAG, stateStr);
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- Log.e(TAG, "get bluetooth status failed", e);
- }
- });
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.
1. Define the barrier.
- final int deviceType = 0; // Value 0 indicates a Bluetooth car stereo.
- AwarenessBarrier connectingBarrier = BluetoothBarrier.connecting(deviceType);
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.
- final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "BLUETOOTH_BARRIER_RECEIVER_ACTION";
- Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- BluetoothBarrierReceiver barrierReceiver = new BluetoothBarrierReceiver();
- registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
3. Add the barrier.
- String bluetoothBarrierLabel = "bluetooth connecting 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 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);
- }
- });
4. Define the broadcast receiver to listen for the barrier event for further processing.
- 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:
- Log.i(TAG, label + " status:true");
- break;
- case BarrierStatus.FALSE:
- Log.i(TAG, label + " status:false");
- break;
- case BarrierStatus.UNKNOWN:
- Log.i(TAG, label + " status:unknown");
- break;
- }
- }
- }