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
17package android.bluetooth.le;
18
19import android.annotation.Nullable;
20import android.bluetooth.BluetoothDevice;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.util.Objects;
25
26/**
27 * ScanResult for Bluetooth LE scan.
28 */
29public final class ScanResult implements Parcelable {
30    // Remote bluetooth device.
31    private BluetoothDevice mDevice;
32
33    // Scan record, including advertising data and scan response data.
34    @Nullable
35    private ScanRecord mScanRecord;
36
37    // Received signal strength.
38    private int mRssi;
39
40    // Device timestamp when the result was last seen.
41    private long mTimestampNanos;
42
43    /**
44     * Constructor of scan result.
45     *
46     * @param device Remote bluetooth device that is found.
47     * @param scanRecord Scan record including both advertising data and scan response data.
48     * @param rssi Received signal strength.
49     * @param timestampNanos Device timestamp when the scan result was observed.
50     */
51    public ScanResult(BluetoothDevice device, ScanRecord scanRecord, int rssi,
52            long timestampNanos) {
53        mDevice = device;
54        mScanRecord = scanRecord;
55        mRssi = rssi;
56        mTimestampNanos = timestampNanos;
57    }
58
59    private ScanResult(Parcel in) {
60        readFromParcel(in);
61    }
62
63    @Override
64    public void writeToParcel(Parcel dest, int flags) {
65        if (mDevice != null) {
66            dest.writeInt(1);
67            mDevice.writeToParcel(dest, flags);
68        } else {
69            dest.writeInt(0);
70        }
71        if (mScanRecord != null) {
72            dest.writeInt(1);
73            dest.writeByteArray(mScanRecord.getBytes());
74        } else {
75            dest.writeInt(0);
76        }
77        dest.writeInt(mRssi);
78        dest.writeLong(mTimestampNanos);
79    }
80
81    private void readFromParcel(Parcel in) {
82        if (in.readInt() == 1) {
83            mDevice = BluetoothDevice.CREATOR.createFromParcel(in);
84        }
85        if (in.readInt() == 1) {
86            mScanRecord = ScanRecord.parseFromBytes(in.createByteArray());
87        }
88        mRssi = in.readInt();
89        mTimestampNanos = in.readLong();
90    }
91
92    @Override
93    public int describeContents() {
94        return 0;
95    }
96
97    /**
98     * Returns the remote bluetooth device identified by the bluetooth device address.
99     */
100    public BluetoothDevice getDevice() {
101        return mDevice;
102    }
103
104    /**
105     * Returns the scan record, which is a combination of advertisement and scan response.
106     */
107    @Nullable
108    public ScanRecord getScanRecord() {
109        return mScanRecord;
110    }
111
112    /**
113     * Returns the received signal strength in dBm. The valid range is [-127, 127].
114     */
115    public int getRssi() {
116        return mRssi;
117    }
118
119    /**
120     * Returns timestamp since boot when the scan record was observed.
121     */
122    public long getTimestampNanos() {
123        return mTimestampNanos;
124    }
125
126    @Override
127    public int hashCode() {
128        return Objects.hash(mDevice, mRssi, mScanRecord, mTimestampNanos);
129    }
130
131    @Override
132    public boolean equals(Object obj) {
133        if (this == obj) {
134            return true;
135        }
136        if (obj == null || getClass() != obj.getClass()) {
137            return false;
138        }
139        ScanResult other = (ScanResult) obj;
140        return Objects.equals(mDevice, other.mDevice) && (mRssi == other.mRssi) &&
141                Objects.equals(mScanRecord, other.mScanRecord)
142                && (mTimestampNanos == other.mTimestampNanos);
143    }
144
145    @Override
146    public String toString() {
147        return "ScanResult{" + "mDevice=" + mDevice + ", mScanRecord="
148                + Objects.toString(mScanRecord) + ", mRssi=" + mRssi + ", mTimestampNanos="
149                + mTimestampNanos + '}';
150    }
151
152    public static final Parcelable.Creator<ScanResult> CREATOR = new Creator<ScanResult>() {
153            @Override
154        public ScanResult createFromParcel(Parcel source) {
155            return new ScanResult(source);
156        }
157
158            @Override
159        public ScanResult[] newArray(int size) {
160            return new ScanResult[size];
161        }
162    };
163
164}
165