blob: 4cc22b4ae909be29f22573d3dad6379f3006a496 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Nick Pellybd022f42009-08-14 18:33:38 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.bluetooth;
18
Nick Pelly005b2282009-09-10 10:21:56 -070019import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Nick Pellybd022f42009-08-14 18:33:38 -070021import android.os.IBinder;
22import android.os.Parcel;
23import android.os.Parcelable;
Nick Pellyaef439e2009-09-28 12:33:17 -070024import android.os.ParcelUuid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.RemoteException;
Nick Pellybd022f42009-08-14 18:33:38 -070026import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.Log;
28
Nick Pellybd022f42009-08-14 18:33:38 -070029import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import java.io.UnsupportedEncodingException;
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -070031import java.util.UUID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33/**
Scott Main9fab0ae2009-11-03 18:17:59 -080034 * Represents a remote Bluetooth device. A {@link BluetoothDevice} lets you
Jake Hambyf51eada2010-09-21 13:39:53 -070035 * create a connection with the respective device or query information about
Scott Main9fab0ae2009-11-03 18:17:59 -080036 * it, such as the name, address, class, and bonding state.
Nick Pelly45e27042009-08-19 11:00:00 -070037 *
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 Main9fab0ae2009-11-03 18:17:59 -080043 *
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 Hambyf51eada2010-09-21 13:39:53 -070051 * {@link BluetoothSocket} for communication with the remote device, using
Scott Main9fab0ae2009-11-03 18:17:59 -080052 * {@link #createRfcommSocketToServiceRecord(UUID)}.
53 *
54 * <p class="note"><strong>Note:</strong>
55 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
56 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080057 * <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 Main9fab0ae2009-11-03 18:17:59 -080063 * {@see BluetoothAdapter}
64 * {@see BluetoothSocket}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
Nick Pellybd022f42009-08-14 18:33:38 -070066public final class BluetoothDevice implements Parcelable {
67 private static final String TAG = "BluetoothDevice";
Ravi Nagarajan70772722012-04-23 04:06:49 -070068 private static final boolean DBG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Nick Pellyb24e11b2009-09-08 17:40:43 -070070 /**
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 Pelly005b2282009-09-10 10:21:56 -070074 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
75 * BluetoothDevice.ERROR)</code>
Nick Pellyb24e11b2009-09-08 17:40:43 -070076 */
Nick Pelly005b2282009-09-10 10:21:56 -070077 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070078
Nick Pelly005b2282009-09-10 10:21:56 -070079 /**
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 Project9066cfe2009-03-03 19:31:44 -080091
Nick Pelly005b2282009-09-10 10:21:56 -070092 /**
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 Brown5bbd4b42012-04-20 19:28:00 -0700164 * 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 Pelly005b2282009-09-10 10:21:56 -0700176 * 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 Wakasaf76a50c2012-03-09 19:56:35 +0900209 * Used as a Parcelable {@link BluetoothClass} extra field in {@link
Nick Pelly005b2282009-09-10 10:21:56 -0700210 * #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 Hambyf51eada2010-09-21 13:39:53 -0700248 * 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 Pelly005b2282009-09-10 10:21:56 -0700250 * 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 Ganeshc88b0c62011-01-05 13:49:00 -0800261 public static final String EXTRA_PAIRING_KEY = "android.bluetooth.device.extra.PAIRING_KEY";
Nick Pelly005b2282009-09-10 10:21:56 -0700262
263 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700264 * Broadcast Action: This intent is used to broadcast the {@link UUID}
Nick Pellyaef439e2009-09-28 12:33:17 -0700265 * 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 Ganesh1caa6d12009-09-18 11:32:54 -0700268 * <p> Always contains the extra field {@link #EXTRA_DEVICE}
Matthew Xiead232102011-11-08 10:58:12 -0800269 * <p> Always contains the extra field {@link #EXTRA_UUID}
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700270 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700271 */
272 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
273 public static final String ACTION_UUID =
Matthew Xiead232102011-11-08 10:58:12 -0800274 "android.bluetooth.device.action.UUID";
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700275
276 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700277 * 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 Lixina4433af2009-07-09 16:56:43 +0800296
Matthew Xiea0c68032011-06-25 21:47:07 -0700297 /** @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 Ganeshc88b0c62011-01-05 13:49:00 -0800361 /**
362 * A bond attempt succeeded
363 * @hide
364 */
Nick Pellyb24e11b2009-09-08 17:40:43 -0700365 public static final int BOND_SUCCESS = 0;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800366
367 /**
368 * A bond attempt failed because pins did not match, or remote device did
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700369 * not respond to pin request in time
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800370 * @hide
371 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 public static final int UNBOND_REASON_AUTH_FAILED = 1;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800373
374 /**
375 * A bond attempt failed because the other side explicitly rejected
Nick Pelly45e27042009-08-19 11:00:00 -0700376 * bonding
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800377 * @hide
378 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 public static final int UNBOND_REASON_AUTH_REJECTED = 2;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800380
381 /**
382 * A bond attempt failed because we canceled the bonding process
383 * @hide
384 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 public static final int UNBOND_REASON_AUTH_CANCELED = 3;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800386
387 /**
388 * A bond attempt failed because we could not contact the remote device
389 * @hide
390 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800392
393 /**
394 * A bond attempt failed because a discovery is in progress
395 * @hide
396 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800398
399 /**
400 * A bond attempt failed because of authentication timeout
401 * @hide
402 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700403 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800404
405 /**
406 * A bond attempt failed because of repeated attempts
407 * @hide
408 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700409 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800410
411 /**
412 * A bond attempt failed because we received an Authentication Cancel
413 * by remote end
414 * @hide
415 */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700416 public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800417
418 /**
419 * An existing bond was explicitly revoked
420 * @hide
421 */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700422 public static final int UNBOND_REASON_REMOVED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800424 /**
425 * The user will be prompted to enter a pin
426 * @hide
427 */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700428 public static final int PAIRING_VARIANT_PIN = 0;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800429
430 /**
431 * The user will be prompted to enter a passkey
432 * @hide
433 */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700434 public static final int PAIRING_VARIANT_PASSKEY = 1;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800435
436 /**
437 * The user will be prompted to confirm the passkey displayed on the screen
438 * @hide
439 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700440 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800441
442 /**
443 * The user will be prompted to accept or deny the incoming pairing request
444 * @hide
445 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700446 public static final int PAIRING_VARIANT_CONSENT = 3;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800447
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 Ganesh32d85712009-09-10 22:00:05 -0700453 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800454
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 Ganesh1caa6d12009-09-18 11:32:54 -0700468 /**
469 * Used as an extra field in {@link #ACTION_UUID} intents,
Nick Pellyaef439e2009-09-28 12:33:17 -0700470 * Contains the {@link android.os.ParcelUuid}s of the remote device which
471 * is a parcelable version of {@link UUID}.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700472 */
473 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID";
474
Nick Pelly16fb88a2009-10-07 07:44:03 +0200475 /**
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 Ganeshd5ac1ae2009-05-05 22:26:12 -0700481
Nick Pellybd022f42009-08-14 18:33:38 -0700482 private final String mAddress;
483
Nick Pelly16fb88a2009-10-07 07:44:03 +0200484 /*package*/ static IBluetooth getService() {
485 synchronized (BluetoothDevice.class) {
486 if (sService == null) {
fredc0f420372012-04-12 00:02:00 -0700487 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
fredc903ac6f2012-04-24 03:59:57 -0700488 sService = adapter.getBluetoothService(mStateChangeCallback);
Nick Pelly16fb88a2009-10-07 07:44:03 +0200489 }
490 }
491 return sService;
492 }
493
fredc903ac6f2012-04-24 03:59:57 -0700494 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 Project9066cfe2009-03-03 19:31:44 -0800510 /**
Nick Pellybd022f42009-08-14 18:33:38 -0700511 * 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 Project9066cfe2009-03-03 19:31:44 -0800518 */
Nick Pellybd022f42009-08-14 18:33:38 -0700519 /*package*/ BluetoothDevice(String address) {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200520 getService(); // ensures sService is initialized
Nick Pelly005b2282009-09-10 10:21:56 -0700521 if (!BluetoothAdapter.checkBluetoothAddress(address)) {
Nick Pellybd022f42009-08-14 18:33:38 -0700522 throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
523 }
524
525 mAddress = address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527
Nick Pellybd022f42009-08-14 18:33:38 -0700528 @Override
529 public boolean equals(Object o) {
530 if (o instanceof BluetoothDevice) {
531 return mAddress.equals(((BluetoothDevice)o).getAddress());
532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 return false;
534 }
535
Nick Pellybd022f42009-08-14 18:33:38 -0700536 @Override
537 public int hashCode() {
538 return mAddress.hashCode();
The Android Open Source Project10592532009-03-18 17:39:46 -0700539 }
540
Nick Pelly45e27042009-08-19 11:00:00 -0700541 /**
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 Pellybd022f42009-08-14 18:33:38 -0700549 @Override
550 public String toString() {
551 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553
Nick Pellybd022f42009-08-14 18:33:38 -0700554 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 Project9066cfe2009-03-03 19:31:44 -0800570 }
571
Nick Pelly45e27042009-08-19 11:00:00 -0700572 /**
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 Project9066cfe2009-03-03 19:31:44 -0800577 public String getAddress() {
fredc0f420372012-04-12 00:02:00 -0700578 if (DBG) Log.d(TAG, "mAddress: " + mAddress);
Nick Pellybd022f42009-08-14 18:33:38 -0700579 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581
582 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700583 * Get the friendly Bluetooth name of the remote device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 *
Nick Pelly45e27042009-08-19 11:00:00 -0700585 * <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 Pellyde893f52009-09-08 13:15:33 -0700588 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 *
590 * @return the Bluetooth name, or null if there was a problem.
591 */
592 public String getName() {
fredc0f420372012-04-12 00:02:00 -0700593 if (sService == null) {
594 Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
595 return null;
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800598 return sService.getRemoteName(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 } catch (RemoteException e) {Log.e(TAG, "", e);}
600 return null;
601 }
602
603 /**
Matthew Xie269e81a2011-07-26 18:36:49 -0700604 * 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() {
fredc0f420372012-04-12 00:02:00 -0700611 if (sService == null) {
612 Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias");
613 return null;
614 }
Matthew Xie269e81a2011-07-26 18:36:49 -0700615 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800616 return sService.getRemoteAlias(this);
Matthew Xie269e81a2011-07-26 18:36:49 -0700617 } 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) {
fredc0f420372012-04-12 00:02:00 -0700632 if (sService == null) {
633 Log.e(TAG, "BT not enabled. Cannot set Remote Device name");
634 return false;
635 }
Matthew Xie269e81a2011-07-26 18:36:49 -0700636 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800637 return sService.setRemoteAlias(this, alias);
Matthew Xie269e81a2011-07-26 18:36:49 -0700638 } 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 Pelly005b2282009-09-10 10:21:56 -0700660 * 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 Project9066cfe2009-03-03 19:31:44 -0800667 *
Nick Pelly005b2282009-09-10 10:21:56 -0700668 * @return false on immediate error, true if bonding will begin
Nick Pelly18b1e792009-09-24 11:14:15 -0700669 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 */
Nick Pellybd022f42009-08-14 18:33:38 -0700671 public boolean createBond() {
fredc0f420372012-04-12 00:02:00 -0700672 if (sService == null) {
673 Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
674 return false;
675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800677 return sService.createBond(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 } catch (RemoteException e) {Log.e(TAG, "", e);}
679 return false;
680 }
681
682 /**
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700683 * 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 Ganeshe4caddb2012-01-25 16:16:48 -0800702 //TODO(BT)
703 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700704 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800705 return sService.createBondOutOfBand(this, hash, randomizer);
706 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700707 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 Ganeshe4caddb2012-01-25 16:16:48 -0800724 //TODO(BT)
725 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700726 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800727 return sService.setDeviceOutOfBandData(this, hash, randomizer);
728 } catch (RemoteException e) {Log.e(TAG, "", e);} */
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700729 return false;
730 }
731
732 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700733 * Cancel an in-progress bonding request started with {@link #createBond}.
734 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
735 *
Jake Hambyf51eada2010-09-21 13:39:53 -0700736 * @return true on success, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700737 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 */
Nick Pellybd022f42009-08-14 18:33:38 -0700739 public boolean cancelBondProcess() {
fredc0f420372012-04-12 00:02:00 -0700740 if (sService == null) {
741 Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
742 return false;
743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800745 return sService.cancelBondProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 } catch (RemoteException e) {Log.e(TAG, "", e);}
747 return false;
748 }
749
750 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700751 * 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 Project9066cfe2009-03-03 19:31:44 -0800756 *
Jake Hambyf51eada2010-09-21 13:39:53 -0700757 * @return true on success, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700758 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 */
Nick Pellybd022f42009-08-14 18:33:38 -0700760 public boolean removeBond() {
fredc0f420372012-04-12 00:02:00 -0700761 if (sService == null) {
762 Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond");
763 return false;
764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800766 return sService.removeBond(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellybd022f42009-08-14 18:33:38 -0700768 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770
771 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700772 * 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 Project9066cfe2009-03-03 19:31:44 -0800778 *
Nick Pelly005b2282009-09-10 10:21:56 -0700779 * @return the bond state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 */
Nick Pellybd022f42009-08-14 18:33:38 -0700781 public int getBondState() {
fredc0f420372012-04-12 00:02:00 -0700782 if (sService == null) {
783 Log.e(TAG, "BT not enabled. Cannot get bond state");
784 return BOND_NONE;
785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800787 return sService.getBondState(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 } catch (RemoteException e) {Log.e(TAG, "", e);}
Syed Ibrahim M305f2402012-06-19 10:14:25 -0700789 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 Pelly005b2282009-09-10 10:21:56 -0700795 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() {
fredc0f420372012-04-12 00:02:00 -0700805 if (sService == null) {
806 Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class");
807 return null;
808 }
Nick Pelly005b2282009-09-10 10:21:56 -0700809 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800810 int classInt = sService.getRemoteClass(this);
Nick Pelly005b2282009-09-10 10:21:56 -0700811 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 Project9066cfe2009-03-03 19:31:44 -0800815 }
816
Lixin Yueefa1dd72009-08-31 15:55:13 +0800817 /**
818 * Get trust state of a remote device.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200819 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Lixin Yueefa1dd72009-08-31 15:55:13 +0800820 * @hide
821 */
822 public boolean getTrustState() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800823 //TODO(BT)
824 /*
Lixin Yueefa1dd72009-08-31 15:55:13 +0800825 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800826 return sService.getTrustState(this);
Lixin Yueefa1dd72009-08-31 15:55:13 +0800827 } catch (RemoteException e) {
828 Log.e(TAG, "", e);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800829 }*/
Lixin Yueefa1dd72009-08-31 15:55:13 +0800830 return false;
831 }
832
833 /**
834 * Set trust state for a remote device.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200835 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Lixin Yueefa1dd72009-08-31 15:55:13 +0800836 * @param value the trust state value (true or false)
837 * @hide
838 */
839 public boolean setTrust(boolean value) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800840 //TODO(BT)
841 /*
Lixin Yueefa1dd72009-08-31 15:55:13 +0800842 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800843 return sService.setTrust(this, value);
Lixin Yueefa1dd72009-08-31 15:55:13 +0800844 } catch (RemoteException e) {
845 Log.e(TAG, "", e);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800846 }*/
Lixin Yueefa1dd72009-08-31 15:55:13 +0800847 return false;
848 }
849
Matthew Xiead232102011-11-08 10:58:12 -0800850 /**
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 Ganeshdd0463a2009-09-16 12:30:02 -0700862 public ParcelUuid[] getUuids() {
fredc0f420372012-04-12 00:02:00 -0700863 if (sService == null) {
864 Log.e(TAG, "BT not enabled. Cannot get remote device Uuids");
865 return null;
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800868 return sService.getRemoteUuids(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 } catch (RemoteException e) {Log.e(TAG, "", e);}
870 return null;
871 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700872
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700873 /**
Matthew Xiead232102011-11-08 10:58:12 -0800874 * Perform a service discovery on the remote device to get the UUIDs supported.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700875 *
Matthew Xiead232102011-11-08 10:58:12 -0800876 * <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 Ganesh1caa6d12009-09-18 11:32:54 -0700885 * of initiating an ACL connection to the remote device
886 * was started.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700887 */
888 public boolean fetchUuidsWithSdp() {
889 try {
fredc4c9caca2012-04-06 02:08:46 -0700890 return sService.fetchRemoteUuids(this);
891 } catch (RemoteException e) {Log.e(TAG, "", e);}
892 return false;
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700893 }
894
Nick Pelly45e27042009-08-19 11:00:00 -0700895 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700896 public int getServiceChannel(ParcelUuid uuid) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800897 //TODO(BT)
898 /*
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700899 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800900 return sService.getRemoteServiceChannel(this, uuid);
901 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Nick Pellyb24e11b2009-09-08 17:40:43 -0700902 return BluetoothDevice.ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
Nick Pelly45e27042009-08-19 11:00:00 -0700905 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700906 public boolean setPin(byte[] pin) {
fredc0f420372012-04-12 00:02:00 -0700907 if (sService == null) {
908 Log.e(TAG, "BT not enabled. Cannot set Remote Device pin");
909 return false;
910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800912 return sService.setPin(this, true, pin.length, pin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 } catch (RemoteException e) {Log.e(TAG, "", e);}
914 return false;
915 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700916
Nick Pelly45e27042009-08-19 11:00:00 -0700917 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700918 public boolean setPasskey(int passkey) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800919 //TODO(BT)
920 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800922 return sService.setPasskey(this, true, 4, passkey);
923 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700924 return false;
925 }
926
Nick Pelly45e27042009-08-19 11:00:00 -0700927 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700928 public boolean setPairingConfirmation(boolean confirm) {
fredc0f420372012-04-12 00:02:00 -0700929 if (sService == null) {
930 Log.e(TAG, "BT not enabled. Cannot set pairing confirmation");
931 return false;
932 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700933 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800934 return sService.setPairingConfirmation(this, confirm);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700935 } catch (RemoteException e) {Log.e(TAG, "", e);}
936 return false;
937 }
938
Nick Pelly45e27042009-08-19 11:00:00 -0700939 /** @hide */
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700940 public boolean setRemoteOutOfBandData() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800941 // TODO(BT)
942 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700943 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800944 return sService.setRemoteOutOfBandData(this);
945 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700946 return false;
947 }
948
949 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700950 public boolean cancelPairingUserInput() {
fredc0f420372012-04-12 00:02:00 -0700951 if (sService == null) {
952 Log.e(TAG, "BT not enabled. Cannot create pairing user input");
953 return false;
954 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700955 try {
Ravi Nagarajan919a4c62012-04-13 21:18:16 +0530956 return sService.cancelBondProcess(this);
Priti Agheradb44b202012-03-13 10:41:41 -0700957 } catch (RemoteException e) {Log.e(TAG, "", e);}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 return false;
959 }
960
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800961 /** @hide */
962 public boolean isBluetoothDock() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800963 // TODO(BT)
964 /*
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800965 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800966 return sService.isBluetoothDock(this);
967 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800968 return false;
969 }
970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700972 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
Nick Pelly16fb88a2009-10-07 07:44:03 +0200973 * outgoing connection to this remote device on given channel.
Nick Pelly45e27042009-08-19 11:00:00 -0700974 * <p>The remote device will be authenticated and communication on this
975 * socket will be encrypted.
Jaikumar Ganeshc8fa4ff2011-01-25 16:03:13 -0800976 * <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 Hambyf51eada2010-09-21 13:39:53 -0700985 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
Nick Pelly45e27042009-08-19 11:00:00 -0700986 * connection.
987 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pellycf440592009-09-08 10:12:06 -0700988 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellyde893f52009-09-08 13:15:33 -0700989 *
Nick Pelly45e27042009-08-19 11:00:00 -0700990 * @param channel RFCOMM channel to connect to
991 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
Nick Pellybd022f42009-08-14 18:33:38 -0700992 * @throws IOException on error, for example Bluetooth not available, or
Nick Pelly45e27042009-08-19 11:00:00 -0700993 * insufficient permissions
Nick Pelly16fb88a2009-10-07 07:44:03 +0200994 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700995 */
Nick Pelly45e27042009-08-19 11:00:00 -0700996 public BluetoothSocket createRfcommSocket(int channel) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200997 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 Hambyf51eada2010-09-21 13:39:53 -07001007 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
Nick Pelly16fb88a2009-10-07 07:44:03 +02001008 * 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 Ganeshc8fa4ff2011-01-25 16:03:13 -08001012 * <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 Pellyea5056e2010-02-24 11:19:10 -08001021 * <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 Pelly16fb88a2009-10-07 07:44:03 +02001025 * <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 Pellybd022f42009-08-14 18:33:38 -07001035 }
1036
1037 /**
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001038 * 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 Pellybd022f42009-08-14 18:33:38 -07001071 * 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 Pellye6ee3be2009-10-08 23:27:28 +02001076 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
1077 *
Nick Pellybd022f42009-08-14 18:33:38 -07001078 * @param port remote port
1079 * @return An RFCOMM BluetoothSocket
1080 * @throws IOException On error, for example Bluetooth not available, or
1081 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001082 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001083 */
1084 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +02001085 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port,
1086 null);
Nick Pellybd022f42009-08-14 18:33:38 -07001087 }
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 Pellye6ee3be2009-10-08 23:27:28 +02001092 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
1093 *
Nick Pellybd022f42009-08-14 18:33:38 -07001094 * @return a SCO BluetoothSocket
1095 * @throws IOException on error, for example Bluetooth not available, or
1096 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001097 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001098 */
1099 public BluetoothSocket createScoSocket() throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +02001100 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null);
Nick Pellybd022f42009-08-14 18:33:38 -07001101 }
1102
1103 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 * Check that a pin is valid and convert to byte array.
1105 *
Jake Hambyf51eada2010-09-21 13:39:53 -07001106 * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 * @param pin pin as java String
Jake Hambyf51eada2010-09-21 13:39:53 -07001108 * @return the pin code as a UTF-8 byte array, or null if it is an invalid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 * Bluetooth pin.
Nick Pelly45e27042009-08-19 11:00:00 -07001110 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 */
1112 public static byte[] convertPinToBytes(String pin) {
1113 if (pin == null) {
1114 return null;
1115 }
1116 byte[] pinBytes;
1117 try {
Jake Hambyf51eada2010-09-21 13:39:53 -07001118 pinBytes = pin.getBytes("UTF-8");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 } catch (UnsupportedEncodingException uee) {
Jake Hambyf51eada2010-09-21 13:39:53 -07001120 Log.e(TAG, "UTF-8 not supported?!?"); // this should not happen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 return null;
1122 }
1123 if (pinBytes.length <= 0 || pinBytes.length > 16) {
1124 return null;
1125 }
1126 return pinBytes;
1127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129}