blob: bd089ff6dac38f88c69fbef397081ee8589c9743 [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";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Nick Pellyb24e11b2009-09-08 17:40:43 -070069 /**
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 Pelly005b2282009-09-10 10:21:56 -070073 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
74 * BluetoothDevice.ERROR)</code>
Nick Pellyb24e11b2009-09-08 17:40:43 -070075 */
Nick Pelly005b2282009-09-10 10:21:56 -070076 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070077
Nick Pelly005b2282009-09-10 10:21:56 -070078 /**
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 Project9066cfe2009-03-03 19:31:44 -080090
Nick Pelly005b2282009-09-10 10:21:56 -070091 /**
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 Brown5bbd4b42012-04-20 19:28:00 -0700163 * 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 Pelly005b2282009-09-10 10:21:56 -0700175 * 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 Wakasaf76a50c2012-03-09 19:56:35 +0900208 * Used as a Parcelable {@link BluetoothClass} extra field in {@link
Nick Pelly005b2282009-09-10 10:21:56 -0700209 * #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 Hambyf51eada2010-09-21 13:39:53 -0700247 * 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 Pelly005b2282009-09-10 10:21:56 -0700249 * 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 Ganeshc88b0c62011-01-05 13:49:00 -0800260 public static final String EXTRA_PAIRING_KEY = "android.bluetooth.device.extra.PAIRING_KEY";
Nick Pelly005b2282009-09-10 10:21:56 -0700261
262 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700263 * Broadcast Action: This intent is used to broadcast the {@link UUID}
Nick Pellyaef439e2009-09-28 12:33:17 -0700264 * 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 Ganesh1caa6d12009-09-18 11:32:54 -0700267 * <p> Always contains the extra field {@link #EXTRA_DEVICE}
Matthew Xiead232102011-11-08 10:58:12 -0800268 * <p> Always contains the extra field {@link #EXTRA_UUID}
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700269 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700270 */
271 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
272 public static final String ACTION_UUID =
Matthew Xiead232102011-11-08 10:58:12 -0800273 "android.bluetooth.device.action.UUID";
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700274
275 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700276 * 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 Lixina4433af2009-07-09 16:56:43 +0800295
Matthew Xiea0c68032011-06-25 21:47:07 -0700296 /** @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 Ganeshc88b0c62011-01-05 13:49:00 -0800360 /**
361 * A bond attempt succeeded
362 * @hide
363 */
Nick Pellyb24e11b2009-09-08 17:40:43 -0700364 public static final int BOND_SUCCESS = 0;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800365
366 /**
367 * A bond attempt failed because pins did not match, or remote device did
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700368 * not respond to pin request in time
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800369 * @hide
370 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 public static final int UNBOND_REASON_AUTH_FAILED = 1;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800372
373 /**
374 * A bond attempt failed because the other side explicitly rejected
Nick Pelly45e27042009-08-19 11:00:00 -0700375 * bonding
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800376 * @hide
377 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 public static final int UNBOND_REASON_AUTH_REJECTED = 2;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800379
380 /**
381 * A bond attempt failed because we canceled the bonding process
382 * @hide
383 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public static final int UNBOND_REASON_AUTH_CANCELED = 3;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800385
386 /**
387 * A bond attempt failed because we could not contact the remote device
388 * @hide
389 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800391
392 /**
393 * A bond attempt failed because a discovery is in progress
394 * @hide
395 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800397
398 /**
399 * A bond attempt failed because of authentication timeout
400 * @hide
401 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700402 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800403
404 /**
405 * A bond attempt failed because of repeated attempts
406 * @hide
407 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700408 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800409
410 /**
411 * A bond attempt failed because we received an Authentication Cancel
412 * by remote end
413 * @hide
414 */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700415 public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800416
417 /**
418 * An existing bond was explicitly revoked
419 * @hide
420 */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700421 public static final int UNBOND_REASON_REMOVED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800423 /**
424 * The user will be prompted to enter a pin
425 * @hide
426 */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700427 public static final int PAIRING_VARIANT_PIN = 0;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800428
429 /**
430 * The user will be prompted to enter a passkey
431 * @hide
432 */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700433 public static final int PAIRING_VARIANT_PASSKEY = 1;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800434
435 /**
436 * The user will be prompted to confirm the passkey displayed on the screen
437 * @hide
438 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700439 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800440
441 /**
442 * The user will be prompted to accept or deny the incoming pairing request
443 * @hide
444 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700445 public static final int PAIRING_VARIANT_CONSENT = 3;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800446
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 Ganesh32d85712009-09-10 22:00:05 -0700452 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800453
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 Ganesh1caa6d12009-09-18 11:32:54 -0700467 /**
468 * Used as an extra field in {@link #ACTION_UUID} intents,
Nick Pellyaef439e2009-09-28 12:33:17 -0700469 * Contains the {@link android.os.ParcelUuid}s of the remote device which
470 * is a parcelable version of {@link UUID}.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700471 */
472 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID";
473
Nick Pelly16fb88a2009-10-07 07:44:03 +0200474 /**
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 Ganeshd5ac1ae2009-05-05 22:26:12 -0700480
Nick Pellybd022f42009-08-14 18:33:38 -0700481 private final String mAddress;
482
Nick Pelly16fb88a2009-10-07 07:44:03 +0200483 /*package*/ static IBluetooth getService() {
484 synchronized (BluetoothDevice.class) {
485 if (sService == null) {
Nick Pellyf242b7b2009-10-08 00:12:45 +0200486 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
Nick Pelly16fb88a2009-10-07 07:44:03 +0200487 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 Project9066cfe2009-03-03 19:31:44 -0800496 /**
Nick Pellybd022f42009-08-14 18:33:38 -0700497 * 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 Project9066cfe2009-03-03 19:31:44 -0800504 */
Nick Pellybd022f42009-08-14 18:33:38 -0700505 /*package*/ BluetoothDevice(String address) {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200506 getService(); // ensures sService is initialized
Nick Pelly005b2282009-09-10 10:21:56 -0700507 if (!BluetoothAdapter.checkBluetoothAddress(address)) {
Nick Pellybd022f42009-08-14 18:33:38 -0700508 throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
509 }
510
511 mAddress = address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
513
Nick Pellybd022f42009-08-14 18:33:38 -0700514 @Override
515 public boolean equals(Object o) {
516 if (o instanceof BluetoothDevice) {
517 return mAddress.equals(((BluetoothDevice)o).getAddress());
518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 return false;
520 }
521
Nick Pellybd022f42009-08-14 18:33:38 -0700522 @Override
523 public int hashCode() {
524 return mAddress.hashCode();
The Android Open Source Project10592532009-03-18 17:39:46 -0700525 }
526
Nick Pelly45e27042009-08-19 11:00:00 -0700527 /**
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 Pellybd022f42009-08-14 18:33:38 -0700535 @Override
536 public String toString() {
537 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539
Nick Pellybd022f42009-08-14 18:33:38 -0700540 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 Project9066cfe2009-03-03 19:31:44 -0800556 }
557
Nick Pelly45e27042009-08-19 11:00:00 -0700558 /**
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 Project9066cfe2009-03-03 19:31:44 -0800563 public String getAddress() {
Nick Pellybd022f42009-08-14 18:33:38 -0700564 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
567 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700568 * Get the friendly Bluetooth name of the remote device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 *
Nick Pelly45e27042009-08-19 11:00:00 -0700570 * <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 Pellyde893f52009-09-08 13:15:33 -0700573 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 *
575 * @return the Bluetooth name, or null if there was a problem.
576 */
577 public String getName() {
578 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800579 return sService.getRemoteName(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 } catch (RemoteException e) {Log.e(TAG, "", e);}
581 return null;
582 }
583
584 /**
Matthew Xie269e81a2011-07-26 18:36:49 -0700585 * 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 Ganeshe4caddb2012-01-25 16:16:48 -0800593 return sService.getRemoteAlias(this);
Matthew Xie269e81a2011-07-26 18:36:49 -0700594 } 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 Ganeshe4caddb2012-01-25 16:16:48 -0800610 return sService.setRemoteAlias(this, alias);
Matthew Xie269e81a2011-07-26 18:36:49 -0700611 } 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 Pelly005b2282009-09-10 10:21:56 -0700633 * 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 Project9066cfe2009-03-03 19:31:44 -0800640 *
Nick Pelly005b2282009-09-10 10:21:56 -0700641 * @return false on immediate error, true if bonding will begin
Nick Pelly18b1e792009-09-24 11:14:15 -0700642 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 */
Nick Pellybd022f42009-08-14 18:33:38 -0700644 public boolean createBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800646 return sService.createBond(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 } catch (RemoteException e) {Log.e(TAG, "", e);}
648 return false;
649 }
650
651 /**
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700652 * 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 Ganeshe4caddb2012-01-25 16:16:48 -0800671 //TODO(BT)
672 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700673 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800674 return sService.createBondOutOfBand(this, hash, randomizer);
675 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700676 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 Ganeshe4caddb2012-01-25 16:16:48 -0800693 //TODO(BT)
694 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700695 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800696 return sService.setDeviceOutOfBandData(this, hash, randomizer);
697 } catch (RemoteException e) {Log.e(TAG, "", e);} */
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700698 return false;
699 }
700
701 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700702 * Cancel an in-progress bonding request started with {@link #createBond}.
703 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
704 *
Jake Hambyf51eada2010-09-21 13:39:53 -0700705 * @return true on success, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700706 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 */
Nick Pellybd022f42009-08-14 18:33:38 -0700708 public boolean cancelBondProcess() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800710 return sService.cancelBondProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 } catch (RemoteException e) {Log.e(TAG, "", e);}
712 return false;
713 }
714
715 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700716 * 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 Project9066cfe2009-03-03 19:31:44 -0800721 *
Jake Hambyf51eada2010-09-21 13:39:53 -0700722 * @return true on success, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700723 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
Nick Pellybd022f42009-08-14 18:33:38 -0700725 public boolean removeBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800727 return sService.removeBond(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellybd022f42009-08-14 18:33:38 -0700729 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700733 * 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 Project9066cfe2009-03-03 19:31:44 -0800739 *
Nick Pelly005b2282009-09-10 10:21:56 -0700740 * @return the bond state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 */
Nick Pellybd022f42009-08-14 18:33:38 -0700742 public int getBondState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800744 return sService.getBondState(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700746 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 Ganeshe4caddb2012-01-25 16:16:48 -0800757 int classInt = sService.getRemoteClass(this);
Nick Pelly005b2282009-09-10 10:21:56 -0700758 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 Project9066cfe2009-03-03 19:31:44 -0800762 }
763
Lixin Yueefa1dd72009-08-31 15:55:13 +0800764 /**
765 * Get trust state of a remote device.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200766 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Lixin Yueefa1dd72009-08-31 15:55:13 +0800767 * @hide
768 */
769 public boolean getTrustState() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800770 //TODO(BT)
771 /*
Lixin Yueefa1dd72009-08-31 15:55:13 +0800772 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800773 return sService.getTrustState(this);
Lixin Yueefa1dd72009-08-31 15:55:13 +0800774 } catch (RemoteException e) {
775 Log.e(TAG, "", e);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800776 }*/
Lixin Yueefa1dd72009-08-31 15:55:13 +0800777 return false;
778 }
779
780 /**
781 * Set trust state for a remote device.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200782 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Lixin Yueefa1dd72009-08-31 15:55:13 +0800783 * @param value the trust state value (true or false)
784 * @hide
785 */
786 public boolean setTrust(boolean value) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800787 //TODO(BT)
788 /*
Lixin Yueefa1dd72009-08-31 15:55:13 +0800789 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800790 return sService.setTrust(this, value);
Lixin Yueefa1dd72009-08-31 15:55:13 +0800791 } catch (RemoteException e) {
792 Log.e(TAG, "", e);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800793 }*/
Lixin Yueefa1dd72009-08-31 15:55:13 +0800794 return false;
795 }
796
Matthew Xiead232102011-11-08 10:58:12 -0800797 /**
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 Ganeshdd0463a2009-09-16 12:30:02 -0700809 public ParcelUuid[] getUuids() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800811 return sService.getRemoteUuids(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 } catch (RemoteException e) {Log.e(TAG, "", e);}
813 return null;
814 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700815
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700816 /**
Matthew Xiead232102011-11-08 10:58:12 -0800817 * Perform a service discovery on the remote device to get the UUIDs supported.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700818 *
Matthew Xiead232102011-11-08 10:58:12 -0800819 * <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 Ganesh1caa6d12009-09-18 11:32:54 -0700828 * of initiating an ACL connection to the remote device
829 * was started.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700830 */
831 public boolean fetchUuidsWithSdp() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800832 //TODO(BT)
833 /*
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700834 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800835 return sService.fetchRemoteUuids(this, null, null);
836 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700837 return false;
838 }
839
Nick Pelly45e27042009-08-19 11:00:00 -0700840 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700841 public int getServiceChannel(ParcelUuid uuid) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800842 //TODO(BT)
843 /*
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700844 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800845 return sService.getRemoteServiceChannel(this, uuid);
846 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Nick Pellyb24e11b2009-09-08 17:40:43 -0700847 return BluetoothDevice.ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849
Nick Pelly45e27042009-08-19 11:00:00 -0700850 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700851 public boolean setPin(byte[] pin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800853 return sService.setPin(this, true, pin.length, pin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 } catch (RemoteException e) {Log.e(TAG, "", e);}
855 return false;
856 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700857
Nick Pelly45e27042009-08-19 11:00:00 -0700858 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700859 public boolean setPasskey(int passkey) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800860 //TODO(BT)
861 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800863 return sService.setPasskey(this, true, 4, passkey);
864 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700865 return false;
866 }
867
Nick Pelly45e27042009-08-19 11:00:00 -0700868 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700869 public boolean setPairingConfirmation(boolean confirm) {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700870 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800871 return sService.setPairingConfirmation(this, confirm);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700872 } catch (RemoteException e) {Log.e(TAG, "", e);}
873 return false;
874 }
875
Nick Pelly45e27042009-08-19 11:00:00 -0700876 /** @hide */
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700877 public boolean setRemoteOutOfBandData() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800878 // TODO(BT)
879 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700880 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800881 return sService.setRemoteOutOfBandData(this);
882 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700883 return false;
884 }
885
886 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700887 public boolean cancelPairingUserInput() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800888 // TODO(BT)
889 /*
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700890 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800891 return sService.cancelPairingUserInput(this);
892 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 return false;
894 }
895
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800896 /** @hide */
897 public boolean isBluetoothDock() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800898 // TODO(BT)
899 /*
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800900 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800901 return sService.isBluetoothDock(this);
902 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800903 return false;
904 }
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700907 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
Nick Pelly16fb88a2009-10-07 07:44:03 +0200908 * outgoing connection to this remote device on given channel.
Nick Pelly45e27042009-08-19 11:00:00 -0700909 * <p>The remote device will be authenticated and communication on this
910 * socket will be encrypted.
Jaikumar Ganeshc8fa4ff2011-01-25 16:03:13 -0800911 * <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 Hambyf51eada2010-09-21 13:39:53 -0700920 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
Nick Pelly45e27042009-08-19 11:00:00 -0700921 * connection.
922 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pellycf440592009-09-08 10:12:06 -0700923 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellyde893f52009-09-08 13:15:33 -0700924 *
Nick Pelly45e27042009-08-19 11:00:00 -0700925 * @param channel RFCOMM channel to connect to
926 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
Nick Pellybd022f42009-08-14 18:33:38 -0700927 * @throws IOException on error, for example Bluetooth not available, or
Nick Pelly45e27042009-08-19 11:00:00 -0700928 * insufficient permissions
Nick Pelly16fb88a2009-10-07 07:44:03 +0200929 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700930 */
Nick Pelly45e27042009-08-19 11:00:00 -0700931 public BluetoothSocket createRfcommSocket(int channel) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200932 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 Hambyf51eada2010-09-21 13:39:53 -0700942 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
Nick Pelly16fb88a2009-10-07 07:44:03 +0200943 * 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 Ganeshc8fa4ff2011-01-25 16:03:13 -0800947 * <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 Pellyea5056e2010-02-24 11:19:10 -0800956 * <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 Pelly16fb88a2009-10-07 07:44:03 +0200960 * <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 Pellybd022f42009-08-14 18:33:38 -0700970 }
971
972 /**
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800973 * 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 Pellybd022f42009-08-14 18:33:38 -07001006 * 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 Pellye6ee3be2009-10-08 23:27:28 +02001011 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
1012 *
Nick Pellybd022f42009-08-14 18:33:38 -07001013 * @param port remote port
1014 * @return An RFCOMM BluetoothSocket
1015 * @throws IOException On error, for example Bluetooth not available, or
1016 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001017 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001018 */
1019 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +02001020 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port,
1021 null);
Nick Pellybd022f42009-08-14 18:33:38 -07001022 }
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 Pellye6ee3be2009-10-08 23:27:28 +02001027 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
1028 *
Nick Pellybd022f42009-08-14 18:33:38 -07001029 * @return a SCO BluetoothSocket
1030 * @throws IOException on error, for example Bluetooth not available, or
1031 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001032 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001033 */
1034 public BluetoothSocket createScoSocket() throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +02001035 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null);
Nick Pellybd022f42009-08-14 18:33:38 -07001036 }
1037
1038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 * Check that a pin is valid and convert to byte array.
1040 *
Jake Hambyf51eada2010-09-21 13:39:53 -07001041 * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 * @param pin pin as java String
Jake Hambyf51eada2010-09-21 13:39:53 -07001043 * @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 -08001044 * Bluetooth pin.
Nick Pelly45e27042009-08-19 11:00:00 -07001045 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static byte[] convertPinToBytes(String pin) {
1048 if (pin == null) {
1049 return null;
1050 }
1051 byte[] pinBytes;
1052 try {
Jake Hambyf51eada2010-09-21 13:39:53 -07001053 pinBytes = pin.getBytes("UTF-8");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 } catch (UnsupportedEncodingException uee) {
Jake Hambyf51eada2010-09-21 13:39:53 -07001055 Log.e(TAG, "UTF-8 not supported?!?"); // this should not happen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 return null;
1057 }
1058 if (pinBytes.length <= 0 || pinBytes.length > 16) {
1059 return null;
1060 }
1061 return pinBytes;
1062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064}