BluetoothDevice.java revision 174928c0aaf020de4514a5c02799299c6e56e4c0
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 * No preferrence of physical transport for GATT connections to remote dual-mode devices 517 * @hide 518 */ 519 public static final int TRANSPORT_AUTO = 0; 520 521 /** 522 * Prefer BR/EDR transport for GATT connections to remote dual-mode devices 523 * @hide 524 */ 525 public static final int TRANSPORT_BREDR = 1; 526 527 /** 528 * Prefer LE transport for GATT connections to remote dual-mode devices 529 * @hide 530 */ 531 public static final int TRANSPORT_LE = 2; 532 533 534 /** 535 * Lazy initialization. Guaranteed final after first object constructed, or 536 * getService() called. 537 * TODO: Unify implementation of sService amongst BluetoothFoo API's 538 */ 539 private static IBluetooth sService; 540 541 private final String mAddress; 542 543 /*package*/ static IBluetooth getService() { 544 synchronized (BluetoothDevice.class) { 545 if (sService == null) { 546 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 547 sService = adapter.getBluetoothService(mStateChangeCallback); 548 } 549 } 550 return sService; 551 } 552 553 static IBluetoothManagerCallback mStateChangeCallback = new IBluetoothManagerCallback.Stub() { 554 555 public void onBluetoothServiceUp(IBluetooth bluetoothService) 556 throws RemoteException { 557 synchronized (BluetoothDevice.class) { 558 sService = bluetoothService; 559 } 560 } 561 562 public void onBluetoothServiceDown() 563 throws RemoteException { 564 synchronized (BluetoothDevice.class) { 565 sService = null; 566 } 567 } 568 }; 569 /** 570 * Create a new BluetoothDevice 571 * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", 572 * and is validated in this constructor. 573 * @param address valid Bluetooth MAC address 574 * @throws RuntimeException Bluetooth is not available on this platform 575 * @throws IllegalArgumentException address is invalid 576 * @hide 577 */ 578 /*package*/ BluetoothDevice(String address) { 579 getService(); // ensures sService is initialized 580 if (!BluetoothAdapter.checkBluetoothAddress(address)) { 581 throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); 582 } 583 584 mAddress = address; 585 } 586 587 @Override 588 public boolean equals(Object o) { 589 if (o instanceof BluetoothDevice) { 590 return mAddress.equals(((BluetoothDevice)o).getAddress()); 591 } 592 return false; 593 } 594 595 @Override 596 public int hashCode() { 597 return mAddress.hashCode(); 598 } 599 600 /** 601 * Returns a string representation of this BluetoothDevice. 602 * <p>Currently this is the Bluetooth hardware address, for example 603 * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress} 604 * if you explicitly require the Bluetooth hardware address in case the 605 * {@link #toString} representation changes in the future. 606 * @return string representation of this BluetoothDevice 607 */ 608 @Override 609 public String toString() { 610 return mAddress; 611 } 612 613 public int describeContents() { 614 return 0; 615 } 616 617 public static final Parcelable.Creator<BluetoothDevice> CREATOR = 618 new Parcelable.Creator<BluetoothDevice>() { 619 public BluetoothDevice createFromParcel(Parcel in) { 620 return new BluetoothDevice(in.readString()); 621 } 622 public BluetoothDevice[] newArray(int size) { 623 return new BluetoothDevice[size]; 624 } 625 }; 626 627 public void writeToParcel(Parcel out, int flags) { 628 out.writeString(mAddress); 629 } 630 631 /** 632 * Returns the hardware address of this BluetoothDevice. 633 * <p> For example, "00:11:22:AA:BB:CC". 634 * @return Bluetooth hardware address as string 635 */ 636 public String getAddress() { 637 if (DBG) Log.d(TAG, "mAddress: " + mAddress); 638 return mAddress; 639 } 640 641 /** 642 * Get the friendly Bluetooth name of the remote device. 643 * 644 * <p>The local adapter will automatically retrieve remote names when 645 * performing a device scan, and will cache them. This method just returns 646 * the name for this device from the cache. 647 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 648 * 649 * @return the Bluetooth name, or null if there was a problem. 650 */ 651 public String getName() { 652 if (sService == null) { 653 Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); 654 return null; 655 } 656 try { 657 return sService.getRemoteName(this); 658 } catch (RemoteException e) {Log.e(TAG, "", e);} 659 return null; 660 } 661 662 /** 663 * Get the Bluetooth device type of the remote device. 664 * 665 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 666 * 667 * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE} 668 * {@link #DEVICE_TYPE_DUAL}. 669 * {@link #DEVICE_TYPE_UNKNOWN} if it's not available 670 */ 671 public int getType() { 672 if (sService == null) { 673 Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); 674 return DEVICE_TYPE_UNKNOWN; 675 } 676 try { 677 return sService.getRemoteType(this); 678 } catch (RemoteException e) {Log.e(TAG, "", e);} 679 return DEVICE_TYPE_UNKNOWN; 680 } 681 682 /** 683 * Get the Bluetooth alias of the remote device. 684 * <p>Alias is the locally modified name of a remote device. 685 * 686 * @return the Bluetooth alias, or null if no alias or there was a problem 687 * @hide 688 */ 689 public String getAlias() { 690 if (sService == null) { 691 Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); 692 return null; 693 } 694 try { 695 return sService.getRemoteAlias(this); 696 } catch (RemoteException e) {Log.e(TAG, "", e);} 697 return null; 698 } 699 700 /** 701 * Set the Bluetooth alias of the remote device. 702 * <p>Alias is the locally modified name of a remote device. 703 * <p>This methoid overwrites the alias. The changed 704 * alias is saved in the local storage so that the change 705 * is preserved over power cycle. 706 * 707 * @return true on success, false on error 708 * @hide 709 */ 710 public boolean setAlias(String alias) { 711 if (sService == null) { 712 Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); 713 return false; 714 } 715 try { 716 return sService.setRemoteAlias(this, alias); 717 } catch (RemoteException e) {Log.e(TAG, "", e);} 718 return false; 719 } 720 721 /** 722 * Get the Bluetooth alias of the remote device. 723 * If Alias is null, get the Bluetooth name instead. 724 * @see #getAlias() 725 * @see #getName() 726 * 727 * @return the Bluetooth alias, or null if no alias or there was a problem 728 * @hide 729 */ 730 public String getAliasName() { 731 String name = getAlias(); 732 if (name == null) { 733 name = getName(); 734 } 735 return name; 736 } 737 738 /** 739 * Start the bonding (pairing) process with the remote device. 740 * <p>This is an asynchronous call, it will return immediately. Register 741 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when 742 * the bonding process completes, and its result. 743 * <p>Android system services will handle the necessary user interactions 744 * to confirm and complete the bonding process. 745 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 746 * 747 * @return false on immediate error, true if bonding will begin 748 */ 749 public boolean createBond() { 750 if (sService == null) { 751 Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); 752 return false; 753 } 754 try { 755 return sService.createBond(this); 756 } catch (RemoteException e) {Log.e(TAG, "", e);} 757 return false; 758 } 759 760 /** 761 * Start the bonding (pairing) process with the remote device using the 762 * Out Of Band mechanism. 763 * 764 * <p>This is an asynchronous call, it will return immediately. Register 765 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when 766 * the bonding process completes, and its result. 767 * 768 * <p>Android system services will handle the necessary user interactions 769 * to confirm and complete the bonding process. 770 * 771 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 772 * 773 * @param hash - Simple Secure pairing hash 774 * @param randomizer - The random key obtained using OOB 775 * @return false on immediate error, true if bonding will begin 776 * 777 * @hide 778 */ 779 public boolean createBondOutOfBand(byte[] hash, byte[] randomizer) { 780 //TODO(BT) 781 /* 782 try { 783 return sService.createBondOutOfBand(this, hash, randomizer); 784 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 785 return false; 786 } 787 788 /** 789 * Set the Out Of Band data for a remote device to be used later 790 * in the pairing mechanism. Users can obtain this data through other 791 * trusted channels 792 * 793 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 794 * 795 * @param hash Simple Secure pairing hash 796 * @param randomizer The random key obtained using OOB 797 * @return false on error; true otherwise 798 * 799 * @hide 800 */ 801 public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) { 802 //TODO(BT) 803 /* 804 try { 805 return sService.setDeviceOutOfBandData(this, hash, randomizer); 806 } catch (RemoteException e) {Log.e(TAG, "", e);} */ 807 return false; 808 } 809 810 /** 811 * Cancel an in-progress bonding request started with {@link #createBond}. 812 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 813 * 814 * @return true on success, false on error 815 * @hide 816 */ 817 public boolean cancelBondProcess() { 818 if (sService == null) { 819 Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); 820 return false; 821 } 822 try { 823 return sService.cancelBondProcess(this); 824 } catch (RemoteException e) {Log.e(TAG, "", e);} 825 return false; 826 } 827 828 /** 829 * Remove bond (pairing) with the remote device. 830 * <p>Delete the link key associated with the remote device, and 831 * immediately terminate connections to that device that require 832 * authentication and encryption. 833 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 834 * 835 * @return true on success, false on error 836 * @hide 837 */ 838 public boolean removeBond() { 839 if (sService == null) { 840 Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); 841 return false; 842 } 843 try { 844 return sService.removeBond(this); 845 } catch (RemoteException e) {Log.e(TAG, "", e);} 846 return false; 847 } 848 849 /** 850 * Get the bond state of the remote device. 851 * <p>Possible values for the bond state are: 852 * {@link #BOND_NONE}, 853 * {@link #BOND_BONDING}, 854 * {@link #BOND_BONDED}. 855 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 856 * 857 * @return the bond state 858 */ 859 public int getBondState() { 860 if (sService == null) { 861 Log.e(TAG, "BT not enabled. Cannot get bond state"); 862 return BOND_NONE; 863 } 864 try { 865 return sService.getBondState(this); 866 } catch (RemoteException e) {Log.e(TAG, "", e);} 867 catch (NullPointerException npe) { 868 // Handle case where bluetooth service proxy 869 // is already null. 870 Log.e(TAG, "NullPointerException for getBondState() of device ("+ 871 getAddress()+")", npe); 872 } 873 return BOND_NONE; 874 } 875 876 /** 877 * Returns whether there is an open connection to this device. 878 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 879 * 880 * @return True if there is at least one open connection to this device. 881 * @hide 882 */ 883 public boolean isConnected() { 884 if (sService == null) { 885 // BT is not enabled, we cannot be connected. 886 return false; 887 } 888 try { 889 return sService.isConnected(this); 890 } catch (RemoteException e) { 891 Log.e(TAG, "", e); 892 return false; 893 } 894 } 895 896 /** 897 * Get the Bluetooth class of the remote device. 898 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 899 * 900 * @return Bluetooth class object, or null on error 901 */ 902 public BluetoothClass getBluetoothClass() { 903 if (sService == null) { 904 Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); 905 return null; 906 } 907 try { 908 int classInt = sService.getRemoteClass(this); 909 if (classInt == BluetoothClass.ERROR) return null; 910 return new BluetoothClass(classInt); 911 } catch (RemoteException e) {Log.e(TAG, "", e);} 912 return null; 913 } 914 915 /** 916 * Get trust state of a remote device. 917 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 918 * @hide 919 */ 920 public boolean getTrustState() { 921 //TODO(BT) 922 /* 923 try { 924 return sService.getTrustState(this); 925 } catch (RemoteException e) { 926 Log.e(TAG, "", e); 927 }*/ 928 return false; 929 } 930 931 /** 932 * Set trust state for a remote device. 933 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 934 * @param value the trust state value (true or false) 935 * @hide 936 */ 937 public boolean setTrust(boolean value) { 938 //TODO(BT) 939 /* 940 try { 941 return sService.setTrust(this, value); 942 } catch (RemoteException e) { 943 Log.e(TAG, "", e); 944 }*/ 945 return false; 946 } 947 948 /** 949 * Returns the supported features (UUIDs) of the remote device. 950 * 951 * <p>This method does not start a service discovery procedure to retrieve the UUIDs 952 * from the remote device. Instead, the local cached copy of the service 953 * UUIDs are returned. 954 * <p>Use {@link #fetchUuidsWithSdp} if fresh UUIDs are desired. 955 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 956 * 957 * @return the supported features (UUIDs) of the remote device, 958 * or null on error 959 */ 960 public ParcelUuid[] getUuids() { 961 if (sService == null) { 962 Log.e(TAG, "BT not enabled. Cannot get remote device Uuids"); 963 return null; 964 } 965 try { 966 return sService.getRemoteUuids(this); 967 } catch (RemoteException e) {Log.e(TAG, "", e);} 968 return null; 969 } 970 971 /** 972 * Perform a service discovery on the remote device to get the UUIDs supported. 973 * 974 * <p>This API is asynchronous and {@link #ACTION_UUID} intent is sent, 975 * with the UUIDs supported by the remote end. If there is an error 976 * in getting the SDP records or if the process takes a long time, 977 * {@link #ACTION_UUID} intent is sent with the UUIDs that is currently 978 * present in the cache. Clients should use the {@link #getUuids} to get UUIDs 979 * if service discovery is not to be performed. 980 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. 981 * 982 * @return False if the sanity check fails, True if the process 983 * of initiating an ACL connection to the remote device 984 * was started. 985 */ 986 public boolean fetchUuidsWithSdp() { 987 IBluetooth service = sService; 988 if (service == null) { 989 Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp"); 990 return false; 991 } 992 try { 993 return service.fetchRemoteUuids(this); 994 } catch (RemoteException e) {Log.e(TAG, "", e);} 995 return false; 996 } 997 998 /** @hide */ 999 public int getServiceChannel(ParcelUuid uuid) { 1000 //TODO(BT) 1001 /* 1002 try { 1003 return sService.getRemoteServiceChannel(this, uuid); 1004 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 1005 return BluetoothDevice.ERROR; 1006 } 1007 1008 /** 1009 * Set the pin during pairing when the pairing method is {@link #PAIRING_VARIANT_PIN} 1010 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 1011 * 1012 * @return true pin has been set 1013 * false for error 1014 */ 1015 public boolean setPin(byte[] pin) { 1016 if (sService == null) { 1017 Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); 1018 return false; 1019 } 1020 try { 1021 return sService.setPin(this, true, pin.length, pin); 1022 } catch (RemoteException e) {Log.e(TAG, "", e);} 1023 return false; 1024 } 1025 1026 /** @hide */ 1027 public boolean setPasskey(int passkey) { 1028 //TODO(BT) 1029 /* 1030 try { 1031 return sService.setPasskey(this, true, 4, passkey); 1032 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 1033 return false; 1034 } 1035 1036 /** 1037 * Confirm passkey for {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION} pairing. 1038 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 1039 * 1040 * @return true confirmation has been sent out 1041 * false for error 1042 */ 1043 public boolean setPairingConfirmation(boolean confirm) { 1044 if (sService == null) { 1045 Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); 1046 return false; 1047 } 1048 try { 1049 return sService.setPairingConfirmation(this, confirm); 1050 } catch (RemoteException e) {Log.e(TAG, "", e);} 1051 return false; 1052 } 1053 1054 /** @hide */ 1055 public boolean setRemoteOutOfBandData() { 1056 // TODO(BT) 1057 /* 1058 try { 1059 return sService.setRemoteOutOfBandData(this); 1060 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 1061 return false; 1062 } 1063 1064 /** @hide */ 1065 public boolean cancelPairingUserInput() { 1066 if (sService == null) { 1067 Log.e(TAG, "BT not enabled. Cannot create pairing user input"); 1068 return false; 1069 } 1070 try { 1071 return sService.cancelBondProcess(this); 1072 } catch (RemoteException e) {Log.e(TAG, "", e);} 1073 return false; 1074 } 1075 1076 /** @hide */ 1077 public boolean isBluetoothDock() { 1078 // TODO(BT) 1079 /* 1080 try { 1081 return sService.isBluetoothDock(this); 1082 } catch (RemoteException e) {Log.e(TAG, "", e);}*/ 1083 return false; 1084 } 1085 1086 /** 1087 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 1088 * outgoing connection to this remote device on given channel. 1089 * <p>The remote device will be authenticated and communication on this 1090 * socket will be encrypted. 1091 * <p> Use this socket only if an authenticated socket link is possible. 1092 * Authentication refers to the authentication of the link key to 1093 * prevent man-in-the-middle type of attacks. 1094 * For example, for Bluetooth 2.1 devices, if any of the devices does not 1095 * have an input and output capability or just has the ability to 1096 * display a numeric key, a secure socket connection is not possible. 1097 * In such a case, use {#link createInsecureRfcommSocket}. 1098 * For more details, refer to the Security Model section 5.2 (vol 3) of 1099 * Bluetooth Core Specification version 2.1 + EDR. 1100 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing 1101 * connection. 1102 * <p>Valid RFCOMM channels are in range 1 to 30. 1103 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 1104 * 1105 * @param channel RFCOMM channel to connect to 1106 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 1107 * @throws IOException on error, for example Bluetooth not available, or 1108 * insufficient permissions 1109 * @hide 1110 */ 1111 public BluetoothSocket createRfcommSocket(int channel) throws IOException { 1112 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel, 1113 null); 1114 } 1115 1116 /** 1117 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure 1118 * outgoing connection to this remote device using SDP lookup of uuid. 1119 * <p>This is designed to be used with {@link 1120 * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer 1121 * Bluetooth applications. 1122 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing 1123 * connection. This will also perform an SDP lookup of the given uuid to 1124 * determine which channel to connect to. 1125 * <p>The remote device will be authenticated and communication on this 1126 * socket will be encrypted. 1127 * <p> Use this socket only if an authenticated socket link is possible. 1128 * Authentication refers to the authentication of the link key to 1129 * prevent man-in-the-middle type of attacks. 1130 * For example, for Bluetooth 2.1 devices, if any of the devices does not 1131 * have an input and output capability or just has the ability to 1132 * display a numeric key, a secure socket connection is not possible. 1133 * In such a case, use {#link createInsecureRfcommSocketToServiceRecord}. 1134 * For more details, refer to the Security Model section 5.2 (vol 3) of 1135 * Bluetooth Core Specification version 2.1 + EDR. 1136 * <p>Hint: If you are connecting to a Bluetooth serial board then try 1137 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. 1138 * However if you are connecting to an Android peer then please generate 1139 * your own unique UUID. 1140 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 1141 * 1142 * @param uuid service record uuid to lookup RFCOMM channel 1143 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 1144 * @throws IOException on error, for example Bluetooth not available, or 1145 * insufficient permissions 1146 */ 1147 public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException { 1148 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1, 1149 new ParcelUuid(uuid)); 1150 } 1151 1152 /** 1153 * Create an RFCOMM {@link BluetoothSocket} socket ready to start an insecure 1154 * outgoing connection to this remote device using SDP lookup of uuid. 1155 * <p> The communication channel will not have an authenticated link key 1156 * i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1 1157 * devices, the link key will be encrypted, as encryption is mandatory. 1158 * For legacy devices (pre Bluetooth 2.1 devices) the link key will 1159 * be not be encrypted. Use {@link #createRfcommSocketToServiceRecord} if an 1160 * encrypted and authenticated communication channel is desired. 1161 * <p>This is designed to be used with {@link 1162 * BluetoothAdapter#listenUsingInsecureRfcommWithServiceRecord} for peer-peer 1163 * Bluetooth applications. 1164 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing 1165 * connection. This will also perform an SDP lookup of the given uuid to 1166 * determine which channel to connect to. 1167 * <p>The remote device will be authenticated and communication on this 1168 * socket will be encrypted. 1169 * <p>Hint: If you are connecting to a Bluetooth serial board then try 1170 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. 1171 * However if you are connecting to an Android peer then please generate 1172 * your own unique UUID. 1173 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 1174 * 1175 * @param uuid service record uuid to lookup RFCOMM channel 1176 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection 1177 * @throws IOException on error, for example Bluetooth not available, or 1178 * insufficient permissions 1179 */ 1180 public BluetoothSocket createInsecureRfcommSocketToServiceRecord(UUID uuid) throws IOException { 1181 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, -1, 1182 new ParcelUuid(uuid)); 1183 } 1184 1185 /** 1186 * Construct an insecure RFCOMM socket ready to start an outgoing 1187 * connection. 1188 * Call #connect on the returned #BluetoothSocket to begin the connection. 1189 * The remote device will not be authenticated and communication on this 1190 * socket will not be encrypted. 1191 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 1192 * 1193 * @param port remote port 1194 * @return An RFCOMM BluetoothSocket 1195 * @throws IOException On error, for example Bluetooth not available, or 1196 * insufficient permissions. 1197 * @hide 1198 */ 1199 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { 1200 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, 1201 null); 1202 } 1203 1204 /** 1205 * Construct a SCO socket ready to start an outgoing connection. 1206 * Call #connect on the returned #BluetoothSocket to begin the connection. 1207 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 1208 * 1209 * @return a SCO BluetoothSocket 1210 * @throws IOException on error, for example Bluetooth not available, or 1211 * insufficient permissions. 1212 * @hide 1213 */ 1214 public BluetoothSocket createScoSocket() throws IOException { 1215 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); 1216 } 1217 1218 /** 1219 * Check that a pin is valid and convert to byte array. 1220 * 1221 * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters. 1222 * @param pin pin as java String 1223 * @return the pin code as a UTF-8 byte array, or null if it is an invalid 1224 * Bluetooth pin. 1225 * @hide 1226 */ 1227 public static byte[] convertPinToBytes(String pin) { 1228 if (pin == null) { 1229 return null; 1230 } 1231 byte[] pinBytes; 1232 try { 1233 pinBytes = pin.getBytes("UTF-8"); 1234 } catch (UnsupportedEncodingException uee) { 1235 Log.e(TAG, "UTF-8 not supported?!?"); // this should not happen 1236 return null; 1237 } 1238 if (pinBytes.length <= 0 || pinBytes.length > 16) { 1239 return null; 1240 } 1241 return pinBytes; 1242 } 1243 1244 /** 1245 * Connect to GATT Server hosted by this device. Caller acts as GATT client. 1246 * The callback is used to deliver results to Caller, such as connection status as well 1247 * as any further GATT client operations. 1248 * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct 1249 * GATT client operations. 1250 * @param callback GATT callback handler that will receive asynchronous callbacks. 1251 * @param autoConnect Whether to directly connect to the remote device (false) 1252 * or to automatically connect as soon as the remote 1253 * device becomes available (true). 1254 * @throws IllegalArgumentException if callback is null 1255 */ 1256 public BluetoothGatt connectGatt(Context context, boolean autoConnect, 1257 BluetoothGattCallback callback) { 1258 return (connectGatt(context, autoConnect,callback, TRANSPORT_AUTO)); 1259 } 1260 1261 /** 1262 * Connect to GATT Server hosted by this device. Caller acts as GATT client. 1263 * The callback is used to deliver results to Caller, such as connection status as well 1264 * as any further GATT client operations. 1265 * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct 1266 * GATT client operations. 1267 * @param callback GATT callback handler that will receive asynchronous callbacks. 1268 * @param autoConnect Whether to directly connect to the remote device (false) 1269 * or to automatically connect as soon as the remote 1270 * device becomes available (true). 1271 * @param transport preferred transport for GATT connections to remote dual-mode devices 1272 * {@link BluetoothDevice#TRANSPORT_AUTO} or 1273 * {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE} 1274 * @throws IllegalArgumentException if callback is null 1275 * @hide 1276 */ 1277 public BluetoothGatt connectGatt(Context context, boolean autoConnect, 1278 BluetoothGattCallback callback, int transport) { 1279 // TODO(Bluetooth) check whether platform support BLE 1280 // Do the check here or in GattServer? 1281 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 1282 IBluetoothManager managerService = adapter.getBluetoothManager(); 1283 try { 1284 IBluetoothGatt iGatt = managerService.getBluetoothGatt(); 1285 if (iGatt == null) { 1286 // BLE is not supported 1287 return null; 1288 } 1289 BluetoothGatt gatt = new BluetoothGatt(context, iGatt, this, transport); 1290 gatt.connect(autoConnect, callback); 1291 return gatt; 1292 } catch (RemoteException e) {Log.e(TAG, "", e);} 1293 return null; 1294 } 1295 1296} 1297