/* * Copyright 2022 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.le; import static android.bluetooth.le.BluetoothLeUtils.getSyncTimeout; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothStatusCodes; import android.bluetooth.IBluetoothGatt; import android.content.AttributionSource; import android.os.Binder; import android.os.ParcelUuid; import android.os.RemoteException; import android.util.Log; import com.android.modules.utils.SynchronousResultReceiver; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Objects; import java.util.concurrent.Executor; import java.util.concurrent.TimeoutException; /** * This class provides a way to control an active distance measurement session. *
It also defines the required {@link DistanceMeasurementSession.Callback} that must be * implemented in order to be notified of distance measurement results and status events related to * the {@link DistanceMeasurementSession}. * *
To get an instance of {@link DistanceMeasurementSession}, first use
* {@link DistanceMeasurementManager#startMeasurementSession(DistanceMeasurementParams, Executor,
* DistanceMeasurementSession.Callback)} to request to start a session. Once the session is started,
* a {@link DistanceMeasurementSession} object is provided through
* {@link DistanceMeasurementSession.Callback#onStarted(DistanceMeasurementSession)}.
* If starting a session fails, the failure is reported through
* {@link DistanceMeasurementSession.Callback#onStartFail(int)} with the failure reason.
*
* @hide
*/
@SystemApi
public final class DistanceMeasurementSession {
private static final String TAG = "DistanceMeasurementSession";
private final IBluetoothGatt mGatt;
private final ParcelUuid mUuid;
private final DistanceMeasurementParams mDistanceMeasurementParams;
private final Executor mExecutor;
private final Callback mCallback;
private final AttributionSource mAttributionSource;
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {
BluetoothStatusCodes.SUCCESS,
BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED,
BluetoothStatusCodes.ERROR_DISTANCE_MEASUREMENT_INTERNAL,
})
public @interface StopSessionReturnValues{}
/**
* @hide
*/
public DistanceMeasurementSession(IBluetoothGatt gatt, ParcelUuid uuid,
DistanceMeasurementParams params, Executor executor,
AttributionSource attributionSource, Callback callback) {
Objects.requireNonNull(gatt, "gatt is null");
Objects.requireNonNull(params, "params is null");
Objects.requireNonNull(executor, "executor is null");
Objects.requireNonNull(callback, "callback is null");
mGatt = gatt;
mUuid = uuid;
mDistanceMeasurementParams = params;
mExecutor = executor;
mAttributionSource = attributionSource;
mCallback = callback;
}
/**
* Stops actively ranging, {@link Callback#onStopped} will be invoked if this succeeds.
*
* @return whether successfully stop or not
*
* @hide
*/
@SystemApi
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @StopSessionReturnValues int stopSession() {
final int defaultValue = BluetoothStatusCodes.ERROR_TIMEOUT;
try {
final SynchronousResultReceiver