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