diff options
author | Jakub Pawlowski <jpawlowski@google.com> | 2017-01-17 07:50:46 -0800 |
---|---|---|
committer | Jakub Pawlowski <jpawlowski@google.com> | 2017-03-08 19:02:25 +0000 |
commit | bfe28687cadf9339242844fb662b2bfb8e7ff109 (patch) | |
tree | 1b09f4b33fce75110c6f37da9308e64a586d357f /framework/java/android/bluetooth/le/ScanResult.java | |
parent | eed95db01e274d0f22730f9b358dcb52ff832d9f (diff) |
Bluetooth 5 enhanced scanning API
Bug: 30622771
Test: manual
Change-Id: I2c8065fbcedf48777ce18c7d8fe621e568b3fd75
(cherry picked from commit 9de522c6e48497028d36a1f8ad8f8adf4b7b1ae6)
Diffstat (limited to 'framework/java/android/bluetooth/le/ScanResult.java')
-rw-r--r-- | framework/java/android/bluetooth/le/ScanResult.java | 205 |
1 files changed, 194 insertions, 11 deletions
diff --git a/framework/java/android/bluetooth/le/ScanResult.java b/framework/java/android/bluetooth/le/ScanResult.java index 2fdfe7f8a8..f92357b59c 100644 --- a/framework/java/android/bluetooth/le/ScanResult.java +++ b/framework/java/android/bluetooth/le/ScanResult.java @@ -27,7 +27,56 @@ import java.util.Objects; * ScanResult for Bluetooth LE scan. */ public final class ScanResult implements Parcelable { - // Remote bluetooth device. + + /** + * For chained advertisements, inidcates tha the data contained in this + * scan result is complete. + */ + public static final int DATA_COMPLETE = 0x00; + + /** + * For chained advertisements, indicates that the controller was + * unable to receive all chained packets and the scan result contains + * incomplete truncated data. + */ + public static final int DATA_TRUNCATED = 0x02; + + /** + * Indicates that the secondary physical layer was not used. + */ + public static final int PHY_UNUSED = 0x00; + + /** + * Bluetooth LE 1Mbit advertiser PHY. + */ + public static final int PHY_LE_1M = 0x01; + + /** + * Bluetooth LE 2Mbit advertiser PHY. + */ + public static final int PHY_LE_2M = 0x02; + + /** + * Bluetooth LE Coded advertiser PHY. + */ + public static final int PHY_LE_CODED = 0x03; + + /** + * Advertising Set ID is not present in the packet. + */ + public static final int SID_NOT_PRESENT = 0xFF; + + /** + * Mask for checking wether event type represents legacy advertisement. + */ + private static final int ET_LEGACY_MASK = 0x10; + + /** + * Mask for checking wether event type represents connectable advertisement. + */ + private static final int ET_CONNECTABLE_MASK = 0x01; + + // Remote Bluetooth device. private BluetoothDevice mDevice; // Scan record, including advertising data and scan response data. @@ -40,13 +89,21 @@ public final class ScanResult implements Parcelable { // Device timestamp when the result was last seen. private long mTimestampNanos; + private int mEventType; + private int mPrimaryPhy; + private int mSecondaryPhy; + private int mAdvertisingSid; + private int mTxPower; + private int mPeriodicAdvertisingInterval; + /** - * Constructor of scan result. + * Constructs a new ScanResult. * - * @param device Remote bluetooth device that is found. + * @param device Remote Bluetooth device found. * @param scanRecord Scan record including both advertising data and scan response data. * @param rssi Received signal strength. - * @param timestampNanos Device timestamp when the scan result was observed. + * @param timestampNanos Timestamp at which the scan result was observed. + * @deprecated use {@link #ScanResult(BluetoothDevice, int, int, int, int, int, int, int, ScanRecord, long)} */ public ScanResult(BluetoothDevice device, ScanRecord scanRecord, int rssi, long timestampNanos) { @@ -54,6 +111,41 @@ public final class ScanResult implements Parcelable { mScanRecord = scanRecord; mRssi = rssi; mTimestampNanos = timestampNanos; + mEventType = (DATA_COMPLETE << 5) | ET_LEGACY_MASK | ET_CONNECTABLE_MASK; + mPrimaryPhy = PHY_LE_1M; + mSecondaryPhy = PHY_UNUSED; + mAdvertisingSid = SID_NOT_PRESENT; + mTxPower = 127; + mPeriodicAdvertisingInterval = 0; + } + + /** + * Constructs a new ScanResult. + * + * @param device Remote Bluetooth device found. + * @param eventType Event type. + * @param primaryPhy Primary advertising phy. + * @param secondaryPhy Secondary advertising phy. + * @param advertisingSid Advertising set ID. + * @param txPower Transmit power. + * @param rssi Received signal strength. + * @param periodicAdvertisingInterval Periodic advertising interval. + * @param scanRecord Scan record including both advertising data and scan response data. + * @param timestampNanos Timestamp at which the scan result was observed. + */ + public ScanResult(BluetoothDevice device, int eventType, int primaryPhy, int secondaryPhy, + int advertisingSid, int txPower, int rssi, int periodicAdvertisingInterval, + ScanRecord scanRecord, long timestampNanos) { + mDevice = device; + mEventType = eventType; + mPrimaryPhy = primaryPhy; + mSecondaryPhy = secondaryPhy; + mAdvertisingSid = advertisingSid; + mTxPower = txPower; + mRssi = rssi; + mPeriodicAdvertisingInterval = periodicAdvertisingInterval; + mScanRecord = scanRecord; + mTimestampNanos = timestampNanos; } private ScanResult(Parcel in) { @@ -76,6 +168,12 @@ public final class ScanResult implements Parcelable { } dest.writeInt(mRssi); dest.writeLong(mTimestampNanos); + dest.writeInt(mEventType); + dest.writeInt(mPrimaryPhy); + dest.writeInt(mSecondaryPhy); + dest.writeInt(mAdvertisingSid); + dest.writeInt(mTxPower); + dest.writeInt(mPeriodicAdvertisingInterval); } private void readFromParcel(Parcel in) { @@ -87,6 +185,12 @@ public final class ScanResult implements Parcelable { } mRssi = in.readInt(); mTimestampNanos = in.readLong(); + mEventType = in.readInt(); + mPrimaryPhy = in.readInt(); + mSecondaryPhy = in.readInt(); + mAdvertisingSid = in.readInt(); + mTxPower = in.readInt(); + mPeriodicAdvertisingInterval = in.readInt(); } @Override @@ -95,7 +199,7 @@ public final class ScanResult implements Parcelable { } /** - * Returns the remote bluetooth device identified by the bluetooth device address. + * Returns the remote Bluetooth device identified by the Bluetooth device address. */ public BluetoothDevice getDevice() { return mDevice; @@ -123,9 +227,79 @@ public final class ScanResult implements Parcelable { return mTimestampNanos; } + /** + * Returns true if this object represents legacy scan result. + * Legacy scan results do not contain advanced advertising information + * as specified in the Bluetooth Core Specification v5. + */ + public boolean isLegacy() { + return (mEventType & ET_LEGACY_MASK) != 0; + } + + /** + * Returns true if this object represents connectable scan result. + */ + public boolean isConnectable() { + return (mEventType & ET_CONNECTABLE_MASK) != 0; + } + + /** + * Returns the data status. + * Can be one of {@link ScanResult#DATA_COMPLETE} or + * {@link ScanResult#DATA_TRUNCATED}. + */ + public int getDataStatus() { + // return bit 5 and 6 + return (mEventType >> 5) & 0x03; + } + + /** + * Returns the primary Physical Layer + * on which this advertisment was received. + * Can be one of {@link ScanResult#PHY_LE_1M} or + * {@link ScanResult#PHY_LE_CODED}. + */ + public int getPrimaryPhy() { return mPrimaryPhy; } + + /** + * Returns the secondary Physical Layer + * on which this advertisment was received. + * Can be one of {@link ScanResult#PHY_LE_1M}, + * {@link ScanResult#PHY_LE_2M}, {@link ScanResult#PHY_LE_CODED} + * or {@link ScanResult#PHY_UNUSED} - if the advertisement + * was not received on a secondary physical channel. + */ + public int getSecondaryPhy() { return mSecondaryPhy; } + + /** + * Returns the advertising set id. + * May return {@link ScanResult#SID_NOT_PRESENT} if + * no set id was is present. + */ + public int getAdvertisingSid() { return mAdvertisingSid; } + + /** + * Returns the transmit power in dBm. + * Valid range is [-127, 126]. A value of 127 indicates that the + * advertisement did not indicate TX power. + */ + public int getTxPower() { return mTxPower; } + + /** + * Returns the periodic advertising interval in units of 1.25ms. + * Valid range is 6 (7.5ms) to 65536 (81918.75ms). A value of 0 means + * periodic advertising is not used for this scan result. + */ + public int getPeriodicAdvertisingInterval() { + return mPeriodicAdvertisingInterval; + } + @Override public int hashCode() { - return Objects.hash(mDevice, mRssi, mScanRecord, mTimestampNanos); + return Objects.hash(mDevice, mRssi, mScanRecord, mTimestampNanos, + mEventType, mPrimaryPhy, mSecondaryPhy, + mAdvertisingSid, mTxPower, + mPeriodicAdvertisingInterval); } @Override @@ -138,15 +312,24 @@ public final class ScanResult implements Parcelable { } ScanResult other = (ScanResult) obj; return Objects.equals(mDevice, other.mDevice) && (mRssi == other.mRssi) && - Objects.equals(mScanRecord, other.mScanRecord) - && (mTimestampNanos == other.mTimestampNanos); + Objects.equals(mScanRecord, other.mScanRecord) && + (mTimestampNanos == other.mTimestampNanos) && + mEventType == other.mEventType && + mPrimaryPhy == other.mPrimaryPhy && + mSecondaryPhy == other.mSecondaryPhy && + mAdvertisingSid == other.mAdvertisingSid && + mTxPower == other.mTxPower && + mPeriodicAdvertisingInterval == other.mPeriodicAdvertisingInterval; } @Override public String toString() { - return "ScanResult{" + "mDevice=" + mDevice + ", mScanRecord=" - + Objects.toString(mScanRecord) + ", mRssi=" + mRssi + ", mTimestampNanos=" - + mTimestampNanos + '}'; + return "ScanResult{" + "device=" + mDevice + ", scanRecord=" + + Objects.toString(mScanRecord) + ", rssi=" + mRssi + + ", timestampNanos=" + mTimestampNanos + ", eventType=" + mEventType + + ", primaryPhy=" + mPrimaryPhy + ", secondaryPhy=" + mSecondaryPhy + + ", advertisingSid=" + mAdvertisingSid + ", txPower=" + mTxPower + + ", periodicAdvertisingInterval=" + mPeriodicAdvertisingInterval + '}'; } public static final Parcelable.Creator<ScanResult> CREATOR = new Creator<ScanResult>() { |