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.FloatRange; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 /** 26 * Result of distance measurement. 27 * 28 * @hide 29 */ 30 @SystemApi 31 public final class DistanceMeasurementResult implements Parcelable { 32 33 private final double mMeters; 34 private final double mErrorMeters; 35 private final double mAzimuthAngle; 36 private final double mErrorAzimuthAngle; 37 private final double mAltitudeAngle; 38 private final double mErrorAltitudeAngle; 39 DistanceMeasurementResult(double meters, double errorMeters, double azimuthAngle, double errorAzimuthAngle, double altitudeAngle, double errorAltitudeAngle)40 private DistanceMeasurementResult(double meters, double errorMeters, double azimuthAngle, 41 double errorAzimuthAngle, double altitudeAngle, double errorAltitudeAngle) { 42 mMeters = meters; 43 mErrorMeters = errorMeters; 44 mAzimuthAngle = azimuthAngle; 45 mErrorAzimuthAngle = errorAzimuthAngle; 46 mAltitudeAngle = altitudeAngle; 47 mErrorAltitudeAngle = errorAltitudeAngle; 48 } 49 50 /** 51 * Distance measurement in meters. 52 * 53 * @return distance in meters 54 * 55 * @hide 56 */ 57 @SystemApi getResultMeters()58 public double getResultMeters() { 59 return mMeters; 60 } 61 62 /** 63 * Error of distance measurement in meters. 64 * <p>Must be positive. 65 * 66 * @return error of distance measurement in meters 67 * 68 * @hide 69 */ 70 @SystemApi 71 @FloatRange(from = 0.0) getErrorMeters()72 public double getErrorMeters() { 73 return mErrorMeters; 74 } 75 76 /** 77 * Azimuth Angle measurement in degrees. 78 * 79 * Azimuth of remote device in horizontal coordinate system, this measured from azimuth north 80 * and increasing eastward. When the remote device in azimuth north, this angle is 0, whe the 81 * remote device in azimuth south, this angle is 180. 82 * 83 * <p>See: <a href="https://en.wikipedia.org/wiki/Horizontal_coordinate_system">Horizontal 84 * coordinate system</a>for the details 85 * 86 * <p>On an Android device, azimuth north is defined as the angle perpendicular away from the 87 * back of the device when holding it in portrait mode upright. 88 * 89 * <p>The Azimuth north is defined as the direction in which the top edge of the device is 90 * facing when it is placed flat. 91 * 92 * @return azimuth angle in degrees or Double.NaN if not available 93 * 94 * @hide 95 */ 96 @SystemApi 97 @FloatRange(from = 0.0, to = 360.0) getAzimuthAngle()98 public double getAzimuthAngle() { 99 return mAzimuthAngle; 100 } 101 102 /** 103 * Error of azimuth angle measurement in degrees. 104 * 105 * <p>Must be a positive value. 106 * 107 * @return azimuth angle measurement error in degrees or Double.NaN if not available 108 * 109 * @hide 110 */ 111 @SystemApi getErrorAzimuthAngle()112 public double getErrorAzimuthAngle() { 113 return mErrorAzimuthAngle; 114 } 115 116 /** 117 * Altitude Angle measurement in degrees. 118 * 119 * <p>Altitude of remote device in horizontal coordinate system, this is the angle between the 120 * remote device and the top edge of local device. When local device is placed flat, the angle 121 * of the zenith is 90, the angle of the nadir is -90. 122 * 123 * <p>See: https://en.wikipedia.org/wiki/Horizontal_coordinate_system 124 * 125 * @return altitude angle in degrees or Double.NaN if not available 126 * 127 * @hide 128 */ 129 @SystemApi 130 @FloatRange(from = -90.0, to = 90.0) getAltitudeAngle()131 public double getAltitudeAngle() { 132 return mAltitudeAngle; 133 } 134 135 /** 136 * Error of altitude angle measurement in degrees. 137 * 138 * <p>Must be a positive value. 139 * 140 * @return altitude angle measurement error in degrees or Double.NaN if not available 141 * 142 * @hide 143 */ 144 @SystemApi getErrorAltitudeAngle()145 public double getErrorAltitudeAngle() { 146 return mErrorAltitudeAngle; 147 } 148 149 /** 150 * {@inheritDoc} 151 * @hide 152 */ 153 @Override describeContents()154 public int describeContents() { 155 return 0; 156 } 157 158 /** 159 * {@inheritDoc} 160 * @hide 161 */ 162 @Override writeToParcel(Parcel out, int flags)163 public void writeToParcel(Parcel out, int flags) { 164 out.writeDouble(mMeters); 165 out.writeDouble(mErrorMeters); 166 out.writeDouble(mAzimuthAngle); 167 out.writeDouble(mErrorAzimuthAngle); 168 out.writeDouble(mAltitudeAngle); 169 out.writeDouble(mErrorAltitudeAngle); 170 } 171 172 /** @hide **/ 173 @Override toString()174 public String toString() { 175 return "DistanceMeasurement[" 176 + "meters: " + mMeters 177 + ", errorMeters: " + mErrorMeters 178 + ", azimuthAngle: " + mAzimuthAngle 179 + ", errorAzimuthAngle: " + mErrorAzimuthAngle 180 + ", altitudeAngle: " + mAltitudeAngle 181 + ", errorAltitudeAngle: " + mErrorAltitudeAngle 182 + "]"; 183 } 184 185 /** 186 * A {@link Parcelable.Creator} to create {@link DistanceMeasurementResult} from parcel. 187 * 188 */ 189 public static final @NonNull Parcelable.Creator<DistanceMeasurementResult> CREATOR = 190 new Parcelable.Creator<DistanceMeasurementResult>() { 191 @Override 192 public @NonNull DistanceMeasurementResult createFromParcel(@NonNull Parcel in) { 193 return new Builder(in.readDouble(), in.readDouble()) 194 .setAzimuthAngle(in.readDouble()) 195 .setErrorAzimuthAngle(in.readDouble()) 196 .setAltitudeAngle(in.readDouble()) 197 .setErrorAltitudeAngle(in.readDouble()) 198 .build(); 199 } 200 201 @Override 202 public @NonNull DistanceMeasurementResult[] newArray(int size) { 203 return new DistanceMeasurementResult[size]; 204 } 205 }; 206 207 /** 208 * Builder for {@link DistanceMeasurementResult}. 209 * 210 * @hide 211 */ 212 @SystemApi 213 public static final class Builder { 214 private double mMeters = Double.NaN; 215 private double mErrorMeters = Double.NaN; 216 private double mAzimuthAngle = Double.NaN; 217 private double mErrorAzimuthAngle = Double.NaN; 218 private double mAltitudeAngle = Double.NaN; 219 private double mErrorAltitudeAngle = Double.NaN; 220 221 /** 222 * Constructor of the Builder. 223 * 224 * @param meters distance in meters 225 * @param errorMeters distance error in meters 226 * @throws IllegalArgumentException if meters is NaN or error is negative or NaN 227 */ Builder(@loatRangefrom = 0.0) double meters, @FloatRange(from = 0.0)double errorMeters)228 public Builder(@FloatRange(from = 0.0) double meters, 229 @FloatRange(from = 0.0)double errorMeters) { 230 if (Double.isNaN(meters) || meters < 0.0) { 231 throw new IllegalArgumentException( 232 "meters must be >= 0.0 and not NaN: " + meters); 233 } 234 if (Double.isNaN(errorMeters) || errorMeters < 0.0) { 235 throw new IllegalArgumentException( 236 "errorMeters must be >= 0.0 and not NaN: " + errorMeters); 237 } 238 mMeters = meters; 239 mErrorMeters = errorMeters; 240 } 241 242 /** 243 * Set the azimuth angle measurement in degrees. 244 * 245 * @param angle azimuth angle in degrees 246 * @throws IllegalArgumentException if value is invalid 247 * 248 * @hide 249 */ 250 @SystemApi 251 @NonNull setAzimuthAngle(@loatRangefrom = 0.0, to = 360.0) double angle)252 public Builder setAzimuthAngle(@FloatRange(from = 0.0, to = 360.0) double angle) { 253 if (angle > 360.0 || angle < 0.0) { 254 throw new IllegalArgumentException( 255 "angle must be in the range from 0.0 to 360.0 : " + angle); 256 } 257 mAzimuthAngle = angle; 258 return this; 259 } 260 261 /** 262 * Set the azimuth angle error in degrees. 263 * 264 * @param angle azimuth angle error in degrees 265 * @throws IllegalArgumentException if value is invalid 266 * 267 * @hide 268 */ 269 @SystemApi 270 @NonNull setErrorAzimuthAngle(@loatRangefrom = 0.0, to = 360.0) double angle)271 public Builder setErrorAzimuthAngle(@FloatRange(from = 0.0, to = 360.0) double angle) { 272 if (angle > 360.0 || angle < 0.0) { 273 throw new IllegalArgumentException( 274 "error angle must be in the range from 0.0 to 360.0 : " + angle); 275 } 276 mErrorAzimuthAngle = angle; 277 return this; 278 } 279 280 /** 281 * Set the altitude angle measurement in degrees. 282 * 283 * @param angle altitude angle in degrees 284 * @throws IllegalArgumentException if value is invalid 285 * 286 * @hide 287 */ 288 @SystemApi 289 @NonNull setAltitudeAngle(@loatRangefrom = -90.0, to = 90.0) double angle)290 public Builder setAltitudeAngle(@FloatRange(from = -90.0, to = 90.0) double angle) { 291 if (angle > 90.0 || angle < -90.0) { 292 throw new IllegalArgumentException( 293 "angle must be in the range from -90.0 to 90.0 : " + angle); 294 } 295 mAltitudeAngle = angle; 296 return this; 297 } 298 299 /** 300 * Set the altitude angle error in degrees. 301 * 302 * @param angle altitude angle error in degrees 303 * @throws IllegalArgumentException if value is invalid 304 * 305 * @hide 306 */ 307 @SystemApi 308 @NonNull setErrorAltitudeAngle(@loatRangefrom = 0.0, to = 180.0) double angle)309 public Builder setErrorAltitudeAngle(@FloatRange(from = 0.0, to = 180.0) double angle) { 310 if (angle > 180.0 || angle < 0.0) { 311 throw new IllegalArgumentException( 312 "error angle must be in the range from 0.0 to 180.0 : " + angle); 313 } 314 mErrorAltitudeAngle = angle; 315 return this; 316 } 317 318 /** 319 * Builds the {@link DistanceMeasurementResult} object. 320 * 321 * @throws IllegalStateException if meters, error, or confidence are not set 322 * 323 * @hide 324 */ 325 @SystemApi 326 @NonNull build()327 public DistanceMeasurementResult build() { 328 return new DistanceMeasurementResult(mMeters, mErrorMeters, mAzimuthAngle, 329 mErrorAzimuthAngle, mAltitudeAngle, mErrorAltitudeAngle); 330 } 331 } 332 } 333