/* * Copyright 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.bluetooth; import android.annotation.CallbackExecutor; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.RequiresPermission; import android.annotation.SdkConstant; import android.annotation.SystemApi; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresBluetoothLocationPermission; import android.bluetooth.annotations.RequiresBluetoothScanPermission; import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanSettings; import android.content.Context; import android.util.Log; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; /** * This class provides the public APIs for the LE Audio Broadcast Assistant role, which implements * client side control points for Broadcast Audio Scan Service (BASS). * *
An LE Audio Broadcast Assistant can help a Broadcast Sink to scan for available Broadcast * Sources. The Broadcast Sink achieves this by offloading the scan to a Broadcast Assistant. * This is facilitated by the Broadcast Audio Scan Service (BASS). A BASS server is a GATT * server that is part of the Scan Delegator on a Broadcast Sink. A BASS client instead runs on * the Broadcast Assistant. * *
Once a GATT connection is established between the BASS client and the BASS server, the * Broadcast Sink can offload the scans to the Broadcast Assistant. Upon finding new Broadcast * Sources, the Broadcast Assistant then notifies the Broadcast Sink about these over the * established GATT connection. The Scan Delegator on the Broadcast Sink can also notify the * Assistant about changes such as addition and removal of Broadcast Sources. * * In the context of this class, BASS server will be addressed as Broadcast Sink and BASS client * will be addressed as Broadcast Assistant. * *
BluetoothLeBroadcastAssistant is a proxy object for controlling the Broadcast Assistant * service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get the * BluetoothLeBroadcastAssistant proxy object. * * @hide */ @SystemApi public final class BluetoothLeBroadcastAssistant implements BluetoothProfile { private static final String TAG = "BluetoothLeBroadcastAssistant"; private static final boolean DBG = true; /** * This class provides a set of callbacks that are invoked when scanning for Broadcast Sources * is offloaded to a Broadcast Assistant. * * @hide */ @SystemApi public interface Callback { /** @hide */ @Retention(RetentionPolicy.SOURCE) @IntDef(value = { BluetoothStatusCodes.ERROR_UNKNOWN, BluetoothStatusCodes.REASON_LOCAL_APP_REQUEST, BluetoothStatusCodes.REASON_LOCAL_STACK_REQUEST, BluetoothStatusCodes.REASON_REMOTE_REQUEST, BluetoothStatusCodes.REASON_SYSTEM_POLICY, BluetoothStatusCodes.ERROR_HARDWARE_GENERIC, BluetoothStatusCodes.ERROR_LE_BROADCAST_ASSISTANT_DUPLICATE_ADDITION, BluetoothStatusCodes.ERROR_BAD_PARAMETERS, BluetoothStatusCodes.ERROR_REMOTE_LINK_ERROR, BluetoothStatusCodes.ERROR_REMOTE_NOT_ENOUGH_RESOURCES, BluetoothStatusCodes.ERROR_LE_BROADCAST_ASSISTANT_INVALID_SOURCE_ID, BluetoothStatusCodes.ERROR_ALREADY_IN_TARGET_STATE, BluetoothStatusCodes.ERROR_REMOTE_OPERATION_REJECTED, }) @interface Reason {} /** * Callback invoked when the implementation started searching for nearby Broadcast Sources. * * @param reason reason code on why search has started * @hide */ @SystemApi void onSearchStarted(@Reason int reason); /** * Callback invoked when the implementation failed to start searching for nearby broadcast * sources. * * @param reason reason for why search failed to start * @hide */ @SystemApi void onSearchStartFailed(@Reason int reason); /** * Callback invoked when the implementation stopped searching for nearby Broadcast Sources. * * @param reason reason code on why search has stopped * @hide */ @SystemApi void onSearchStopped(@Reason int reason); /** * Callback invoked when the implementation failed to stop searching for nearby broadcast * sources. * * @param reason for why search failed to start * @hide */ @SystemApi void onSearchStopFailed(@Reason int reason); /** * Callback invoked when a new Broadcast Source is found together with the * {@link BluetoothLeBroadcastMetadata}. * * @param source {@link BluetoothLeBroadcastMetadata} representing a Broadcast Source * @hide */ @SystemApi void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source); /** * Callback invoked when a new Broadcast Source has been successfully added to the * Broadcast Sink. * * Broadcast audio stream may not have been started after this callback, the caller need * to monitor * {@link #onReceiveStateChanged(BluetoothDevice, int, BluetoothLeBroadcastReceiveState)} * to see if synchronization with Broadcast Source is successful * * When isGroupOp is true when * {@link #addSource(BluetoothDevice, BluetoothLeBroadcastMetadata, boolean)} * is called, each Broadcast Sink device in the coordinated set will trigger and individual * update * * A new source could be added by the Broadcast Sink itself or other Broadcast Assistants * connected to the Broadcast Sink and in this case the reason code will be * {@link BluetoothStatusCodes#REASON_REMOTE_REQUEST} * * @param sink Broadcast Sink device on which a new Broadcast Source has been added * @param sourceId source ID as defined in the BASS specification * @param reason reason of source addition * @hide */ @SystemApi void onSourceAdded(@NonNull BluetoothDevice sink, @Reason int sourceId, @Reason int reason); /** * Callback invoked when the new Broadcast Source failed to be added to the Broadcast Sink. * * @param sink Broadcast Sink device on which a new Broadcast Source has been added * @param source metadata representation of the Broadcast Source * @param reason reason why the addition has failed * @hide */ @SystemApi void onSourceAddFailed(@NonNull BluetoothDevice sink, @NonNull BluetoothLeBroadcastMetadata source, @Reason int reason); /** * Callback invoked when an existing Broadcast Source within a Broadcast Sink has been * modified. * * Actual state after the modification will be delivered via the next * {@link Callback#onReceiveStateChanged(BluetoothDevice, int, * BluetoothLeBroadcastReceiveState)} * callback. * * A source could be modified by the Broadcast Sink itself or other Broadcast Assistants * connected to the Broadcast Sink and in this case the reason code will be * {@link BluetoothStatusCodes#REASON_REMOTE_REQUEST} * * @param sink Broadcast Sink device on which a Broadcast Source has been modified * @param sourceId source ID as defined in the BASS specification * @param reason reason of source modification * @hide */ @SystemApi void onSourceModified(@NonNull BluetoothDevice sink, int sourceId, @Reason int reason); /** * Callback invoked when the Broadcast Assistant failed to modify an existing Broadcast * Source on a Broadcast Sink. * * @param sink Broadcast Sink device on which a Broadcast Source has been modified * @param sourceId source ID as defined in the BASS specification * @param reason reason why the modification has failed * @hide */ @SystemApi void onSourceModifyFailed(@NonNull BluetoothDevice sink, int sourceId, @Reason int reason); /** * Callback invoked when a Broadcast Source has been successfully removed from the * Broadcast Sink. * * No more update for the source ID via * {@link Callback#onReceiveStateChanged(BluetoothDevice, int, * BluetoothLeBroadcastReceiveState)} * after this callback. * * A source could be removed by the Broadcast Sink itself or other Broadcast Assistants * connected to the Broadcast Sink and in this case the reason code will be * {@link BluetoothStatusCodes#REASON_REMOTE_REQUEST} * * @param sink Broadcast Sink device from which a Broadcast Source has been removed * @param sourceId source ID as defined in the BASS specification * @param reason reason why the Broadcast Source was removed * @hide */ @SystemApi void onSourceRemoved(@NonNull BluetoothDevice sink, int sourceId, @Reason int reason); /** * Callback invoked when the Broadcast Assistant failed to remove an existing Broadcast * Source on a Broadcast Sink. * * @param sink Broadcast Sink device on which a Broadcast Source was to be removed * @param sourceId source ID as defined in the BASS specification * @param reason reason why the modification has failed * @hide */ @SystemApi void onSourceRemoveFailed(@NonNull BluetoothDevice sink, int sourceId, @Reason int reason); /** * Callback invoked when the Broadcast Receive State information of a Broadcast Sink device * changes. * * @param sink BASS server device that is also a Broadcast Sink device * @param sourceId source ID as defined in the BASS specification * @param state latest state information between the Broadcast Sink and a Broadcast Source * @hide */ @SystemApi void onReceiveStateChanged(@NonNull BluetoothDevice sink, int sourceId, @NonNull BluetoothLeBroadcastReceiveState state); } /** * Intent used to broadcast the change in connection state of devices via Broadcast Audio Scan * Service (BASS). Please note that in a coordinated set, each set member will connect via BASS * individually. Group operations on a single set member will propagate to the entire set. * * For example, in the binaural case, there will be two different LE devices for the left and * right side and each device will have their own connection state changes. If both devices * belongs to on Coordinated Set, group operation on one of them will affect both devices. * *
This intent will have 3 extras: *
{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
* {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
* {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
*
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.action.CONNECTION_STATE_CHANGED";
/**
* Create a new instance of an LE Audio Broadcast Assistant.
*
* @hide
*/
/*package*/ BluetoothLeBroadcastAssistant(
@NonNull Context context, @NonNull ServiceListener listener) {
}
/**
* {@inheritDoc}
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@Override
public @BluetoothProfile.BtProfileState int getConnectionState(@NonNull BluetoothDevice sink) {
return BluetoothProfile.STATE_DISCONNECTED;
}
/**
* {@inheritDoc}
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@Override
public @NonNull List The device should already be paired. Connection policy can be one of {
* @link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN},
* {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Paired bluetooth device
* @param connectionPolicy is the connection policy to set to for this profile
* @return true if connectionPolicy is set, false on error
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public boolean setConnectionPolicy(@NonNull BluetoothDevice device,
@ConnectionPolicy int connectionPolicy) {
return false;
}
/**
* Get the connection policy of the profile.
*
* The connection policy can be any of:
* {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN},
* {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Bluetooth device
* @return connection policy of the device
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) {
return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
}
/**
* Register a {@link Callback} that will be invoked during the operation of this profile.
*
* Repeated registration of the same callback object will have no effect after the
* first call to this method, even when the executor is different. Caller would have
* to call {@link #unregisterCallback(Callback)} with the same callback object before
* registering it again.
*
* @param executor an {@link Executor} to execute given callback
* @param callback user implementation of the {@link Callback}
* @throws IllegalArgumentException if a null executor, sink, or callback is given
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void registerCallback(@NonNull @CallbackExecutor Executor executor,
@NonNull Callback callback) {
if (executor == null) {
throw new IllegalArgumentException("executor cannot be null");
}
if (callback == null) {
throw new IllegalArgumentException("callback cannot be null");
}
log("registerCallback");
throw new UnsupportedOperationException("Not Implemented");
}
/**
* Unregister the specified {@link Callback}.
*
* The same {@link Callback} object used when calling
* {@link #registerCallback(Executor, Callback)} must be used.
*
* Callbacks are automatically unregistered when application process goes away.
*
* @param callback user implementation of the {@link Callback}
* @throws IllegalArgumentException when callback is null or when no callback is registered
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void unregisterCallback(@NonNull Callback callback) {
if (callback == null) {
throw new IllegalArgumentException("callback cannot be null");
}
log("unregisterCallback");
throw new UnsupportedOperationException("Not Implemented");
}
/**
* Search for LE Audio Broadcast Sources on behalf of all devices connected via Broadcast Audio
* Scan Service, filtered by filters.
*
* On success, {@link Callback#onSearchStarted(int)} will be called with reason code
* {@link BluetoothStatusCodes#REASON_LOCAL_APP_REQUEST}.
*
* On failure, {@link Callback#onSearchStartFailed(int)} will be called with reason code
*
* The implementation will also synchronize with discovered Broadcast Sources and get their
* metadata before passing the Broadcast Source metadata back to the application using {@link
* Callback#onSourceFound(BluetoothLeBroadcastMetadata)}.
*
* Please disconnect the Broadcast Sink's BASS server by calling
* {@link #setConnectionPolicy(BluetoothDevice, int)} with
* {@link BluetoothProfile#CONNECTION_POLICY_FORBIDDEN} if you do not want the Broadcast Sink
* to receive notifications about this search before calling this method.
*
* App must also have
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION ACCESS_FINE_LOCATION}
* permission in order to get results.
*
* filters will be AND'ed with internal filters in the implementation and
* {@link ScanSettings} will be managed by the implementation.
*
* @param filters {@link ScanFilter}s for finding exact Broadcast Source, if no filter is
* needed, please provide an empty list instead
* @throws IllegalArgumentException when filters argument is null
* @throws IllegalStateException when no callback is registered
* @hide
*/
@SystemApi
@RequiresBluetoothScanPermission
@RequiresBluetoothLocationPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_SCAN,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void startSearchingForSources(@NonNull List If there are multiple members in the coordinated set the sink belongs to, and isGroupOp is
* set to true, the Broadcast Source will be added to each sink in the coordinated set and a
* separate {@link Callback#onSourceAdded} callback will be invoked for each member of the
* coordinated set.
*
* The isGroupOp option is sticky. This means that subsequent operations using
* {@link #modifySource(BluetoothDevice, int, BluetoothLeBroadcastMetadata)} and
* {@link #removeSource(BluetoothDevice, int)} will act on all devices in the same coordinated
* set for the sink and sourceID pair until the sourceId is
* removed from the sink by any Broadcast role (could be another remote device).
*
* When isGroupOp is true, if one Broadcast Sink in a coordinated set
* disconnects from this Broadcast Assistant or lost the Broadcast Source, this Broadcast
* Assistant will try to add it back automatically to make sure the whole coordinated set
* is in the same state.
*
* @param sink Broadcast Sink to which the Broadcast Source should be added
* @param sourceMetadata Broadcast Source metadata to be added to the Broadcast Sink
* @param isGroupOp {@code true} if Application wants to perform this operation for all
* coordinated set members throughout this session. Otherwise, caller
* would have to add, modify, and remove individual set members.
* @throws IllegalArgumentException if sink or source are null
* @throws IllegalStateException if callback was not registered
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void addSource(@NonNull BluetoothDevice sink,
@NonNull BluetoothLeBroadcastMetadata sourceMetadata, boolean isGroupOp) {
log("addBroadcastSource: " + sourceMetadata + " on " + sink);
throw new UnsupportedOperationException("Not Implemented");
}
/**
* Modify the Broadcast Source information on a Broadcast Sink.
*
* One can modify {@link BluetoothLeBroadcastMetadata#getBroadcastCode()} if
* {@link BluetoothLeBroadcastReceiveState#getBigEncryptionState()} returns
* {@link BluetoothLeBroadcastReceiveState#BIG_ENCRYPTION_STATE_BAD_CODE} or
* {@link BluetoothLeBroadcastReceiveState#BIG_ENCRYPTION_STATE_CODE_REQUIRED}
*
* One can modify {@link BluetoothLeBroadcastMetadata#getPaSyncInterval()} if the Broadcast
* Assistant received updated information.
*
* One can modify {@link BluetoothLeBroadcastChannel#isSelected()} to select different broadcast
* channel to listen to (one per {@link BluetoothLeBroadcastSubgroup} or set
* {@link BluetoothLeBroadcastSubgroup#isNoChannelPreference()} to leave the choice to the
* Broadcast Sink.
*
* One can modify {@link BluetoothLeBroadcastSubgroup#getContentMetadata()} if the subgroup
* metadata changes and the Broadcast Sink need help updating the metadata from Broadcast
* Assistant.
*
* Each of the above modifications can be accepted or rejected by the Broadcast Assistant
* implement and/or the Broadcast Sink.
*
* On success, {@link Callback#onSourceModified(BluetoothDevice, int, int)} will be invoked
* with reason code {@link BluetoothStatusCodes#REASON_LOCAL_APP_REQUEST}.
*
* On failure, {@link Callback#onSourceModifyFailed(BluetoothDevice, int, int)} will be
* invoked with reason code.
*
* If there are multiple members in the coordinated set the sink belongs to, and isGroupOp
* is set to true during
* {@link #addSource(BluetoothDevice, BluetoothLeBroadcastMetadata, boolean)},
* the source will be modified on each sink in the coordinated set and a separate
* {@link Callback#onSourceModified(BluetoothDevice, int, int)} callback will be invoked for
* each member of the coordinated set.
*
* @param sink Broadcast Sink to which the Broadcast Source should be updated
* @param sourceId source ID as delivered in
* {@link Callback#onSourceAdded(BluetoothDevice, int, int)}
* @param updatedMetadata updated Broadcast Source metadata to be updated on the Broadcast Sink
* @throws IllegalStateException if callback was not registered
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void modifySource(@NonNull BluetoothDevice sink, int sourceId,
@NonNull BluetoothLeBroadcastMetadata updatedMetadata) {
log("updateBroadcastSource: " + updatedMetadata + " on " + sink);
throw new UnsupportedOperationException("Not Implemented");
}
/**
* Removes the Broadcast Source from a Broadcast Sink.
*
* On success, {@link Callback#onSourceRemoved(BluetoothDevice, int, int)} will be invoked
* with reason code {@link BluetoothStatusCodes#REASON_LOCAL_APP_REQUEST}.
*
* On failure, {@link Callback#onSourceRemoveFailed(BluetoothDevice, int, int)} will be
* invoked with reason code.
*
* If there are multiple members in the coordinated set the sink belongs to, and isGroupOp is
* set to true during
* {@link #addSource(BluetoothDevice, BluetoothLeBroadcastMetadata, boolean)},
* the source will be removed from each sink in the coordinated set and a separate
* {@link Callback#onSourceRemoved(BluetoothDevice, int, int)} callback will be invoked for
* each member of the coordinated set.
*
* @param sink Broadcast Sink from which a Broadcast Source should be removed
* @param sourceId source ID as delivered in
* {@link Callback#onSourceAdded(BluetoothDevice, int, int)}
* @throws IllegalArgumentException when the sink is null
* @throws IllegalStateException if callback was not registered
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void removeSource(@NonNull BluetoothDevice sink, int sourceId) {
log("removeBroadcastSource: " + sourceId + " from " + sink);
return;
}
/**
* Get information about all Broadcast Sources that a Broadcast Sink knows about.
*
* @param sink Broadcast Sink from which to get all Broadcast Sources
* @return the list of Broadcast Receive State {@link BluetoothLeBroadcastReceiveState}
* stored in the Broadcast Sink
* @throws IllegalArgumentException when sink is null
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @NonNull List