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