blob: 5c9e2ee4fc423cfded846e10d39646e5de7bbfca [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
Tor Norbye2d497522015-04-23 17:10:21 -070019import android.Manifest;
20import android.annotation.RequiresPermission;
Nick Pelly005b2282009-09-10 10:21:56 -070021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Andre Eisenbach2b8696e2015-02-05 20:06:33 -080023import android.annotation.SystemApi;
Matthew Xieddf7e472013-03-01 18:41:02 -080024import android.content.Context;
Nick Pellybd022f42009-08-14 18:33:38 -070025import android.os.Parcel;
26import android.os.Parcelable;
Nick Pellyaef439e2009-09-28 12:33:17 -070027import android.os.ParcelUuid;
Christine Hallstrom044b8f32015-11-17 12:24:07 -080028import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.RemoteException;
30import android.util.Log;
31
Nick Pellybd022f42009-08-14 18:33:38 -070032import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import java.io.UnsupportedEncodingException;
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -070034import java.util.UUID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36/**
Scott Main9fab0ae2009-11-03 18:17:59 -080037 * Represents a remote Bluetooth device. A {@link BluetoothDevice} lets you
Jake Hambyf51eada2010-09-21 13:39:53 -070038 * create a connection with the respective device or query information about
Scott Main9fab0ae2009-11-03 18:17:59 -080039 * it, such as the name, address, class, and bonding state.
Nick Pelly45e27042009-08-19 11:00:00 -070040 *
41 * <p>This class is really just a thin wrapper for a Bluetooth hardware
42 * address. Objects of this class are immutable. Operations on this class
43 * are performed on the remote Bluetooth hardware address, using the
44 * {@link BluetoothAdapter} that was used to create this {@link
45 * BluetoothDevice}.
Scott Main9fab0ae2009-11-03 18:17:59 -080046 *
47 * <p>To get a {@link BluetoothDevice}, use
48 * {@link BluetoothAdapter#getRemoteDevice(String)
49 * BluetoothAdapter.getRemoteDevice(String)} to create one representing a device
50 * of a known MAC address (which you can get through device discovery with
51 * {@link BluetoothAdapter}) or get one from the set of bonded devices
52 * returned by {@link BluetoothAdapter#getBondedDevices()
53 * BluetoothAdapter.getBondedDevices()}. You can then open a
Jake Hambyf51eada2010-09-21 13:39:53 -070054 * {@link BluetoothSocket} for communication with the remote device, using
Scott Main9fab0ae2009-11-03 18:17:59 -080055 * {@link #createRfcommSocketToServiceRecord(UUID)}.
56 *
57 * <p class="note"><strong>Note:</strong>
58 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
59 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080060 * <div class="special reference">
61 * <h3>Developer Guides</h3>
Hemal Patel65813df2016-08-17 13:18:14 -070062 * <p>
63 * For more information about using Bluetooth, read the <a href=
64 * "{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer
65 * guide.
66 * </p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080067 * </div>
68 *
Scott Main9fab0ae2009-11-03 18:17:59 -080069 * {@see BluetoothAdapter}
70 * {@see BluetoothSocket}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
Nick Pellybd022f42009-08-14 18:33:38 -070072public final class BluetoothDevice implements Parcelable {
73 private static final String TAG = "BluetoothDevice";
Ravi Nagarajan70772722012-04-23 04:06:49 -070074 private static final boolean DBG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Nick Pellyb24e11b2009-09-08 17:40:43 -070076 /**
Andre Eisenbach2b8696e2015-02-05 20:06:33 -080077 * Connection state bitmask as returned by getConnectionState.
78 */
79 private static final int CONNECTION_STATE_DISCONNECTED = 0;
80 private static final int CONNECTION_STATE_CONNECTED = 1;
81 private static final int CONNECTION_STATE_ENCRYPTED_BREDR = 2;
82 private static final int CONNECTION_STATE_ENCRYPTED_LE = 4;
83
84 /**
Nick Pellyb24e11b2009-09-08 17:40:43 -070085 * Sentinel error value for this class. Guaranteed to not equal any other
86 * integer constant in this class. Provided as a convenience for functions
87 * that require a sentinel error value, for example:
Nick Pelly005b2282009-09-10 10:21:56 -070088 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
89 * BluetoothDevice.ERROR)</code>
Nick Pellyb24e11b2009-09-08 17:40:43 -070090 */
Nick Pelly005b2282009-09-10 10:21:56 -070091 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070092
Nick Pelly005b2282009-09-10 10:21:56 -070093 /**
94 * Broadcast Action: Remote device discovered.
95 * <p>Sent when a remote device is found during discovery.
96 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
97 * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or
98 * {@link #EXTRA_RSSI} if they are available.
Fyodor Kupolov72f491d2015-07-14 11:38:58 -070099 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} and
100 * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} to receive.
Nick Pelly005b2282009-09-10 10:21:56 -0700101 */
102 // TODO: Change API to not broadcast RSSI if not available (incoming connection)
103 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
104 public static final String ACTION_FOUND =
105 "android.bluetooth.device.action.FOUND";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Nick Pelly005b2282009-09-10 10:21:56 -0700107 /**
108 * Broadcast Action: Remote device disappeared.
109 * <p>Sent when a remote device that was found in the last discovery is not
110 * found in the current discovery.
111 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
112 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
113 * @hide
114 */
115 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
116 public static final String ACTION_DISAPPEARED =
117 "android.bluetooth.device.action.DISAPPEARED";
118
119 /**
120 * Broadcast Action: Bluetooth class of a remote device has changed.
121 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
122 * #EXTRA_CLASS}.
123 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
Matthew Xieb30f91e2013-05-29 10:19:06 -0700124 * {@see BluetoothClass}
Nick Pelly005b2282009-09-10 10:21:56 -0700125 */
126 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
127 public static final String ACTION_CLASS_CHANGED =
128 "android.bluetooth.device.action.CLASS_CHANGED";
129
130 /**
131 * Broadcast Action: Indicates a low level (ACL) connection has been
132 * established with a remote device.
133 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
134 * <p>ACL connections are managed automatically by the Android Bluetooth
135 * stack.
136 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
137 */
138 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
139 public static final String ACTION_ACL_CONNECTED =
140 "android.bluetooth.device.action.ACL_CONNECTED";
141
142 /**
143 * Broadcast Action: Indicates that a low level (ACL) disconnection has
144 * been requested for a remote device, and it will soon be disconnected.
145 * <p>This is useful for graceful disconnection. Applications should use
146 * this intent as a hint to immediately terminate higher level connections
147 * (RFCOMM, L2CAP, or profile connections) to the remote device.
148 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
149 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
150 */
151 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
152 public static final String ACTION_ACL_DISCONNECT_REQUESTED =
153 "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED";
154
155 /**
156 * Broadcast Action: Indicates a low level (ACL) disconnection from a
157 * remote device.
158 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
159 * <p>ACL connections are managed automatically by the Android Bluetooth
160 * stack.
161 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
162 */
163 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
164 public static final String ACTION_ACL_DISCONNECTED =
165 "android.bluetooth.device.action.ACL_DISCONNECTED";
166
167 /**
168 * Broadcast Action: Indicates the friendly name of a remote device has
169 * been retrieved for the first time, or changed since the last retrieval.
170 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
171 * #EXTRA_NAME}.
172 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
173 */
174 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
175 public static final String ACTION_NAME_CHANGED =
176 "android.bluetooth.device.action.NAME_CHANGED";
177
178 /**
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700179 * Broadcast Action: Indicates the alias of a remote device has been
180 * changed.
181 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
182 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
183 *
184 * @hide
185 */
186 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
187 public static final String ACTION_ALIAS_CHANGED =
188 "android.bluetooth.device.action.ALIAS_CHANGED";
189
190 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700191 * Broadcast Action: Indicates a change in the bond state of a remote
192 * device. For example, if a device is bonded (paired).
193 * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link
194 * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}.
195 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
196 */
197 // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also
198 // contain a hidden extra field EXTRA_REASON with the result code.
199 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
200 public static final String ACTION_BOND_STATE_CHANGED =
201 "android.bluetooth.device.action.BOND_STATE_CHANGED";
202
203 /**
204 * Used as a Parcelable {@link BluetoothDevice} extra field in every intent
205 * broadcast by this class. It contains the {@link BluetoothDevice} that
206 * the intent applies to.
207 */
208 public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
209
210 /**
211 * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link
212 * #ACTION_FOUND} intents. It contains the friendly Bluetooth name.
213 */
214 public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME";
215
216 /**
217 * Used as an optional short extra field in {@link #ACTION_FOUND} intents.
218 * Contains the RSSI value of the remote device as reported by the
219 * Bluetooth hardware.
220 */
221 public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI";
222
223 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900224 * Used as a Parcelable {@link BluetoothClass} extra field in {@link
Nick Pelly005b2282009-09-10 10:21:56 -0700225 * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents.
226 */
227 public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS";
228
229 /**
230 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
231 * Contains the bond state of the remote device.
232 * <p>Possible values are:
233 * {@link #BOND_NONE},
234 * {@link #BOND_BONDING},
235 * {@link #BOND_BONDED}.
Matthew Xie091fc2b2013-09-23 23:23:13 -0700236 */
Nick Pelly005b2282009-09-10 10:21:56 -0700237 public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE";
238 /**
239 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
240 * Contains the previous bond state of the remote device.
241 * <p>Possible values are:
242 * {@link #BOND_NONE},
243 * {@link #BOND_BONDING},
244 * {@link #BOND_BONDED}.
Matthew Xie091fc2b2013-09-23 23:23:13 -0700245 */
Nick Pelly005b2282009-09-10 10:21:56 -0700246 public static final String EXTRA_PREVIOUS_BOND_STATE =
247 "android.bluetooth.device.extra.PREVIOUS_BOND_STATE";
248 /**
249 * Indicates the remote device is not bonded (paired).
250 * <p>There is no shared link key with the remote device, so communication
251 * (if it is allowed at all) will be unauthenticated and unencrypted.
252 */
253 public static final int BOND_NONE = 10;
254 /**
255 * Indicates bonding (pairing) is in progress with the remote device.
256 */
257 public static final int BOND_BONDING = 11;
258 /**
259 * Indicates the remote device is bonded (paired).
260 * <p>A shared link keys exists locally for the remote device, so
261 * communication can be authenticated and encrypted.
262 * <p><i>Being bonded (paired) with a remote device does not necessarily
Jake Hambyf51eada2010-09-21 13:39:53 -0700263 * mean the device is currently connected. It just means that the pending
264 * procedure was completed at some earlier time, and the link key is still
Nick Pelly005b2282009-09-10 10:21:56 -0700265 * stored locally, ready to use on the next connection.
266 * </i>
267 */
268 public static final int BOND_BONDED = 12;
269
Matthew Xie091fc2b2013-09-23 23:23:13 -0700270 /**
271 * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST}
272 * intents for unbond reason.
273 * @hide
274 */
Nick Pelly005b2282009-09-10 10:21:56 -0700275 public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON";
Matthew Xie091fc2b2013-09-23 23:23:13 -0700276
277 /**
278 * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST}
279 * intents to indicate pairing method used. Possible values are:
280 * {@link #PAIRING_VARIANT_PIN},
281 * {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION},
282 */
Nick Pelly005b2282009-09-10 10:21:56 -0700283 public static final String EXTRA_PAIRING_VARIANT =
284 "android.bluetooth.device.extra.PAIRING_VARIANT";
Matthew Xie091fc2b2013-09-23 23:23:13 -0700285
286 /**
287 * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST}
288 * intents as the value of passkey.
289 */
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800290 public static final String EXTRA_PAIRING_KEY = "android.bluetooth.device.extra.PAIRING_KEY";
Nick Pelly005b2282009-09-10 10:21:56 -0700291
292 /**
Matthew Xie33ec9842013-04-03 00:29:27 -0700293 * Bluetooth device type, Unknown
294 */
295 public static final int DEVICE_TYPE_UNKNOWN = 0;
296
297 /**
298 * Bluetooth device type, Classic - BR/EDR devices
299 */
300 public static final int DEVICE_TYPE_CLASSIC = 1;
301
302 /**
303 * Bluetooth device type, Low Energy - LE-only
304 */
305 public static final int DEVICE_TYPE_LE = 2;
306
307 /**
308 * Bluetooth device type, Dual Mode - BR/EDR/LE
309 */
310 public static final int DEVICE_TYPE_DUAL = 3;
311
Casper Bonde238e0f92015-04-09 09:24:48 +0200312
313 /** @hide */
314 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
315 public static final String ACTION_SDP_RECORD =
316 "android.bluetooth.device.action.SDP_RECORD";
317
Matthew Xie33ec9842013-04-03 00:29:27 -0700318 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700319 * Broadcast Action: This intent is used to broadcast the {@link UUID}
Nick Pellyaef439e2009-09-28 12:33:17 -0700320 * wrapped as a {@link android.os.ParcelUuid} of the remote device after it
321 * has been fetched. This intent is sent only when the UUIDs of the remote
322 * device are requested to be fetched using Service Discovery Protocol
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700323 * <p> Always contains the extra field {@link #EXTRA_DEVICE}
Matthew Xiead232102011-11-08 10:58:12 -0800324 * <p> Always contains the extra field {@link #EXTRA_UUID}
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700325 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700326 */
327 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
328 public static final String ACTION_UUID =
Matthew Xiead232102011-11-08 10:58:12 -0800329 "android.bluetooth.device.action.UUID";
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700330
Hemant Gupta8949bfb2013-08-16 14:57:55 +0530331 /** @hide */
332 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
333 public static final String ACTION_MAS_INSTANCE =
334 "android.bluetooth.device.action.MAS_INSTANCE";
335
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700336 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700337 * Broadcast Action: Indicates a failure to retrieve the name of a remote
338 * device.
339 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
340 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
341 * @hide
342 */
343 //TODO: is this actually useful?
344 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
345 public static final String ACTION_NAME_FAILED =
346 "android.bluetooth.device.action.NAME_FAILED";
347
Matthew Xie091fc2b2013-09-23 23:23:13 -0700348 /**
349 * Broadcast Action: This intent is used to broadcast PAIRING REQUEST
Matthew Xieac2c6c32013-10-21 14:56:33 -0700350 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} to
Edward Jee430e3612013-10-07 18:12:15 -0700351 * receive.
Matthew Xie091fc2b2013-09-23 23:23:13 -0700352 */
Nick Pelly005b2282009-09-10 10:21:56 -0700353 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
354 public static final String ACTION_PAIRING_REQUEST =
355 "android.bluetooth.device.action.PAIRING_REQUEST";
356 /** @hide */
357 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
358 public static final String ACTION_PAIRING_CANCEL =
359 "android.bluetooth.device.action.PAIRING_CANCEL";
Yue Lixina4433af2009-07-09 16:56:43 +0800360
Matthew Xiea0c68032011-06-25 21:47:07 -0700361 /** @hide */
362 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
363 public static final String ACTION_CONNECTION_ACCESS_REQUEST =
364 "android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST";
365
366 /** @hide */
367 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
368 public static final String ACTION_CONNECTION_ACCESS_REPLY =
369 "android.bluetooth.device.action.CONNECTION_ACCESS_REPLY";
370
371 /** @hide */
372 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
373 public static final String ACTION_CONNECTION_ACCESS_CANCEL =
374 "android.bluetooth.device.action.CONNECTION_ACCESS_CANCEL";
375
376 /**
377 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intent.
378 * @hide
379 */
380 public static final String EXTRA_ACCESS_REQUEST_TYPE =
381 "android.bluetooth.device.extra.ACCESS_REQUEST_TYPE";
382
383 /**@hide*/
384 public static final int REQUEST_TYPE_PROFILE_CONNECTION = 1;
385
386 /**@hide*/
387 public static final int REQUEST_TYPE_PHONEBOOK_ACCESS = 2;
388
Matthew Xiefe3807a2013-07-18 17:31:50 -0700389 /**@hide*/
390 public static final int REQUEST_TYPE_MESSAGE_ACCESS = 3;
391
Casper Bonde2a5f6082015-03-19 10:36:45 +0100392 /**@hide*/
393 public static final int REQUEST_TYPE_SIM_ACCESS = 4;
394
Matthew Xiea0c68032011-06-25 21:47:07 -0700395 /**
396 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents,
397 * Contains package name to return reply intent to.
398 * @hide
399 */
400 public static final String EXTRA_PACKAGE_NAME = "android.bluetooth.device.extra.PACKAGE_NAME";
401
402 /**
403 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents,
404 * Contains class name to return reply intent to.
405 * @hide
406 */
407 public static final String EXTRA_CLASS_NAME = "android.bluetooth.device.extra.CLASS_NAME";
408
409 /**
410 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intent.
411 * @hide
412 */
413 public static final String EXTRA_CONNECTION_ACCESS_RESULT =
414 "android.bluetooth.device.extra.CONNECTION_ACCESS_RESULT";
415
416 /**@hide*/
417 public static final int CONNECTION_ACCESS_YES = 1;
418
419 /**@hide*/
420 public static final int CONNECTION_ACCESS_NO = 2;
421
422 /**
423 * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intents,
424 * Contains boolean to indicate if the allowed response is once-for-all so that
425 * next request will be granted without asking user again.
426 * @hide
427 */
428 public static final String EXTRA_ALWAYS_ALLOWED =
429 "android.bluetooth.device.extra.ALWAYS_ALLOWED";
430
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800431 /**
432 * A bond attempt succeeded
433 * @hide
434 */
Nick Pellyb24e11b2009-09-08 17:40:43 -0700435 public static final int BOND_SUCCESS = 0;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800436
437 /**
438 * A bond attempt failed because pins did not match, or remote device did
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700439 * not respond to pin request in time
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800440 * @hide
441 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 public static final int UNBOND_REASON_AUTH_FAILED = 1;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800443
444 /**
445 * A bond attempt failed because the other side explicitly rejected
Nick Pelly45e27042009-08-19 11:00:00 -0700446 * bonding
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800447 * @hide
448 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 public static final int UNBOND_REASON_AUTH_REJECTED = 2;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800450
451 /**
452 * A bond attempt failed because we canceled the bonding process
453 * @hide
454 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public static final int UNBOND_REASON_AUTH_CANCELED = 3;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800456
457 /**
458 * A bond attempt failed because we could not contact the remote device
459 * @hide
460 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800462
463 /**
464 * A bond attempt failed because a discovery is in progress
465 * @hide
466 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800468
469 /**
470 * A bond attempt failed because of authentication timeout
471 * @hide
472 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700473 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800474
475 /**
476 * A bond attempt failed because of repeated attempts
477 * @hide
478 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700479 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800480
481 /**
482 * A bond attempt failed because we received an Authentication Cancel
483 * by remote end
484 * @hide
485 */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700486 public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800487
488 /**
489 * An existing bond was explicitly revoked
490 * @hide
491 */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700492 public static final int UNBOND_REASON_REMOVED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800494 /**
Matthew Xie091fc2b2013-09-23 23:23:13 -0700495 * The user will be prompted to enter a pin or
Matthew Xieac2c6c32013-10-21 14:56:33 -0700496 * an app will enter a pin for user.
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800497 */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700498 public static final int PAIRING_VARIANT_PIN = 0;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800499
500 /**
501 * The user will be prompted to enter a passkey
502 * @hide
503 */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700504 public static final int PAIRING_VARIANT_PASSKEY = 1;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800505
506 /**
Matthew Xie091fc2b2013-09-23 23:23:13 -0700507 * The user will be prompted to confirm the passkey displayed on the screen or
Matthew Xieac2c6c32013-10-21 14:56:33 -0700508 * an app will confirm the passkey for the user.
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800509 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700510 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800511
512 /**
513 * The user will be prompted to accept or deny the incoming pairing request
514 * @hide
515 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700516 public static final int PAIRING_VARIANT_CONSENT = 3;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800517
518 /**
519 * The user will be prompted to enter the passkey displayed on remote device
520 * This is used for Bluetooth 2.1 pairing.
521 * @hide
522 */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700523 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4;
Jaikumar Ganeshc88b0c62011-01-05 13:49:00 -0800524
525 /**
526 * The user will be prompted to enter the PIN displayed on remote device.
527 * This is used for Bluetooth 2.0 pairing.
528 * @hide
529 */
530 public static final int PAIRING_VARIANT_DISPLAY_PIN = 5;
531
532 /**
533 * The user will be prompted to accept or deny the OOB pairing request
534 * @hide
535 */
536 public static final int PAIRING_VARIANT_OOB_CONSENT = 6;
537
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700538 /**
Casper Bonde3b3d1fe2015-05-08 14:32:24 +0200539 * The user will be prompted to enter a 16 digit pin or
540 * an app will enter a 16 digit pin for user.
541 * @hide
542 */
543 public static final int PAIRING_VARIANT_PIN_16_DIGITS = 7;
544
545 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700546 * Used as an extra field in {@link #ACTION_UUID} intents,
Nick Pellyaef439e2009-09-28 12:33:17 -0700547 * Contains the {@link android.os.ParcelUuid}s of the remote device which
548 * is a parcelable version of {@link UUID}.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700549 */
550 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID";
551
Casper Bonde238e0f92015-04-09 09:24:48 +0200552 /** @hide */
553 public static final String EXTRA_SDP_RECORD =
554 "android.bluetooth.device.extra.SDP_RECORD";
555
556 /** @hide */
557 public static final String EXTRA_SDP_SEARCH_STATUS =
558 "android.bluetooth.device.extra.SDP_SEARCH_STATUS";
Edward Jee8dd30aa2014-09-05 00:29:14 -0700559 /**
560 * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission},
561 * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}.
562 * @hide
563 */
564 public static final int ACCESS_UNKNOWN = 0;
565
566 /**
567 * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission},
568 * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}.
569 * @hide
570 */
571 public static final int ACCESS_ALLOWED = 1;
572
573 /**
574 * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission},
575 * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}.
576 * @hide
577 */
578 public static final int ACCESS_REJECTED = 2;
579
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700580 /**
581 * No preferrence of physical transport for GATT connections to remote dual-mode devices
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700582 */
583 public static final int TRANSPORT_AUTO = 0;
584
585 /**
586 * Prefer BR/EDR transport for GATT connections to remote dual-mode devices
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700587 */
Hemant Gupta8949bfb2013-08-16 14:57:55 +0530588 public static final int TRANSPORT_BREDR = 1;
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700589
590 /**
591 * Prefer LE transport for GATT connections to remote dual-mode devices
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700592 */
Hemant Gupta8949bfb2013-08-16 14:57:55 +0530593 public static final int TRANSPORT_LE = 2;
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700594
Hemant Gupta8949bfb2013-08-16 14:57:55 +0530595 /** @hide */
596 public static final String EXTRA_MAS_INSTANCE =
597 "android.bluetooth.device.extra.MAS_INSTANCE";
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700598
Nick Pelly16fb88a2009-10-07 07:44:03 +0200599 /**
600 * Lazy initialization. Guaranteed final after first object constructed, or
601 * getService() called.
602 * TODO: Unify implementation of sService amongst BluetoothFoo API's
603 */
604 private static IBluetooth sService;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700605
Nick Pellybd022f42009-08-14 18:33:38 -0700606 private final String mAddress;
607
Nick Pelly16fb88a2009-10-07 07:44:03 +0200608 /*package*/ static IBluetooth getService() {
609 synchronized (BluetoothDevice.class) {
610 if (sService == null) {
fredc0f420372012-04-12 00:02:00 -0700611 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
fredc903ac6f2012-04-24 03:59:57 -0700612 sService = adapter.getBluetoothService(mStateChangeCallback);
Nick Pelly16fb88a2009-10-07 07:44:03 +0200613 }
614 }
615 return sService;
616 }
617
fredc903ac6f2012-04-24 03:59:57 -0700618 static IBluetoothManagerCallback mStateChangeCallback = new IBluetoothManagerCallback.Stub() {
619
620 public void onBluetoothServiceUp(IBluetooth bluetoothService)
621 throws RemoteException {
622 synchronized (BluetoothDevice.class) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800623 if (sService == null) {
624 sService = bluetoothService;
625 }
fredc903ac6f2012-04-24 03:59:57 -0700626 }
627 }
628
629 public void onBluetoothServiceDown()
630 throws RemoteException {
631 synchronized (BluetoothDevice.class) {
632 sService = null;
633 }
634 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800635
636 public void onBrEdrDown()
637 {
638 if (DBG) Log.d(TAG, "onBrEdrDown: reached BLE ON state");
639 }
fredc903ac6f2012-04-24 03:59:57 -0700640 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 /**
Nick Pellybd022f42009-08-14 18:33:38 -0700642 * Create a new BluetoothDevice
643 * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB",
644 * and is validated in this constructor.
645 * @param address valid Bluetooth MAC address
646 * @throws RuntimeException Bluetooth is not available on this platform
647 * @throws IllegalArgumentException address is invalid
648 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 */
Nick Pellybd022f42009-08-14 18:33:38 -0700650 /*package*/ BluetoothDevice(String address) {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200651 getService(); // ensures sService is initialized
Nick Pelly005b2282009-09-10 10:21:56 -0700652 if (!BluetoothAdapter.checkBluetoothAddress(address)) {
Nick Pellybd022f42009-08-14 18:33:38 -0700653 throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
654 }
655
656 mAddress = address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 }
658
Nick Pellybd022f42009-08-14 18:33:38 -0700659 @Override
660 public boolean equals(Object o) {
661 if (o instanceof BluetoothDevice) {
662 return mAddress.equals(((BluetoothDevice)o).getAddress());
663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 return false;
665 }
666
Nick Pellybd022f42009-08-14 18:33:38 -0700667 @Override
668 public int hashCode() {
669 return mAddress.hashCode();
The Android Open Source Project10592532009-03-18 17:39:46 -0700670 }
671
Nick Pelly45e27042009-08-19 11:00:00 -0700672 /**
673 * Returns a string representation of this BluetoothDevice.
674 * <p>Currently this is the Bluetooth hardware address, for example
675 * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress}
676 * if you explicitly require the Bluetooth hardware address in case the
677 * {@link #toString} representation changes in the future.
678 * @return string representation of this BluetoothDevice
679 */
Nick Pellybd022f42009-08-14 18:33:38 -0700680 @Override
681 public String toString() {
682 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684
Nick Pellybd022f42009-08-14 18:33:38 -0700685 public int describeContents() {
686 return 0;
687 }
688
689 public static final Parcelable.Creator<BluetoothDevice> CREATOR =
690 new Parcelable.Creator<BluetoothDevice>() {
691 public BluetoothDevice createFromParcel(Parcel in) {
692 return new BluetoothDevice(in.readString());
693 }
694 public BluetoothDevice[] newArray(int size) {
695 return new BluetoothDevice[size];
696 }
697 };
698
699 public void writeToParcel(Parcel out, int flags) {
700 out.writeString(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702
Nick Pelly45e27042009-08-19 11:00:00 -0700703 /**
704 * Returns the hardware address of this BluetoothDevice.
705 * <p> For example, "00:11:22:AA:BB:CC".
706 * @return Bluetooth hardware address as string
707 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 public String getAddress() {
fredc0f420372012-04-12 00:02:00 -0700709 if (DBG) Log.d(TAG, "mAddress: " + mAddress);
Nick Pellybd022f42009-08-14 18:33:38 -0700710 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700714 * Get the friendly Bluetooth name of the remote device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 *
Nick Pelly45e27042009-08-19 11:00:00 -0700716 * <p>The local adapter will automatically retrieve remote names when
717 * performing a device scan, and will cache them. This method just returns
718 * the name for this device from the cache.
Nick Pellyde893f52009-09-08 13:15:33 -0700719 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 *
721 * @return the Bluetooth name, or null if there was a problem.
722 */
Tor Norbye2d497522015-04-23 17:10:21 -0700723 @RequiresPermission(Manifest.permission.BLUETOOTH)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 public String getName() {
fredc0f420372012-04-12 00:02:00 -0700725 if (sService == null) {
726 Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
727 return null;
728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800730 return sService.getRemoteName(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 } catch (RemoteException e) {Log.e(TAG, "", e);}
732 return null;
733 }
734
735 /**
Matthew Xie33ec9842013-04-03 00:29:27 -0700736 * Get the Bluetooth device type of the remote device.
737 *
738 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
739 *
740 * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE}
741 * {@link #DEVICE_TYPE_DUAL}.
742 * {@link #DEVICE_TYPE_UNKNOWN} if it's not available
743 */
Tor Norbye2d497522015-04-23 17:10:21 -0700744 @RequiresPermission(Manifest.permission.BLUETOOTH)
Matthew Xie33ec9842013-04-03 00:29:27 -0700745 public int getType() {
746 if (sService == null) {
747 Log.e(TAG, "BT not enabled. Cannot get Remote Device type");
748 return DEVICE_TYPE_UNKNOWN;
749 }
750 try {
751 return sService.getRemoteType(this);
752 } catch (RemoteException e) {Log.e(TAG, "", e);}
753 return DEVICE_TYPE_UNKNOWN;
754 }
755
756 /**
Matthew Xie269e81a2011-07-26 18:36:49 -0700757 * Get the Bluetooth alias of the remote device.
758 * <p>Alias is the locally modified name of a remote device.
759 *
760 * @return the Bluetooth alias, or null if no alias or there was a problem
761 * @hide
762 */
763 public String getAlias() {
fredc0f420372012-04-12 00:02:00 -0700764 if (sService == null) {
765 Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias");
766 return null;
767 }
Matthew Xie269e81a2011-07-26 18:36:49 -0700768 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800769 return sService.getRemoteAlias(this);
Matthew Xie269e81a2011-07-26 18:36:49 -0700770 } catch (RemoteException e) {Log.e(TAG, "", e);}
771 return null;
772 }
773
774 /**
775 * Set the Bluetooth alias of the remote device.
776 * <p>Alias is the locally modified name of a remote device.
777 * <p>This methoid overwrites the alias. The changed
778 * alias is saved in the local storage so that the change
779 * is preserved over power cycle.
780 *
781 * @return true on success, false on error
782 * @hide
783 */
784 public boolean setAlias(String alias) {
fredc0f420372012-04-12 00:02:00 -0700785 if (sService == null) {
786 Log.e(TAG, "BT not enabled. Cannot set Remote Device name");
787 return false;
788 }
Matthew Xie269e81a2011-07-26 18:36:49 -0700789 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800790 return sService.setRemoteAlias(this, alias);
Matthew Xie269e81a2011-07-26 18:36:49 -0700791 } catch (RemoteException e) {Log.e(TAG, "", e);}
792 return false;
793 }
794
795 /**
796 * Get the Bluetooth alias of the remote device.
797 * If Alias is null, get the Bluetooth name instead.
798 * @see #getAlias()
799 * @see #getName()
800 *
801 * @return the Bluetooth alias, or null if no alias or there was a problem
802 * @hide
803 */
804 public String getAliasName() {
805 String name = getAlias();
806 if (name == null) {
807 name = getName();
808 }
809 return name;
810 }
811
812 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700813 * Start the bonding (pairing) process with the remote device.
814 * <p>This is an asynchronous call, it will return immediately. Register
815 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
816 * the bonding process completes, and its result.
817 * <p>Android system services will handle the necessary user interactions
818 * to confirm and complete the bonding process.
Matthew Xieac2c6c32013-10-21 14:56:33 -0700819 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 *
Nick Pelly005b2282009-09-10 10:21:56 -0700821 * @return false on immediate error, true if bonding will begin
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 */
Tor Norbye2d497522015-04-23 17:10:21 -0700823 @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
Nick Pellybd022f42009-08-14 18:33:38 -0700824 public boolean createBond() {
fredc0f420372012-04-12 00:02:00 -0700825 if (sService == null) {
826 Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
827 return false;
828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 try {
Christine Hallstrom044b8f32015-11-17 12:24:07 -0800830 Log.i(TAG, "createBond() for device " + getAddress() +
831 " called by pid: " + Process.myPid() +
832 " tid: " + Process.myTid());
Andre Eisenbach57210c72014-08-04 17:51:43 -0700833 return sService.createBond(this, TRANSPORT_AUTO);
834 } catch (RemoteException e) {Log.e(TAG, "", e);}
835 return false;
836 }
837
838 /**
839 * Start the bonding (pairing) process with the remote device using the
840 * specified transport.
841 *
842 * <p>This is an asynchronous call, it will return immediately. Register
843 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
844 * the bonding process completes, and its result.
845 * <p>Android system services will handle the necessary user interactions
846 * to confirm and complete the bonding process.
847 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
848 *
849 * @param transport The transport to use for the pairing procedure.
850 * @return false on immediate error, true if bonding will begin
851 * @throws IllegalArgumentException if an invalid transport was specified
852 * @hide
853 */
854 public boolean createBond(int transport) {
855 if (sService == null) {
856 Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
857 return false;
858 }
859 if (TRANSPORT_AUTO > transport || transport > TRANSPORT_LE)
860 {
861 throw new IllegalArgumentException(transport + " is not a valid Bluetooth transport");
862 }
863 try {
Christine Hallstrom044b8f32015-11-17 12:24:07 -0800864 Log.i(TAG, "createBond() for device " + getAddress() +
865 " called by pid: " + Process.myPid() +
866 " tid: " + Process.myTid());
Andre Eisenbach57210c72014-08-04 17:51:43 -0700867 return sService.createBond(this, transport);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 } catch (RemoteException e) {Log.e(TAG, "", e);}
869 return false;
870 }
871
872 /**
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700873 * Start the bonding (pairing) process with the remote device using the
874 * Out Of Band mechanism.
875 *
876 * <p>This is an asynchronous call, it will return immediately. Register
877 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
878 * the bonding process completes, and its result.
879 *
880 * <p>Android system services will handle the necessary user interactions
881 * to confirm and complete the bonding process.
882 *
883 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
884 *
Jakub Pawlowski2fc6e6b2015-12-29 13:19:21 -0800885 * @param transport - Transport to use
886 * @param oobData - Out Of Band data
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700887 * @return false on immediate error, true if bonding will begin
888 *
889 * @hide
890 */
Jakub Pawlowski2fc6e6b2015-12-29 13:19:21 -0800891 public boolean createBondOutOfBand(int transport, OobData oobData) {
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700892 try {
Jakub Pawlowski2fc6e6b2015-12-29 13:19:21 -0800893 return sService.createBondOutOfBand(this, transport, oobData);
894 } catch (RemoteException e) {Log.e(TAG, "", e);}
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700895 return false;
896 }
897
Jakub Pawlowski0278ab92016-07-20 11:55:48 -0700898 /** @hide */
899 public boolean isBondingInitiatedLocally() {
900 try {
901 return sService.isBondingInitiatedLocally(this);
902 } catch (RemoteException e) {Log.e(TAG, "", e);}
903 return false;
904 }
905
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700906 /**
907 * Set the Out Of Band data for a remote device to be used later
908 * in the pairing mechanism. Users can obtain this data through other
909 * trusted channels
910 *
911 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
912 *
913 * @param hash Simple Secure pairing hash
914 * @param randomizer The random key obtained using OOB
915 * @return false on error; true otherwise
916 *
917 * @hide
918 */
919 public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800920 //TODO(BT)
921 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700922 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800923 return sService.setDeviceOutOfBandData(this, hash, randomizer);
924 } catch (RemoteException e) {Log.e(TAG, "", e);} */
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -0700925 return false;
926 }
927
928 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700929 * Cancel an in-progress bonding request started with {@link #createBond}.
930 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
931 *
Jake Hambyf51eada2010-09-21 13:39:53 -0700932 * @return true on success, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700933 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 */
Nick Pellybd022f42009-08-14 18:33:38 -0700935 public boolean cancelBondProcess() {
fredc0f420372012-04-12 00:02:00 -0700936 if (sService == null) {
937 Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
938 return false;
939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 try {
Christine Hallstrom044b8f32015-11-17 12:24:07 -0800941 Log.i(TAG, "cancelBondProcess() for device " + getAddress() +
942 " called by pid: " + Process.myPid() +
943 " tid: " + Process.myTid());
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800944 return sService.cancelBondProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 } catch (RemoteException e) {Log.e(TAG, "", e);}
946 return false;
947 }
948
949 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700950 * Remove bond (pairing) with the remote device.
951 * <p>Delete the link key associated with the remote device, and
952 * immediately terminate connections to that device that require
953 * authentication and encryption.
954 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 *
Jake Hambyf51eada2010-09-21 13:39:53 -0700956 * @return true on success, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700957 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 */
Nick Pellybd022f42009-08-14 18:33:38 -0700959 public boolean removeBond() {
fredc0f420372012-04-12 00:02:00 -0700960 if (sService == null) {
961 Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond");
962 return false;
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 try {
Christine Hallstrom044b8f32015-11-17 12:24:07 -0800965 Log.i(TAG, "removeBond() for device " + getAddress() +
966 " called by pid: " + Process.myPid() +
967 " tid: " + Process.myTid());
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800968 return sService.removeBond(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellybd022f42009-08-14 18:33:38 -0700970 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
972
973 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700974 * Get the bond state of the remote device.
975 * <p>Possible values for the bond state are:
976 * {@link #BOND_NONE},
977 * {@link #BOND_BONDING},
978 * {@link #BOND_BONDED}.
979 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 *
Nick Pelly005b2282009-09-10 10:21:56 -0700981 * @return the bond state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 */
Tor Norbye2d497522015-04-23 17:10:21 -0700983 @RequiresPermission(Manifest.permission.BLUETOOTH)
Nick Pellybd022f42009-08-14 18:33:38 -0700984 public int getBondState() {
fredc0f420372012-04-12 00:02:00 -0700985 if (sService == null) {
986 Log.e(TAG, "BT not enabled. Cannot get bond state");
987 return BOND_NONE;
988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800990 return sService.getBondState(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 } catch (RemoteException e) {Log.e(TAG, "", e);}
Syed Ibrahim M305f2402012-06-19 10:14:25 -0700992 catch (NullPointerException npe) {
993 // Handle case where bluetooth service proxy
994 // is already null.
995 Log.e(TAG, "NullPointerException for getBondState() of device ("+
996 getAddress()+")", npe);
997 }
Nick Pelly005b2282009-09-10 10:21:56 -0700998 return BOND_NONE;
999 }
1000
1001 /**
Jay Civelli174928c2014-05-08 09:24:08 -07001002 * Returns whether there is an open connection to this device.
1003 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
1004 *
1005 * @return True if there is at least one open connection to this device.
1006 * @hide
1007 */
Andre Eisenbach2b8696e2015-02-05 20:06:33 -08001008 @SystemApi
Jay Civelli174928c2014-05-08 09:24:08 -07001009 public boolean isConnected() {
1010 if (sService == null) {
1011 // BT is not enabled, we cannot be connected.
1012 return false;
1013 }
1014 try {
Andre Eisenbach2b8696e2015-02-05 20:06:33 -08001015 return sService.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED;
1016 } catch (RemoteException e) {
1017 Log.e(TAG, "", e);
1018 return false;
1019 }
1020 }
1021
1022 /**
1023 * Returns whether there is an open connection to this device
1024 * that has been encrypted.
1025 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
1026 *
1027 * @return True if there is at least one encrypted connection to this device.
1028 * @hide
1029 */
1030 @SystemApi
1031 public boolean isEncrypted() {
1032 if (sService == null) {
1033 // BT is not enabled, we cannot be connected.
1034 return false;
1035 }
1036 try {
1037 return sService.getConnectionState(this) > CONNECTION_STATE_CONNECTED;
Jay Civelli174928c2014-05-08 09:24:08 -07001038 } catch (RemoteException e) {
1039 Log.e(TAG, "", e);
1040 return false;
1041 }
1042 }
1043
1044 /**
Nick Pelly005b2282009-09-10 10:21:56 -07001045 * Get the Bluetooth class of the remote device.
1046 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
1047 *
1048 * @return Bluetooth class object, or null on error
1049 */
Tor Norbye2d497522015-04-23 17:10:21 -07001050 @RequiresPermission(Manifest.permission.BLUETOOTH)
Nick Pelly005b2282009-09-10 10:21:56 -07001051 public BluetoothClass getBluetoothClass() {
fredc0f420372012-04-12 00:02:00 -07001052 if (sService == null) {
1053 Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class");
1054 return null;
1055 }
Nick Pelly005b2282009-09-10 10:21:56 -07001056 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001057 int classInt = sService.getRemoteClass(this);
Nick Pelly005b2282009-09-10 10:21:56 -07001058 if (classInt == BluetoothClass.ERROR) return null;
1059 return new BluetoothClass(classInt);
1060 } catch (RemoteException e) {Log.e(TAG, "", e);}
1061 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063
Lixin Yueefa1dd72009-08-31 15:55:13 +08001064 /**
Matthew Xiead232102011-11-08 10:58:12 -08001065 * Returns the supported features (UUIDs) of the remote device.
1066 *
1067 * <p>This method does not start a service discovery procedure to retrieve the UUIDs
1068 * from the remote device. Instead, the local cached copy of the service
1069 * UUIDs are returned.
1070 * <p>Use {@link #fetchUuidsWithSdp} if fresh UUIDs are desired.
1071 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
1072 *
1073 * @return the supported features (UUIDs) of the remote device,
1074 * or null on error
1075 */
Tor Norbye2d497522015-04-23 17:10:21 -07001076 @RequiresPermission(Manifest.permission.BLUETOOTH)
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -07001077 public ParcelUuid[] getUuids() {
Nitin Arorad055adb2015-03-02 15:03:51 -08001078 if (sService == null || isBluetoothEnabled() == false) {
fredc0f420372012-04-12 00:02:00 -07001079 Log.e(TAG, "BT not enabled. Cannot get remote device Uuids");
1080 return null;
1081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001083 return sService.getRemoteUuids(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 } catch (RemoteException e) {Log.e(TAG, "", e);}
1085 return null;
1086 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -07001087
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001088 /**
Matthew Xiead232102011-11-08 10:58:12 -08001089 * Perform a service discovery on the remote device to get the UUIDs supported.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001090 *
Matthew Xiead232102011-11-08 10:58:12 -08001091 * <p>This API is asynchronous and {@link #ACTION_UUID} intent is sent,
1092 * with the UUIDs supported by the remote end. If there is an error
1093 * in getting the SDP records or if the process takes a long time,
1094 * {@link #ACTION_UUID} intent is sent with the UUIDs that is currently
1095 * present in the cache. Clients should use the {@link #getUuids} to get UUIDs
1096 * if service discovery is not to be performed.
1097 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
1098 *
1099 * @return False if the sanity check fails, True if the process
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001100 * of initiating an ACL connection to the remote device
1101 * was started.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001102 */
Tor Norbye2d497522015-04-23 17:10:21 -07001103 @RequiresPermission(Manifest.permission.BLUETOOTH)
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001104 public boolean fetchUuidsWithSdp() {
Zhihai Xu5d04f1c2014-01-13 16:54:38 -08001105 IBluetooth service = sService;
Nitin Arorad055adb2015-03-02 15:03:51 -08001106 if (service == null || isBluetoothEnabled() == false) {
Zhihai Xu5d04f1c2014-01-13 16:54:38 -08001107 Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp");
1108 return false;
1109 }
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001110 try {
Zhihai Xu5d04f1c2014-01-13 16:54:38 -08001111 return service.fetchRemoteUuids(this);
fredc4c9caca2012-04-06 02:08:46 -07001112 } catch (RemoteException e) {Log.e(TAG, "", e);}
1113 return false;
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -07001114 }
1115
Casper Bonde238e0f92015-04-09 09:24:48 +02001116 /**
1117 * Perform a service discovery on the remote device to get the SDP records associated
1118 * with the specified UUID.
1119 *
1120 * <p>This API is asynchronous and {@link #ACTION_SDP_RECORD} intent is sent,
1121 * with the SDP records found on the remote end. If there is an error
1122 * in getting the SDP records or if the process takes a long time,
1123 * {@link #ACTION_SDP_RECORD} intent is sent with an status value in
1124 * {@link #EXTRA_SDP_SEARCH_STATUS} different from 0.
1125 * Detailed status error codes can be found by members of the Bluetooth package in
1126 * the AbstractionLayer class.
1127 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
1128 * The SDP record data will be stored in the intent as {@link #EXTRA_SDP_RECORD}.
1129 * The object type will match one of the SdpXxxRecord types, depending on the UUID searched
1130 * for.
1131 *
1132 * @return False if the sanity check fails, True if the process
1133 * of initiating an ACL connection to the remote device
1134 * was started.
1135 */
Hemant Gupta8949bfb2013-08-16 14:57:55 +05301136 /** @hide */
Casper Bonde238e0f92015-04-09 09:24:48 +02001137 public boolean sdpSearch(ParcelUuid uuid) {
Hemant Gupta8949bfb2013-08-16 14:57:55 +05301138 if (sService == null) {
Casper Bonde238e0f92015-04-09 09:24:48 +02001139 Log.e(TAG, "BT not enabled. Cannot query remote device sdp records");
Hemant Gupta8949bfb2013-08-16 14:57:55 +05301140 return false;
1141 }
1142 try {
Casper Bonde238e0f92015-04-09 09:24:48 +02001143 return sService.sdpSearch(this,uuid);
Hemant Gupta8949bfb2013-08-16 14:57:55 +05301144 } catch (RemoteException e) {Log.e(TAG, "", e);}
1145 return false;
1146 }
1147
Matthew Xie091fc2b2013-09-23 23:23:13 -07001148 /**
1149 * Set the pin during pairing when the pairing method is {@link #PAIRING_VARIANT_PIN}
Matthew Xieac2c6c32013-10-21 14:56:33 -07001150 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Matthew Xie091fc2b2013-09-23 23:23:13 -07001151 *
1152 * @return true pin has been set
1153 * false for error
1154 */
Nick Pellybd022f42009-08-14 18:33:38 -07001155 public boolean setPin(byte[] pin) {
fredc0f420372012-04-12 00:02:00 -07001156 if (sService == null) {
1157 Log.e(TAG, "BT not enabled. Cannot set Remote Device pin");
1158 return false;
1159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001161 return sService.setPin(this, true, pin.length, pin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 } catch (RemoteException e) {Log.e(TAG, "", e);}
1163 return false;
1164 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -07001165
Nick Pelly45e27042009-08-19 11:00:00 -07001166 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -07001167 public boolean setPasskey(int passkey) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001168 //TODO(BT)
1169 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001171 return sService.setPasskey(this, true, 4, passkey);
1172 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -07001173 return false;
1174 }
1175
Matthew Xie091fc2b2013-09-23 23:23:13 -07001176 /**
1177 * Confirm passkey for {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION} pairing.
Marie Janssen93326cf2016-08-09 13:23:39 -07001178 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}.
Matthew Xie091fc2b2013-09-23 23:23:13 -07001179 *
1180 * @return true confirmation has been sent out
1181 * false for error
1182 */
Marie Janssen93326cf2016-08-09 13:23:39 -07001183 @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
Nick Pellybd022f42009-08-14 18:33:38 -07001184 public boolean setPairingConfirmation(boolean confirm) {
fredc0f420372012-04-12 00:02:00 -07001185 if (sService == null) {
1186 Log.e(TAG, "BT not enabled. Cannot set pairing confirmation");
1187 return false;
1188 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -07001189 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001190 return sService.setPairingConfirmation(this, confirm);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -07001191 } catch (RemoteException e) {Log.e(TAG, "", e);}
1192 return false;
1193 }
1194
Nick Pelly45e27042009-08-19 11:00:00 -07001195 /** @hide */
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001196 public boolean setRemoteOutOfBandData() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001197 // TODO(BT)
1198 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001199 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001200 return sService.setRemoteOutOfBandData(this);
1201 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001202 return false;
1203 }
1204
1205 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -07001206 public boolean cancelPairingUserInput() {
fredc0f420372012-04-12 00:02:00 -07001207 if (sService == null) {
1208 Log.e(TAG, "BT not enabled. Cannot create pairing user input");
1209 return false;
1210 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -07001211 try {
Ravi Nagarajan919a4c62012-04-13 21:18:16 +05301212 return sService.cancelBondProcess(this);
Priti Agheradb44b202012-03-13 10:41:41 -07001213 } catch (RemoteException e) {Log.e(TAG, "", e);}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 return false;
1215 }
1216
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -08001217 /** @hide */
1218 public boolean isBluetoothDock() {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001219 // TODO(BT)
1220 /*
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -08001221 try {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001222 return sService.isBluetoothDock(this);
1223 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -08001224 return false;
1225 }
1226
Nitin Arorad055adb2015-03-02 15:03:51 -08001227 boolean isBluetoothEnabled() {
1228 boolean ret = false;
1229 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
1230 if (adapter != null && adapter.isEnabled() == true) {
1231 ret = true;
1232 }
1233 return ret;
1234 }
1235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
Edward Jee8dd30aa2014-09-05 00:29:14 -07001237 * Requires {@link android.Manifest.permission#BLUETOOTH}.
1238 * @return Whether the phonebook access is allowed to this device. Can be
1239 * {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}.
1240 * @hide
1241 */
1242 public int getPhonebookAccessPermission() {
1243 if (sService == null) {
1244 return ACCESS_UNKNOWN;
1245 }
1246 try {
1247 return sService.getPhonebookAccessPermission(this);
1248 } catch (RemoteException e) {
1249 Log.e(TAG, "", e);
1250 }
1251 return ACCESS_UNKNOWN;
1252 }
1253
1254 /**
1255 * Sets whether the phonebook access is allowed to this device.
1256 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}.
1257 * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or
1258 * {@link #ACCESS_REJECTED}.
1259 * @return Whether the value has been successfully set.
1260 * @hide
1261 */
1262 public boolean setPhonebookAccessPermission(int value) {
1263 if (sService == null) {
1264 return false;
1265 }
1266 try {
1267 return sService.setPhonebookAccessPermission(this, value);
1268 } catch (RemoteException e) {
1269 Log.e(TAG, "", e);
1270 }
1271 return false;
1272 }
1273
1274 /**
1275 * Requires {@link android.Manifest.permission#BLUETOOTH}.
1276 * @return Whether the message access is allowed to this device. Can be
1277 * {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}.
1278 * @hide
1279 */
1280 public int getMessageAccessPermission() {
1281 if (sService == null) {
1282 return ACCESS_UNKNOWN;
1283 }
1284 try {
1285 return sService.getMessageAccessPermission(this);
1286 } catch (RemoteException e) {
1287 Log.e(TAG, "", e);
1288 }
1289 return ACCESS_UNKNOWN;
1290 }
1291
1292 /**
1293 * Sets whether the message access is allowed to this device.
1294 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}.
1295 * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or
1296 * {@link #ACCESS_REJECTED}.
1297 * @return Whether the value has been successfully set.
1298 * @hide
1299 */
1300 public boolean setMessageAccessPermission(int value) {
1301 if (sService == null) {
1302 return false;
1303 }
1304 try {
1305 return sService.setMessageAccessPermission(this, value);
1306 } catch (RemoteException e) {
1307 Log.e(TAG, "", e);
1308 }
1309 return false;
1310 }
1311
1312 /**
Casper Bonde2a5f6082015-03-19 10:36:45 +01001313 * Requires {@link android.Manifest.permission#BLUETOOTH}.
1314 * @return Whether the Sim access is allowed to this device. Can be
1315 * {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}.
1316 * @hide
1317 */
1318 public int getSimAccessPermission() {
1319 if (sService == null) {
1320 return ACCESS_UNKNOWN;
1321 }
1322 try {
1323 return sService.getSimAccessPermission(this);
1324 } catch (RemoteException e) {
1325 Log.e(TAG, "", e);
1326 }
1327 return ACCESS_UNKNOWN;
1328 }
1329
1330 /**
1331 * Sets whether the Sim access is allowed to this device.
1332 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}.
1333 * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or
1334 * {@link #ACCESS_REJECTED}.
1335 * @return Whether the value has been successfully set.
1336 * @hide
1337 */
1338 public boolean setSimAccessPermission(int value) {
1339 if (sService == null) {
1340 return false;
1341 }
1342 try {
1343 return sService.setSimAccessPermission(this, value);
1344 } catch (RemoteException e) {
1345 Log.e(TAG, "", e);
1346 }
1347 return false;
Casper Bonde3b3d1fe2015-05-08 14:32:24 +02001348 }
1349
Casper Bonde2a5f6082015-03-19 10:36:45 +01001350 /**
Nick Pelly45e27042009-08-19 11:00:00 -07001351 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
Nick Pelly16fb88a2009-10-07 07:44:03 +02001352 * outgoing connection to this remote device on given channel.
Nick Pelly45e27042009-08-19 11:00:00 -07001353 * <p>The remote device will be authenticated and communication on this
1354 * socket will be encrypted.
Jaikumar Ganeshc8fa4ff2011-01-25 16:03:13 -08001355 * <p> Use this socket only if an authenticated socket link is possible.
1356 * Authentication refers to the authentication of the link key to
1357 * prevent man-in-the-middle type of attacks.
1358 * For example, for Bluetooth 2.1 devices, if any of the devices does not
1359 * have an input and output capability or just has the ability to
1360 * display a numeric key, a secure socket connection is not possible.
1361 * In such a case, use {#link createInsecureRfcommSocket}.
1362 * For more details, refer to the Security Model section 5.2 (vol 3) of
1363 * Bluetooth Core Specification version 2.1 + EDR.
Jake Hambyf51eada2010-09-21 13:39:53 -07001364 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
Nick Pelly45e27042009-08-19 11:00:00 -07001365 * connection.
1366 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pellycf440592009-09-08 10:12:06 -07001367 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellyde893f52009-09-08 13:15:33 -07001368 *
Nick Pelly45e27042009-08-19 11:00:00 -07001369 * @param channel RFCOMM channel to connect to
1370 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
Nick Pellybd022f42009-08-14 18:33:38 -07001371 * @throws IOException on error, for example Bluetooth not available, or
Nick Pelly45e27042009-08-19 11:00:00 -07001372 * insufficient permissions
Nick Pelly16fb88a2009-10-07 07:44:03 +02001373 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001374 */
Nick Pelly45e27042009-08-19 11:00:00 -07001375 public BluetoothSocket createRfcommSocket(int channel) throws IOException {
Nitin Arorad055adb2015-03-02 15:03:51 -08001376 if (isBluetoothEnabled() == false) {
1377 Log.e(TAG, "Bluetooth is not enabled");
1378 throw new IOException();
1379 }
Nick Pelly16fb88a2009-10-07 07:44:03 +02001380 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel,
1381 null);
1382 }
1383
1384 /**
Casper Bonde238e0f92015-04-09 09:24:48 +02001385 * Create an L2cap {@link BluetoothSocket} ready to start a secure
1386 * outgoing connection to this remote device on given channel.
1387 * <p>The remote device will be authenticated and communication on this
1388 * socket will be encrypted.
1389 * <p> Use this socket only if an authenticated socket link is possible.
1390 * Authentication refers to the authentication of the link key to
1391 * prevent man-in-the-middle type of attacks.
1392 * For example, for Bluetooth 2.1 devices, if any of the devices does not
1393 * have an input and output capability or just has the ability to
1394 * display a numeric key, a secure socket connection is not possible.
1395 * In such a case, use {#link createInsecureRfcommSocket}.
1396 * For more details, refer to the Security Model section 5.2 (vol 3) of
1397 * Bluetooth Core Specification version 2.1 + EDR.
1398 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
1399 * connection.
1400 * <p>Valid L2CAP PSM channels are in range 1 to 2^16.
1401 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1402 *
1403 * @param channel L2cap PSM/channel to connect to
1404 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
1405 * @throws IOException on error, for example Bluetooth not available, or
1406 * insufficient permissions
1407 * @hide
1408 */
1409 public BluetoothSocket createL2capSocket(int channel) throws IOException {
1410 return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, true, true, this, channel,
1411 null);
1412 }
1413
1414 /**
Nick Pelly16fb88a2009-10-07 07:44:03 +02001415 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
1416 * outgoing connection to this remote device using SDP lookup of uuid.
1417 * <p>This is designed to be used with {@link
1418 * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer
1419 * Bluetooth applications.
Jake Hambyf51eada2010-09-21 13:39:53 -07001420 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
Nick Pelly16fb88a2009-10-07 07:44:03 +02001421 * connection. This will also perform an SDP lookup of the given uuid to
1422 * determine which channel to connect to.
1423 * <p>The remote device will be authenticated and communication on this
1424 * socket will be encrypted.
Jaikumar Ganeshc8fa4ff2011-01-25 16:03:13 -08001425 * <p> Use this socket only if an authenticated socket link is possible.
1426 * Authentication refers to the authentication of the link key to
1427 * prevent man-in-the-middle type of attacks.
1428 * For example, for Bluetooth 2.1 devices, if any of the devices does not
1429 * have an input and output capability or just has the ability to
1430 * display a numeric key, a secure socket connection is not possible.
1431 * In such a case, use {#link createInsecureRfcommSocketToServiceRecord}.
1432 * For more details, refer to the Security Model section 5.2 (vol 3) of
1433 * Bluetooth Core Specification version 2.1 + EDR.
Nick Pellyea5056e2010-02-24 11:19:10 -08001434 * <p>Hint: If you are connecting to a Bluetooth serial board then try
1435 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB.
1436 * However if you are connecting to an Android peer then please generate
1437 * your own unique UUID.
Nick Pelly16fb88a2009-10-07 07:44:03 +02001438 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1439 *
1440 * @param uuid service record uuid to lookup RFCOMM channel
1441 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
1442 * @throws IOException on error, for example Bluetooth not available, or
1443 * insufficient permissions
1444 */
Tor Norbye2d497522015-04-23 17:10:21 -07001445 @RequiresPermission(Manifest.permission.BLUETOOTH)
Nick Pelly16fb88a2009-10-07 07:44:03 +02001446 public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException {
Nitin Arorad055adb2015-03-02 15:03:51 -08001447 if (isBluetoothEnabled() == false) {
1448 Log.e(TAG, "Bluetooth is not enabled");
1449 throw new IOException();
1450 }
1451
Nick Pelly16fb88a2009-10-07 07:44:03 +02001452 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1,
1453 new ParcelUuid(uuid));
Nick Pellybd022f42009-08-14 18:33:38 -07001454 }
1455
1456 /**
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001457 * Create an RFCOMM {@link BluetoothSocket} socket ready to start an insecure
1458 * outgoing connection to this remote device using SDP lookup of uuid.
1459 * <p> The communication channel will not have an authenticated link key
1460 * i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1
1461 * devices, the link key will be encrypted, as encryption is mandatory.
1462 * For legacy devices (pre Bluetooth 2.1 devices) the link key will
1463 * be not be encrypted. Use {@link #createRfcommSocketToServiceRecord} if an
1464 * encrypted and authenticated communication channel is desired.
1465 * <p>This is designed to be used with {@link
1466 * BluetoothAdapter#listenUsingInsecureRfcommWithServiceRecord} for peer-peer
1467 * Bluetooth applications.
1468 * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing
1469 * connection. This will also perform an SDP lookup of the given uuid to
1470 * determine which channel to connect to.
1471 * <p>The remote device will be authenticated and communication on this
1472 * socket will be encrypted.
1473 * <p>Hint: If you are connecting to a Bluetooth serial board then try
1474 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB.
1475 * However if you are connecting to an Android peer then please generate
1476 * your own unique UUID.
1477 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1478 *
1479 * @param uuid service record uuid to lookup RFCOMM channel
1480 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
1481 * @throws IOException on error, for example Bluetooth not available, or
1482 * insufficient permissions
1483 */
Tor Norbye2d497522015-04-23 17:10:21 -07001484 @RequiresPermission(Manifest.permission.BLUETOOTH)
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001485 public BluetoothSocket createInsecureRfcommSocketToServiceRecord(UUID uuid) throws IOException {
Nitin Arorad055adb2015-03-02 15:03:51 -08001486 if (isBluetoothEnabled() == false) {
1487 Log.e(TAG, "Bluetooth is not enabled");
1488 throw new IOException();
1489 }
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001490 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, -1,
1491 new ParcelUuid(uuid));
1492 }
1493
1494 /**
Nick Pellybd022f42009-08-14 18:33:38 -07001495 * Construct an insecure RFCOMM socket ready to start an outgoing
1496 * connection.
1497 * Call #connect on the returned #BluetoothSocket to begin the connection.
1498 * The remote device will not be authenticated and communication on this
1499 * socket will not be encrypted.
Nick Pellye6ee3be2009-10-08 23:27:28 +02001500 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
1501 *
Nick Pellybd022f42009-08-14 18:33:38 -07001502 * @param port remote port
1503 * @return An RFCOMM BluetoothSocket
1504 * @throws IOException On error, for example Bluetooth not available, or
1505 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001506 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001507 */
1508 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
Nitin Arorad055adb2015-03-02 15:03:51 -08001509
1510 if (isBluetoothEnabled() == false) {
1511 Log.e(TAG, "Bluetooth is not enabled");
1512 throw new IOException();
1513 }
Nick Pelly16fb88a2009-10-07 07:44:03 +02001514 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port,
1515 null);
Nick Pellybd022f42009-08-14 18:33:38 -07001516 }
1517
1518 /**
1519 * Construct a SCO socket ready to start an outgoing connection.
1520 * Call #connect on the returned #BluetoothSocket to begin the connection.
Nick Pellye6ee3be2009-10-08 23:27:28 +02001521 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
1522 *
Nick Pellybd022f42009-08-14 18:33:38 -07001523 * @return a SCO BluetoothSocket
1524 * @throws IOException on error, for example Bluetooth not available, or
1525 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001526 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001527 */
1528 public BluetoothSocket createScoSocket() throws IOException {
Nitin Arorad055adb2015-03-02 15:03:51 -08001529
1530 if (isBluetoothEnabled() == false) {
1531 Log.e(TAG, "Bluetooth is not enabled");
1532 throw new IOException();
1533 }
Nick Pelly16fb88a2009-10-07 07:44:03 +02001534 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null);
Nick Pellybd022f42009-08-14 18:33:38 -07001535 }
1536
1537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * Check that a pin is valid and convert to byte array.
1539 *
Jake Hambyf51eada2010-09-21 13:39:53 -07001540 * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * @param pin pin as java String
Jake Hambyf51eada2010-09-21 13:39:53 -07001542 * @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 -08001543 * Bluetooth pin.
Nick Pelly45e27042009-08-19 11:00:00 -07001544 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 */
1546 public static byte[] convertPinToBytes(String pin) {
1547 if (pin == null) {
1548 return null;
1549 }
1550 byte[] pinBytes;
1551 try {
Jake Hambyf51eada2010-09-21 13:39:53 -07001552 pinBytes = pin.getBytes("UTF-8");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 } catch (UnsupportedEncodingException uee) {
Jake Hambyf51eada2010-09-21 13:39:53 -07001554 Log.e(TAG, "UTF-8 not supported?!?"); // this should not happen
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 return null;
1556 }
1557 if (pinBytes.length <= 0 || pinBytes.length > 16) {
1558 return null;
1559 }
1560 return pinBytes;
1561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
Matthew Xieddf7e472013-03-01 18:41:02 -08001563 /**
1564 * Connect to GATT Server hosted by this device. Caller acts as GATT client.
1565 * The callback is used to deliver results to Caller, such as connection status as well
1566 * as any further GATT client operations.
1567 * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
1568 * GATT client operations.
1569 * @param callback GATT callback handler that will receive asynchronous callbacks.
1570 * @param autoConnect Whether to directly connect to the remote device (false)
1571 * or to automatically connect as soon as the remote
1572 * device becomes available (true).
1573 * @throws IllegalArgumentException if callback is null
1574 */
Matthew Xie33ec9842013-04-03 00:29:27 -07001575 public BluetoothGatt connectGatt(Context context, boolean autoConnect,
1576 BluetoothGattCallback callback) {
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -07001577 return (connectGatt(context, autoConnect,callback, TRANSPORT_AUTO));
1578 }
1579
1580 /**
1581 * Connect to GATT Server hosted by this device. Caller acts as GATT client.
1582 * The callback is used to deliver results to Caller, such as connection status as well
1583 * as any further GATT client operations.
1584 * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
1585 * GATT client operations.
1586 * @param callback GATT callback handler that will receive asynchronous callbacks.
1587 * @param autoConnect Whether to directly connect to the remote device (false)
1588 * or to automatically connect as soon as the remote
1589 * device becomes available (true).
1590 * @param transport preferred transport for GATT connections to remote dual-mode devices
1591 * {@link BluetoothDevice#TRANSPORT_AUTO} or
1592 * {@link BluetoothDevice#TRANSPORT_BREDR} or {@link BluetoothDevice#TRANSPORT_LE}
1593 * @throws IllegalArgumentException if callback is null
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -07001594 */
1595 public BluetoothGatt connectGatt(Context context, boolean autoConnect,
1596 BluetoothGattCallback callback, int transport) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001597 // TODO(Bluetooth) check whether platform support BLE
1598 // Do the check here or in GattServer?
1599 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
1600 IBluetoothManager managerService = adapter.getBluetoothManager();
1601 try {
1602 IBluetoothGatt iGatt = managerService.getBluetoothGatt();
Matthew Xie32ab77b2013-05-08 19:26:57 -07001603 if (iGatt == null) {
1604 // BLE is not supported
1605 return null;
1606 }
Jeremy Kleinadc26ec2016-09-27 14:34:33 -07001607 BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport);
Matthew Xieddf7e472013-03-01 18:41:02 -08001608 gatt.connect(autoConnect, callback);
1609 return gatt;
1610 } catch (RemoteException e) {Log.e(TAG, "", e);}
1611 return null;
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613}