1 /* 2 * Copyright (C) 2014 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.NonNull; 20 import android.annotation.SystemApi; 21 import android.bluetooth.le.AdvertisingSetParameters.AddressTypeStatus; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 /** 26 * The {@link AdvertiseSettings} provide a way to adjust advertising preferences for each 27 * Bluetooth LE advertisement instance. Use {@link AdvertiseSettings.Builder} to create an 28 * instance of this class. 29 */ 30 public final class AdvertiseSettings implements Parcelable { 31 /** 32 * Perform Bluetooth LE advertising in low power mode. This is the default and preferred 33 * advertising mode as it consumes the least power. 34 */ 35 public static final int ADVERTISE_MODE_LOW_POWER = 0; 36 37 /** 38 * Perform Bluetooth LE advertising in balanced power mode. This is balanced between advertising 39 * frequency and power consumption. 40 */ 41 public static final int ADVERTISE_MODE_BALANCED = 1; 42 43 /** 44 * Perform Bluetooth LE advertising in low latency, high power mode. This has the highest power 45 * consumption and should not be used for continuous background advertising. 46 */ 47 public static final int ADVERTISE_MODE_LOW_LATENCY = 2; 48 49 /** 50 * Advertise using the lowest transmission (TX) power level. Low transmission power can be used 51 * to restrict the visibility range of advertising packets. 52 */ 53 public static final int ADVERTISE_TX_POWER_ULTRA_LOW = 0; 54 55 /** 56 * Advertise using low TX power level. 57 */ 58 public static final int ADVERTISE_TX_POWER_LOW = 1; 59 60 /** 61 * Advertise using medium TX power level. 62 */ 63 public static final int ADVERTISE_TX_POWER_MEDIUM = 2; 64 65 /** 66 * Advertise using high TX power level. This corresponds to largest visibility range of the 67 * advertising packet. 68 */ 69 public static final int ADVERTISE_TX_POWER_HIGH = 3; 70 71 /** 72 * The maximum limited advertisement duration as specified by the Bluetooth SIG 73 */ 74 private static final int LIMITED_ADVERTISING_MAX_MILLIS = 180 * 1000; 75 76 77 private final int mAdvertiseMode; 78 private final int mAdvertiseTxPowerLevel; 79 private final int mAdvertiseTimeoutMillis; 80 private final boolean mAdvertiseConnectable; 81 private final int mOwnAddressType; 82 AdvertiseSettings(int advertiseMode, int advertiseTxPowerLevel, boolean advertiseConnectable, int advertiseTimeout, @AddressTypeStatus int ownAddressType)83 private AdvertiseSettings(int advertiseMode, int advertiseTxPowerLevel, 84 boolean advertiseConnectable, int advertiseTimeout, 85 @AddressTypeStatus int ownAddressType) { 86 mAdvertiseMode = advertiseMode; 87 mAdvertiseTxPowerLevel = advertiseTxPowerLevel; 88 mAdvertiseConnectable = advertiseConnectable; 89 mAdvertiseTimeoutMillis = advertiseTimeout; 90 mOwnAddressType = ownAddressType; 91 } 92 AdvertiseSettings(Parcel in)93 private AdvertiseSettings(Parcel in) { 94 mAdvertiseMode = in.readInt(); 95 mAdvertiseTxPowerLevel = in.readInt(); 96 mAdvertiseConnectable = in.readInt() != 0; 97 mAdvertiseTimeoutMillis = in.readInt(); 98 mOwnAddressType = in.readInt(); 99 } 100 101 /** 102 * Returns the advertise mode. 103 */ getMode()104 public int getMode() { 105 return mAdvertiseMode; 106 } 107 108 /** 109 * Returns the TX power level for advertising. 110 */ getTxPowerLevel()111 public int getTxPowerLevel() { 112 return mAdvertiseTxPowerLevel; 113 } 114 115 /** 116 * Returns whether the advertisement will indicate connectable. 117 */ isConnectable()118 public boolean isConnectable() { 119 return mAdvertiseConnectable; 120 } 121 122 /** 123 * Returns the advertising time limit in milliseconds. 124 */ getTimeout()125 public int getTimeout() { 126 return mAdvertiseTimeoutMillis; 127 } 128 129 /** 130 * @return the own address type for advertising 131 * 132 * @hide 133 */ 134 @SystemApi getOwnAddressType()135 public @AddressTypeStatus int getOwnAddressType() { 136 return mOwnAddressType; 137 } 138 139 @Override toString()140 public String toString() { 141 return "Settings [mAdvertiseMode=" + mAdvertiseMode 142 + ", mAdvertiseTxPowerLevel=" + mAdvertiseTxPowerLevel 143 + ", mAdvertiseConnectable=" + mAdvertiseConnectable 144 + ", mAdvertiseTimeoutMillis=" + mAdvertiseTimeoutMillis 145 + ", mOwnAddressType=" + mOwnAddressType + "]"; 146 } 147 148 @Override describeContents()149 public int describeContents() { 150 return 0; 151 } 152 153 @Override writeToParcel(Parcel dest, int flags)154 public void writeToParcel(Parcel dest, int flags) { 155 dest.writeInt(mAdvertiseMode); 156 dest.writeInt(mAdvertiseTxPowerLevel); 157 dest.writeInt(mAdvertiseConnectable ? 1 : 0); 158 dest.writeInt(mAdvertiseTimeoutMillis); 159 dest.writeInt(mOwnAddressType); 160 } 161 162 public static final @android.annotation.NonNull Parcelable.Creator<AdvertiseSettings> CREATOR = 163 new Creator<AdvertiseSettings>() { 164 @Override 165 public AdvertiseSettings[] newArray(int size) { 166 return new AdvertiseSettings[size]; 167 } 168 169 @Override 170 public AdvertiseSettings createFromParcel(Parcel in) { 171 return new AdvertiseSettings(in); 172 } 173 }; 174 175 /** 176 * Builder class for {@link AdvertiseSettings}. 177 */ 178 public static final class Builder { 179 private int mMode = ADVERTISE_MODE_LOW_POWER; 180 private int mTxPowerLevel = ADVERTISE_TX_POWER_MEDIUM; 181 private int mTimeoutMillis = 0; 182 private boolean mConnectable = true; 183 private int mOwnAddressType = AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT; 184 185 /** 186 * Set advertise mode to control the advertising power and latency. 187 * 188 * @param advertiseMode Bluetooth LE Advertising mode, can only be one of {@link 189 * AdvertiseSettings#ADVERTISE_MODE_LOW_POWER}, 190 * {@link AdvertiseSettings#ADVERTISE_MODE_BALANCED}, 191 * or {@link AdvertiseSettings#ADVERTISE_MODE_LOW_LATENCY}. 192 * @throws IllegalArgumentException If the advertiseMode is invalid. 193 */ setAdvertiseMode(int advertiseMode)194 public Builder setAdvertiseMode(int advertiseMode) { 195 if (advertiseMode < ADVERTISE_MODE_LOW_POWER 196 || advertiseMode > ADVERTISE_MODE_LOW_LATENCY) { 197 throw new IllegalArgumentException("unknown mode " + advertiseMode); 198 } 199 mMode = advertiseMode; 200 return this; 201 } 202 203 /** 204 * Set advertise TX power level to control the transmission power level for the advertising. 205 * 206 * @param txPowerLevel Transmission power of Bluetooth LE Advertising, can only be one of 207 * {@link AdvertiseSettings#ADVERTISE_TX_POWER_ULTRA_LOW}, {@link 208 * AdvertiseSettings#ADVERTISE_TX_POWER_LOW}, 209 * {@link AdvertiseSettings#ADVERTISE_TX_POWER_MEDIUM} 210 * or {@link AdvertiseSettings#ADVERTISE_TX_POWER_HIGH}. 211 * @throws IllegalArgumentException If the {@code txPowerLevel} is invalid. 212 */ setTxPowerLevel(int txPowerLevel)213 public Builder setTxPowerLevel(int txPowerLevel) { 214 if (txPowerLevel < ADVERTISE_TX_POWER_ULTRA_LOW 215 || txPowerLevel > ADVERTISE_TX_POWER_HIGH) { 216 throw new IllegalArgumentException("unknown tx power level " + txPowerLevel); 217 } 218 mTxPowerLevel = txPowerLevel; 219 return this; 220 } 221 222 /** 223 * Set whether the advertisement type should be connectable or non-connectable. 224 * 225 * @param connectable Controls whether the advertisment type will be connectable (true) or 226 * non-connectable (false). 227 */ setConnectable(boolean connectable)228 public Builder setConnectable(boolean connectable) { 229 mConnectable = connectable; 230 return this; 231 } 232 233 /** 234 * Limit advertising to a given amount of time. 235 * 236 * @param timeoutMillis Advertising time limit. May not exceed 180000 milliseconds. A value 237 * of 0 will disable the time limit. 238 * @throws IllegalArgumentException If the provided timeout is over 180000 ms. 239 */ setTimeout(int timeoutMillis)240 public Builder setTimeout(int timeoutMillis) { 241 if (timeoutMillis < 0 || timeoutMillis > LIMITED_ADVERTISING_MAX_MILLIS) { 242 throw new IllegalArgumentException("timeoutMillis invalid (must be 0-" 243 + LIMITED_ADVERTISING_MAX_MILLIS + " milliseconds)"); 244 } 245 mTimeoutMillis = timeoutMillis; 246 return this; 247 } 248 249 /** 250 * Set own address type for advertising to control public or privacy mode. If used to set 251 * address type anything other than {@link AdvertisingSetParameters#ADDRESS_TYPE_DEFAULT}, 252 * then it will require BLUETOOTH_PRIVILEGED permission and will be checked at the 253 * time of starting advertising. 254 * 255 * @throws IllegalArgumentException If the {@code ownAddressType} is invalid 256 * 257 * @hide 258 */ 259 @SystemApi setOwnAddressType(@ddressTypeStatus int ownAddressType)260 public @NonNull Builder setOwnAddressType(@AddressTypeStatus int ownAddressType) { 261 if (ownAddressType < AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT 262 || ownAddressType > AdvertisingSetParameters.ADDRESS_TYPE_RANDOM) { 263 throw new IllegalArgumentException("unknown address type " + ownAddressType); 264 } 265 mOwnAddressType = ownAddressType; 266 return this; 267 } 268 269 /** 270 * Build the {@link AdvertiseSettings} object. 271 */ build()272 public AdvertiseSettings build() { 273 return new AdvertiseSettings(mMode, mTxPowerLevel, mConnectable, mTimeoutMillis, 274 mOwnAddressType); 275 } 276 } 277 } 278