Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 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 | |
| 17 | package android.bluetooth.le; |
| 18 | |
Jakub Pawlowski | 64307cc | 2019-04-10 18:48:57 +0200 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 22 | import android.bluetooth.BluetoothUuid; |
| 23 | import android.os.ParcelUuid; |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 24 | import android.util.ArrayMap; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 25 | import android.util.Log; |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 26 | import android.util.SparseArray; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.List; |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 31 | import java.util.Map; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Represents a scan record from Bluetooth LE scan. |
| 35 | */ |
| 36 | public final class ScanRecord { |
| 37 | |
| 38 | private static final String TAG = "ScanRecord"; |
| 39 | |
| 40 | // The following data type values are assigned by Bluetooth SIG. |
| 41 | // For more details refer to Bluetooth 4.1 specification, Volume 3, Part C, Section 18. |
| 42 | private static final int DATA_TYPE_FLAGS = 0x01; |
| 43 | private static final int DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL = 0x02; |
| 44 | private static final int DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE = 0x03; |
| 45 | private static final int DATA_TYPE_SERVICE_UUIDS_32_BIT_PARTIAL = 0x04; |
| 46 | private static final int DATA_TYPE_SERVICE_UUIDS_32_BIT_COMPLETE = 0x05; |
| 47 | private static final int DATA_TYPE_SERVICE_UUIDS_128_BIT_PARTIAL = 0x06; |
| 48 | private static final int DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE = 0x07; |
| 49 | private static final int DATA_TYPE_LOCAL_NAME_SHORT = 0x08; |
| 50 | private static final int DATA_TYPE_LOCAL_NAME_COMPLETE = 0x09; |
| 51 | private static final int DATA_TYPE_TX_POWER_LEVEL = 0x0A; |
Jakub Pawlowski | 45033a7 | 2017-05-26 13:59:55 -0700 | [diff] [blame] | 52 | private static final int DATA_TYPE_SERVICE_DATA_16_BIT = 0x16; |
| 53 | private static final int DATA_TYPE_SERVICE_DATA_32_BIT = 0x20; |
| 54 | private static final int DATA_TYPE_SERVICE_DATA_128_BIT = 0x21; |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 55 | private static final int DATA_TYPE_SERVICE_SOLICITATION_UUIDS_16_BIT = 0x14; |
| 56 | private static final int DATA_TYPE_SERVICE_SOLICITATION_UUIDS_32_BIT = 0x1F; |
| 57 | private static final int DATA_TYPE_SERVICE_SOLICITATION_UUIDS_128_BIT = 0x15; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 58 | private static final int DATA_TYPE_MANUFACTURER_SPECIFIC_DATA = 0xFF; |
| 59 | |
| 60 | // Flags of the advertising data. |
| 61 | private final int mAdvertiseFlags; |
| 62 | |
| 63 | @Nullable |
| 64 | private final List<ParcelUuid> mServiceUuids; |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 65 | @Nullable |
| 66 | private final List<ParcelUuid> mServiceSolicitationUuids; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 67 | |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 68 | private final SparseArray<byte[]> mManufacturerSpecificData; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 69 | |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 70 | private final Map<ParcelUuid, byte[]> mServiceData; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 71 | |
| 72 | // Transmission power level(in dB). |
| 73 | private final int mTxPowerLevel; |
| 74 | |
| 75 | // Local name of the Bluetooth LE device. |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 76 | private final String mDeviceName; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 77 | |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 78 | // Raw bytes of scan record. |
| 79 | private final byte[] mBytes; |
| 80 | |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Returns the advertising flags indicating the discoverable mode and capability of the device. |
| 83 | * Returns -1 if the flag field is not set. |
| 84 | */ |
| 85 | public int getAdvertiseFlags() { |
| 86 | return mAdvertiseFlags; |
| 87 | } |
| 88 | |
| 89 | /** |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 90 | * Returns a list of service UUIDs within the advertisement that are used to identify the |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 91 | * bluetooth GATT services. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 92 | */ |
| 93 | public List<ParcelUuid> getServiceUuids() { |
| 94 | return mServiceUuids; |
| 95 | } |
| 96 | |
| 97 | /** |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 98 | * Returns a list of service solicitation UUIDs within the advertisement that are used to |
| 99 | * identify the Bluetooth GATT services. |
| 100 | */ |
Jakub Pawlowski | 64307cc | 2019-04-10 18:48:57 +0200 | [diff] [blame] | 101 | @NonNull |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 102 | public List<ParcelUuid> getServiceSolicitationUuids() { |
| 103 | return mServiceSolicitationUuids; |
| 104 | } |
| 105 | |
| 106 | /** |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 107 | * Returns a sparse array of manufacturer identifier and its corresponding manufacturer specific |
| 108 | * data. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 109 | */ |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 110 | public SparseArray<byte[]> getManufacturerSpecificData() { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 111 | return mManufacturerSpecificData; |
| 112 | } |
| 113 | |
| 114 | /** |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 115 | * Returns the manufacturer specific data associated with the manufacturer id. Returns |
| 116 | * {@code null} if the {@code manufacturerId} is not found. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 117 | */ |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 118 | @Nullable |
| 119 | public byte[] getManufacturerSpecificData(int manufacturerId) { |
Jakub Pawlowski | 4c2aa61 | 2018-11-28 23:36:53 +0100 | [diff] [blame] | 120 | if (mManufacturerSpecificData == null) { |
| 121 | return null; |
| 122 | } |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 123 | return mManufacturerSpecificData.get(manufacturerId); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 127 | * Returns a map of service UUID and its corresponding service data. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 128 | */ |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 129 | public Map<ParcelUuid, byte[]> getServiceData() { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 130 | return mServiceData; |
| 131 | } |
| 132 | |
| 133 | /** |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 134 | * Returns the service data byte array associated with the {@code serviceUuid}. Returns |
| 135 | * {@code null} if the {@code serviceDataUuid} is not found. |
| 136 | */ |
| 137 | @Nullable |
| 138 | public byte[] getServiceData(ParcelUuid serviceDataUuid) { |
Jakub Pawlowski | 5988012 | 2018-06-04 10:32:06 -0700 | [diff] [blame] | 139 | if (serviceDataUuid == null || mServiceData == null) { |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 140 | return null; |
| 141 | } |
| 142 | return mServiceData.get(serviceDataUuid); |
| 143 | } |
| 144 | |
| 145 | /** |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 146 | * Returns the transmission power level of the packet in dBm. Returns {@link Integer#MIN_VALUE} |
| 147 | * if the field is not set. This value can be used to calculate the path loss of a received |
| 148 | * packet using the following equation: |
| 149 | * <p> |
| 150 | * <code>pathloss = txPowerLevel - rssi</code> |
| 151 | */ |
| 152 | public int getTxPowerLevel() { |
| 153 | return mTxPowerLevel; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the local name of the BLE device. The is a UTF-8 encoded string. |
| 158 | */ |
| 159 | @Nullable |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 160 | public String getDeviceName() { |
| 161 | return mDeviceName; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Returns raw bytes of scan record. |
| 166 | */ |
| 167 | public byte[] getBytes() { |
| 168 | return mBytes; |
| 169 | } |
| 170 | |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 171 | private ScanRecord(List<ParcelUuid> serviceUuids, |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 172 | List<ParcelUuid> serviceSolicitationUuids, |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 173 | SparseArray<byte[]> manufacturerData, |
| 174 | Map<ParcelUuid, byte[]> serviceData, |
| 175 | int advertiseFlags, int txPowerLevel, |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 176 | String localName, byte[] bytes) { |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 177 | mServiceSolicitationUuids = serviceSolicitationUuids; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 178 | mServiceUuids = serviceUuids; |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 179 | mManufacturerSpecificData = manufacturerData; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 180 | mServiceData = serviceData; |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 181 | mDeviceName = localName; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 182 | mAdvertiseFlags = advertiseFlags; |
| 183 | mTxPowerLevel = txPowerLevel; |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 184 | mBytes = bytes; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Parse scan record bytes to {@link ScanRecord}. |
| 189 | * <p> |
| 190 | * The format is defined in Bluetooth 4.1 specification, Volume 3, Part C, Section 11 and 18. |
| 191 | * <p> |
| 192 | * All numerical multi-byte entities and values shall use little-endian <strong>byte</strong> |
| 193 | * order. |
| 194 | * |
| 195 | * @param scanRecord The scan record of Bluetooth LE advertisement and/or scan response. |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 196 | * @hide |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 197 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 199 | public static ScanRecord parseFromBytes(byte[] scanRecord) { |
| 200 | if (scanRecord == null) { |
| 201 | return null; |
| 202 | } |
| 203 | |
| 204 | int currentPos = 0; |
| 205 | int advertiseFlag = -1; |
| 206 | List<ParcelUuid> serviceUuids = new ArrayList<ParcelUuid>(); |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 207 | List<ParcelUuid> serviceSolicitationUuids = new ArrayList<ParcelUuid>(); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 208 | String localName = null; |
| 209 | int txPowerLevel = Integer.MIN_VALUE; |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 210 | |
| 211 | SparseArray<byte[]> manufacturerData = new SparseArray<byte[]>(); |
| 212 | Map<ParcelUuid, byte[]> serviceData = new ArrayMap<ParcelUuid, byte[]>(); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 213 | |
| 214 | try { |
| 215 | while (currentPos < scanRecord.length) { |
| 216 | // length is unsigned int. |
| 217 | int length = scanRecord[currentPos++] & 0xFF; |
| 218 | if (length == 0) { |
| 219 | break; |
| 220 | } |
| 221 | // Note the length includes the length of the field type itself. |
| 222 | int dataLength = length - 1; |
| 223 | // fieldType is unsigned int. |
| 224 | int fieldType = scanRecord[currentPos++] & 0xFF; |
| 225 | switch (fieldType) { |
| 226 | case DATA_TYPE_FLAGS: |
| 227 | advertiseFlag = scanRecord[currentPos] & 0xFF; |
| 228 | break; |
| 229 | case DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL: |
| 230 | case DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE: |
| 231 | parseServiceUuid(scanRecord, currentPos, |
| 232 | dataLength, BluetoothUuid.UUID_BYTES_16_BIT, serviceUuids); |
| 233 | break; |
| 234 | case DATA_TYPE_SERVICE_UUIDS_32_BIT_PARTIAL: |
| 235 | case DATA_TYPE_SERVICE_UUIDS_32_BIT_COMPLETE: |
| 236 | parseServiceUuid(scanRecord, currentPos, dataLength, |
| 237 | BluetoothUuid.UUID_BYTES_32_BIT, serviceUuids); |
| 238 | break; |
| 239 | case DATA_TYPE_SERVICE_UUIDS_128_BIT_PARTIAL: |
| 240 | case DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE: |
| 241 | parseServiceUuid(scanRecord, currentPos, dataLength, |
| 242 | BluetoothUuid.UUID_BYTES_128_BIT, serviceUuids); |
| 243 | break; |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 244 | case DATA_TYPE_SERVICE_SOLICITATION_UUIDS_16_BIT: |
| 245 | parseServiceSolicitationUuid(scanRecord, currentPos, dataLength, |
| 246 | BluetoothUuid.UUID_BYTES_16_BIT, serviceSolicitationUuids); |
| 247 | break; |
| 248 | case DATA_TYPE_SERVICE_SOLICITATION_UUIDS_32_BIT: |
| 249 | parseServiceSolicitationUuid(scanRecord, currentPos, dataLength, |
| 250 | BluetoothUuid.UUID_BYTES_32_BIT, serviceSolicitationUuids); |
| 251 | break; |
| 252 | case DATA_TYPE_SERVICE_SOLICITATION_UUIDS_128_BIT: |
| 253 | parseServiceSolicitationUuid(scanRecord, currentPos, dataLength, |
| 254 | BluetoothUuid.UUID_BYTES_128_BIT, serviceSolicitationUuids); |
| 255 | break; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 256 | case DATA_TYPE_LOCAL_NAME_SHORT: |
| 257 | case DATA_TYPE_LOCAL_NAME_COMPLETE: |
| 258 | localName = new String( |
| 259 | extractBytes(scanRecord, currentPos, dataLength)); |
| 260 | break; |
| 261 | case DATA_TYPE_TX_POWER_LEVEL: |
| 262 | txPowerLevel = scanRecord[currentPos]; |
| 263 | break; |
Jakub Pawlowski | 45033a7 | 2017-05-26 13:59:55 -0700 | [diff] [blame] | 264 | case DATA_TYPE_SERVICE_DATA_16_BIT: |
| 265 | case DATA_TYPE_SERVICE_DATA_32_BIT: |
| 266 | case DATA_TYPE_SERVICE_DATA_128_BIT: |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 267 | int serviceUuidLength = BluetoothUuid.UUID_BYTES_16_BIT; |
Jakub Pawlowski | 45033a7 | 2017-05-26 13:59:55 -0700 | [diff] [blame] | 268 | if (fieldType == DATA_TYPE_SERVICE_DATA_32_BIT) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 269 | serviceUuidLength = BluetoothUuid.UUID_BYTES_32_BIT; |
Jakub Pawlowski | 45033a7 | 2017-05-26 13:59:55 -0700 | [diff] [blame] | 270 | } else if (fieldType == DATA_TYPE_SERVICE_DATA_128_BIT) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 271 | serviceUuidLength = BluetoothUuid.UUID_BYTES_128_BIT; |
Jakub Pawlowski | 45033a7 | 2017-05-26 13:59:55 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 274 | byte[] serviceDataUuidBytes = extractBytes(scanRecord, currentPos, |
| 275 | serviceUuidLength); |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 276 | ParcelUuid serviceDataUuid = BluetoothUuid.parseUuidFrom( |
| 277 | serviceDataUuidBytes); |
| 278 | byte[] serviceDataArray = extractBytes(scanRecord, |
| 279 | currentPos + serviceUuidLength, dataLength - serviceUuidLength); |
| 280 | serviceData.put(serviceDataUuid, serviceDataArray); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 281 | break; |
| 282 | case DATA_TYPE_MANUFACTURER_SPECIFIC_DATA: |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 283 | // The first two bytes of the manufacturer specific data are |
| 284 | // manufacturer ids in little endian. |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 285 | int manufacturerId = ((scanRecord[currentPos + 1] & 0xFF) << 8) |
| 286 | + (scanRecord[currentPos] & 0xFF); |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 287 | byte[] manufacturerDataBytes = extractBytes(scanRecord, currentPos + 2, |
Wei Wang | ab2ed62 | 2014-07-25 15:14:55 -0700 | [diff] [blame] | 288 | dataLength - 2); |
Wei Wang | 6bf513d | 2014-08-01 11:12:37 -0700 | [diff] [blame] | 289 | manufacturerData.put(manufacturerId, manufacturerDataBytes); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 290 | break; |
| 291 | default: |
| 292 | // Just ignore, we don't handle such data type. |
| 293 | break; |
| 294 | } |
| 295 | currentPos += dataLength; |
| 296 | } |
| 297 | |
| 298 | if (serviceUuids.isEmpty()) { |
| 299 | serviceUuids = null; |
| 300 | } |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 301 | return new ScanRecord(serviceUuids, serviceSolicitationUuids, manufacturerData, |
| 302 | serviceData, advertiseFlag, txPowerLevel, localName, scanRecord); |
Prerepa Viswanadham | 621085e | 2014-08-13 16:52:55 -0700 | [diff] [blame] | 303 | } catch (Exception e) { |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 304 | Log.e(TAG, "unable to parse scan record: " + Arrays.toString(scanRecord)); |
Prerepa Viswanadham | 621085e | 2014-08-13 16:52:55 -0700 | [diff] [blame] | 305 | // As the record is invalid, ignore all the parsed results for this packet |
| 306 | // and return an empty record with raw scanRecord bytes in results |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 307 | return new ScanRecord(null, null, null, null, -1, Integer.MIN_VALUE, null, scanRecord); |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public String toString() { |
| 313 | return "ScanRecord [mAdvertiseFlags=" + mAdvertiseFlags + ", mServiceUuids=" + mServiceUuids |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 314 | + ", mServiceSolicitationUuids=" + mServiceSolicitationUuids |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 315 | + ", mManufacturerSpecificData=" + BluetoothLeUtils.toString( |
| 316 | mManufacturerSpecificData) |
Wei Wang | 833559d | 2014-08-29 10:26:13 -0700 | [diff] [blame] | 317 | + ", mServiceData=" + BluetoothLeUtils.toString(mServiceData) |
Wei Wang | 685c1758 | 2014-07-16 22:02:03 -0700 | [diff] [blame] | 318 | + ", mTxPowerLevel=" + mTxPowerLevel + ", mDeviceName=" + mDeviceName + "]"; |
| 319 | } |
| 320 | |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 321 | // Parse service UUIDs. |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 322 | private static int parseServiceUuid(byte[] scanRecord, int currentPos, int dataLength, |
| 323 | int uuidLength, List<ParcelUuid> serviceUuids) { |
| 324 | while (dataLength > 0) { |
| 325 | byte[] uuidBytes = extractBytes(scanRecord, currentPos, |
| 326 | uuidLength); |
| 327 | serviceUuids.add(BluetoothUuid.parseUuidFrom(uuidBytes)); |
| 328 | dataLength -= uuidLength; |
| 329 | currentPos += uuidLength; |
| 330 | } |
| 331 | return currentPos; |
| 332 | } |
| 333 | |
Nitin Shivpure | 1555eae | 2018-04-02 13:45:45 +0530 | [diff] [blame] | 334 | /** |
| 335 | * Parse service Solicitation UUIDs. |
| 336 | */ |
| 337 | private static int parseServiceSolicitationUuid(byte[] scanRecord, int currentPos, |
| 338 | int dataLength, int uuidLength, List<ParcelUuid> serviceSolicitationUuids) { |
| 339 | while (dataLength > 0) { |
| 340 | byte[] uuidBytes = extractBytes(scanRecord, currentPos, uuidLength); |
| 341 | serviceSolicitationUuids.add(BluetoothUuid.parseUuidFrom(uuidBytes)); |
| 342 | dataLength -= uuidLength; |
| 343 | currentPos += uuidLength; |
| 344 | } |
| 345 | return currentPos; |
| 346 | } |
| 347 | |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 348 | // Helper method to extract bytes from byte array. |
| 349 | private static byte[] extractBytes(byte[] scanRecord, int start, int length) { |
| 350 | byte[] bytes = new byte[length]; |
| 351 | System.arraycopy(scanRecord, start, bytes, 0, length); |
| 352 | return bytes; |
| 353 | } |
| 354 | } |