blob: 7129d762cd95b4e18f7478e889356069011896a7 [file] [log] [blame]
Wei Wang6d811182014-05-22 12:10:25 -07001/*
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
17package android.bluetooth.le;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * The {@link AdvertiseSettings} provide a way to adjust advertising preferences for each
Wei Wangaf74e662014-07-09 14:03:42 -070024 * Bluetooth LE advertisement instance. Use {@link AdvertiseSettings.Builder} to create an
25 * instance of this class.
Wei Wang6d811182014-05-22 12:10:25 -070026 */
27public final class AdvertiseSettings implements Parcelable {
28 /**
29 * Perform Bluetooth LE advertising in low power mode. This is the default and preferred
30 * advertising mode as it consumes the least power.
31 */
32 public static final int ADVERTISE_MODE_LOW_POWER = 0;
Wei Wangaf74e662014-07-09 14:03:42 -070033
Wei Wang6d811182014-05-22 12:10:25 -070034 /**
35 * Perform Bluetooth LE advertising in balanced power mode. This is balanced between advertising
36 * frequency and power consumption.
37 */
38 public static final int ADVERTISE_MODE_BALANCED = 1;
Wei Wangaf74e662014-07-09 14:03:42 -070039
Wei Wang6d811182014-05-22 12:10:25 -070040 /**
41 * Perform Bluetooth LE advertising in low latency, high power mode. This has the highest power
Wei Wangaf74e662014-07-09 14:03:42 -070042 * consumption and should not be used for continuous background advertising.
Wei Wang6d811182014-05-22 12:10:25 -070043 */
44 public static final int ADVERTISE_MODE_LOW_LATENCY = 2;
45
46 /**
Wei Wangaf74e662014-07-09 14:03:42 -070047 * Advertise using the lowest transmission (TX) power level. Low transmission power can be used
48 * to restrict the visibility range of advertising packets.
Wei Wang6d811182014-05-22 12:10:25 -070049 */
50 public static final int ADVERTISE_TX_POWER_ULTRA_LOW = 0;
Wei Wangaf74e662014-07-09 14:03:42 -070051
Wei Wang6d811182014-05-22 12:10:25 -070052 /**
Wei Wangaf74e662014-07-09 14:03:42 -070053 * Advertise using low TX power level.
Wei Wang6d811182014-05-22 12:10:25 -070054 */
55 public static final int ADVERTISE_TX_POWER_LOW = 1;
Wei Wangaf74e662014-07-09 14:03:42 -070056
Wei Wang6d811182014-05-22 12:10:25 -070057 /**
Wei Wangaf74e662014-07-09 14:03:42 -070058 * Advertise using medium TX power level.
Wei Wang6d811182014-05-22 12:10:25 -070059 */
60 public static final int ADVERTISE_TX_POWER_MEDIUM = 2;
Wei Wangaf74e662014-07-09 14:03:42 -070061
Wei Wang6d811182014-05-22 12:10:25 -070062 /**
Wei Wangaf74e662014-07-09 14:03:42 -070063 * Advertise using high TX power level. This corresponds to largest visibility range of the
Wei Wang6d811182014-05-22 12:10:25 -070064 * advertising packet.
65 */
66 public static final int ADVERTISE_TX_POWER_HIGH = 3;
67
68 /**
Wei Wang9960b572014-08-15 13:25:30 -070069 * The maximum limited advertisement duration as specified by the Bluetooth SIG
Wei Wang6d811182014-05-22 12:10:25 -070070 */
Wei Wang9960b572014-08-15 13:25:30 -070071 private static final int LIMITED_ADVERTISING_MAX_MILLIS = 180 * 1000;
Wei Wang6d811182014-05-22 12:10:25 -070072
73 private final int mAdvertiseMode;
74 private final int mAdvertiseTxPowerLevel;
Wei Wang9960b572014-08-15 13:25:30 -070075 private final int mAdvertiseTimeoutMillis;
Wei Wangaf74e662014-07-09 14:03:42 -070076 private final boolean mAdvertiseConnectable;
Wei Wang6d811182014-05-22 12:10:25 -070077
78 private AdvertiseSettings(int advertiseMode, int advertiseTxPowerLevel,
Wei Wangaf74e662014-07-09 14:03:42 -070079 boolean advertiseConnectable, int advertiseTimeout) {
Wei Wang6d811182014-05-22 12:10:25 -070080 mAdvertiseMode = advertiseMode;
81 mAdvertiseTxPowerLevel = advertiseTxPowerLevel;
Wei Wangaf74e662014-07-09 14:03:42 -070082 mAdvertiseConnectable = advertiseConnectable;
Wei Wang9960b572014-08-15 13:25:30 -070083 mAdvertiseTimeoutMillis = advertiseTimeout;
Wei Wang6d811182014-05-22 12:10:25 -070084 }
85
86 private AdvertiseSettings(Parcel in) {
87 mAdvertiseMode = in.readInt();
88 mAdvertiseTxPowerLevel = in.readInt();
Jack He2992cd02017-08-22 21:21:23 -070089 mAdvertiseConnectable = in.readInt() != 0;
Wei Wang9960b572014-08-15 13:25:30 -070090 mAdvertiseTimeoutMillis = in.readInt();
Wei Wang6d811182014-05-22 12:10:25 -070091 }
92
93 /**
94 * Returns the advertise mode.
95 */
96 public int getMode() {
97 return mAdvertiseMode;
98 }
99
100 /**
Wei Wangaf74e662014-07-09 14:03:42 -0700101 * Returns the TX power level for advertising.
Wei Wang6d811182014-05-22 12:10:25 -0700102 */
103 public int getTxPowerLevel() {
104 return mAdvertiseTxPowerLevel;
105 }
106
107 /**
Wei Wangaf74e662014-07-09 14:03:42 -0700108 * Returns whether the advertisement will indicate connectable.
Wei Wang6d811182014-05-22 12:10:25 -0700109 */
Wei Wang9960b572014-08-15 13:25:30 -0700110 public boolean isConnectable() {
Wei Wangaf74e662014-07-09 14:03:42 -0700111 return mAdvertiseConnectable;
112 }
113
114 /**
Wei Wang9960b572014-08-15 13:25:30 -0700115 * Returns the advertising time limit in milliseconds.
Wei Wangaf74e662014-07-09 14:03:42 -0700116 */
117 public int getTimeout() {
Wei Wang9960b572014-08-15 13:25:30 -0700118 return mAdvertiseTimeoutMillis;
Wei Wang6d811182014-05-22 12:10:25 -0700119 }
120
121 @Override
122 public String toString() {
Wei Wangaf74e662014-07-09 14:03:42 -0700123 return "Settings [mAdvertiseMode=" + mAdvertiseMode
Jack Hea355e5e2017-08-22 16:06:54 -0700124 + ", mAdvertiseTxPowerLevel=" + mAdvertiseTxPowerLevel
125 + ", mAdvertiseConnectable=" + mAdvertiseConnectable
126 + ", mAdvertiseTimeoutMillis=" + mAdvertiseTimeoutMillis + "]";
Wei Wang6d811182014-05-22 12:10:25 -0700127 }
128
129 @Override
130 public int describeContents() {
131 return 0;
132 }
133
134 @Override
135 public void writeToParcel(Parcel dest, int flags) {
136 dest.writeInt(mAdvertiseMode);
137 dest.writeInt(mAdvertiseTxPowerLevel);
Wei Wangaf74e662014-07-09 14:03:42 -0700138 dest.writeInt(mAdvertiseConnectable ? 1 : 0);
Wei Wang9960b572014-08-15 13:25:30 -0700139 dest.writeInt(mAdvertiseTimeoutMillis);
Wei Wang6d811182014-05-22 12:10:25 -0700140 }
141
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700142 public static final @android.annotation.NonNull Parcelable.Creator<AdvertiseSettings> CREATOR =
Wei Wang6d811182014-05-22 12:10:25 -0700143 new Creator<AdvertiseSettings>() {
Jack Hea355e5e2017-08-22 16:06:54 -0700144 @Override
Wei Wang6d811182014-05-22 12:10:25 -0700145 public AdvertiseSettings[] newArray(int size) {
146 return new AdvertiseSettings[size];
147 }
148
Jack Hea355e5e2017-08-22 16:06:54 -0700149 @Override
Wei Wang6d811182014-05-22 12:10:25 -0700150 public AdvertiseSettings createFromParcel(Parcel in) {
151 return new AdvertiseSettings(in);
152 }
153 };
154
155 /**
156 * Builder class for {@link AdvertiseSettings}.
157 */
158 public static final class Builder {
159 private int mMode = ADVERTISE_MODE_LOW_POWER;
160 private int mTxPowerLevel = ADVERTISE_TX_POWER_MEDIUM;
Wei Wang9960b572014-08-15 13:25:30 -0700161 private int mTimeoutMillis = 0;
Wei Wangaf74e662014-07-09 14:03:42 -0700162 private boolean mConnectable = true;
Wei Wang6d811182014-05-22 12:10:25 -0700163
164 /**
165 * Set advertise mode to control the advertising power and latency.
166 *
Jack Hea355e5e2017-08-22 16:06:54 -0700167 * @param advertiseMode Bluetooth LE Advertising mode, can only be one of {@link
168 * AdvertiseSettings#ADVERTISE_MODE_LOW_POWER},
169 * {@link AdvertiseSettings#ADVERTISE_MODE_BALANCED},
170 * or {@link AdvertiseSettings#ADVERTISE_MODE_LOW_LATENCY}.
Wei Wang6d811182014-05-22 12:10:25 -0700171 * @throws IllegalArgumentException If the advertiseMode is invalid.
172 */
173 public Builder setAdvertiseMode(int advertiseMode) {
174 if (advertiseMode < ADVERTISE_MODE_LOW_POWER
175 || advertiseMode > ADVERTISE_MODE_LOW_LATENCY) {
176 throw new IllegalArgumentException("unknown mode " + advertiseMode);
177 }
178 mMode = advertiseMode;
179 return this;
180 }
181
182 /**
Wei Wangaf74e662014-07-09 14:03:42 -0700183 * Set advertise TX power level to control the transmission power level for the advertising.
Wei Wang6d811182014-05-22 12:10:25 -0700184 *
185 * @param txPowerLevel Transmission power of Bluetooth LE Advertising, can only be one of
Jack Hea355e5e2017-08-22 16:06:54 -0700186 * {@link AdvertiseSettings#ADVERTISE_TX_POWER_ULTRA_LOW}, {@link
187 * AdvertiseSettings#ADVERTISE_TX_POWER_LOW},
188 * {@link AdvertiseSettings#ADVERTISE_TX_POWER_MEDIUM}
189 * or {@link AdvertiseSettings#ADVERTISE_TX_POWER_HIGH}.
Wei Wang6d811182014-05-22 12:10:25 -0700190 * @throws IllegalArgumentException If the {@code txPowerLevel} is invalid.
191 */
192 public Builder setTxPowerLevel(int txPowerLevel) {
193 if (txPowerLevel < ADVERTISE_TX_POWER_ULTRA_LOW
194 || txPowerLevel > ADVERTISE_TX_POWER_HIGH) {
195 throw new IllegalArgumentException("unknown tx power level " + txPowerLevel);
196 }
197 mTxPowerLevel = txPowerLevel;
198 return this;
199 }
200
201 /**
Wei Wangaf74e662014-07-09 14:03:42 -0700202 * Set whether the advertisement type should be connectable or non-connectable.
Wei Wang6d811182014-05-22 12:10:25 -0700203 *
Jack Hea355e5e2017-08-22 16:06:54 -0700204 * @param connectable Controls whether the advertisment type will be connectable (true) or
205 * non-connectable (false).
Wei Wang6d811182014-05-22 12:10:25 -0700206 */
Wei Wang9960b572014-08-15 13:25:30 -0700207 public Builder setConnectable(boolean connectable) {
208 mConnectable = connectable;
Wei Wangaf74e662014-07-09 14:03:42 -0700209 return this;
210 }
211
212 /**
213 * Limit advertising to a given amount of time.
Jack Hea355e5e2017-08-22 16:06:54 -0700214 *
215 * @param timeoutMillis Advertising time limit. May not exceed 180000 milliseconds. A value
216 * of 0 will disable the time limit.
Wei Wang9960b572014-08-15 13:25:30 -0700217 * @throws IllegalArgumentException If the provided timeout is over 180000 ms.
Wei Wangaf74e662014-07-09 14:03:42 -0700218 */
Wei Wang9960b572014-08-15 13:25:30 -0700219 public Builder setTimeout(int timeoutMillis) {
220 if (timeoutMillis < 0 || timeoutMillis > LIMITED_ADVERTISING_MAX_MILLIS) {
221 throw new IllegalArgumentException("timeoutMillis invalid (must be 0-"
Jack Hea355e5e2017-08-22 16:06:54 -0700222 + LIMITED_ADVERTISING_MAX_MILLIS + " milliseconds)");
Wei Wang6d811182014-05-22 12:10:25 -0700223 }
Wei Wang9960b572014-08-15 13:25:30 -0700224 mTimeoutMillis = timeoutMillis;
Wei Wang6d811182014-05-22 12:10:25 -0700225 return this;
226 }
227
228 /**
229 * Build the {@link AdvertiseSettings} object.
230 */
231 public AdvertiseSettings build() {
Wei Wang9960b572014-08-15 13:25:30 -0700232 return new AdvertiseSettings(mMode, mTxPowerLevel, mConnectable, mTimeoutMillis);
Wei Wang6d811182014-05-22 12:10:25 -0700233 }
234 }
235}