BluetoothDevice.java revision 6090995951c6e2e4dcf38102f01793f8a94166e1
1/* 2 * Copyright (C) 2009 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 17package android.bluetooth; 18 19import android.annotation.SdkConstant; 20import android.annotation.SdkConstant.SdkConstantType; 21import android.content.Context; 22import android.os.Parcel; 23import android.os.Parcelable; 24import android.os.ParcelUuid; 25import android.os.RemoteException; 26import android.util.Log; 27 28import java.io.IOException; 29import java.io.UnsupportedEncodingException; 30import java.util.UUID; 31 32/** 33 * Represents a remote Bluetooth device. A {@link BluetoothDevice} lets you 34 * create a connection with the respective device or query information about 35 * it, such as the name, address, class, and bonding state. 36 * 37 * <p>This class is really just a thin wrapper for a Bluetooth hardware 38 * address. Objects of this class are immutable. Operations on this class 39 * are performed on the remote Bluetooth hardware address, using the 40 * {@link BluetoothAdapter} that was used to create this {@link 41 * BluetoothDevice}. 42 * 43 * <p>To get a {@link BluetoothDevice}, use 44 * {@link BluetoothAdapter#getRemoteDevice(String) 45 * BluetoothAdapter.getRemoteDevice(String)} to create one representing a device 46 * of a known MAC address (which you can get through device discovery with 47 * {@link BluetoothAdapter}) or get one from the set of bonded devices 48 * returned by {@link BluetoothAdapter#getBondedDevices() 49 * BluetoothAdapter.getBondedDevices()}. You can then open a 50 * {@link BluetoothSocket} for communication with the remote device, using 51 * {@link #createRfcommSocketToServiceRecord(UUID)}. 52 * 53 * <p class="note"><strong>Note:</strong> 54 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. 55 * 56 * <div class="special reference"> 57 * <h3>Developer Guides</h3> 58 * <p>For more information about using Bluetooth, read the 59 * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p> 60 * </div> 61 * 62 * {@see BluetoothAdapter} 63 * {@see BluetoothSocket} 64 */ 65public final class BluetoothDevice implements Parcelable { 66 private static final String TAG = "BluetoothDevice"; 67 private static final boolean DBG = false; 68 69 /** 70 * Sentinel error value for this class. Guaranteed to not equal any other 71 * integer constant in this class. Provided as a convenience for functions 72 * that require a sentinel error value, for example: 73 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 74 * BluetoothDevice.ERROR)</code> 75 */ 76 public static final int ERROR = Integer.MIN_VALUE; 77 78 /** 79 * Broadcast Action: Remote device discovered. 80 * <p>Sent when a remote device is found during discovery. 81 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link 82 * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or 83 * {@link #EXTRA_RSSI} if they are available. 84 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 85 */ 86 // TODO: Change API to not broadcast RSSI if not available (incoming connection) 87 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 88 public static final String ACTION_FOUND = 89 "android.bluetooth.device.action.FOUND"; 90 91 /** 92 * Broadcast Action: Remote device disappeared. 93 * <p>Sent when a remote device that was found in the last discovery is not 94 * found in the current discovery. 95 * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 96 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 97 * @hide 98 */ 99 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 100 public static final String ACTION_DISAPPEARED = 101 "android.bluetooth.device.action.DISAPPEARED"; 102 103 /** 104 * Broadcast Action: Bluetooth class of a remote device has changed. 105 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link 106 * #EXTRA_CLASS}. 107 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 108 * {@see BluetoothClass} 109 */ 110 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 111 public static final String ACTION_CLASS_CHANGED = 112 "android.bluetooth.device.action.CLASS_CHANGED"; 113 114 /** 115 * Broadcast Action: Indicates a low level (ACL) connection has been 116 * established with a remote device. 117 * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 118 * <p>ACL connections are managed automatically by the Android Bluetooth 119 * stack. 120 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 121 */ 122 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 123 public static final String ACTION_ACL_CONNECTED = 124 "android.bluetooth.device.action.ACL_CONNECTED"; 125 126 /** 127 * Broadcast Action: Indicates that a low level (ACL) disconnection has 128 * been requested for a remote device, and it will soon be disconnected. 129 * <p>This is useful for graceful disconnection. Applications should use 130 * this intent as a hint to immediately terminate higher level connections 131 * (RFCOMM, L2CAP, or profile connections) to the remote device. 132 * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 133 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 134 */ 135 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 136 public static final String ACTION_ACL_DISCONNECT_REQUESTED = 137 "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"; 138 139 /** 140 * Broadcast Action: Indicates a low level (ACL) disconnection from a 141 * remote device. 142 * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 143 * <p>ACL connections are managed automatically by the Android Bluetooth 144 * stack. 145 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 146 */ 147 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 148 public static final String ACTION_ACL_DISCONNECTED = 149 "android.bluetooth.device.action.ACL_DISCONNECTED"; 150 151 /** 152 * Broadcast Action: Indicates the friendly name of a remote device has 153 * been retrieved for the first time, or changed since the last retrieval. 154 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link 155 * #EXTRA_NAME}. 156 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 157 */ 158 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 159 public static final String ACTION_NAME_CHANGED = 160 "android.bluetooth.device.action.NAME_CHANGED"; 161 162 /** 163 * Broadcast Action: Indicates the alias of a remote device has been 164 * changed. 165 * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 166 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 167 * 168 * @hide 169 */ 170 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 171 public static final String ACTION_ALIAS_CHANGED = 172 "android.bluetooth.device.action.ALIAS_CHANGED"; 173 174 /** 175 * Broadcast Action: Indicates a change in the bond state of a remote 176 * device. For example, if a device is bonded (paired). 177 * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link 178 * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}. 179 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 180 */ 181 // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also 182 // contain a hidden extra field EXTRA_REASON with the result code. 183 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 184 public static final String ACTION_BOND_STATE_CHANGED = 185 "android.bluetooth.device.action.BOND_STATE_CHANGED"; 186 187 /** 188 * Used as a Parcelable {@link BluetoothDevice} extra field in every intent 189 * broadcast by this class. It contains the {@link BluetoothDevice} that 190 * the intent applies to. 191 */ 192 public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE"; 193 194 /** 195 * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link 196 * #ACTION_FOUND} intents. It contains the friendly Bluetooth name. 197 */ 198 public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME"; 199 200 /** 201 * Used as an optional short extra field in {@link #ACTION_FOUND} intents. 202 * Contains the RSSI value of the remote device as reported by the 203 * Bluetooth hardware. 204 */ 205 public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI"; 206 207 /** 208 * Used as a Parcelable {@link BluetoothClass} extra field in {@link 209 * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents. 210 */ 211 public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS"; 212 213 /** 214 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents. 215 * Contains the bond state of the remote device. 216 * <p>Possible values are: 217 * {@link #BOND_NONE}, 218 * {@link #BOND_BONDING}, 219 * {@link #BOND_BONDED}. 220 */ 221 public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE"; 222 /** 223 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents. 224 * Contains the previous bond state of the remote device. 225 * <p>Possible values are: 226 * {@link #BOND_NONE}, 227 * {@link #BOND_BONDING}, 228 * {@link #BOND_BONDED}. 229 */ 230 public static final String EXTRA_PREVIOUS_BOND_STATE = 231 "android.bluetooth.device.extra.PREVIOUS_BOND_STATE"; 232 /** 233 * Indicates the remote device is not bonded (paired). 234 * <p>There is no shared link key with the remote device, so communication 235 * (if it is allowed at all) will be unauthenticated and unencrypted. 236 */ 237 public static final int BOND_NONE = 10; 238 /** 239 * Indicates bonding (pairing) is in progress with the remote device. 240 */ 241 public static final int BOND_BONDING = 11; 242 /** 243 * Indicates the remote device is bonded (paired). 244 * <p>A shared link keys exists locally for the remote device, so 245 * communication can be authenticated and encrypted. 246 * <p><i>Being bonded (paired) with a remote device does not necessarily 247 * mean the device is currently connected. It just means that the pending 248 * procedure was completed at some earlier time, and the link key is still 249 * stored locally, ready to use on the next connection. 250 * </i> 251 */ 252 public static final int BOND_BONDED = 12; 253 254 /** 255 * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} 256 * intents for unbond reason. 257 * @hide 258 */ 259 public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON"; 260 261 /** 262 * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} 263 * intents to indicate pairing method used. Possible values are: 264 * {@link #PAIRING_VARIANT_PIN}, 265 * {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION}, 266 */ 267 public static final String EXTRA_PAIRING_VARIANT = 268 "android.bluetooth.device.extra.PAIRING_VARIANT"; 269 270 /** 271 * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} 272 * intents as the value of passkey. 273 */ 274 public static final String EXTRA_PAIRING_KEY = "android.bluetooth.device.extra.PAIRING_KEY"; 275 276 /** 277 * Bluetooth device type, Unknown 278 */ 279 public static final int DEVICE_TYPE_UNKNOWN = 0; 280 281 /** 282 * Bluetooth device type, Classic - BR/EDR devices 283 */ 284 public static final int DEVICE_TYPE_CLASSIC = 1; 285 286 /** 287 * Bluetooth device type, Low Energy - LE-only 288 */ 289 public static final int DEVICE_TYPE_LE = 2; 290 291 /** 292 * Bluetooth device type, Dual Mode - BR/EDR/LE 293 */ 294 public static final int DEVICE_TYPE_DUAL = 3; 295 296 /** 297 * Broadcast Action: This intent is used to broadcast the {@link UUID} 298 * wrapped as a {@link android.os.ParcelUuid} of the remote device after it 299 * has been fetched. This intent is sent only when the UUIDs of the remote 300 * device are requested to be fetched using Service Discovery Protocol 301 * <p> Always contains the extra field {@link #EXTRA_DEVICE} 302 * <p> Always contains the extra field {@link #EXTRA_UUID} 303 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 304 */ 305 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 306 public static final String ACTION_UUID = 307 "android.bluetooth.device.action.UUID"; 308 309 /** 310 * Broadcast Action: Indicates a failure to retrieve the name of a remote 311 * device. 312 * <p>Always contains the extra field {@link #EXTRA_DEVICE}. 313 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 314 * @hide 315 */ 316 //TODO: is this actually useful? 317 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 318 public static final String ACTION_NAME_FAILED = 319 "android.bluetooth.device.action.NAME_FAILED"; 320 321 /** 322 * Broadcast Action: This intent is used to broadcast PAIRING REQUEST 323 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} to 324 * receive. 325 */ 326 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 327 public static final String ACTION_PAIRING_REQUEST = 328 "android.bluetooth.device.action.PAIRING_REQUEST"; 329 /** @hide */ 330 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 331 public static final String ACTION_PAIRING_CANCEL = 332 "android.bluetooth.device.action.PAIRING_CANCEL"; 333 334 /** @hide */ 335 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 336 public static final String ACTION_CONNECTION_ACCESS_REQUEST = 337 "android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST"; 338 339 /** @hide */ 340 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 341 public static final String ACTION_CONNECTION_ACCESS_REPLY = 342 "android.bluetooth.device.action.CONNECTION_ACCESS_REPLY"; 343 344 /** @hide */ 345 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 346 public static final String ACTION_CONNECTION_ACCESS_CANCEL = 347 "android.bluetooth.device.action.CONNECTION_ACCESS_CANCEL"; 348 349 /** 350 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intent. 351 * @hide 352 */ 353 public static final String EXTRA_ACCESS_REQUEST_TYPE = 354 "android.bluetooth.device.extra.ACCESS_REQUEST_TYPE"; 355 356 /**@hide*/ 357 public static final int REQUEST_TYPE_PROFILE_CONNECTION = 1; 358 359 /**@hide*/ 360 public static final int REQUEST_TYPE_PHONEBOOK_ACCESS = 2; 361 362 /**@hide*/ 363 public static final int REQUEST_TYPE_MESSAGE_ACCESS = 3; 364 365 /** 366 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents, 367 * Contains package name to return reply intent to. 368 * @hide 369 */ 370 public static final String EXTRA_PACKAGE_NAME = "android.bluetooth.device.extra.PACKAGE_NAME"; 371 372 /** 373 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents, 374 * Contains class name to return reply intent to. 375 * @hide 376 */ 377 public static final String EXTRA_CLASS_NAME = "android.bluetooth.device.extra.CLASS_NAME"; 378 379 /** 380 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intent. 381 * @hide 382 */ 383 public static final String EXTRA_CONNECTION_ACCESS_RESULT = 384 "android.bluetooth.device.extra.CONNECTION_ACCESS_RESULT"; 385 386 /**@hide*/ 387 public static final int CONNECTION_ACCESS_YES = 1; 388 389 /**@hide*/ 390 public static final int CONNECTION_ACCESS_NO = 2; 391 392 /** 393 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intents, 394 * Contains boolean to indicate if the allowed response is once-for-all so that 395 * next request will be granted without asking user again. 396 * @hide 397 */ 398 public static final String EXTRA_ALWAYS_ALLOWED = 399 "android.bluetooth.device.extra.ALWAYS_ALLOWED"; 400 401 /** 402 * A bond attempt succeeded 403 * @hide 404 */ 405 public static final int BOND_SUCCESS = 0; 406 407 /** 408 * A bond attempt failed because pins did not match, or remote device did 409 * not respond to pin request in time 410 * @hide 411 */ 412 public static final int UNBOND_REASON_AUTH_FAILED = 1; 413 414 /** 415 * A bond attempt failed because the other side explicitly rejected 416 * bonding 417 * @hide 418 */ 419 public static final int UNBOND_REASON_AUTH_REJECTED = 2; 420 421 /** 422 * A bond attempt failed because we canceled the bonding process 423 * @hide 424 */ 425 public static final int UNBOND_REASON_AUTH_CANCELED = 3; 426 427 /** 428 * A bond attempt failed because we could not contact the remote device 429 * @hide 430 */ 431 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4; 432 433 /** 434 * A bond attempt failed because a discovery is in progress 435 * @hide 436 */ 437 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5; 438 439 /** 440 * A bond attempt failed because of authentication timeout 441 * @hide 442 */ 443 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6; 444 445 /** 446 * A bond attempt failed because of repeated attempts 447 * @hide 448 */ 449 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7; 450 451 /** 452 * A bond attempt failed because we received an Authentication Cancel 453 * by remote end 454 * @hide 455 */ 456 public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8; 457 458 /** 459 * An existing bond was explicitly revoked 460 * @hide 461 */ 462 public static final int UNBOND_REASON_REMOVED = 9; 463 464 /** 465 * The user will be prompted to enter a pin or 466 * an app will enter a pin for user. 467 */ 468 public static final int PAIRING_VARIANT_PIN = 0; 469 470 /** 471 * The user will be prompted to enter a passkey 472 * @hide 473 */ 474 public static final int PAIRING_VARIANT_PASSKEY = 1; 475 476 /** 477 * The user will be prompted to confirm the passkey displayed on the screen or 478 * an app will confirm the passkey for the user. 479 */ 480 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2; 481 482 /** 483 * The user will be prompted to accept or deny the incoming pairing request 484 * @hide 485 */ 486 public static final int PAIRING_VARIANT_CONSENT = 3; 487 488 /** 489 * The user will be prompted to enter the passkey displayed on remote device 490 * This is used for Bluetooth 2.1 pairing. 491 * @hide 492 */ 493 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4; 494 495 /** 496 * The user will be prompted to enter the PIN displayed on remote device. 497 * This is used for Bluetooth 2.0 pairing. 498 * @hide 499 */ 500 public static final int PAIRING_VARIANT_DISPLAY_PIN = 5; 501 502 /** 503 * The user will be prompted to accept or deny the OOB pairing request 504 * @hide 505 */ 506 public static final int PAIRING_VARIANT_OOB_CONSENT = 6; 507 508 /** 509 * Used as an extra field in {@link #ACTION_UUID} intents, 510 * Contains the {@link android.os.ParcelUuid}s of the remote device which 511 * is a parcelable version of {@link UUID}. 512 */ 513 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID"; 514 515 /** 516 * Lazy initialization. Guaranteed final after first object constructed, or 517 * getService() called. 518 * TODO: Unify implementation of sService amongst BluetoothFoo API's 519 */ 520 private static IBluetooth sService; 521 522 private final String mAddress; 523 524 /*package*/ static IBluetooth getService() { 525 synchronized (BluetoothDevice.class) { 526 if (sService == null) { 527 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 528 sService = adapter.getBluetoothService(mStateChangeCallback); 529 } 530 } 531 return sService; 532 } 533 534 static IBluetoothManagerCallback mStateChangeCallback = new IBluetoothManagerCallback.Stub() { 535 536 public void onBluetoothServiceUp(IBluetooth bluetoothService) 537 throws RemoteException { 538 synchronized (BluetoothDevice.class) { 539 sService = bluetoothService; 540 } 541 } 542 543 public void onBluetoothServiceDown() 544 throws RemoteException { 545 synchronized (BluetoothDevice.class) { 546 sService = null; 547 } 548 } 549 }; 550 /** 551 * Create a new BluetoothDevice 552 * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", 553 * and is validated in this constructor. 554 * @param address valid Bluetooth MAC address 555 * @throws RuntimeException Bluetooth is not available on this platform 556 * @throws IllegalArgumentException address is invalid 557 * @hide 558 */ 559 /*package*/ BluetoothDevice(String address) { 560 getService(); // ensures sService is initialized 561 if (!BluetoothAdapter.checkBluetoothAddress(address)) { 562 throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); 563 } 564 565 mAddress = address; 566 } 567 568 @Override 569 public boolean equals(Object o) { 570 if (o instanceof BluetoothDevice) { 571 return mAddress.equals(((BluetoothDevice)o).getAddress()); 572 } 573 return false; 574 } 575 576 @Override 577 public int hashCode() { 578 return mAddress.hashCode(); 579 } 580 581 /** 582 * Returns a string representation of this BluetoothDevice. 583 * <p>Currently this is the Bluetooth hardware address, for example 584 * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress} 585 * if you explicitly require the Bluetooth hardware address in case the 586 * {@link #toString} representation changes in the future. 587 * @return string representation of this BluetoothDevice 588 */ 589 @Override 590 public String toString() { 591 return mAddress; 592 } 593 594 public int describeContents() { 595 return 0; 596 } 597 598 public static final Parcelable.Creator<BluetoothDevice> CREATOR = 599 new Parcelable.Creator<BluetoothDevice>() { 600 public BluetoothDevice createFromParcel(Parcel in) { 601 return new BluetoothDevice(in.readString()); 602 } 603 public BluetoothDevice[] newArray(int size) { 604 return new BluetoothDevice[size]; 605 } 606 }; 607 608 public void writeToParcel(Parcel out, int flags) { 609 out.writeString(mAddress); 610 } 611 612 /** 613 * Returns the hardware address of this BluetoothDevice. 614 * <p> For example, "00:11:22:AA:BB:CC". 615 * @return Bluetooth hardware address as string 616 */ 617 public String getAddress() { 618 if (DBG) Log.d(TAG, "mAddress: " + mAddress); 619 return mAddress; 620 } 621 622 /** 623 * Get the friendly Bluetooth name of the remote device. 624 * 625 * <p>The local adapter will automatically retrieve remote names when 626 * performing a device scan, and will cache them. This method just returns 627 * the name for this device from the cache. 628 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 629 * 630 * @return the Bluetooth name, or null if there was a problem. 631 */ 632 public String getName() { 633 if (sService == null) { 634 Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); 635 return null; 636 } 637 try { 638 return sService.getRemoteName(this); 639 } catch (RemoteException e) {Log.e(TAG, "", e);} 640 return null; 641 } 642 643 /** 644 * Get the Bluetooth device type of the remote device. 645 * 646 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 647 * 648 * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE} 649 * {@link #DEVICE_TYPE_DUAL}. 650 * {@link #DEVICE_TYPE_UNKNOWN} if it's not available 651 */ 652 public int getType() { 653 if (sService == null) { 654 Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); 655 return DEVICE_TYPE_UNKNOWN; 656 } 657 try { 658 return sService.getRemoteType(this); 659 } catch (RemoteException e) {Log.e(TAG, "", e);} 660 return DEVICE_TYPE_UNKNOWN; 661 } 662 663 /** 664 * Get the Bluetooth alias of the remote device. 665 * <p>Alias is the locally modified name of a remote device. 666 * 667 * @return the Bluetooth alias, or null if no alias or there was a problem 668 * @hide 669 */ 670 public String getAlias() { 671 if (sService == null) { 672 Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); 673 return null; 674 } 675 try { 676 return sService.getRemoteAlias(this); 677 } catch (RemoteException e) {Log.e(TAG, "", e);} 678 return null; 679 } 680 681 /** 682 * Set the Bluetooth alias of the remote device. 683 * <p>Alias is the locally modified name of a remote device. 684 * <p>This methoid overwrites the alias. The changed 685 * alias is saved in the local storage so that the change 686 * is preserved over power cycle. 687 * 688 * @return true on success, false on error 689 * @hide 690 */ 691 public boolean setAlias(String alias) { 692 if (sService == null) { 693 Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); 694 return false; 695 } 696 try { 697 return sService.setRemoteAlias(this, alias); 698 } catch (RemoteException e) {Log.e(TAG, "", e);} 699 return false; 700 } 701 702 /** 703 * Get the Bluetooth alias of the remote device. 704 * If Alias is null, get the Bluetooth name instead. 705 * @see #getAlias() 706 * @see #getName() 707 * 708 * @return the Bluetooth alias, or null if no alias or there was a problem 709 * @hide 710 */ 711 public String getAliasName() { 712 String name = getAlias(); 713 if (name == null) { 714 name = getName(); 715 } 716 return name; 717 } 718 719 /** 720 * Start the bonding (pairing) process with the remote device. 721 * <p>This is an asynchronous call, it will return immediately. Register 722 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when 723 * the bonding process completes, and its result. 724 * <p>Android system services will handle the necessary user interactions 725 * to confirm and complete the bonding process. 726 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 727 * 728 * @return false on immediate error, true if bonding will begin 729 */ 730 public boolean createBond() { 731 if (sService == null) { 732 Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); 733 return false; 734 } 735 try { 736 return sService.createBond(this); 737 } catch (RemoteException e) {Log.e(TAG, "", e);} 738 return false; 739 } 740 741 /** 742 * Start the bonding (pairing) process with the remote device using the 743 * Out Of Band mechanism. 744 * 745 * <p>This is an asynchronous call, it will return immediately. Register 746 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when 747 * the bonding process completes, and its result. 748 * 749 * <p>Android system services will handle the necessary user interactions 750 * to confirm and complete the bonding process. 751 * 752 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 753 * 754 * @param hash - Simple Secure pairing hash 755 * @param randomizer - The random key obtained using OOB 756 * @return false on immediate error, true if bonding will begin 757 * 758 * @hide 759 */ 760 public boolean createBondOutOfBand(byte[] hash, byte[] randomizer) { 761 //TODO(BT) 762 /* 763 try { 764 return sService.createBondOutOfBand(this, hash, randomizer); 765 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 766 return false; 767 } 768 769 /** 770 * Set the Out Of Band data for a remote device to be used later 771 * in the pairing mechanism. Users can obtain this data through other 772 * trusted channels 773 * 774 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 775 * 776 * @param hash Simple Secure pairing hash 777 * @param randomizer The random key obtained using OOB 778 * @return false on error; true otherwise 779 * 780 * @hide 781 */ 782 public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) { 783 //TODO(BT) 784 /* 785 try { 786 return sService.setDeviceOutOfBandData(this, hash, randomizer); 787 } catch (RemoteException e) {Log.e(TAG, "", e);} */ 788 return false; 789 } 790 791 /** 792 * Cancel an in-progress bonding request started with {@link #createBond}. 793 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 794 * 795 * @return true on success, false on error 796 * @hide 797 */ 798 public boolean cancelBondProcess() { 799 if (sService == null) { 800 Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); 801 return false; 802 } 803 try { 804 return sService.cancelBondProcess(this); 805 } catch (RemoteException e) {Log.e(TAG, "", e);} 806 return false; 807 } 808 809 /** 810 * Remove bond (pairing) with the remote device. 811 * <p>Delete the link key associated with the remote device, and 812 * immediately terminate connections to that device that require 813 * authentication and encryption. 814 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 815 * 816 * @return true on success, false on error 817 * @hide 818 */ 819 public boolean removeBond() { 820 if (sService == null) { 821 Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); 822 return false; 823 } 824 try { 825 return sService.removeBond(this); 826 } catch (RemoteException e) {Log.e(TAG, "", e);} 827 return false; 828 } 829 830 /** 831 * Get the bond state of the remote device. 832 * <p>Possible values for the bond state are: 833 * {@link #BOND_NONE}, 834 * {@link #BOND_BONDING}, 835 * {@link #BOND_BONDED}. 836 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 837 * 838 * @return the bond state 839 */ 840 public int getBondState() { 841 if (sService == null) { 842 Log.e(TAG, "BT not enabled. Cannot get bond state"); 843 return BOND_NONE; 844 } 845 try { 846 return sService.getBondState(this); 847 } catch (RemoteException e) {Log.e(TAG, "", e);} 848 catch (NullPointerException npe) { 849 // Handle case where bluetooth service proxy 850 // is already null. 851 Log.e(TAG, "NullPointerException for getBondState() of device ("+ 852 getAddress()+")", npe); 853 } 854 return BOND_NONE; 855 } 856 857 /** 858 * Get the Bluetooth class of the remote device. 859 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 860 * 861 * @return Bluetooth class object, or null on error 862 */ 863 public BluetoothClass getBluetoothClass() { 864 if (sService == null) { 865 Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); 866 return null; 867 } 868 try { 869 int classInt = sService.getRemoteClass(this); 870 if (classInt == BluetoothClass.ERROR) return null; 871 return new BluetoothClass(classInt); 872 } catch (RemoteException e) {Log.e(TAG, "", e);} 873 return null; 874 } 875 876 /** 877 * Get trust state of a remote device. 878 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 879 * @hide 880 */ 881 public boolean getTrustState() { 882 //TODO(BT) 883 /* 884 try { 885 return sService.getTrustState(this); 886 } catch (RemoteException e) { 887 Log.e(TAG, "", e); 888 }*/ 889 return false; 890 } 891 892 /** 893 * Set trust state for a remote device. 894 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 895 * @param value the trust state value (true or false) 896 * @hide 897 */ 898 public boolean setTrust(boolean value) { 899 //TODO(BT) 900 /* 901 try { 902 return sService.setTrust(this, value); 903 } catch (RemoteException e) { 904 Log.e(TAG, "", e); 905 }*/ 906 return false; 907 } 908 909 /** 910 * Returns the supported features (UUIDs) of the remote device. 911 * 912 * <p>This method does not start a service discovery procedure to retrieve the UUIDs 913 * from the remote device. Instead, the local cached copy of the service 914 * UUIDs are returned. 915 * <p>Use {@link #fetchUuidsWithSdp} if fresh UUIDs are desired. 916 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 917 * 918 * @return the supported features (UUIDs) of the remote device, 919 * or null on error 920 */ 921 public ParcelUuid[] getUuids() { 922 if (sService == null) { 923 Log.e(TAG, "BT not enabled. Cannot get remote device Uuids"); 924 return null; 925 } 926 try { 927 return sService.getRemoteUuids(this); 928 } catch (RemoteException e) {Log.e(TAG, "", e);} 929 return null; 930 } 931 932 /** 933 * Perform a service discovery on the remote device to get the UUIDs supported. 934 * 935 * <p>This API is asynchronous and {@link #ACTION_UUID} intent is sent, 936 * with the UUIDs supported by the remote end. If there is an error 937 * in getting the SDP records or if the process takes a long time, 938 * {@link #ACTION_UUID} intent is sent with the UUIDs that is currently 939 * present in the cache. Clients should use the {@link #getUuids} to get UUIDs 940 * if service discovery is not to be performed. 941 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 942 * 943 * @return False if the sanity check fails, True if the process 944 * of initiating an ACL connection to the remote device 945 * was started. 946 */ 947 public boolean fetchUuidsWithSdp() { 948 try { 949 return sService.fetchRemoteUuids(this); 950 } catch (RemoteException e) {Log.e(TAG, "", e);} 951 return false; 952 } 953 954 /** @hide */ 955 public int getServiceChannel(ParcelUuid uuid) { 956 //TODO(BT) 957 /* 958 try { 959 return sService.getRemoteServiceChannel(this, uuid); 960 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 961 return BluetoothDevice.ERROR; 962 } 963 964 /** 965 * Set the pin during pairing when the pairing method is {@link #PAIRING_VARIANT_PIN} 966 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 967 * 968 * @return true pin has been set 969 * false for error 970 */ 971 public boolean setPin(byte[] pin) { 972 if (sService == null) { 973 Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); 974 return false; 975 } 976 try { 977 return sService.setPin(this, true, pin.length, pin); 978 } catch (RemoteException e) {Log.e(TAG, "", e);} 979 return false; 980 } 981 982 /** @hide */ 983 public boolean setPasskey(int passkey) { 984 //TODO(BT) 985 /* 986 try { 987 return sService.setPasskey(this, true, 4, passkey); 988 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 989 return false; 990 } 991 992 /** 993 * Confirm passkey for {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION} pairing. 994 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 995 * 996 * @return true confirmation has been sent out 997 * false for error 998 */ 999 public boolean setPairingConfirmation(boolean confirm) { 1000 if (sService == null) { 1001 Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); 1002 return false; 1003 } 1004 try { 1005 return sService.setPairingConfirmation(this, confirm); 1006 } catch (RemoteException e) {Log.e(TAG, "", e);} 1007 return false; 1008 } 1009 1010 /** @hide */ 1011 public boolean setRemoteOutOfBandData() { 1012 // TODO(BT) 1013 /* 1014 try { 1015 return sService.setRemoteOutOfBandData(this); 1016 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 1017 return false; 1018 } 1019 1020 /** @hide */ 1021 public boolean cancelPairingUserInput() { 1022 if (sService == null) { 1023 Log.e(TAG, "BT not enabled. Cannot create pairing user input"); 1024 return false; 1025 } 1026 try { 1027 return sService.cancelBondProcess(this); 1028 } catch (RemoteException e) {Log.e(TAG, "", e);} 1029 return false; 1030 } 1031 1032 /** @hide */ 1033 public boolean isBluetoothDock() { 1034 // TODO(BT) 1035 /* 1036 try { 1037 return sService.isBluetoothDock(this); 1038 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 1039 return false; 1040 } 1041 1042 /** 1043 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 1044 * outgoing connection to this remote device on given channel. 1045 * <p>The remote device will be authenticated and communication on this 1046 * socket will be encrypted. 1047 * <p> Use this socket only if an authenticated socket link is possible. 1048 * Authentication refers to the authentication of the link key to 1049 * prevent man-in-the-middle type of attacks. 1050 * For example, for Bluetooth 2.1 devices, if any of the devices does not 1051 * have an input and output capability or just has the ability to 1052 * display a numeric key, a secure socket connection is not possible. 1053 * In such a case, use {#link createInsecureRfcommSocket}. 1054 * For more details, refer to the Security Model section 5.2 (vol 3) of 1055 * Bluetooth Core Specification version 2.1 + EDR. 1056 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing 1057 * connection. 1058 * <p>Valid RFCOMM channels are in range 1 to 30. 1059 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 1060 * 1061 * @param channel RFCOMM channel to connect to 1062 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 1063 * @throws IOException on error, for example Bluetooth not available, or 1064 * insufficient permissions 1065 * @hide 1066 */ 1067 public BluetoothSocket createRfcommSocket(int channel) throws IOException { 1068 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel, 1069 null); 1070 } 1071 1072 /** 1073 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 1074 * outgoing connection to this remote device using SDP lookup of uuid. 1075 * <p>This is designed to be used with {@link 1076 * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer 1077 * Bluetooth applications. 1078 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing 1079 * connection. This will also perform an SDP lookup of the given uuid to 1080 * determine which channel to connect to. 1081 * <p>The remote device will be authenticated and communication on this 1082 * socket will be encrypted. 1083 * <p> Use this socket only if an authenticated socket link is possible. 1084 * Authentication refers to the authentication of the link key to 1085 * prevent man-in-the-middle type of attacks. 1086 * For example, for Bluetooth 2.1 devices, if any of the devices does not 1087 * have an input and output capability or just has the ability to 1088 * display a numeric key, a secure socket connection is not possible. 1089 * In such a case, use {#link createInsecureRfcommSocketToServiceRecord}. 1090 * For more details, refer to the Security Model section 5.2 (vol 3) of 1091 * Bluetooth Core Specification version 2.1 + EDR. 1092 * <p>Hint: If you are connecting to a Bluetooth serial board then try 1093 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. 1094 * However if you are connecting to an Android peer then please generate 1095 * your own unique UUID. 1096 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 1097 * 1098 * @param uuid service record uuid to lookup RFCOMM channel 1099 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 1100 * @throws IOException on error, for example Bluetooth not available, or 1101 * insufficient permissions 1102 */ 1103 public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException { 1104 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1, 1105 new ParcelUuid(uuid)); 1106 } 1107 1108 /** 1109 * Create an RFCOMM {@link BluetoothSocket} socket ready to start an insecure 1110 * outgoing connection to this remote device using SDP lookup of uuid. 1111 * <p> The communication channel will not have an authenticated link key 1112 * i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1 1113 * devices, the link key will be encrypted, as encryption is mandatory. 1114 * For legacy devices (pre Bluetooth 2.1 devices) the link key will 1115 * be not be encrypted. Use {@link #createRfcommSocketToServiceRecord} if an 1116 * encrypted and authenticated communication channel is desired. 1117 * <p>This is designed to be used with {@link 1118 * BluetoothAdapter#listenUsingInsecureRfcommWithServiceRecord} for peer-peer 1119 * Bluetooth applications. 1120 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing 1121 * connection. This will also perform an SDP lookup of the given uuid to 1122 * determine which channel to connect to. 1123 * <p>The remote device will be authenticated and communication on this 1124 * socket will be encrypted. 1125 * <p>Hint: If you are connecting to a Bluetooth serial board then try 1126 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. 1127 * However if you are connecting to an Android peer then please generate 1128 * your own unique UUID. 1129 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 1130 * 1131 * @param uuid service record uuid to lookup RFCOMM channel 1132 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 1133 * @throws IOException on error, for example Bluetooth not available, or 1134 * insufficient permissions 1135 */ 1136 public BluetoothSocket createInsecureRfcommSocketToServiceRecord(UUID uuid) throws IOException { 1137 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, -1, 1138 new ParcelUuid(uuid)); 1139 } 1140 1141 /** 1142 * Construct an insecure RFCOMM socket ready to start an outgoing 1143 * connection. 1144 * Call #connect on the returned #BluetoothSocket to begin the connection. 1145 * The remote device will not be authenticated and communication on this 1146 * socket will not be encrypted. 1147 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 1148 * 1149 * @param port remote port 1150 * @return An RFCOMM BluetoothSocket 1151 * @throws IOException On error, for example Bluetooth not available, or 1152 * insufficient permissions. 1153 * @hide 1154 */ 1155 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { 1156 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, 1157 null); 1158 } 1159 1160 /** 1161 * Construct a SCO socket ready to start an outgoing connection. 1162 * Call #connect on the returned #BluetoothSocket to begin the connection. 1163 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 1164 * 1165 * @return a SCO BluetoothSocket 1166 * @throws IOException on error, for example Bluetooth not available, or 1167 * insufficient permissions. 1168 * @hide 1169 */ 1170 public BluetoothSocket createScoSocket() throws IOException { 1171 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); 1172 } 1173 1174 /** 1175 * Check that a pin is valid and convert to byte array. 1176 * 1177 * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters. 1178 * @param pin pin as java String 1179 * @return the pin code as a UTF-8 byte array, or null if it is an invalid 1180 * Bluetooth pin. 1181 * @hide 1182 */ 1183 public static byte[] convertPinToBytes(String pin) { 1184 if (pin == null) { 1185 return null; 1186 } 1187 byte[] pinBytes; 1188 try { 1189 pinBytes = pin.getBytes("UTF-8"); 1190 } catch (UnsupportedEncodingException uee) { 1191 Log.e(TAG, "UTF-8 not supported?!?"); // this should not happen 1192 return null; 1193 } 1194 if (pinBytes.length <= 0 || pinBytes.length > 16) { 1195 return null; 1196 } 1197 return pinBytes; 1198 } 1199 1200 /** 1201 * Connect to GATT Server hosted by this device. Caller acts as GATT client. 1202 * The callback is used to deliver results to Caller, such as connection status as well 1203 * as any further GATT client operations. 1204 * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct 1205 * GATT client operations. 1206 * @param callback GATT callback handler that will receive asynchronous callbacks. 1207 * @param autoConnect Whether to directly connect to the remote device (false) 1208 * or to automatically connect as soon as the remote 1209 * device becomes available (true). 1210 * @throws IllegalArgumentException if callback is null 1211 */ 1212 public BluetoothGatt connectGatt(Context context, boolean autoConnect, 1213 BluetoothGattCallback callback) { 1214 // TODO(Bluetooth) check whether platform support BLE 1215 // Do the check here or in GattServer? 1216 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 1217 IBluetoothManager managerService = adapter.getBluetoothManager(); 1218 try { 1219 IBluetoothGatt iGatt = managerService.getBluetoothGatt(); 1220 if (iGatt == null) { 1221 // BLE is not supported 1222 return null; 1223 } 1224 BluetoothGatt gatt = new BluetoothGatt(context, iGatt, this); 1225 gatt.connect(autoConnect, callback); 1226 return gatt; 1227 } catch (RemoteException e) {Log.e(TAG, "", e);} 1228 return null; 1229 } 1230} 1231