Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
Jakub Pawlowski | 9e37719 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 20 | import android.bluetooth.BluetoothDevice; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| 24 | /** |
| 25 | * The {@link AdvertisingSetParameters} provide a way to adjust advertising |
| 26 | * preferences for each |
| 27 | * Bluetooth LE advertising set. Use {@link AdvertisingSetParameters.Builder} to |
| 28 | * create an |
| 29 | * instance of this class. |
| 30 | */ |
| 31 | public final class AdvertisingSetParameters implements Parcelable { |
| 32 | |
| 33 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 34 | * Advertise on low frequency, around every 1000ms. This is the default and |
| 35 | * preferred advertising mode as it consumes the least power. |
| 36 | */ |
Jakub Pawlowski | 6b1f395 | 2017-05-09 14:28:21 -0700 | [diff] [blame] | 37 | public static final int INTERVAL_HIGH = 1600; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Advertise on medium frequency, around every 250ms. This is balanced |
| 41 | * between advertising frequency and power consumption. |
| 42 | */ |
| 43 | public static final int INTERVAL_MEDIUM = 400; |
| 44 | |
| 45 | /** |
| 46 | * Perform high frequency, low latency advertising, around every 100ms. This |
| 47 | * has the highest power consumption and should not be used for continuous |
| 48 | * background advertising. |
| 49 | */ |
Jakub Pawlowski | 6b1f395 | 2017-05-09 14:28:21 -0700 | [diff] [blame] | 50 | public static final int INTERVAL_LOW = 160; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Minimum value for advertising interval. |
| 54 | */ |
| 55 | public static final int INTERVAL_MIN = 160; |
| 56 | |
| 57 | /** |
| 58 | * Maximum value for advertising interval. |
| 59 | */ |
| 60 | public static final int INTERVAL_MAX = 16777215; |
| 61 | |
| 62 | /** |
| 63 | * Advertise using the lowest transmission (TX) power level. Low transmission |
| 64 | * power can be used to restrict the visibility range of advertising packets. |
| 65 | */ |
| 66 | public static final int TX_POWER_ULTRA_LOW = -21; |
| 67 | |
| 68 | /** |
| 69 | * Advertise using low TX power level. |
| 70 | */ |
| 71 | public static final int TX_POWER_LOW = -15; |
| 72 | |
| 73 | /** |
| 74 | * Advertise using medium TX power level. |
| 75 | */ |
| 76 | public static final int TX_POWER_MEDIUM = -7; |
| 77 | |
| 78 | /** |
| 79 | * Advertise using high TX power level. This corresponds to largest visibility |
| 80 | * range of the advertising packet. |
| 81 | */ |
| 82 | public static final int TX_POWER_HIGH = 1; |
| 83 | |
| 84 | /** |
| 85 | * Minimum value for TX power. |
| 86 | */ |
| 87 | public static final int TX_POWER_MIN = -127; |
| 88 | |
| 89 | /** |
| 90 | * Maximum value for TX power. |
| 91 | */ |
| 92 | public static final int TX_POWER_MAX = 1; |
| 93 | |
| 94 | /** |
| 95 | * The maximum limited advertisement duration as specified by the Bluetooth |
| 96 | * SIG |
| 97 | */ |
| 98 | private static final int LIMITED_ADVERTISING_MAX_MILLIS = 180 * 1000; |
| 99 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 100 | private final boolean mIsLegacy; |
| 101 | private final boolean mIsAnonymous; |
| 102 | private final boolean mIncludeTxPower; |
| 103 | private final int mPrimaryPhy; |
| 104 | private final int mSecondaryPhy; |
| 105 | private final boolean mConnectable; |
| 106 | private final boolean mScannable; |
| 107 | private final int mInterval; |
| 108 | private final int mTxPowerLevel; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 109 | |
Jakub Pawlowski | d12b568 | 2017-03-20 15:57:46 -0700 | [diff] [blame] | 110 | private AdvertisingSetParameters(boolean connectable, boolean scannable, boolean isLegacy, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 111 | boolean isAnonymous, boolean includeTxPower, |
| 112 | int primaryPhy, int secondaryPhy, |
| 113 | int interval, int txPowerLevel) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 114 | mConnectable = connectable; |
| 115 | mScannable = scannable; |
| 116 | mIsLegacy = isLegacy; |
| 117 | mIsAnonymous = isAnonymous; |
| 118 | mIncludeTxPower = includeTxPower; |
| 119 | mPrimaryPhy = primaryPhy; |
| 120 | mSecondaryPhy = secondaryPhy; |
| 121 | mInterval = interval; |
| 122 | mTxPowerLevel = txPowerLevel; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private AdvertisingSetParameters(Parcel in) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 126 | mConnectable = in.readInt() != 0; |
| 127 | mScannable = in.readInt() != 0; |
| 128 | mIsLegacy = in.readInt() != 0; |
| 129 | mIsAnonymous = in.readInt() != 0; |
| 130 | mIncludeTxPower = in.readInt() != 0; |
| 131 | mPrimaryPhy = in.readInt(); |
| 132 | mSecondaryPhy = in.readInt(); |
| 133 | mInterval = in.readInt(); |
| 134 | mTxPowerLevel = in.readInt(); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns whether the advertisement will be connectable. |
| 139 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 140 | public boolean isConnectable() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 141 | return mConnectable; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 142 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 143 | |
| 144 | /** |
Jakub Pawlowski | d12b568 | 2017-03-20 15:57:46 -0700 | [diff] [blame] | 145 | * Returns whether the advertisement will be scannable. |
| 146 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 147 | public boolean isScannable() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 148 | return mScannable; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 149 | } |
Jakub Pawlowski | d12b568 | 2017-03-20 15:57:46 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 152 | * Returns whether the legacy advertisement will be used. |
| 153 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 154 | public boolean isLegacy() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 155 | return mIsLegacy; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 156 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Returns whether the advertisement will be anonymous. |
| 160 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 161 | public boolean isAnonymous() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 162 | return mIsAnonymous; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 163 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Returns whether the TX Power will be included. |
| 167 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 168 | public boolean includeTxPower() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 169 | return mIncludeTxPower; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 170 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Returns the primary advertising phy. |
| 174 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 175 | public int getPrimaryPhy() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 176 | return mPrimaryPhy; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 177 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Returns the secondary advertising phy. |
| 181 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 182 | public int getSecondaryPhy() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 183 | return mSecondaryPhy; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 184 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Returns the advertising interval. |
| 188 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 189 | public int getInterval() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 190 | return mInterval; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 191 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * Returns the TX power level for advertising. |
| 195 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 196 | public int getTxPowerLevel() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 197 | return mTxPowerLevel; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 198 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 199 | |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 200 | @Override |
| 201 | public String toString() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 202 | return "AdvertisingSetParameters [connectable=" + mConnectable |
| 203 | + ", isLegacy=" + mIsLegacy |
| 204 | + ", isAnonymous=" + mIsAnonymous |
| 205 | + ", includeTxPower=" + mIncludeTxPower |
| 206 | + ", primaryPhy=" + mPrimaryPhy |
| 207 | + ", secondaryPhy=" + mSecondaryPhy |
| 208 | + ", interval=" + mInterval |
| 209 | + ", txPowerLevel=" + mTxPowerLevel + "]"; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public int describeContents() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 214 | return 0; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void writeToParcel(Parcel dest, int flags) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 219 | dest.writeInt(mConnectable ? 1 : 0); |
| 220 | dest.writeInt(mScannable ? 1 : 0); |
| 221 | dest.writeInt(mIsLegacy ? 1 : 0); |
| 222 | dest.writeInt(mIsAnonymous ? 1 : 0); |
| 223 | dest.writeInt(mIncludeTxPower ? 1 : 0); |
| 224 | dest.writeInt(mPrimaryPhy); |
| 225 | dest.writeInt(mSecondaryPhy); |
| 226 | dest.writeInt(mInterval); |
| 227 | dest.writeInt(mTxPowerLevel); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 230 | public static final @android.annotation.NonNull Parcelable.Creator<AdvertisingSetParameters> CREATOR = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 231 | new Creator<AdvertisingSetParameters>() { |
| 232 | @Override |
| 233 | public AdvertisingSetParameters[] newArray(int size) { |
| 234 | return new AdvertisingSetParameters[size]; |
| 235 | } |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 236 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 237 | @Override |
| 238 | public AdvertisingSetParameters createFromParcel(Parcel in) { |
| 239 | return new AdvertisingSetParameters(in); |
| 240 | } |
| 241 | }; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * Builder class for {@link AdvertisingSetParameters}. |
| 245 | */ |
| 246 | public static final class Builder { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 247 | private boolean mConnectable = false; |
| 248 | private boolean mScannable = false; |
| 249 | private boolean mIsLegacy = false; |
| 250 | private boolean mIsAnonymous = false; |
| 251 | private boolean mIncludeTxPower = false; |
| 252 | private int mPrimaryPhy = BluetoothDevice.PHY_LE_1M; |
| 253 | private int mSecondaryPhy = BluetoothDevice.PHY_LE_1M; |
| 254 | private int mInterval = INTERVAL_LOW; |
| 255 | private int mTxPowerLevel = TX_POWER_MEDIUM; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Set whether the advertisement type should be connectable or |
| 259 | * non-connectable. |
Jakub Pawlowski | 010cc95 | 2017-04-06 07:22:57 -0700 | [diff] [blame] | 260 | * Legacy advertisements can be both connectable and scannable. Non-legacy |
Jakub Pawlowski | e7f89b0 | 2017-03-22 22:53:18 -0700 | [diff] [blame] | 261 | * advertisements can be only scannable or only connectable. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 262 | * |
| 263 | * @param connectable Controls whether the advertisement type will be connectable (true) or |
| 264 | * non-connectable (false). |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 265 | */ |
| 266 | public Builder setConnectable(boolean connectable) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 267 | mConnectable = connectable; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 268 | return this; |
| 269 | } |
| 270 | |
| 271 | /** |
Jakub Pawlowski | e7f89b0 | 2017-03-22 22:53:18 -0700 | [diff] [blame] | 272 | * Set whether the advertisement type should be scannable. |
Jakub Pawlowski | 010cc95 | 2017-04-06 07:22:57 -0700 | [diff] [blame] | 273 | * Legacy advertisements can be both connectable and scannable. Non-legacy |
Jakub Pawlowski | e7f89b0 | 2017-03-22 22:53:18 -0700 | [diff] [blame] | 274 | * advertisements can be only scannable or only connectable. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 275 | * |
| 276 | * @param scannable Controls whether the advertisement type will be scannable (true) or |
| 277 | * non-scannable (false). |
Jakub Pawlowski | d12b568 | 2017-03-20 15:57:46 -0700 | [diff] [blame] | 278 | */ |
| 279 | public Builder setScannable(boolean scannable) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 280 | mScannable = scannable; |
Jakub Pawlowski | d12b568 | 2017-03-20 15:57:46 -0700 | [diff] [blame] | 281 | return this; |
| 282 | } |
| 283 | |
| 284 | /** |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 285 | * When set to true, advertising set will advertise 4.x Spec compliant |
| 286 | * advertisements. |
| 287 | * |
Jakub Pawlowski | 08ed924 | 2017-03-22 22:44:09 -0700 | [diff] [blame] | 288 | * @param isLegacy whether legacy advertising mode should be used. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 289 | */ |
| 290 | public Builder setLegacyMode(boolean isLegacy) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 291 | mIsLegacy = isLegacy; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 292 | return this; |
| 293 | } |
| 294 | |
| 295 | /** |
Jakub Pawlowski | 08ed924 | 2017-03-22 22:44:09 -0700 | [diff] [blame] | 296 | * Set whether advertiser address should be ommited from all packets. If this |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 297 | * mode is used, periodic advertising can't be enabled for this set. |
| 298 | * |
| 299 | * This is used only if legacy mode is not used. |
| 300 | * |
Jakub Pawlowski | 08ed924 | 2017-03-22 22:44:09 -0700 | [diff] [blame] | 301 | * @param isAnonymous whether anonymous advertising should be used. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 302 | */ |
Jakub Pawlowski | bc2991e | 2017-03-10 16:07:59 -0800 | [diff] [blame] | 303 | public Builder setAnonymous(boolean isAnonymous) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 304 | mIsAnonymous = isAnonymous; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 305 | return this; |
| 306 | } |
| 307 | |
| 308 | /** |
Jakub Pawlowski | 08ed924 | 2017-03-22 22:44:09 -0700 | [diff] [blame] | 309 | * Set whether TX power should be included in the extended header. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 310 | * |
| 311 | * This is used only if legacy mode is not used. |
| 312 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 313 | * @param includeTxPower whether TX power should be included in extended header |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 314 | */ |
| 315 | public Builder setIncludeTxPower(boolean includeTxPower) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 316 | mIncludeTxPower = includeTxPower; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 317 | return this; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Set the primary physical channel used for this advertising set. |
| 322 | * |
| 323 | * This is used only if legacy mode is not used. |
| 324 | * |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 325 | * Use {@link BluetoothAdapter#isLeCodedPhySupported} to determine if LE Coded PHY is |
| 326 | * supported on this device. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 327 | * |
| 328 | * @param primaryPhy Primary advertising physical channel, can only be {@link |
| 329 | * BluetoothDevice#PHY_LE_1M} or {@link BluetoothDevice#PHY_LE_CODED}. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 330 | * @throws IllegalArgumentException If the primaryPhy is invalid. |
| 331 | */ |
| 332 | public Builder setPrimaryPhy(int primaryPhy) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 333 | if (primaryPhy != BluetoothDevice.PHY_LE_1M |
| 334 | && primaryPhy != BluetoothDevice.PHY_LE_CODED) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 335 | throw new IllegalArgumentException("bad primaryPhy " + primaryPhy); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 336 | } |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 337 | mPrimaryPhy = primaryPhy; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 338 | return this; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Set the secondary physical channel used for this advertising set. |
| 343 | * |
| 344 | * This is used only if legacy mode is not used. |
| 345 | * |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 346 | * Use {@link BluetoothAdapter#isLeCodedPhySupported} and |
| 347 | * {@link BluetoothAdapter#isLe2MPhySupported} to determine if LE Coded PHY or 2M PHY is |
| 348 | * supported on this device. |
| 349 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 350 | * @param secondaryPhy Secondary advertising physical channel, can only be one of {@link |
| 351 | * BluetoothDevice#PHY_LE_1M}, {@link BluetoothDevice#PHY_LE_2M} or {@link |
| 352 | * BluetoothDevice#PHY_LE_CODED}. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 353 | * @throws IllegalArgumentException If the secondaryPhy is invalid. |
| 354 | */ |
| 355 | public Builder setSecondaryPhy(int secondaryPhy) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 356 | if (secondaryPhy != BluetoothDevice.PHY_LE_1M |
| 357 | && secondaryPhy != BluetoothDevice.PHY_LE_2M |
| 358 | && secondaryPhy != BluetoothDevice.PHY_LE_CODED) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 359 | throw new IllegalArgumentException("bad secondaryPhy " + secondaryPhy); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 360 | } |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 361 | mSecondaryPhy = secondaryPhy; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 362 | return this; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Set advertising interval. |
| 367 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 368 | * @param interval Bluetooth LE Advertising interval, in 0.625ms unit. Valid range is from |
| 369 | * 160 (100ms) to 16777215 (10,485.759375 s). Recommended values are: {@link |
| 370 | * AdvertisingSetParameters#INTERVAL_LOW}, {@link AdvertisingSetParameters#INTERVAL_MEDIUM}, |
| 371 | * or {@link AdvertisingSetParameters#INTERVAL_HIGH}. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 372 | * @throws IllegalArgumentException If the interval is invalid. |
| 373 | */ |
| 374 | public Builder setInterval(int interval) { |
| 375 | if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 376 | throw new IllegalArgumentException("unknown interval " + interval); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 377 | } |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 378 | mInterval = interval; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 379 | return this; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Set the transmission power level for the advertising. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 384 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 385 | * @param txPowerLevel Transmission power of Bluetooth LE Advertising, in dBm. The valid |
| 386 | * range is [-127, 1] Recommended values are: |
| 387 | * {@link AdvertisingSetParameters#TX_POWER_ULTRA_LOW}, |
| 388 | * {@link AdvertisingSetParameters#TX_POWER_LOW}, |
| 389 | * {@link AdvertisingSetParameters#TX_POWER_MEDIUM}, |
| 390 | * or {@link AdvertisingSetParameters#TX_POWER_HIGH}. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 391 | * @throws IllegalArgumentException If the {@code txPowerLevel} is invalid. |
| 392 | */ |
| 393 | public Builder setTxPowerLevel(int txPowerLevel) { |
| 394 | if (txPowerLevel < TX_POWER_MIN || txPowerLevel > TX_POWER_MAX) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 395 | throw new IllegalArgumentException("unknown txPowerLevel " + txPowerLevel); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 396 | } |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 397 | mTxPowerLevel = txPowerLevel; |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 398 | return this; |
| 399 | } |
| 400 | |
| 401 | /** |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 402 | * Build the {@link AdvertisingSetParameters} object. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 403 | * |
Jakub Pawlowski | 9d4abb5 | 2017-04-28 04:11:26 -0700 | [diff] [blame] | 404 | * @throws IllegalStateException if invalid combination of parameters is used. |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 405 | */ |
| 406 | public AdvertisingSetParameters build() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 407 | if (mIsLegacy) { |
| 408 | if (mIsAnonymous) { |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 409 | throw new IllegalArgumentException("Legacy advertising can't be anonymous"); |
| 410 | } |
| 411 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 412 | if (mConnectable && !mScannable) { |
Jakub Pawlowski | 9d4abb5 | 2017-04-28 04:11:26 -0700 | [diff] [blame] | 413 | throw new IllegalStateException( |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 414 | "Legacy advertisement can't be connectable and non-scannable"); |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 417 | if (mIncludeTxPower) { |
Jakub Pawlowski | 9d4abb5 | 2017-04-28 04:11:26 -0700 | [diff] [blame] | 418 | throw new IllegalStateException( |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 419 | "Legacy advertising can't include TX power level in header"); |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 420 | } |
| 421 | } else { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 422 | if (mConnectable && mScannable) { |
Jakub Pawlowski | 9d4abb5 | 2017-04-28 04:11:26 -0700 | [diff] [blame] | 423 | throw new IllegalStateException( |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 424 | "Advertising can't be both connectable and scannable"); |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 427 | if (mIsAnonymous && mConnectable) { |
Jakub Pawlowski | 9d4abb5 | 2017-04-28 04:11:26 -0700 | [diff] [blame] | 428 | throw new IllegalStateException( |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 429 | "Advertising can't be both connectable and anonymous"); |
Jakub Pawlowski | f4ed33f | 2017-03-30 11:19:24 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 433 | return new AdvertisingSetParameters(mConnectable, mScannable, mIsLegacy, mIsAnonymous, |
| 434 | mIncludeTxPower, mPrimaryPhy, mSecondaryPhy, mInterval, mTxPowerLevel); |
Jakub Pawlowski | a9d1a32 | 2017-01-10 06:15:54 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 437 | } |