1ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski/*
2ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * Copyright (C) 2017 The Android Open Source Project
3ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski *
4ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * Licensed under the Apache License, Version 2.0 (the "License");
5ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * you may not use this file except in compliance with the License.
6ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * You may obtain a copy of the License at
7ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski *
8ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski *      http://www.apache.org/licenses/LICENSE-2.0
9ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski *
10ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * Unless required by applicable law or agreed to in writing, software
11ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * distributed under the License is distributed on an "AS IS" BASIS,
12ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * See the License for the specific language governing permissions and
14ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * limitations under the License.
15ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski */
16ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
17ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowskipackage android.bluetooth.le;
18ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
19ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowskiimport android.annotation.Nullable;
20ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowskiimport android.os.Parcel;
21ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowskiimport android.os.Parcelable;
22ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
23ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowskiimport java.util.Objects;
24ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
25ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski/**
26ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski * PeriodicAdvertisingReport for Bluetooth LE synchronized advertising.
27a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He *
2863fad69a19bbfe78e64d56c9d775ddf6d7cd84c9Jakub Pawlowski * @hide
29ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski */
30ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowskipublic final class PeriodicAdvertisingReport implements Parcelable {
31ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
32ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
33ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * The data returned is complete
34ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
35ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public static final int DATA_COMPLETE = 0;
36ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
37ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
38ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * The data returned is incomplete. The controller was unsuccessfull to
39ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * receive all chained packets, returning only partial data.
40ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
41ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public static final int DATA_INCOMPLETE_TRUNCATED = 2;
42ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
432992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    private int mSyncHandle;
442992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    private int mTxPower;
452992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    private int mRssi;
462992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    private int mDataStatus;
47ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
48ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    // periodic advertising data.
49ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Nullable
502992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    private ScanRecord mData;
51ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
52ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    // Device timestamp when the result was last seen.
532992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    private long mTimestampNanos;
54ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
55ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
56ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Constructor of periodic advertising result.
57ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
58ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public PeriodicAdvertisingReport(int syncHandle, int txPower, int rssi,
59a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            int dataStatus, ScanRecord data) {
602992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mSyncHandle = syncHandle;
612992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mTxPower = txPower;
622992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mRssi = rssi;
632992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mDataStatus = dataStatus;
642992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mData = data;
65ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
66ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
67ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    private PeriodicAdvertisingReport(Parcel in) {
68ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        readFromParcel(in);
69ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
70ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
71ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Override
72ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public void writeToParcel(Parcel dest, int flags) {
732992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        dest.writeInt(mSyncHandle);
744609285e291b6c5de657c8391ec43e617fb2a247Jakub Pawlowski        dest.writeInt(mTxPower);
752992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        dest.writeInt(mRssi);
762992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        dest.writeInt(mDataStatus);
772992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        if (mData != null) {
78ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski            dest.writeInt(1);
792992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He            dest.writeByteArray(mData.getBytes());
80ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        } else {
81ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski            dest.writeInt(0);
82ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        }
83ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
84ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
85ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    private void readFromParcel(Parcel in) {
862992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mSyncHandle = in.readInt();
872992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mTxPower = in.readInt();
882992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mRssi = in.readInt();
892992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mDataStatus = in.readInt();
90ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        if (in.readInt() == 1) {
912992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He            mData = ScanRecord.parseFromBytes(in.createByteArray());
92ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        }
93ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
94ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
95ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Override
96ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public int describeContents() {
97ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        return 0;
98ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
99ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
100ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
101ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Returns the synchronization handle.
102ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
103ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public int getSyncHandle() {
1042992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return mSyncHandle;
105ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
106ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
107ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
108ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Returns the transmit power in dBm. The valid range is [-127, 126]. Value
109ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * of 127 means information was not available.
110ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
111ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public int getTxPower() {
1122992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return mTxPower;
113ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
114ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
115ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
116ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Returns the received signal strength in dBm. The valid range is [-127, 20].
117ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
118ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public int getRssi() {
1192992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return mRssi;
120ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
121ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
122ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
123ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Returns the data status. Can be one of {@link PeriodicAdvertisingReport#DATA_COMPLETE}
124ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * or {@link PeriodicAdvertisingReport#DATA_INCOMPLETE_TRUNCATED}.
125ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
126ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public int getDataStatus() {
1272992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return mDataStatus;
128ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
129ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
130ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
131ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Returns the data contained in this periodic advertising report.
132ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
133ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Nullable
134ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public ScanRecord getData() {
1352992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return mData;
136ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
137ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
138ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    /**
139ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     * Returns timestamp since boot when the scan record was observed.
140ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski     */
141ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public long getTimestampNanos() {
1422992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return mTimestampNanos;
143ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
144ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
145ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Override
146ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public int hashCode() {
1472992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return Objects.hash(mSyncHandle, mTxPower, mRssi, mDataStatus, mData, mTimestampNanos);
148ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
149ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
150ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Override
151ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public boolean equals(Object obj) {
152ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        if (this == obj) {
153ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski            return true;
154ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        }
155ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        if (obj == null || getClass() != obj.getClass()) {
156ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski            return false;
157ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        }
158ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski        PeriodicAdvertisingReport other = (PeriodicAdvertisingReport) obj;
1592992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return (mSyncHandle == other.mSyncHandle)
1602992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                && (mTxPower == other.mTxPower)
1612992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                && (mRssi == other.mRssi)
1622992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                && (mDataStatus == other.mDataStatus)
1632992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                && Objects.equals(mData, other.mData)
1642992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                && (mTimestampNanos == other.mTimestampNanos);
165ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
166ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
167ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    @Override
168ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    public String toString() {
1692992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        return "PeriodicAdvertisingReport{syncHandle=" + mSyncHandle
1702992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                + ", txPower=" + mTxPower + ", rssi=" + mRssi + ", dataStatus=" + mDataStatus
1712992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                + ", data=" + Objects.toString(mData) + ", timestampNanos=" + mTimestampNanos + '}';
172ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski    }
173ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski
174a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He    public static final Parcelable.Creator<PeriodicAdvertisingReport> CREATOR =
175a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            new Creator<PeriodicAdvertisingReport>() {
176a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                @Override
177a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                public PeriodicAdvertisingReport createFromParcel(Parcel source) {
178a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                    return new PeriodicAdvertisingReport(source);
179a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                }
180a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He
181a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                @Override
182a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                public PeriodicAdvertisingReport[] newArray(int size) {
183a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                    return new PeriodicAdvertisingReport[size];
184a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                }
185a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            };
186ac7fcfa36160f1ce8b1ba8e0a3a90338a99a02a4Jakub Pawlowski}
187