• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.bluetooth.le;
18 
19 import android.annotation.IntDef;
20 import android.annotation.IntRange;
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.bluetooth.BluetoothDevice;
24 import android.bluetooth.le.DistanceMeasurementMethod.DistanceMeasurementMethodId;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Objects;
31 
32 /**
33  * The {@link DistanceMeasurementParams} provide a way to adjust distance measurement preferences.
34  * Use {@link DistanceMeasurementParams.Builder} to create an instance of this class.
35  *
36  * @hide
37  */
38 @SystemApi
39 public final class DistanceMeasurementParams implements Parcelable {
40 
41     /**
42      * @hide
43      */
44     @Retention(RetentionPolicy.SOURCE)
45     @IntDef(value = {
46             REPORT_FREQUENCY_LOW,
47             REPORT_FREQUENCY_MEDIUM,
48             REPORT_FREQUENCY_HIGH})
49     @interface ReportFrequency  {}
50 
51     /**
52      * Perform distance measurement in low frequency. This is the default frequency as it consumes
53      * the least power.
54      *
55      * @hide
56      */
57     @SystemApi
58     public static final int REPORT_FREQUENCY_LOW = 0;
59 
60 
61     /**
62      * Perform distance measurement in medium frequency. Provides a good trade-off between report
63      * frequency and power consumption.
64      *
65      * @hide
66      */
67     @SystemApi
68     public static final int REPORT_FREQUENCY_MEDIUM = 1;
69 
70     /**
71      * Perform distance measurement in high frequency. It's recommended to only use this mode when
72      * the application is running in the foreground.
73      *
74      * @hide
75      */
76     @SystemApi
77     public static final int REPORT_FREQUENCY_HIGH = 2;
78 
79     private static final int REPORT_DURATION_DEFAULT = 60;
80     private static final int REPORT_DURATION_MAX = 3600;
81 
82     private BluetoothDevice mDevice = null;
83     private int mDuration;
84     private int mFrequency;
85     private int mMethodId;
86 
87     /**
88      * @hide
89      */
DistanceMeasurementParams(BluetoothDevice device, int duration, int frequency, int methodId)90     public DistanceMeasurementParams(BluetoothDevice device, int duration, int frequency,
91             int methodId) {
92         mDevice = Objects.requireNonNull(device);
93         mDuration = duration;
94         mFrequency = frequency;
95         mMethodId = methodId;
96     }
97 
98     /**
99      * Returns device of this DistanceMeasurementParams.
100      *
101      * @hide
102      */
103     @SystemApi
getDevice()104     public @NonNull BluetoothDevice getDevice() {
105         return mDevice;
106     }
107 
108     /**
109      * Returns duration in seconds of this DistanceMeasurementParams. Once the distance measurement
110      * successfully started, the Bluetooth process will keep reporting the measurement result
111      * until this time has been reached or the session is explicitly stopped with
112      * {@link DistanceMeasurementSession#stopSession}
113      *
114      * @hide
115      */
116     @SystemApi
getDurationSeconds()117     public @IntRange(from = 0) int getDurationSeconds() {
118         return mDuration;
119     }
120 
121     /**
122      * Returns frequency of this DistanceMeasurementParams. The Bluetooth process uses this value
123      * to determine report frequency of the measurement result.
124      *
125      * @hide
126      */
127     @SystemApi
getFrequency()128     public @ReportFrequency int getFrequency() {
129         return mFrequency;
130     }
131 
132     /**
133      * Returns method id of this DistanceMeasurementParams.
134      *
135      * @hide
136      */
137     @SystemApi
getMethodId()138     public @DistanceMeasurementMethodId int getMethodId() {
139         return mMethodId;
140     }
141 
142     /**
143      * Get the default duration in seconds of the parameter.
144      * @hide
145      */
146     @SystemApi
getDefaultDurationSeconds()147     public static int getDefaultDurationSeconds() {
148         return REPORT_DURATION_DEFAULT;
149     }
150 
151     /**
152      * Get the maximum duration in seconds that can be set for the parameter.
153      * @hide
154      */
155     @SystemApi
getMaxDurationSeconds()156     public static int getMaxDurationSeconds() {
157         return REPORT_DURATION_MAX;
158     }
159 
160     /**
161      * {@inheritDoc}
162      * @hide
163      */
164     @Override
describeContents()165     public int describeContents() {
166         return 0;
167     }
168 
169     /**
170      * {@inheritDoc}
171      * @hide
172      */
173     @Override
writeToParcel(Parcel out, int flags)174     public void writeToParcel(Parcel out, int flags) {
175         out.writeParcelable(mDevice, 0);
176         out.writeInt(mDuration);
177         out.writeInt(mFrequency);
178         out.writeInt(mMethodId);
179     }
180 
181     /**
182      * A {@link Parcelable.Creator} to create {@link DistanceMeasurementParams} from parcel.
183      *
184      */
185     public static final @NonNull Parcelable.Creator<DistanceMeasurementParams> CREATOR =
186             new Parcelable.Creator<DistanceMeasurementParams>() {
187                 @Override
188                 public @NonNull DistanceMeasurementParams createFromParcel(@NonNull Parcel in) {
189                     Builder builder = new Builder((BluetoothDevice) in.readParcelable(null));
190                     builder.setDurationSeconds(in.readInt());
191                     builder.setFrequency(in.readInt());
192                     builder.setMethodId(in.readInt());
193                     return builder.build();
194                 }
195 
196                 @Override
197                 public @NonNull DistanceMeasurementParams[] newArray(int size) {
198                     return new DistanceMeasurementParams[size];
199                 }
200         };
201 
202 
203     /**
204      * Builder for {@link DistanceMeasurementParams}.
205      *
206      * @hide
207      */
208     @SystemApi
209     public static final class Builder {
210         private BluetoothDevice mDevice = null;
211         private int mDuration = REPORT_DURATION_DEFAULT;
212         private int mFrequency = REPORT_FREQUENCY_LOW;
213         private int mMethodId = DistanceMeasurementMethod.DISTANCE_MEASUREMENT_METHOD_RSSI;
214 
215         /**
216          * Constructor of the Builder.
217          *
218          * @param device the remote device for the distance measurement
219          */
Builder(@onNull BluetoothDevice device)220         public Builder(@NonNull BluetoothDevice device) {
221             mDevice = Objects.requireNonNull(device);
222         }
223 
224         /**
225          * Set duration in seconds for the DistanceMeasurementParams. Once the distance measurement
226          * successfully started, the Bluetooth process will keep reporting the measurement result
227          * until this time has been reached or the session is explicitly stopped with
228          * {@link DistanceMeasurementSession#stopSession}.
229          *
230          * @param duration duration in seconds of this DistanceMeasurementParams
231          * @return the same Builder instance
232          * @throws IllegalArgumentException if duration greater than
233          * {@link DistanceMeasurementParams#getMaxDurationSeconds()} or less than zero.
234          * @hide
235          */
236         @SystemApi
setDurationSeconds(@ntRangefrom = 0) int duration)237         public @NonNull Builder setDurationSeconds(@IntRange(from = 0) int duration) {
238             if (duration < 0 || duration > getMaxDurationSeconds()) {
239                 throw new IllegalArgumentException("illegal duration " + duration);
240             }
241             mDuration = duration;
242             return this;
243         }
244 
245         /**
246          * Set frequency for the DistanceMeasurementParams. The Bluetooth process uses this value
247          * to determine report frequency of the measurement result.
248          *
249          * @param frequency frequency of this DistanceMeasurementParams
250          * @return the same Builder instance
251          *
252          * @hide
253          */
254         @SystemApi
setFrequency(@eportFrequency int frequency)255         public @NonNull Builder setFrequency(@ReportFrequency int frequency) {
256             switch (frequency) {
257                 case REPORT_FREQUENCY_LOW:
258                 case REPORT_FREQUENCY_MEDIUM:
259                 case REPORT_FREQUENCY_HIGH:
260                     mFrequency = frequency;
261                     break;
262                 default:
263                     throw new IllegalArgumentException("unknown frequency " + frequency);
264             }
265             return this;
266         }
267 
268         /**
269          * Set method id for the DistanceMeasurementParams.
270          *
271          * @param methodId method id of this DistanceMeasurementParams
272          * @return the same Builder instance
273          *
274          * @hide
275          */
276         @SystemApi
setMethodId(@istanceMeasurementMethodId int methodId)277         public @NonNull Builder setMethodId(@DistanceMeasurementMethodId int methodId) {
278             switch (methodId) {
279                 case DistanceMeasurementMethod.DISTANCE_MEASUREMENT_METHOD_AUTO:
280                 case DistanceMeasurementMethod.DISTANCE_MEASUREMENT_METHOD_RSSI:
281                     mMethodId = methodId;
282                     break;
283                 default:
284                     throw new IllegalArgumentException("unknown method id " + methodId);
285             }
286             return this;
287         }
288 
289         /**
290          * Build the {@link DistanceMeasurementParams} object.
291          *
292          * @hide
293          */
294         @SystemApi
build()295         public @NonNull DistanceMeasurementParams build() {
296             return new DistanceMeasurementParams(mDevice, mDuration, mFrequency, mMethodId);
297         }
298     }
299 }
300