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