15324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski/*
25324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * Copyright (C) 2017 The Android Open Source Project
35324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski *
45324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * Licensed under the Apache License, Version 2.0 (the "License");
55324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * you may not use this file except in compliance with the License.
65324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * You may obtain a copy of the License at
75324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski *
85324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski *      http://www.apache.org/licenses/LICENSE-2.0
95324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski *
105324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * Unless required by applicable law or agreed to in writing, software
115324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * distributed under the License is distributed on an "AS IS" BASIS,
125324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * See the License for the specific language governing permissions and
145324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * limitations under the License.
155324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski */
165324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
175324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowskipackage android.bluetooth.le;
185324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
195324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowskiimport android.os.Parcel;
205324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowskiimport android.os.Parcelable;
215324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
225324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski/**
235324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * The {@link PeriodicAdvertisingParameters} provide a way to adjust periodic
245324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * advertising preferences for each Bluetooth LE advertising set. Use {@link
255324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski * AdvertisingSetParameters.Builder} to create an instance of this class.
265324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski */
275324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowskipublic final class PeriodicAdvertisingParameters implements Parcelable {
285324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
295324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    private static final int INTERVAL_MAX = 80;
305324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    private static final int INTERVAL_MIN = 65519;
315324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
325324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    private final boolean includeTxPower;
335324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    private final int interval;
345324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
35326996916df976b0939aa30551b7c30f6b3675f7Jakub Pawlowski    private PeriodicAdvertisingParameters(boolean includeTxPower, int interval) {
365324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        this.includeTxPower = includeTxPower;
375324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        this.interval = interval;
385324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    }
395324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
405324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    private PeriodicAdvertisingParameters(Parcel in) {
415324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        includeTxPower = in.readInt() != 0 ? true : false;
425324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        interval = in.readInt();
435324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    }
445324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
455324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    /**
465324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski     * Returns whether the TX Power will be included.
475324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski     */
485324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    public boolean getIncludeTxPower() { return includeTxPower; }
495324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
505324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    /**
515324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski     * Returns the periodic advertising interval, in 1.25ms unit.
525324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski     * Valid values are from 80 (100ms) to 65519 (81.89875s).
535324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski     */
545324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    public int getInterval() { return interval; }
555324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
565324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    @Override
575324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    public int describeContents() {
585324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        return 0;
595324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    }
605324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
615324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    @Override
625324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    public void writeToParcel(Parcel dest, int flags) {
635324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        dest.writeInt(includeTxPower ? 1 : 0);
645324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        dest.writeInt(interval);
655324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    }
665324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
675324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    public static final Parcelable
685324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        .Creator<PeriodicAdvertisingParameters> CREATOR =
695324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        new Creator<PeriodicAdvertisingParameters>() {
705324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            @Override
715324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            public PeriodicAdvertisingParameters[] newArray(int size) {
725324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski                return new PeriodicAdvertisingParameters[size];
735324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            }
745324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
755324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            @Override
765324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            public PeriodicAdvertisingParameters createFromParcel(Parcel in) {
775324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski                return new PeriodicAdvertisingParameters(in);
785324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            }
795324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        };
805324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
815324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    public static final class Builder {
825324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        private boolean includeTxPower = false;
835324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        private int interval = INTERVAL_MAX;
845324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
855324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        /**
865324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * Whether the transmission power level should be included in the periodic
875324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * packet.
885324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         */
895324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        public Builder setIncludeTxPower(boolean includeTxPower) {
905324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            this.includeTxPower = includeTxPower;
915324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            return this;
925324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        }
935324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
945324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        /**
955324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * Set advertising interval for periodic advertising, in 1.25ms unit.
965324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * Valid values are from 80 (100ms) to 65519 (81.89875s).
975324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * Value from range [interval, interval+20ms] will be picked as the actual value.
985324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * @throws IllegalArgumentException If the interval is invalid.
995324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         */
1005324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        public Builder setInterval(int interval) {
1015324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) {
1025324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski                throw new IllegalArgumentException("Invalid interval (must be " + INTERVAL_MIN +
1035324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski                                                   "-" + INTERVAL_MAX + ")");
1045324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            }
1055324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            this.interval = interval;
1065324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski            return this;
1075324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        }
1085324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski
1095324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        /**
1105324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         * Build the {@link AdvertisingSetParameters} object.
1115324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski         */
1125324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        public PeriodicAdvertisingParameters build() {
113326996916df976b0939aa30551b7c30f6b3675f7Jakub Pawlowski            return new PeriodicAdvertisingParameters(includeTxPower, interval);
1145324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski        }
1155324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski    }
1165324a14cf490656269ef862d7f8f6b139a21c0e6Jakub Pawlowski}
117