• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 boolean mAdvertiseDiscoverable;
82     private final int mOwnAddressType;
83 
AdvertiseSettings( int advertiseMode, int advertiseTxPowerLevel, boolean advertiseConnectable, boolean discoverable, int advertiseTimeout, @AddressTypeStatus int ownAddressType)84     private AdvertiseSettings(
85             int advertiseMode,
86             int advertiseTxPowerLevel,
87             boolean advertiseConnectable,
88             boolean discoverable,
89             int advertiseTimeout,
90             @AddressTypeStatus int ownAddressType) {
91         mAdvertiseMode = advertiseMode;
92         mAdvertiseTxPowerLevel = advertiseTxPowerLevel;
93         mAdvertiseConnectable = advertiseConnectable;
94         mAdvertiseDiscoverable = discoverable;
95         mAdvertiseTimeoutMillis = advertiseTimeout;
96         mOwnAddressType = ownAddressType;
97     }
98 
AdvertiseSettings(Parcel in)99     private AdvertiseSettings(Parcel in) {
100         mAdvertiseMode = in.readInt();
101         mAdvertiseTxPowerLevel = in.readInt();
102         mAdvertiseConnectable = in.readInt() != 0;
103         mAdvertiseTimeoutMillis = in.readInt();
104         mOwnAddressType = in.readInt();
105         mAdvertiseDiscoverable = in.readInt() != 0;
106     }
107 
108     /**
109      * Returns the advertise mode.
110      */
getMode()111     public int getMode() {
112         return mAdvertiseMode;
113     }
114 
115     /**
116      * Returns the TX power level for advertising.
117      */
getTxPowerLevel()118     public int getTxPowerLevel() {
119         return mAdvertiseTxPowerLevel;
120     }
121 
122     /**
123      * Returns whether the advertisement will indicate connectable.
124      */
isConnectable()125     public boolean isConnectable() {
126         return mAdvertiseConnectable;
127     }
128 
129     /** Returns whether the advertisement will be discoverable. */
isDiscoverable()130     public boolean isDiscoverable() {
131         return mAdvertiseDiscoverable;
132     }
133 
134     /**
135      * Returns the advertising time limit in milliseconds.
136      */
getTimeout()137     public int getTimeout() {
138         return mAdvertiseTimeoutMillis;
139     }
140 
141     /**
142      * @return the own address type for advertising
143      *
144      * @hide
145      */
146     @SystemApi
getOwnAddressType()147     public @AddressTypeStatus int getOwnAddressType() {
148         return mOwnAddressType;
149     }
150 
151     @Override
toString()152     public String toString() {
153         return "Settings [mAdvertiseMode=" + mAdvertiseMode
154                 + ", mAdvertiseTxPowerLevel=" + mAdvertiseTxPowerLevel
155                 + ", mAdvertiseConnectable=" + mAdvertiseConnectable
156                 + ", mAdvertiseDiscoverable=" + mAdvertiseDiscoverable
157                 + ", mAdvertiseTimeoutMillis=" + mAdvertiseTimeoutMillis
158                 + ", mOwnAddressType=" + mOwnAddressType + "]";
159     }
160 
161     @Override
describeContents()162     public int describeContents() {
163         return 0;
164     }
165 
166     @Override
writeToParcel(Parcel dest, int flags)167     public void writeToParcel(Parcel dest, int flags) {
168         dest.writeInt(mAdvertiseMode);
169         dest.writeInt(mAdvertiseTxPowerLevel);
170         dest.writeInt(mAdvertiseConnectable ? 1 : 0);
171         dest.writeInt(mAdvertiseTimeoutMillis);
172         dest.writeInt(mOwnAddressType);
173         dest.writeInt(mAdvertiseDiscoverable ? 1 : 0);
174     }
175 
176     public static final @android.annotation.NonNull Parcelable.Creator<AdvertiseSettings> CREATOR =
177             new Creator<AdvertiseSettings>() {
178                 @Override
179                 public AdvertiseSettings[] newArray(int size) {
180                     return new AdvertiseSettings[size];
181                 }
182 
183                 @Override
184                 public AdvertiseSettings createFromParcel(Parcel in) {
185                     return new AdvertiseSettings(in);
186                 }
187             };
188 
189     /**
190      * Builder class for {@link AdvertiseSettings}.
191      */
192     public static final class Builder {
193         private int mMode = ADVERTISE_MODE_LOW_POWER;
194         private int mTxPowerLevel = ADVERTISE_TX_POWER_MEDIUM;
195         private int mTimeoutMillis = 0;
196         private boolean mConnectable = true;
197         private boolean mDiscoverable = true;
198         private int mOwnAddressType = AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT;
199 
200         /**
201          * Set advertise mode to control the advertising power and latency.
202          *
203          * @param advertiseMode Bluetooth LE Advertising mode, can only be one of {@link
204          * AdvertiseSettings#ADVERTISE_MODE_LOW_POWER},
205          * {@link AdvertiseSettings#ADVERTISE_MODE_BALANCED},
206          * or {@link AdvertiseSettings#ADVERTISE_MODE_LOW_LATENCY}.
207          * @throws IllegalArgumentException If the advertiseMode is invalid.
208          */
setAdvertiseMode(int advertiseMode)209         public Builder setAdvertiseMode(int advertiseMode) {
210             if (advertiseMode < ADVERTISE_MODE_LOW_POWER
211                     || advertiseMode > ADVERTISE_MODE_LOW_LATENCY) {
212                 throw new IllegalArgumentException("unknown mode " + advertiseMode);
213             }
214             mMode = advertiseMode;
215             return this;
216         }
217 
218         /**
219          * Set advertise TX power level to control the transmission power level for the advertising.
220          *
221          * @param txPowerLevel Transmission power of Bluetooth LE Advertising, can only be one of
222          * {@link AdvertiseSettings#ADVERTISE_TX_POWER_ULTRA_LOW}, {@link
223          * AdvertiseSettings#ADVERTISE_TX_POWER_LOW},
224          * {@link AdvertiseSettings#ADVERTISE_TX_POWER_MEDIUM}
225          * or {@link AdvertiseSettings#ADVERTISE_TX_POWER_HIGH}.
226          * @throws IllegalArgumentException If the {@code txPowerLevel} is invalid.
227          */
setTxPowerLevel(int txPowerLevel)228         public Builder setTxPowerLevel(int txPowerLevel) {
229             if (txPowerLevel < ADVERTISE_TX_POWER_ULTRA_LOW
230                     || txPowerLevel > ADVERTISE_TX_POWER_HIGH) {
231                 throw new IllegalArgumentException("unknown tx power level " + txPowerLevel);
232             }
233             mTxPowerLevel = txPowerLevel;
234             return this;
235         }
236 
237         /**
238          * Set whether the advertisement type should be connectable or non-connectable.
239          *
240          * @param connectable Controls whether the advertisment type will be connectable (true) or
241          * non-connectable (false).
242          */
setConnectable(boolean connectable)243         public Builder setConnectable(boolean connectable) {
244             mConnectable = connectable;
245             return this;
246         }
247 
248         /**
249          * Set whether the advertisement type should be discoverable or non-discoverable.
250          *
251          * @param discoverable Controls whether the advertisment type will be discoverable
252          * ({@code true}) or non-discoverable ({@code false}).
253          */
setDiscoverable(boolean discoverable)254         public @NonNull Builder setDiscoverable(boolean discoverable) {
255             mDiscoverable = discoverable;
256             return this;
257         }
258 
259         /**
260          * Limit advertising to a given amount of time.
261          *
262          * @param timeoutMillis Advertising time limit. May not exceed 180000 milliseconds. A value
263          * of 0 will disable the time limit.
264          * @throws IllegalArgumentException If the provided timeout is over 180000 ms.
265          */
setTimeout(int timeoutMillis)266         public Builder setTimeout(int timeoutMillis) {
267             if (timeoutMillis < 0 || timeoutMillis > LIMITED_ADVERTISING_MAX_MILLIS) {
268                 throw new IllegalArgumentException("timeoutMillis invalid (must be 0-"
269                         + LIMITED_ADVERTISING_MAX_MILLIS + " milliseconds)");
270             }
271             mTimeoutMillis = timeoutMillis;
272             return this;
273         }
274 
275         /**
276          * Set own address type for advertising to control public or privacy mode. If used to set
277          * address type anything other than {@link AdvertisingSetParameters#ADDRESS_TYPE_DEFAULT},
278          * then it will require BLUETOOTH_PRIVILEGED permission and will be checked at the
279          * time of starting advertising.
280          *
281          * @throws IllegalArgumentException If the {@code ownAddressType} is invalid
282          *
283          * @hide
284          */
285         @SystemApi
setOwnAddressType(@ddressTypeStatus int ownAddressType)286         public @NonNull Builder setOwnAddressType(@AddressTypeStatus int ownAddressType) {
287             if (ownAddressType < AdvertisingSetParameters.ADDRESS_TYPE_DEFAULT
288                     || ownAddressType
289                             > AdvertisingSetParameters.ADDRESS_TYPE_RANDOM_NON_RESOLVABLE) {
290                 throw new IllegalArgumentException("unknown address type " + ownAddressType);
291             }
292             mOwnAddressType = ownAddressType;
293             return this;
294         }
295 
296         /**
297          * Build the {@link AdvertiseSettings} object.
298          */
build()299         public AdvertiseSettings build() {
300             return new AdvertiseSettings(
301                     mMode,
302                     mTxPowerLevel,
303                     mConnectable,
304                     mDiscoverable,
305                     mTimeoutMillis,
306                     mOwnAddressType);
307         }
308     }
309 }
310