16d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang/*
26d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * Copyright (C) 2014 The Android Open Source Project
36d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang *
46d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * Licensed under the Apache License, Version 2.0 (the "License");
56d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * you may not use this file except in compliance with the License.
66d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * You may obtain a copy of the License at
76d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang *
86d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang *      http://www.apache.org/licenses/LICENSE-2.0
96d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang *
106d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * Unless required by applicable law or agreed to in writing, software
116d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * distributed under the License is distributed on an "AS IS" BASIS,
126d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * See the License for the specific language governing permissions and
146d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang * limitations under the License.
156d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang */
166d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
176d81118032b92caa0f5cfebe11af02a98f819d5eWei Wangpackage android.bluetooth.le;
186d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
19af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wangimport android.annotation.SystemApi;
209e377194e35c0fb9ac5771f5658c095ed97e0838Jakub Pawlowskiimport android.bluetooth.BluetoothDevice;
216d81118032b92caa0f5cfebe11af02a98f819d5eWei Wangimport android.os.Parcel;
226d81118032b92caa0f5cfebe11af02a98f819d5eWei Wangimport android.os.Parcelable;
236d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
246d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang/**
25ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang * Bluetooth LE scan settings are passed to {@link BluetoothLeScanner#startScan} to define the
26ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang * parameters for the scan.
276d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang */
286d81118032b92caa0f5cfebe11af02a98f819d5eWei Wangpublic final class ScanSettings implements Parcelable {
294190762d94e05e6c51e25e2743f8d622848d0945Wei Wang
304190762d94e05e6c51e25e2743f8d622848d0945Wei Wang    /**
314190762d94e05e6c51e25e2743f8d622848d0945Wei Wang     * A special Bluetooth LE scan mode. Applications using this scan mode will passively listen for
324190762d94e05e6c51e25e2743f8d622848d0945Wei Wang     * other scan results without starting BLE scans themselves.
334190762d94e05e6c51e25e2743f8d622848d0945Wei Wang     */
344190762d94e05e6c51e25e2743f8d622848d0945Wei Wang    public static final int SCAN_MODE_OPPORTUNISTIC = -1;
354190762d94e05e6c51e25e2743f8d622848d0945Wei Wang
366d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
37ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * Perform Bluetooth LE scan in low power mode. This is the default scan mode as it consumes the
38958154b5e2744cc370eaa18c870d04987e153630Vinay Kalia     * least power. This mode is enforced if the scanning application is not in foreground.
396d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
406d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public static final int SCAN_MODE_LOW_POWER = 0;
41af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang
426d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
43ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * Perform Bluetooth LE scan in balanced power mode. Scan results are returned at a rate that
44ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * provides a good trade-off between scan frequency and power consumption.
456d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
466d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public static final int SCAN_MODE_BALANCED = 1;
47af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang
486d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
49ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * Scan using highest duty cycle. It's recommended to only use this mode when the application is
50ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * running in the foreground.
516d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
526d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public static final int SCAN_MODE_LOW_LATENCY = 2;
536d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
546d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
55ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * Trigger a callback for every Bluetooth advertisement found that matches the filter criteria.
56ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * If no filter is active, all advertisement packets are reported.
576d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
58af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang    public static final int CALLBACK_TYPE_ALL_MATCHES = 1;
59af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang
606d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
61ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * A result callback is only triggered for the first advertisement packet received that matches
62ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * the filter criteria.
636d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
64af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang    public static final int CALLBACK_TYPE_FIRST_MATCH = 2;
65af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang
666d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
67af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang     * Receive a callback when advertisements are no longer received from a device that has been
68af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang     * previously reported by a first match callback.
696d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
70af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang    public static final int CALLBACK_TYPE_MATCH_LOST = 4;
716d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
72e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
73e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
74e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * Determines how many advertisements to match per filter, as this is scarce hw resource
75e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
76e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
77e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * Match one advertisement per filter
78e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
79e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public static final int MATCH_NUM_ONE_ADVERTISEMENT = 1;
80e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
81e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
82e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * Match few advertisement per filter, depends on current capability and availibility of
83e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * the resources in hw
84e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
85e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public static final int MATCH_NUM_FEW_ADVERTISEMENT = 2;
86e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
87e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
88e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * Match as many advertisement per filter as hw could allow, depends on current
89e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * capability and availibility of the resources in hw
90e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
91e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public static final int MATCH_NUM_MAX_ADVERTISEMENT = 3;
92e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
93e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
94e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
95e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * In Aggressive mode, hw will determine a match sooner even with feeble signal strength
96e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * and few number of sightings/match in a duration.
97e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
98e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public static final int MATCH_MODE_AGGRESSIVE = 1;
99e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
1006d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
101e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * For sticky mode, higher threshold of signal strength and sightings is required
102e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * before reporting by hw
103e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
104e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public static final int MATCH_MODE_STICKY = 2;
105e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
106e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
107e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * Request full scan results which contain the device, rssi, advertising data, scan response
108e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * as well as the scan timestamp.
1099960b576533041d07b141008c5925dcecb8c9224Wei Wang     *
1109960b576533041d07b141008c5925dcecb8c9224Wei Wang     * @hide
1116d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
1129960b576533041d07b141008c5925dcecb8c9224Wei Wang    @SystemApi
1136d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public static final int SCAN_RESULT_TYPE_FULL = 0;
114af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang
1156d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
116af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang     * Request abbreviated scan results which contain the device, rssi and scan timestamp.
117ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * <p>
118ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * <b>Note:</b> It is possible for an application to get more scan results than it asked for, if
119ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     * there are multiple apps using this type.
120ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang     *
1216d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     * @hide
1226d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
123af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang    @SystemApi
124af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang    public static final int SCAN_RESULT_TYPE_ABBREVIATED = 1;
1256d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1263aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    /**
1273aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * Use all supported PHYs for scanning.
1283aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * This will check the controller capabilities, and start
1293aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * the scan on 1Mbit and LE Coded PHYs if supported, or on
1303aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * the 1Mbit PHY only.
1313aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     */
1323aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    public static final int PHY_LE_ALL_SUPPORTED = 255;
1333aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
1346d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    // Bluetooth LE scan mode.
1356d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    private int mScanMode;
1366d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1376d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    // Bluetooth LE scan callback type
1386d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    private int mCallbackType;
1396d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1406d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    // Bluetooth LE scan result type
1416d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    private int mScanResultType;
1426d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1436d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    // Time of delay for reporting the scan result
144685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang    private long mReportDelayMillis;
1456d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
146e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    private int mMatchMode;
147e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
148e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    private int mNumOfMatchesPerFilter;
149e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
1503aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    // Include only legacy advertising results
1513aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    private boolean mLegacy;
1523aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
1533aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    private int mPhy;
1543aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
1556d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public int getScanMode() {
1566d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        return mScanMode;
1576d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
1586d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1596d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public int getCallbackType() {
1606d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        return mCallbackType;
1616d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
1626d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1636d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public int getScanResultType() {
1646d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        return mScanResultType;
1656d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
1666d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
1676d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
168e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * @hide
169e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
170e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public int getMatchMode() {
171e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        return mMatchMode;
172e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    }
173e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
174e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
175e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     * @hide
176e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham     */
177e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    public int getNumOfMatches() {
178e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        return mNumOfMatchesPerFilter;
179e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    }
180e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
181e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham    /**
1823aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * Returns whether only legacy advertisements will be returned.
1833aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * Legacy advertisements include advertisements as specified
1843aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * by the Bluetooth core specification 4.2 and below.
1853aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     */
1863aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    public boolean getLegacy() {
1873aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        return mLegacy;
1883aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    }
1893aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
1903aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    /**
1913aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     * Returns the physical layer used during a scan.
1923aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski     */
1933aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    public int getPhy() {
1943aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        return mPhy;
1953aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    }
1963aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
1973aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski    /**
1986d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     * Returns report delay timestamp based on the device clock.
1996d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
200685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang    public long getReportDelayMillis() {
201685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang        return mReportDelayMillis;
2026d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
2036d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2046d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    private ScanSettings(int scanMode, int callbackType, int scanResultType,
205a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            long reportDelayMillis, int matchMode,
206a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            int numOfMatchesPerFilter, boolean legacy, int phy) {
2076d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        mScanMode = scanMode;
2086d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        mCallbackType = callbackType;
2096d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        mScanResultType = scanResultType;
210ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang        mReportDelayMillis = reportDelayMillis;
211e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        mNumOfMatchesPerFilter = numOfMatchesPerFilter;
212c5065ee98d9ac9ff10ee89f42595cb6f135ddc65Wei Wang        mMatchMode = matchMode;
2133aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        mLegacy = legacy;
2143aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        mPhy = phy;
2156d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
2166d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2176d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    private ScanSettings(Parcel in) {
2186d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        mScanMode = in.readInt();
2196d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        mCallbackType = in.readInt();
2206d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        mScanResultType = in.readInt();
221685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang        mReportDelayMillis = in.readLong();
222e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        mMatchMode = in.readInt();
223e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        mNumOfMatchesPerFilter = in.readInt();
2242992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He        mLegacy = in.readInt() != 0;
2253aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        mPhy = in.readInt();
2266d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
2276d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2286d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    @Override
2296d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public void writeToParcel(Parcel dest, int flags) {
2306d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        dest.writeInt(mScanMode);
2316d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        dest.writeInt(mCallbackType);
2326d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        dest.writeInt(mScanResultType);
233685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang        dest.writeLong(mReportDelayMillis);
234e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        dest.writeInt(mMatchMode);
235e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        dest.writeInt(mNumOfMatchesPerFilter);
2363aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        dest.writeInt(mLegacy ? 1 : 0);
2373aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        dest.writeInt(mPhy);
2386d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
2396d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2406d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    @Override
2416d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public int describeContents() {
2426d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        return 0;
2436d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
2446d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2452992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He    public static final Parcelable.Creator<ScanSettings> CREATOR =
2462992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He            new Creator<ScanSettings>() {
247a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        @Override
248a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        public ScanSettings[] newArray(int size) {
249a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            return new ScanSettings[size];
250a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        }
2516d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
252a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        @Override
253a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        public ScanSettings createFromParcel(Parcel in) {
254a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He            return new ScanSettings(in);
255a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        }
256a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He    };
2576d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2586d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    /**
2596d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     * Builder for {@link ScanSettings}.
2606d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang     */
2616d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    public static final class Builder {
2626d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        private int mScanMode = SCAN_MODE_LOW_POWER;
263af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang        private int mCallbackType = CALLBACK_TYPE_ALL_MATCHES;
2646d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        private int mScanResultType = SCAN_RESULT_TYPE_FULL;
265685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang        private long mReportDelayMillis = 0;
266e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        private int mMatchMode = MATCH_MODE_AGGRESSIVE;
267a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He        private int mNumOfMatchesPerFilter = MATCH_NUM_MAX_ADVERTISEMENT;
2683aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        private boolean mLegacy = true;
2693aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        private int mPhy = PHY_LE_ALL_SUPPORTED;
2703aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
2716d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        /**
2726d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * Set scan mode for Bluetooth LE scan.
2736d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         *
274af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang         * @param scanMode The scan mode can be one of {@link ScanSettings#SCAN_MODE_LOW_POWER},
275a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * {@link ScanSettings#SCAN_MODE_BALANCED} or {@link ScanSettings#SCAN_MODE_LOW_LATENCY}.
2766d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * @throws IllegalArgumentException If the {@code scanMode} is invalid.
2776d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         */
2786d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        public Builder setScanMode(int scanMode) {
2794190762d94e05e6c51e25e2743f8d622848d0945Wei Wang            if (scanMode < SCAN_MODE_OPPORTUNISTIC || scanMode > SCAN_MODE_LOW_LATENCY) {
2806d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang                throw new IllegalArgumentException("invalid scan mode " + scanMode);
2816d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            }
2826d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            mScanMode = scanMode;
2836d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            return this;
2846d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        }
2856d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
2866d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        /**
2876d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * Set callback type for Bluetooth LE scan.
2886d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         *
289af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang         * @param callbackType The callback type flags for the scan.
2906d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * @throws IllegalArgumentException If the {@code callbackType} is invalid.
2916d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         */
2926d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        public Builder setCallbackType(int callbackType) {
293ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang
294ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang            if (!isValidCallbackType(callbackType)) {
2956d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang                throw new IllegalArgumentException("invalid callback type - " + callbackType);
2966d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            }
2976d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            mCallbackType = callbackType;
2986d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            return this;
2996d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        }
3006d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
301ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang        // Returns true if the callbackType is valid.
302ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang        private boolean isValidCallbackType(int callbackType) {
3032992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He            if (callbackType == CALLBACK_TYPE_ALL_MATCHES
3042992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                    || callbackType == CALLBACK_TYPE_FIRST_MATCH
3052992cd084cd5cfd9ef253c37ef269d6c75e7e144Jack He                    || callbackType == CALLBACK_TYPE_MATCH_LOST) {
306ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang                return true;
307ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang            }
308ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang            return callbackType == (CALLBACK_TYPE_FIRST_MATCH | CALLBACK_TYPE_MATCH_LOST);
309ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang        }
310ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang
3116d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        /**
3126d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * Set scan result type for Bluetooth LE scan.
3136d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         *
314a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * @param scanResultType Type for scan result, could be either {@link
315a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * ScanSettings#SCAN_RESULT_TYPE_FULL} or {@link ScanSettings#SCAN_RESULT_TYPE_ABBREVIATED}.
3166d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * @throws IllegalArgumentException If the {@code scanResultType} is invalid.
3176d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * @hide
3186d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         */
319af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang        @SystemApi
3206d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        public Builder setScanResultType(int scanResultType) {
3216d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            if (scanResultType < SCAN_RESULT_TYPE_FULL
322af74e66e29a518157cb78fcef4b4fc532b7f60b0Wei Wang                    || scanResultType > SCAN_RESULT_TYPE_ABBREVIATED) {
3236d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang                throw new IllegalArgumentException(
3246d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang                        "invalid scanResultType - " + scanResultType);
3256d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            }
3266d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            mScanResultType = scanResultType;
3276d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            return this;
3286d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        }
3296d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
3306d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        /**
3316d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * Set report delay timestamp for Bluetooth LE scan.
332685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang         *
3339960b576533041d07b141008c5925dcecb8c9224Wei Wang         * @param reportDelayMillis Delay of report in milliseconds. Set to 0 to be notified of
334a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * results immediately. Values &gt; 0 causes the scan results to be queued up and delivered
335a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * after the requested delay or when the internal buffers fill up.
336ec64dbfbc0c7ecf41e17f3872c2d0109096f1c7aWei Wang         * @throws IllegalArgumentException If {@code reportDelayMillis} &lt; 0.
3376d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         */
3389960b576533041d07b141008c5925dcecb8c9224Wei Wang        public Builder setReportDelay(long reportDelayMillis) {
339685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang            if (reportDelayMillis < 0) {
3409960b576533041d07b141008c5925dcecb8c9224Wei Wang                throw new IllegalArgumentException("reportDelay must be > 0");
341685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang            }
342685c1758902a42a7beb030d8bbaed3f7ce6f6135Wei Wang            mReportDelayMillis = reportDelayMillis;
3436d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            return this;
3446d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        }
3456d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang
3466d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        /**
347e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         * Set the number of matches for Bluetooth LE scan filters hardware match
348e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         *
349e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         * @param numOfMatches The num of matches can be one of
350a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * {@link ScanSettings#MATCH_NUM_ONE_ADVERTISEMENT}
351a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * or {@link ScanSettings#MATCH_NUM_FEW_ADVERTISEMENT} or {@link
352a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * ScanSettings#MATCH_NUM_MAX_ADVERTISEMENT}
353e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         * @throws IllegalArgumentException If the {@code matchMode} is invalid.
354e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         */
355e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        public Builder setNumOfMatches(int numOfMatches) {
356e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            if (numOfMatches < MATCH_NUM_ONE_ADVERTISEMENT
357e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham                    || numOfMatches > MATCH_NUM_MAX_ADVERTISEMENT) {
358e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham                throw new IllegalArgumentException("invalid numOfMatches " + numOfMatches);
359e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            }
360e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            mNumOfMatchesPerFilter = numOfMatches;
361e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            return this;
362e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        }
363e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
364e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        /**
365e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         * Set match mode for Bluetooth LE scan filters hardware match
366e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         *
367a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * @param matchMode The match mode can be one of {@link ScanSettings#MATCH_MODE_AGGRESSIVE}
368a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * or {@link ScanSettings#MATCH_MODE_STICKY}
369e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         * @throws IllegalArgumentException If the {@code matchMode} is invalid.
370e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham         */
371e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        public Builder setMatchMode(int matchMode) {
372e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            if (matchMode < MATCH_MODE_AGGRESSIVE
373e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham                    || matchMode > MATCH_MODE_STICKY) {
374e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham                throw new IllegalArgumentException("invalid matchMode " + matchMode);
375e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            }
376e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            mMatchMode = matchMode;
377e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham            return this;
378e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        }
379e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham
380e593d0aec6430d98731d9751facd0414a1c9c6a2Prerepa Viswanadham        /**
3813aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * Set whether only legacy advertisments should be returned in scan results.
3823aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * Legacy advertisements include advertisements as specified by the
3833aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * Bluetooth core specification 4.2 and below. This is true by default
3843aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * for compatibility with older apps.
3853aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         *
3863aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * @param legacy true if only legacy advertisements will be returned
3873aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         */
3883aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        public Builder setLegacy(boolean legacy) {
3893aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski            mLegacy = legacy;
3903aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski            return this;
3913aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        }
3923aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
3933aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        /**
3943aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * Set the Physical Layer to use during this scan.
3953aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * This is used only if {@link ScanSettings.Builder#setLegacy}
3963aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * is set to false.
3973aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * {@link android.bluetooth.BluetoothAdapter#isLeCodedPhySupported}
3983aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * may be used to check whether LE Coded phy is supported by calling
3993aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * {@link android.bluetooth.BluetoothAdapter#isLeCodedPhySupported}.
4003aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         * Selecting an unsupported phy will result in failure to start scan.
4013aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         *
402a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * @param phy Can be one of {@link BluetoothDevice#PHY_LE_1M}, {@link
403a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He         * BluetoothDevice#PHY_LE_CODED} or {@link ScanSettings#PHY_LE_ALL_SUPPORTED}
4043aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski         */
4053aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        public Builder setPhy(int phy) {
4063aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski            mPhy = phy;
4073aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski            return this;
4083aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        }
4093aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski
4103aca933861822f66984a9a3ae1db8242fe6c9521Jakub Pawlowski        /**
4116d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         * Build {@link ScanSettings}.
4126d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang         */
4136d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        public ScanSettings build() {
4146d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang            return new ScanSettings(mScanMode, mCallbackType, mScanResultType,
415a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                    mReportDelayMillis, mMatchMode,
416a355e5efaf45a534ee6437aa4bae7d30f18c0ec2Jack He                    mNumOfMatchesPerFilter, mLegacy, mPhy);
4176d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang        }
4186d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang    }
4196d81118032b92caa0f5cfebe11af02a98f819d5eWei Wang}
420