1 /* 2 * Copyright (C) 2012 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 com.android.nfc.handover; 18 19 import java.nio.BufferUnderflowException; 20 import java.nio.ByteBuffer; 21 import java.nio.ByteOrder; 22 import java.nio.charset.StandardCharsets; 23 import java.util.ArrayList; 24 import java.nio.charset.Charset; 25 import java.util.Arrays; 26 import java.util.Random; 27 28 import android.bluetooth.BluetoothAdapter; 29 import android.bluetooth.BluetoothClass; 30 import android.bluetooth.BluetoothDevice; 31 import android.bluetooth.BluetoothUuid; 32 import android.bluetooth.OobData; 33 import android.content.Context; 34 import android.content.Intent; 35 import android.nfc.FormatException; 36 import android.nfc.NdefMessage; 37 import android.nfc.NdefRecord; 38 import android.os.ParcelUuid; 39 import android.os.UserHandle; 40 import android.util.Log; 41 42 /** 43 * Manages handover of NFC to other technologies. 44 */ 45 public class HandoverDataParser { 46 private static final String TAG = "NfcHandover"; 47 private static final boolean DBG = false; 48 49 private static final byte[] TYPE_BT_OOB = "application/vnd.bluetooth.ep.oob" 50 .getBytes(StandardCharsets.US_ASCII); 51 private static final byte[] TYPE_BLE_OOB = "application/vnd.bluetooth.le.oob" 52 .getBytes(StandardCharsets.US_ASCII); 53 54 private static final byte[] TYPE_NOKIA = "nokia.com:bt".getBytes(StandardCharsets.US_ASCII); 55 56 private static final byte[] RTD_COLLISION_RESOLUTION = {0x63, 0x72}; // "cr"; 57 58 private static final int CARRIER_POWER_STATE_INACTIVE = 0; 59 private static final int CARRIER_POWER_STATE_ACTIVE = 1; 60 private static final int CARRIER_POWER_STATE_ACTIVATING = 2; 61 private static final int CARRIER_POWER_STATE_UNKNOWN = 3; 62 63 private static final int BT_HANDOVER_TYPE_MAC = 0x1B; 64 private static final int BT_HANDOVER_TYPE_LE_ROLE = 0x1C; 65 private static final int BT_HANDOVER_TYPE_LONG_LOCAL_NAME = 0x09; 66 private static final int BT_HANDOVER_TYPE_SHORT_LOCAL_NAME = 0x08; 67 private static final int BT_HANDOVER_TYPE_16_BIT_UUIDS_PARTIAL = 0x02; 68 private static final int BT_HANDOVER_TYPE_16_BIT_UUIDS_COMPLETE = 0x03; 69 private static final int BT_HANDOVER_TYPE_32_BIT_UUIDS_PARTIAL = 0x04; 70 private static final int BT_HANDOVER_TYPE_32_BIT_UUIDS_COMPLETE = 0x05; 71 private static final int BT_HANDOVER_TYPE_128_BIT_UUIDS_PARTIAL = 0x06; 72 private static final int BT_HANDOVER_TYPE_128_BIT_UUIDS_COMPLETE = 0x07; 73 private static final int BT_HANDOVER_TYPE_CLASS_OF_DEVICE = 0x0D; 74 private static final int BT_HANDOVER_TYPE_SECURITY_MANAGER_TK = 0x10; 75 private static final int BT_HANDOVER_TYPE_APPEARANCE = 0x19; 76 private static final int BT_HANDOVER_TYPE_LE_SC_CONFIRMATION = 0x22; 77 private static final int BT_HANDOVER_TYPE_LE_SC_RANDOM = 0x23; 78 79 public static final int BT_HANDOVER_LE_ROLE_CENTRAL_ONLY = 0x01; 80 81 public static final int SECURITY_MANAGER_TK_SIZE = 16; 82 public static final int SECURITY_MANAGER_LE_SC_C_SIZE = 16; 83 public static final int SECURITY_MANAGER_LE_SC_R_SIZE = 16; 84 private static final int CLASS_OF_DEVICE_SIZE = 3; 85 86 private final BluetoothAdapter mBluetoothAdapter; 87 88 private final Object mLock = new Object(); 89 // Variables below synchronized on mLock 90 91 private String mLocalBluetoothAddress; 92 93 public static class BluetoothHandoverData { 94 public boolean valid = false; 95 public BluetoothDevice device; 96 public String name; 97 public boolean carrierActivating = false; 98 public int transport = BluetoothDevice.TRANSPORT_AUTO; 99 public OobData oobData; 100 public ParcelUuid[] uuids = null; 101 public BluetoothClass btClass = null; 102 } 103 104 public static class IncomingHandoverData { 105 public final NdefMessage handoverSelect; 106 public final BluetoothHandoverData handoverData; 107 IncomingHandoverData(NdefMessage handoverSelect, BluetoothHandoverData handoverData)108 public IncomingHandoverData(NdefMessage handoverSelect, 109 BluetoothHandoverData handoverData) { 110 this.handoverSelect = handoverSelect; 111 this.handoverData = handoverData; 112 } 113 } 114 HandoverDataParser()115 public HandoverDataParser() { 116 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 117 } 118 createCollisionRecord()119 static NdefRecord createCollisionRecord() { 120 byte[] random = new byte[2]; 121 new Random().nextBytes(random); 122 return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, RTD_COLLISION_RESOLUTION, null, random); 123 } 124 createBluetoothAlternateCarrierRecord(boolean activating)125 NdefRecord createBluetoothAlternateCarrierRecord(boolean activating) { 126 byte[] payload = new byte[4]; 127 payload[0] = (byte) (activating ? CARRIER_POWER_STATE_ACTIVATING : 128 CARRIER_POWER_STATE_ACTIVE); // Carrier Power State: Activating or active 129 payload[1] = 1; // length of carrier data reference 130 payload[2] = 'b'; // carrier data reference: ID for Bluetooth OOB data record 131 payload[3] = 0; // Auxiliary data reference count 132 return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_ALTERNATIVE_CARRIER, null, 133 payload); 134 } 135 createBluetoothOobDataRecord()136 NdefRecord createBluetoothOobDataRecord() { 137 byte[] payload = new byte[8]; 138 // Note: this field should be little-endian per the BTSSP spec 139 // The Android 4.1 implementation used big-endian order here. 140 // No single Android implementation has ever interpreted this 141 // length field when parsing this record though. 142 payload[0] = (byte) (payload.length & 0xFF); 143 payload[1] = (byte) ((payload.length >> 8) & 0xFF); 144 145 synchronized (mLock) { 146 if (mLocalBluetoothAddress == null) { 147 mLocalBluetoothAddress = mBluetoothAdapter.getAddress(); 148 } 149 150 byte[] addressBytes = addressToReverseBytes(mLocalBluetoothAddress); 151 System.arraycopy(addressBytes, 0, payload, 2, 6); 152 } 153 154 return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, TYPE_BT_OOB, new byte[]{'b'}, payload); 155 } 156 isHandoverSupported()157 public boolean isHandoverSupported() { 158 return (mBluetoothAdapter != null); 159 } 160 createHandoverRequestMessage()161 public NdefMessage createHandoverRequestMessage() { 162 if (mBluetoothAdapter == null) { 163 return null; 164 } 165 166 NdefRecord[] dataRecords = new NdefRecord[] { 167 createBluetoothOobDataRecord() 168 }; 169 return new NdefMessage( 170 createHandoverRequestRecord(), 171 dataRecords); 172 } 173 createBluetoothHandoverSelectMessage(boolean activating)174 NdefMessage createBluetoothHandoverSelectMessage(boolean activating) { 175 return new NdefMessage(createHandoverSelectRecord( 176 createBluetoothAlternateCarrierRecord(activating)), 177 createBluetoothOobDataRecord()); 178 } 179 createHandoverSelectRecord(NdefRecord alternateCarrier)180 NdefRecord createHandoverSelectRecord(NdefRecord alternateCarrier) { 181 NdefMessage nestedMessage = new NdefMessage(alternateCarrier); 182 byte[] nestedPayload = nestedMessage.toByteArray(); 183 184 ByteBuffer payload = ByteBuffer.allocate(nestedPayload.length + 1); 185 payload.put((byte)0x12); // connection handover v1.2 186 payload.put(nestedPayload); 187 188 byte[] payloadBytes = new byte[payload.position()]; 189 payload.position(0); 190 payload.get(payloadBytes); 191 return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_HANDOVER_SELECT, null, 192 payloadBytes); 193 } 194 createHandoverRequestRecord()195 NdefRecord createHandoverRequestRecord() { 196 NdefRecord[] messages = new NdefRecord[] { 197 createBluetoothAlternateCarrierRecord(false) 198 }; 199 200 NdefMessage nestedMessage = new NdefMessage(createCollisionRecord(), messages); 201 202 byte[] nestedPayload = nestedMessage.toByteArray(); 203 204 ByteBuffer payload = ByteBuffer.allocate(nestedPayload.length + 1); 205 payload.put((byte) 0x12); // connection handover v1.2 206 payload.put(nestedMessage.toByteArray()); 207 208 byte[] payloadBytes = new byte[payload.position()]; 209 payload.position(0); 210 payload.get(payloadBytes); 211 return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_HANDOVER_REQUEST, null, 212 payloadBytes); 213 } 214 215 /** 216 * Returns null if message is not a Handover Request, 217 * returns the IncomingHandoverData (Hs + parsed data) if it is. 218 */ getIncomingHandoverData(NdefMessage handoverRequest)219 public IncomingHandoverData getIncomingHandoverData(NdefMessage handoverRequest) { 220 if (handoverRequest == null) return null; 221 if (mBluetoothAdapter == null) return null; 222 223 if (DBG) Log.d(TAG, "getIncomingHandoverData():" + handoverRequest.toString()); 224 225 NdefRecord handoverRequestRecord = handoverRequest.getRecords()[0]; 226 if (handoverRequestRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) { 227 return null; 228 } 229 230 if (!Arrays.equals(handoverRequestRecord.getType(), NdefRecord.RTD_HANDOVER_REQUEST)) { 231 return null; 232 } 233 234 // we have a handover request, look for BT OOB record 235 BluetoothHandoverData bluetoothData = null; 236 for (NdefRecord dataRecord : handoverRequest.getRecords()) { 237 if (dataRecord.getTnf() == NdefRecord.TNF_MIME_MEDIA) { 238 if (Arrays.equals(dataRecord.getType(), TYPE_BT_OOB)) { 239 bluetoothData = parseBtOob(ByteBuffer.wrap(dataRecord.getPayload())); 240 } 241 } 242 } 243 244 NdefMessage hs = tryBluetoothHandoverRequest(bluetoothData); 245 if (hs != null) { 246 return new IncomingHandoverData(hs, bluetoothData); 247 } 248 249 return null; 250 } 251 getOutgoingHandoverData(NdefMessage handoverSelect)252 public BluetoothHandoverData getOutgoingHandoverData(NdefMessage handoverSelect) { 253 return parseBluetooth(handoverSelect); 254 } 255 tryBluetoothHandoverRequest(BluetoothHandoverData bluetoothData)256 private NdefMessage tryBluetoothHandoverRequest(BluetoothHandoverData bluetoothData) { 257 NdefMessage selectMessage = null; 258 if (bluetoothData != null) { 259 // Note: there could be a race where we conclude 260 // that Bluetooth is already enabled, and shortly 261 // after the user turns it off. That will cause 262 // the transfer to fail, but there's nothing 263 // much we can do about it anyway. It shouldn't 264 // be common for the user to be changing BT settings 265 // while waiting to receive a picture. 266 boolean bluetoothActivating = !mBluetoothAdapter.isEnabled(); 267 268 // return BT OOB record so they can perform handover 269 selectMessage = (createBluetoothHandoverSelectMessage(bluetoothActivating)); 270 if (DBG) Log.d(TAG, "Waiting for incoming transfer, [" + 271 bluetoothData.device.getAddress() + "]->[" + mLocalBluetoothAddress + "]"); 272 } 273 274 return selectMessage; 275 } 276 277 278 isCarrierActivating(NdefRecord handoverRec, byte[] carrierId)279 boolean isCarrierActivating(NdefRecord handoverRec, byte[] carrierId) { 280 byte[] payload = handoverRec.getPayload(); 281 if (payload == null || payload.length <= 1) return false; 282 // Skip version 283 byte[] payloadNdef = new byte[payload.length - 1]; 284 System.arraycopy(payload, 1, payloadNdef, 0, payload.length - 1); 285 NdefMessage msg; 286 try { 287 msg = new NdefMessage(payloadNdef); 288 } catch (FormatException e) { 289 return false; 290 } 291 292 for (NdefRecord alt : msg.getRecords()) { 293 byte[] acPayload = alt.getPayload(); 294 if (acPayload != null) { 295 ByteBuffer buf = ByteBuffer.wrap(acPayload); 296 int cps = buf.get() & 0x03; // Carrier Power State is in lower 2 bits 297 int carrierRefLength = buf.get() & 0xFF; 298 if (carrierRefLength != carrierId.length) return false; 299 300 byte[] carrierRefId = new byte[carrierRefLength]; 301 buf.get(carrierRefId); 302 if (Arrays.equals(carrierRefId, carrierId)) { 303 // Found match, returning whether power state is activating 304 return (cps == CARRIER_POWER_STATE_ACTIVATING); 305 } 306 } 307 } 308 309 return true; 310 } 311 parseBluetoothHandoverSelect(NdefMessage m)312 BluetoothHandoverData parseBluetoothHandoverSelect(NdefMessage m) { 313 // TODO we could parse this a lot more strictly; right now 314 // we just search for a BT OOB record, and try to cross-reference 315 // the carrier state inside the 'hs' payload. 316 for (NdefRecord oob : m.getRecords()) { 317 if (oob.getTnf() == NdefRecord.TNF_MIME_MEDIA && 318 Arrays.equals(oob.getType(), TYPE_BT_OOB)) { 319 BluetoothHandoverData data = parseBtOob(ByteBuffer.wrap(oob.getPayload())); 320 if (data != null && isCarrierActivating(m.getRecords()[0], oob.getId())) { 321 data.carrierActivating = true; 322 } 323 return data; 324 } 325 326 if (oob.getTnf() == NdefRecord.TNF_MIME_MEDIA && 327 Arrays.equals(oob.getType(), TYPE_BLE_OOB)) { 328 return parseBleOob(ByteBuffer.wrap(oob.getPayload())); 329 } 330 } 331 332 return null; 333 } 334 parseBluetooth(NdefMessage m)335 public BluetoothHandoverData parseBluetooth(NdefMessage m) { 336 NdefRecord r = m.getRecords()[0]; 337 short tnf = r.getTnf(); 338 byte[] type = r.getType(); 339 340 // Check for BT OOB record 341 if (r.getTnf() == NdefRecord.TNF_MIME_MEDIA && Arrays.equals(r.getType(), TYPE_BT_OOB)) { 342 return parseBtOob(ByteBuffer.wrap(r.getPayload())); 343 } 344 345 // Check for BLE OOB record 346 if (r.getTnf() == NdefRecord.TNF_MIME_MEDIA && Arrays.equals(r.getType(), TYPE_BLE_OOB)) { 347 return parseBleOob(ByteBuffer.wrap(r.getPayload())); 348 } 349 350 // Check for Handover Select, followed by a BT OOB record 351 if (tnf == NdefRecord.TNF_WELL_KNOWN && 352 Arrays.equals(type, NdefRecord.RTD_HANDOVER_SELECT)) { 353 return parseBluetoothHandoverSelect(m); 354 } 355 356 // Check for Nokia BT record, found on some Nokia BH-505 Headsets 357 if (tnf == NdefRecord.TNF_EXTERNAL_TYPE && Arrays.equals(type, TYPE_NOKIA)) { 358 return parseNokia(ByteBuffer.wrap(r.getPayload())); 359 } 360 361 return null; 362 } 363 parseNokia(ByteBuffer payload)364 BluetoothHandoverData parseNokia(ByteBuffer payload) { 365 BluetoothHandoverData result = new BluetoothHandoverData(); 366 result.valid = false; 367 368 try { 369 payload.position(1); 370 byte[] address = new byte[6]; 371 payload.get(address); 372 result.device = mBluetoothAdapter.getRemoteDevice(address); 373 result.valid = true; 374 payload.position(14); 375 int nameLength = payload.get(); 376 byte[] nameBytes = new byte[nameLength]; 377 payload.get(nameBytes); 378 result.name = new String(nameBytes, StandardCharsets.UTF_8); 379 } catch (IllegalArgumentException e) { 380 Log.i(TAG, "nokia: invalid BT address"); 381 } catch (BufferUnderflowException e) { 382 Log.i(TAG, "nokia: payload shorter than expected"); 383 } 384 if (result.valid && result.name == null) result.name = ""; 385 return result; 386 } 387 parseBtOob(ByteBuffer payload)388 BluetoothHandoverData parseBtOob(ByteBuffer payload) { 389 BluetoothHandoverData result = new BluetoothHandoverData(); 390 result.valid = false; 391 392 try { 393 payload.position(2); // length 394 byte[] address = parseMacFromBluetoothRecord(payload); 395 result.device = mBluetoothAdapter.getRemoteDevice(address); 396 result.valid = true; 397 398 while (payload.remaining() > 0) { 399 boolean success = false; 400 byte[] nameBytes; 401 int len = payload.get(); 402 int type = payload.get(); 403 switch (type) { 404 case BT_HANDOVER_TYPE_SHORT_LOCAL_NAME: 405 nameBytes = new byte[len - 1]; 406 payload.get(nameBytes); 407 result.name = new String(nameBytes, StandardCharsets.UTF_8); 408 success = true; 409 break; 410 case BT_HANDOVER_TYPE_LONG_LOCAL_NAME: 411 if (result.name != null) break; // prefer short name 412 nameBytes = new byte[len - 1]; 413 payload.get(nameBytes); 414 result.name = new String(nameBytes, StandardCharsets.UTF_8); 415 success = true; 416 break; 417 case BT_HANDOVER_TYPE_16_BIT_UUIDS_PARTIAL: 418 case BT_HANDOVER_TYPE_16_BIT_UUIDS_COMPLETE: 419 case BT_HANDOVER_TYPE_32_BIT_UUIDS_PARTIAL: 420 case BT_HANDOVER_TYPE_32_BIT_UUIDS_COMPLETE: 421 case BT_HANDOVER_TYPE_128_BIT_UUIDS_PARTIAL: 422 case BT_HANDOVER_TYPE_128_BIT_UUIDS_COMPLETE: 423 result.uuids = parseUuidFromBluetoothRecord(payload, type, len - 1); 424 if (result.uuids != null) { 425 success = true; 426 } 427 break; 428 case BT_HANDOVER_TYPE_CLASS_OF_DEVICE: 429 if (len - 1 != CLASS_OF_DEVICE_SIZE) { 430 Log.i(TAG, "BT OOB: invalid size of Class of Device, should be " + 431 CLASS_OF_DEVICE_SIZE + " bytes."); 432 break; 433 } 434 result.btClass = parseBluetoothClassFromBluetoothRecord(payload); 435 success = true; 436 break; 437 default: 438 break; 439 } 440 if (!success) { 441 payload.position(payload.position() + len - 1); 442 } 443 } 444 } catch (IllegalArgumentException e) { 445 Log.i(TAG, "BT OOB: invalid BT address"); 446 } catch (BufferUnderflowException e) { 447 Log.i(TAG, "BT OOB: payload shorter than expected"); 448 } 449 if (result.valid && result.name == null) result.name = ""; 450 return result; 451 } 452 parseBleOob(ByteBuffer payload)453 BluetoothHandoverData parseBleOob(ByteBuffer payload) { 454 BluetoothHandoverData result = new BluetoothHandoverData(); 455 result.valid = false; 456 result.transport = BluetoothDevice.TRANSPORT_LE; 457 458 try { 459 460 while (payload.remaining() > 0) { 461 int len = payload.get(); 462 int type = payload.get(); 463 switch (type) { 464 case BT_HANDOVER_TYPE_MAC: // mac address 465 466 int startpos = payload.position(); 467 byte[] bdaddr = new byte[7]; // 6 bytes for mac, 1 for addres type 468 payload.get(bdaddr); 469 if (result.oobData == null) 470 result.oobData = new OobData(); 471 result.oobData.setLeBluetoothDeviceAddress(bdaddr); 472 payload.position(startpos); 473 474 byte[] address = parseMacFromBluetoothRecord(payload); 475 payload.position(payload.position() + 1); // advance over random byte 476 result.device = mBluetoothAdapter.getRemoteDevice(address); 477 result.valid = true; 478 break; 479 case BT_HANDOVER_TYPE_LE_ROLE: 480 byte role = payload.get(); 481 if (role == BT_HANDOVER_LE_ROLE_CENTRAL_ONLY) { 482 // only central role supported, can't pair 483 result.valid = false; 484 return result; 485 } 486 break; 487 case BT_HANDOVER_TYPE_LONG_LOCAL_NAME: 488 byte[] nameBytes = new byte[len - 1]; 489 payload.get(nameBytes); 490 result.name = new String(nameBytes, StandardCharsets.UTF_8); 491 break; 492 case BT_HANDOVER_TYPE_SECURITY_MANAGER_TK: 493 if (len-1 != SECURITY_MANAGER_TK_SIZE) { 494 Log.i(TAG, "BT OOB: invalid size of SM TK, should be " + 495 SECURITY_MANAGER_TK_SIZE + " bytes."); 496 break; 497 } 498 499 byte[] securityManagerTK = new byte[len - 1]; 500 payload.get(securityManagerTK); 501 502 if (result.oobData == null) 503 result.oobData = new OobData(); 504 result.oobData.setSecurityManagerTk(securityManagerTK); 505 break; 506 507 case BT_HANDOVER_TYPE_LE_SC_CONFIRMATION: 508 if (len - 1 != SECURITY_MANAGER_LE_SC_C_SIZE) { 509 Log.i(TAG, "BT OOB: invalid size of LE SC Confirmation, should be " + 510 SECURITY_MANAGER_LE_SC_C_SIZE + " bytes."); 511 break; 512 } 513 514 byte[] leScC = new byte[len - 1]; 515 payload.get(leScC); 516 517 if (result.oobData == null) 518 result.oobData = new OobData(); 519 result.oobData.setLeSecureConnectionsConfirmation(leScC); 520 break; 521 522 case BT_HANDOVER_TYPE_LE_SC_RANDOM: 523 if (len-1 != SECURITY_MANAGER_LE_SC_R_SIZE) { 524 Log.i(TAG, "BT OOB: invalid size of LE SC Random, should be " + 525 SECURITY_MANAGER_LE_SC_R_SIZE + " bytes."); 526 break; 527 } 528 529 byte[] leScR = new byte[len - 1]; 530 payload.get(leScR); 531 532 if (result.oobData == null) 533 result.oobData = new OobData(); 534 result.oobData.setLeSecureConnectionsRandom(leScR); 535 break; 536 537 default: 538 payload.position(payload.position() + len - 1); 539 break; 540 } 541 } 542 } catch (IllegalArgumentException e) { 543 Log.i(TAG, "BLE OOB: error parsing OOB data", e); 544 } catch (BufferUnderflowException e) { 545 Log.i(TAG, "BT OOB: payload shorter than expected"); 546 } 547 if (result.valid && result.name == null) result.name = ""; 548 return result; 549 } 550 parseMacFromBluetoothRecord(ByteBuffer payload)551 private byte[] parseMacFromBluetoothRecord(ByteBuffer payload) { 552 byte[] address = new byte[6]; 553 payload.get(address); 554 // ByteBuffer.order(LITTLE_ENDIAN) doesn't work for 555 // ByteBuffer.get(byte[]), so manually swap order 556 for (int i = 0; i < 3; i++) { 557 byte temp = address[i]; 558 address[i] = address[5 - i]; 559 address[5 - i] = temp; 560 } 561 return address; 562 } 563 addressToReverseBytes(String address)564 static byte[] addressToReverseBytes(String address) { 565 String[] split = address.split(":"); 566 byte[] result = new byte[split.length]; 567 568 for (int i = 0; i < split.length; i++) { 569 // need to parse as int because parseByte() expects a signed byte 570 result[split.length - 1 - i] = (byte)Integer.parseInt(split[i], 16); 571 } 572 573 return result; 574 } 575 parseUuidFromBluetoothRecord(ByteBuffer payload, int type, int len)576 private ParcelUuid[] parseUuidFromBluetoothRecord(ByteBuffer payload, int type, int len) { 577 int uuidSize; 578 switch (type) { 579 case BT_HANDOVER_TYPE_16_BIT_UUIDS_PARTIAL: 580 case BT_HANDOVER_TYPE_16_BIT_UUIDS_COMPLETE: 581 uuidSize = BluetoothUuid.UUID_BYTES_16_BIT; 582 break; 583 case BT_HANDOVER_TYPE_32_BIT_UUIDS_PARTIAL: 584 case BT_HANDOVER_TYPE_32_BIT_UUIDS_COMPLETE: 585 uuidSize = BluetoothUuid.UUID_BYTES_32_BIT; 586 break; 587 case BT_HANDOVER_TYPE_128_BIT_UUIDS_PARTIAL: 588 case BT_HANDOVER_TYPE_128_BIT_UUIDS_COMPLETE: 589 uuidSize = BluetoothUuid.UUID_BYTES_128_BIT; 590 break; 591 default: 592 Log.i(TAG, "BT OOB: invalid size of UUID"); 593 return null; 594 } 595 596 if (len == 0 || len % uuidSize != 0) { 597 Log.i(TAG, "BT OOB: invalid size of UUIDs, should be multiples of UUID bytes length"); 598 return null; 599 } 600 601 int num = len / uuidSize; 602 ParcelUuid[] uuids = new ParcelUuid[num]; 603 byte[] data = new byte[uuidSize]; 604 for (int i = 0; i < num; i++) { 605 payload.get(data); 606 uuids[i] = BluetoothUuid.parseUuidFrom(data); 607 } 608 return uuids; 609 } 610 parseBluetoothClassFromBluetoothRecord(ByteBuffer payload)611 private BluetoothClass parseBluetoothClassFromBluetoothRecord(ByteBuffer payload) { 612 byte[] btClass = new byte[CLASS_OF_DEVICE_SIZE]; 613 payload.get(btClass); 614 615 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); 616 buffer.put(btClass); 617 buffer.order(ByteOrder.LITTLE_ENDIAN); 618 619 return new BluetoothClass(buffer.getInt(0)); 620 } 621 } 622