BluetoothAdapter.java revision 1acdcc1a508c00dc31751a2a2eef539a59cb21ce
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.os.RemoteException; 22import android.util.Log; 23 24import java.io.IOException; 25import java.util.Collections; 26import java.util.Set; 27import java.util.HashSet; 28 29/** 30 * Represents the local Bluetooth adapter. 31 * 32 * <p>Use {@link android.content.Context#getSystemService} with {@link 33 * android.content.Context#BLUETOOTH_SERVICE} to get the default local 34 * Bluetooth adapter. On most Android devices there is only one local 35 * Bluetotoh adapter. 36 * 37 * <p>Use the {@link BluetoothDevice} class for operations on remote Bluetooth 38 * devices. 39 */ 40public final class BluetoothAdapter { 41 private static final String TAG = "BluetoothAdapter"; 42 43 /** 44 * Sentinel error value for this class. Guaranteed to not equal any other 45 * integer constant in this class. Provided as a convenience for functions 46 * that require a sentinel error value, for example: 47 * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 48 * BluetoothAdapter.ERROR)</code> 49 */ 50 public static final int ERROR = Integer.MIN_VALUE; 51 52 /** 53 * Broadcast Action: The state of the local Bluetooth adapter has been 54 * changed. 55 * <p>For example, Bluetooth has been turned on or off. 56 * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link 57 * #EXTRA_PREVIOUS_STATE} containing the new and old states 58 * respectively. 59 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 60 */ 61 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 62 public static final String ACTION_STATE_CHANGED = 63 "android.bluetooth.adapter.action.STATE_CHANGED"; 64 65 /** 66 * Used as an int extra field in {@link #ACTION_STATE_CHANGED} 67 * intents to request the current power state. Possible values are: 68 * {@link #STATE_OFF}, 69 * {@link #STATE_TURNING_ON}, 70 * {@link #STATE_ON}, 71 * {@link #STATE_TURNING_OFF}, 72 */ 73 public static final String EXTRA_STATE = 74 "android.bluetooth.adapter.extra.STATE"; 75 /** 76 * Used as an int extra field in {@link #ACTION_STATE_CHANGED} 77 * intents to request the previous power state. Possible values are: 78 * {@link #STATE_OFF}, 79 * {@link #STATE_TURNING_ON}, 80 * {@link #STATE_ON}, 81 * {@link #STATE_TURNING_OFF}, 82 */ 83 public static final String EXTRA_PREVIOUS_STATE = 84 "android.bluetooth.adapter.extra.PREVIOUS_STATE"; 85 86 /** 87 * Indicates the local Bluetooth adapter is off. 88 */ 89 public static final int STATE_OFF = 10; 90 /** 91 * Indicates the local Bluetooth adapter is turning on. However local 92 * clients should wait for {@link #STATE_ON} before attempting to 93 * use the adapter. 94 */ 95 public static final int STATE_TURNING_ON = 11; 96 /** 97 * Indicates the local Bluetooth adapter is on, and ready for use. 98 */ 99 public static final int STATE_ON = 12; 100 /** 101 * Indicates the local Bluetooth adapter is turning off. Local clients 102 * should immediately attempt graceful disconnection of any remote links. 103 */ 104 public static final int STATE_TURNING_OFF = 13; 105 106 /** 107 * Activity Action: Show a system activity that requests discoverable mode. 108 * <p>This activity will also request the user to turn on Bluetooth if it 109 * is not currently enabled. 110 * <p>Discoverable mode is equivalent to {@link 111 * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see 112 * this Bluetooth adapter when they perform a discovery. 113 * <p>For privacy, Android is not by default discoverable. 114 * <p>The sender can optionally use extra field {@link 115 * #EXTRA_DISCOVERABLE_DURATION} to request the duration of 116 * discoverability. Currently the default duration is 120 seconds, and 117 * maximum duration is capped at 300 seconds for each request. 118 * <p>Notification of the result of this activity is posted using the 119 * {@link android.app.Activity#onActivityResult} callback. The 120 * <code>resultCode</code> 121 * will be the duration (in seconds) of discoverability, or a negative 122 * value if the user rejected discoverability. 123 * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED} 124 * for global notification whenever the scan mode changes. 125 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 126 */ 127 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 128 public static final String ACTION_REQUEST_DISCOVERABLE = 129 "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE"; 130 131 /** 132 * Used as an optional int extra field in {@link 133 * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration 134 * for discoverability in seconds. The current default is 120 seconds, and 135 * requests over 300 seconds will be capped. These values could change. 136 */ 137 public static final String EXTRA_DISCOVERABLE_DURATION = 138 "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION"; 139 140 /** 141 * Activity Action: Show a system activity that allows the user to turn on 142 * Bluetooth. 143 * <p>This system activity will return once Bluetooth has completed turning 144 * on, or the user has decided not to turn Bluetooth on. 145 * <p>Notification of the result of this activity is posted using the 146 * {@link android.app.Activity#onActivityResult} callback. The 147 * <code>resultCode</code> 148 * will be negative if the user did not turn on Bluetooth, and non-negative 149 * if Bluetooth has been turned on. 150 * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED} 151 * for global notification whenever Bluetooth is turned on or off. 152 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 153 */ 154 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 155 public static final String ACTION_REQUEST_ENABLE = 156 "android.bluetooth.adapter.action.REQUEST_ENABLE"; 157 158 /** 159 * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter 160 * has changed. 161 * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link 162 * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes 163 * respectively. 164 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 165 */ 166 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 167 public static final String ACTION_SCAN_MODE_CHANGED = 168 "android.bluetooth.adapter.action.SCAN_MODE_CHANGED"; 169 170 /** 171 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED} 172 * intents to request the current scan mode. Possible values are: 173 * {@link #SCAN_MODE_NONE}, 174 * {@link #SCAN_MODE_CONNECTABLE}, 175 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}, 176 */ 177 public static final String EXTRA_SCAN_MODE = "android.bluetooth.adapter.extra.SCAN_MODE"; 178 /** 179 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED} 180 * intents to request the previous scan mode. Possible values are: 181 * {@link #SCAN_MODE_NONE}, 182 * {@link #SCAN_MODE_CONNECTABLE}, 183 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}, 184 */ 185 public static final String EXTRA_PREVIOUS_SCAN_MODE = 186 "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE"; 187 188 /** 189 * Indicates that both inquiry scan and page scan are disabled on the local 190 * Bluetooth adapter. Therefore this device is neither discoverable 191 * nor connectable from remote Bluetooth devices. 192 */ 193 public static final int SCAN_MODE_NONE = 20; 194 /** 195 * Indicates that inquiry scan is disabled, but page scan is enabled on the 196 * local Bluetooth adapter. Therefore this device is not discoverable from 197 * remote Bluetooth devices, but is connectable from remote devices that 198 * have previously discovered this device. 199 */ 200 public static final int SCAN_MODE_CONNECTABLE = 21; 201 /** 202 * Indicates that both inquiry scan and page scan are enabled on the local 203 * Bluetooth adapter. Therefore this device is both discoverable and 204 * connectable from remote Bluetooth devices. 205 */ 206 public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23; 207 208 209 /** 210 * Broadcast Action: The local Bluetooth adapter has started the remote 211 * device discovery process. 212 * <p>This usually involves an inquiry scan of about 12 seconds, followed 213 * by a page scan of each new device to retrieve its Bluetooth name. 214 * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as 215 * remote Bluetooth devices are found. 216 * <p>Device discovery is a heavyweight procedure. New connections to 217 * remote Bluetooth devices should not be attempted while discovery is in 218 * progress, and existing connections will experience limited bandwidth 219 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing 220 * discovery. 221 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 222 */ 223 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 224 public static final String ACTION_DISCOVERY_STARTED = 225 "android.bluetooth.adapter.action.DISCOVERY_STARTED"; 226 /** 227 * Broadcast Action: The local Bluetooth adapter has finished the device 228 * discovery process. 229 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 230 */ 231 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 232 public static final String ACTION_DISCOVERY_FINISHED = 233 "android.bluetooth.adapter.action.DISCOVERY_FINISHED"; 234 235 /** 236 * Broadcast Action: The local Bluetooth adapter has changed its friendly 237 * Bluetooth name. 238 * <p>This name is visible to remote Bluetooth devices. 239 * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing 240 * the name. 241 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. 242 */ 243 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 244 public static final String ACTION_LOCAL_NAME_CHANGED = 245 "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED"; 246 /** 247 * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED} 248 * intents to request the local Bluetooth name. 249 */ 250 public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME"; 251 252 private static final int ADDRESS_LENGTH = 17; 253 254 private final IBluetooth mService; 255 256 /** 257 * Do not use this constructor. Use Context.getSystemService() instead. 258 * @hide 259 */ 260 public BluetoothAdapter(IBluetooth service) { 261 if (service == null) { 262 throw new IllegalArgumentException("service is null"); 263 } 264 mService = service; 265 } 266 267 /** 268 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware 269 * address. 270 * <p>Valid Bluetooth hardware addresses must be upper case, in a format 271 * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is 272 * available to validate a Bluetooth address. 273 * <p>A {@link BluetoothDevice} will always be returned for a valid 274 * hardware address, even if this adapter has never seen that device. 275 * 276 * @param address valid Bluetooth MAC address 277 * @throws IllegalArgumentException if address is invalid 278 */ 279 public BluetoothDevice getRemoteDevice(String address) { 280 return new BluetoothDevice(address); 281 } 282 283 /** 284 * Return true if Bluetooth is currently enabled and ready for use. 285 * <p>Equivalent to: 286 * <code>getBluetoothState() == STATE_ON</code> 287 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 288 * 289 * @return true if the local adapter is turned on 290 */ 291 public boolean isEnabled() { 292 try { 293 return mService.isEnabled(); 294 } catch (RemoteException e) {Log.e(TAG, "", e);} 295 return false; 296 } 297 298 /** 299 * Get the current state of the local Bluetooth adapter. 300 * <p>Possible return values are 301 * {@link #STATE_OFF}, 302 * {@link #STATE_TURNING_ON}, 303 * {@link #STATE_ON}, 304 * {@link #STATE_TURNING_OFF}. 305 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 306 * 307 * @return current state of Bluetooth adapter 308 */ 309 public int getState() { 310 try { 311 return mService.getBluetoothState(); 312 } catch (RemoteException e) {Log.e(TAG, "", e);} 313 return STATE_OFF; 314 } 315 316 /** 317 * Turn on the local Bluetooth adapter. 318 * <p>This powers on the underlying Bluetooth hardware, and starts all 319 * Bluetooth system services. 320 * <p>This is an asynchronous call: it will return immediately, and 321 * clients should listen for {@link #ACTION_STATE_CHANGED} 322 * to be notified of subsequent adapter state changes. If this call returns 323 * true, then the adapter state will immediately transition from {@link 324 * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time 325 * later transition to either {@link #STATE_OFF} or {@link 326 * #STATE_ON}. If this call returns false then there was an 327 * immediate problem that will prevent the adapter from being turned on - 328 * such as Airplane mode, or the adapter is already turned on. 329 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 330 * 331 * @return true to indicate adapter startup has begun, or false on 332 * immediate error 333 */ 334 public boolean enable() { 335 try { 336 return mService.enable(); 337 } catch (RemoteException e) {Log.e(TAG, "", e);} 338 return false; 339 } 340 341 /** 342 * Turn off the local Bluetooth adapter. 343 * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth 344 * system services, and powers down the underlying Bluetooth hardware. 345 * <p>This is an asynchronous call: it will return immediately, and 346 * clients should listen for {@link #ACTION_STATE_CHANGED} 347 * to be notified of subsequent adapter state changes. If this call returns 348 * true, then the adapter state will immediately transition from {@link 349 * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time 350 * later transition to either {@link #STATE_OFF} or {@link 351 * #STATE_ON}. If this call returns false then there was an 352 * immediate problem that will prevent the adapter from being turned off - 353 * such as the adapter already being turned off. 354 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 355 * 356 * @return true to indicate adapter shutdown has begun, or false on 357 * immediate error 358 */ 359 public boolean disable() { 360 try { 361 return mService.disable(true); 362 } catch (RemoteException e) {Log.e(TAG, "", e);} 363 return false; 364 } 365 366 /** 367 * Returns the hardware address of the local Bluetooth adapter. 368 * <p>For example, "00:11:22:AA:BB:CC". 369 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 370 * 371 * @return Bluetooth hardware address as string 372 */ 373 public String getAddress() { 374 try { 375 return mService.getAddress(); 376 } catch (RemoteException e) {Log.e(TAG, "", e);} 377 return null; 378 } 379 380 /** 381 * Get the friendly Bluetooth name of the local Bluetooth adapter. 382 * <p>This name is visible to remote Bluetooth devices. 383 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 384 * 385 * @return the Bluetooth name, or null on error 386 */ 387 public String getName() { 388 try { 389 return mService.getName(); 390 } catch (RemoteException e) {Log.e(TAG, "", e);} 391 return null; 392 } 393 394 /** 395 * Set the friendly Bluetooth name of the local Bluetoth adapter. 396 * <p>This name is visible to remote Bluetooth devices. 397 * <p>Valid Bluetooth names are a maximum of 248 UTF-8 characters, however 398 * many remote devices can only display the first 40 characters, and some 399 * may be limited to just 20. 400 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 401 * 402 * @param name a valid Bluetooth name 403 * @return true if the name was set, false otherwise 404 */ 405 public boolean setName(String name) { 406 try { 407 return mService.setName(name); 408 } catch (RemoteException e) {Log.e(TAG, "", e);} 409 return false; 410 } 411 412 /** 413 * Get the current Bluetooth scan mode of the local Bluetooth adaper. 414 * <p>The Bluetooth scan mode determines if the local adapter is 415 * connectable and/or discoverable from remote Bluetooth devices. 416 * <p>Possible values are: 417 * {@link #SCAN_MODE_NONE}, 418 * {@link #SCAN_MODE_CONNECTABLE}, 419 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. 420 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 421 * 422 * @return scan mode 423 */ 424 public int getScanMode() { 425 try { 426 return mService.getScanMode(); 427 } catch (RemoteException e) {Log.e(TAG, "", e);} 428 return SCAN_MODE_NONE; 429 } 430 431 /** 432 * Set the Bluetooth scan mode of the local Bluetooth adapter. 433 * <p>The Bluetooth scan mode determines if the local adapter is 434 * connectable and/or discoverable from remote Bluetooth devices. 435 * <p>For privacy reasons, discoverable mode is automatically turned off 436 * after <code>duration</code> seconds. For example, 120 seconds should be 437 * enough for a remote device to initiate and complete its discovery 438 * process. 439 * <p>Valid scan mode values are: 440 * {@link #SCAN_MODE_NONE}, 441 * {@link #SCAN_MODE_CONNECTABLE}, 442 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. 443 * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} 444 * <p>Applications cannot set the scan mode. They should use 445 * <code>startActivityForResult( 446 * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE}) 447 * </code>instead. 448 * 449 * @param mode valid scan mode 450 * @param duration time in seconds to apply scan mode, only used for 451 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE} 452 * @return true if the scan mode was set, false otherwise 453 * @hide 454 */ 455 public boolean setScanMode(int mode, int duration) { 456 try { 457 return mService.setScanMode(mode, duration); 458 } catch (RemoteException e) {Log.e(TAG, "", e);} 459 return false; 460 } 461 462 /** @hide */ 463 public boolean setScanMode(int mode) { 464 return setScanMode(mode, 120); 465 } 466 467 /** @hide */ 468 public int getDiscoverableTimeout() { 469 try { 470 return mService.getDiscoverableTimeout(); 471 } catch (RemoteException e) {Log.e(TAG, "", e);} 472 return -1; 473 } 474 475 /** @hide */ 476 public void setDiscoverableTimeout(int timeout) { 477 try { 478 mService.setDiscoverableTimeout(timeout); 479 } catch (RemoteException e) {Log.e(TAG, "", e);} 480 } 481 482 /** 483 * Start the remote device discovery process. 484 * <p>The discovery process usually involves an inquiry scan of about 12 485 * seconds, followed by a page scan of each new device to retrieve its 486 * Bluetooth name. 487 * <p>This is an asynchronous call, it will return immediately. Register 488 * for {@link #ACTION_DISCOVERY_STARTED} and {@link 489 * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the 490 * discovery starts and completes. Register for {@link 491 * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices 492 * are found. 493 * <p>Device discovery is a heavyweight procedure. New connections to 494 * remote Bluetooth devices should not be attempted while discovery is in 495 * progress, and existing connections will experience limited bandwidth 496 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing 497 * discovery. 498 * <p>Device discovery will only find remote devices that are currently 499 * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are 500 * not discoverable by default, and need to be entered into a special mode. 501 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 502 * 503 * @return true on success, false on error 504 */ 505 public boolean startDiscovery() { 506 try { 507 return mService.startDiscovery(); 508 } catch (RemoteException e) {Log.e(TAG, "", e);} 509 return false; 510 } 511 512 /** 513 * Cancel the current device discovery process. 514 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. 515 * 516 * @return true on success, false on error 517 */ 518 public boolean cancelDiscovery() { 519 try { 520 mService.cancelDiscovery(); 521 } catch (RemoteException e) {Log.e(TAG, "", e);} 522 return false; 523 } 524 525 /** 526 * Return true if the local Bluetooth adapter is currently in the device 527 * discovery process. 528 * <p>Device discovery is a heavyweight procedure. New connections to 529 * remote Bluetooth devices should not be attempted while discovery is in 530 * progress, and existing connections will experience limited bandwidth 531 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing 532 * discovery. 533 * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED} 534 * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery 535 * starts or completes. 536 * 537 * @return true if discovering 538 */ 539 public boolean isDiscovering() { 540 try { 541 return mService.isDiscovering(); 542 } catch (RemoteException e) {Log.e(TAG, "", e);} 543 return false; 544 } 545 546 /** 547 * Return the set of {@link BluetoothDevice} objects that are bonded 548 * (paired) to the local adapter. 549 * 550 * @return unmodifiable set of {@link BluetoothDevice}, or null on error 551 */ 552 public Set<BluetoothDevice> getBondedDevices() { 553 try { 554 return toDeviceSet(mService.listBonds()); 555 } catch (RemoteException e) {Log.e(TAG, "", e);} 556 return null; 557 } 558 559 /** 560 * Create a listening, secure RFCOMM Bluetooth socket. 561 * <p>A remote device connecting to this socket will be authenticated and 562 * communication on this socket will be encrypted. 563 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming 564 * connections to listening {@link BluetoothServerSocket}. 565 * <p>Valid RFCOMM channels are in range 1 to 30. 566 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} 567 * @param channel RFCOMM channel to listen on 568 * @return a listening RFCOMM BluetoothServerSocket 569 * @throws IOException on error, for example Bluetooth not available, or 570 * insufficient permissions, or channel in use. 571 */ 572 public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException { 573 BluetoothServerSocket socket = new BluetoothServerSocket( 574 BluetoothSocket.TYPE_RFCOMM, true, true, channel); 575 try { 576 socket.mSocket.bindListen(); 577 } catch (IOException e) { 578 try { 579 socket.close(); 580 } catch (IOException e2) { } 581 throw e; 582 } 583 return socket; 584 } 585 586 /** 587 * Construct an unencrypted, unauthenticated, RFCOMM server socket. 588 * Call #accept to retrieve connections to this socket. 589 * @return An RFCOMM BluetoothServerSocket 590 * @throws IOException On error, for example Bluetooth not available, or 591 * insufficient permissions. 592 * @hide 593 */ 594 public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException { 595 BluetoothServerSocket socket = new BluetoothServerSocket( 596 BluetoothSocket.TYPE_RFCOMM, false, false, port); 597 try { 598 socket.mSocket.bindListen(); 599 } catch (IOException e) { 600 try { 601 socket.close(); 602 } catch (IOException e2) { } 603 throw e; 604 } 605 return socket; 606 } 607 608 /** 609 * Construct a SCO server socket. 610 * Call #accept to retrieve connections to this socket. 611 * @return A SCO BluetoothServerSocket 612 * @throws IOException On error, for example Bluetooth not available, or 613 * insufficient permissions. 614 * @hide 615 */ 616 public static BluetoothServerSocket listenUsingScoOn() throws IOException { 617 BluetoothServerSocket socket = new BluetoothServerSocket( 618 BluetoothSocket.TYPE_SCO, false, false, -1); 619 try { 620 socket.mSocket.bindListen(); 621 } catch (IOException e) { 622 try { 623 socket.close(); 624 } catch (IOException e2) { } 625 throw e; 626 } 627 return socket; 628 } 629 630 private Set<BluetoothDevice> toDeviceSet(String[] addresses) { 631 Set<BluetoothDevice> devices = new HashSet<BluetoothDevice>(addresses.length); 632 for (int i = 0; i < addresses.length; i++) { 633 devices.add(getRemoteDevice(addresses[i])); 634 } 635 return Collections.unmodifiableSet(devices); 636 } 637 638 /** 639 * Validate a Bluetooth address, such as "00:43:A8:23:10:F0" 640 * <p>Alphabetic characters must be uppercase to be valid. 641 * 642 * @param address Bluetooth address as string 643 * @return true if the address is valid, false otherwise 644 */ 645 public static boolean checkBluetoothAddress(String address) { 646 if (address == null || address.length() != ADDRESS_LENGTH) { 647 return false; 648 } 649 for (int i = 0; i < ADDRESS_LENGTH; i++) { 650 char c = address.charAt(i); 651 switch (i % 3) { 652 case 0: 653 case 1: 654 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) { 655 // hex character, OK 656 break; 657 } 658 return false; 659 case 2: 660 if (c == ':') { 661 break; // OK 662 } 663 return false; 664 } 665 } 666 return true; 667 } 668} 669