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 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 /** 23 * The {@link PeriodicAdvertisingParameters} provide a way to adjust periodic advertising 24 * preferences for each Bluetooth LE advertising set. Use {@link 25 * PeriodicAdvertisingParameters.Builder} to create an instance of this class. 26 */ 27 public final class PeriodicAdvertisingParameters implements Parcelable { 28 29 private static final int INTERVAL_MIN = 80; 30 private static final int INTERVAL_MAX = 65519; 31 32 private final boolean mIncludeTxPower; 33 private final int mInterval; 34 PeriodicAdvertisingParameters(boolean includeTxPower, int interval)35 private PeriodicAdvertisingParameters(boolean includeTxPower, int interval) { 36 mIncludeTxPower = includeTxPower; 37 mInterval = interval; 38 } 39 PeriodicAdvertisingParameters(Parcel in)40 private PeriodicAdvertisingParameters(Parcel in) { 41 mIncludeTxPower = in.readInt() != 0; 42 mInterval = in.readInt(); 43 } 44 45 /** Returns whether the TX Power will be included. */ getIncludeTxPower()46 public boolean getIncludeTxPower() { 47 return mIncludeTxPower; 48 } 49 50 /** 51 * Returns the periodic advertising interval, in 1.25ms unit. Valid values are from 80 (100ms) 52 * to 65519 (81.89875s). 53 */ getInterval()54 public int getInterval() { 55 return mInterval; 56 } 57 58 @Override describeContents()59 public int describeContents() { 60 return 0; 61 } 62 63 @Override writeToParcel(Parcel dest, int flags)64 public void writeToParcel(Parcel dest, int flags) { 65 dest.writeInt(mIncludeTxPower ? 1 : 0); 66 dest.writeInt(mInterval); 67 } 68 69 public static final Parcelable.Creator<PeriodicAdvertisingParameters> CREATOR = 70 new Creator<PeriodicAdvertisingParameters>() { 71 @Override 72 public PeriodicAdvertisingParameters[] newArray(int size) { 73 return new PeriodicAdvertisingParameters[size]; 74 } 75 76 @Override 77 public PeriodicAdvertisingParameters createFromParcel(Parcel in) { 78 return new PeriodicAdvertisingParameters(in); 79 } 80 }; 81 82 public static final class Builder { 83 private boolean mIncludeTxPower = false; 84 private int mInterval = INTERVAL_MAX; 85 86 /** Whether the transmission power level should be included in the periodic packet. */ setIncludeTxPower(boolean includeTxPower)87 public Builder setIncludeTxPower(boolean includeTxPower) { 88 mIncludeTxPower = includeTxPower; 89 return this; 90 } 91 92 /** 93 * Set advertising interval for periodic advertising, in 1.25ms unit. Valid values are from 94 * 80 (100ms) to 65519 (81.89875s). Value from range [interval, interval+20ms] will be 95 * picked as the actual value. 96 * 97 * @throws IllegalArgumentException If the interval is invalid. 98 */ setInterval(int interval)99 public Builder setInterval(int interval) { 100 if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) { 101 throw new IllegalArgumentException( 102 "Invalid interval (must be " + INTERVAL_MIN + "-" + INTERVAL_MAX + ")"); 103 } 104 mInterval = interval; 105 return this; 106 } 107 108 /** Build the {@link AdvertisingSetParameters} object. */ build()109 public PeriodicAdvertisingParameters build() { 110 return new PeriodicAdvertisingParameters(mIncludeTxPower, mInterval); 111 } 112 } 113 } 114