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