blob: e77e76f79b2c4a70bb2542552ef1e8cd20d6d7a0 [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
35 * create a connection with the repective device or query information about
36 * 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
51 * {@link BluetoothSocket} for communciation with the remote device, using
52 * {@link #createRfcommSocketToServiceRecord(UUID)}.
53 *
54 * <p class="note"><strong>Note:</strong>
55 * Requires the {@link android.Manifest.permission#BLUETOOTH} permission.
56 *
57 * {@see BluetoothAdapter}
58 * {@see BluetoothSocket}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 */
Nick Pellybd022f42009-08-14 18:33:38 -070060public final class BluetoothDevice implements Parcelable {
61 private static final String TAG = "BluetoothDevice";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Nick Pellyb24e11b2009-09-08 17:40:43 -070063 /**
64 * Sentinel error value for this class. Guaranteed to not equal any other
65 * integer constant in this class. Provided as a convenience for functions
66 * that require a sentinel error value, for example:
Nick Pelly005b2282009-09-10 10:21:56 -070067 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
68 * BluetoothDevice.ERROR)</code>
Nick Pellyb24e11b2009-09-08 17:40:43 -070069 */
Nick Pelly005b2282009-09-10 10:21:56 -070070 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070071
Nick Pelly005b2282009-09-10 10:21:56 -070072 /**
73 * Broadcast Action: Remote device discovered.
74 * <p>Sent when a remote device is found during discovery.
75 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
76 * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or
77 * {@link #EXTRA_RSSI} if they are available.
78 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
79 */
80 // TODO: Change API to not broadcast RSSI if not available (incoming connection)
81 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
82 public static final String ACTION_FOUND =
83 "android.bluetooth.device.action.FOUND";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Nick Pelly005b2282009-09-10 10:21:56 -070085 /**
86 * Broadcast Action: Remote device disappeared.
87 * <p>Sent when a remote device that was found in the last discovery is not
88 * found in the current discovery.
89 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
90 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
91 * @hide
92 */
93 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
94 public static final String ACTION_DISAPPEARED =
95 "android.bluetooth.device.action.DISAPPEARED";
96
97 /**
98 * Broadcast Action: Bluetooth class of a remote device has changed.
99 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
100 * #EXTRA_CLASS}.
101 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
102 * @see {@link BluetoothClass}
103 */
104 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
105 public static final String ACTION_CLASS_CHANGED =
106 "android.bluetooth.device.action.CLASS_CHANGED";
107
108 /**
109 * Broadcast Action: Indicates a low level (ACL) connection has been
110 * established with a remote device.
111 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
112 * <p>ACL connections are managed automatically by the Android Bluetooth
113 * stack.
114 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
115 */
116 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
117 public static final String ACTION_ACL_CONNECTED =
118 "android.bluetooth.device.action.ACL_CONNECTED";
119
120 /**
121 * Broadcast Action: Indicates that a low level (ACL) disconnection has
122 * been requested for a remote device, and it will soon be disconnected.
123 * <p>This is useful for graceful disconnection. Applications should use
124 * this intent as a hint to immediately terminate higher level connections
125 * (RFCOMM, L2CAP, or profile connections) to the remote device.
126 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
127 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
128 */
129 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
130 public static final String ACTION_ACL_DISCONNECT_REQUESTED =
131 "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED";
132
133 /**
134 * Broadcast Action: Indicates a low level (ACL) disconnection from a
135 * remote device.
136 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
137 * <p>ACL connections are managed automatically by the Android Bluetooth
138 * stack.
139 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
140 */
141 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
142 public static final String ACTION_ACL_DISCONNECTED =
143 "android.bluetooth.device.action.ACL_DISCONNECTED";
144
145 /**
146 * Broadcast Action: Indicates the friendly name of a remote device has
147 * been retrieved for the first time, or changed since the last retrieval.
148 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
149 * #EXTRA_NAME}.
150 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
151 */
152 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
153 public static final String ACTION_NAME_CHANGED =
154 "android.bluetooth.device.action.NAME_CHANGED";
155
156 /**
157 * Broadcast Action: Indicates a change in the bond state of a remote
158 * device. For example, if a device is bonded (paired).
159 * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link
160 * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}.
161 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
162 */
163 // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also
164 // contain a hidden extra field EXTRA_REASON with the result code.
165 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
166 public static final String ACTION_BOND_STATE_CHANGED =
167 "android.bluetooth.device.action.BOND_STATE_CHANGED";
168
169 /**
170 * Used as a Parcelable {@link BluetoothDevice} extra field in every intent
171 * broadcast by this class. It contains the {@link BluetoothDevice} that
172 * the intent applies to.
173 */
174 public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
175
176 /**
177 * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link
178 * #ACTION_FOUND} intents. It contains the friendly Bluetooth name.
179 */
180 public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME";
181
182 /**
183 * Used as an optional short extra field in {@link #ACTION_FOUND} intents.
184 * Contains the RSSI value of the remote device as reported by the
185 * Bluetooth hardware.
186 */
187 public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI";
188
189 /**
190 * Used as an Parcelable {@link BluetoothClass} extra field in {@link
191 * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents.
192 */
193 public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS";
194
195 /**
196 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
197 * Contains the bond state of the remote device.
198 * <p>Possible values are:
199 * {@link #BOND_NONE},
200 * {@link #BOND_BONDING},
201 * {@link #BOND_BONDED}.
202 */
203 public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE";
204 /**
205 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
206 * Contains the previous bond state of the remote device.
207 * <p>Possible values are:
208 * {@link #BOND_NONE},
209 * {@link #BOND_BONDING},
210 * {@link #BOND_BONDED}.
211 */
212 public static final String EXTRA_PREVIOUS_BOND_STATE =
213 "android.bluetooth.device.extra.PREVIOUS_BOND_STATE";
214 /**
215 * Indicates the remote device is not bonded (paired).
216 * <p>There is no shared link key with the remote device, so communication
217 * (if it is allowed at all) will be unauthenticated and unencrypted.
218 */
219 public static final int BOND_NONE = 10;
220 /**
221 * Indicates bonding (pairing) is in progress with the remote device.
222 */
223 public static final int BOND_BONDING = 11;
224 /**
225 * Indicates the remote device is bonded (paired).
226 * <p>A shared link keys exists locally for the remote device, so
227 * communication can be authenticated and encrypted.
228 * <p><i>Being bonded (paired) with a remote device does not necessarily
229 * mean the device is currently connected. It just means that the ponding
230 * procedure was compeleted at some earlier time, and the link key is still
231 * stored locally, ready to use on the next connection.
232 * </i>
233 */
234 public static final int BOND_BONDED = 12;
235
236 /** @hide */
237 public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON";
238 /** @hide */
239 public static final String EXTRA_PAIRING_VARIANT =
240 "android.bluetooth.device.extra.PAIRING_VARIANT";
241 /** @hide */
242 public static final String EXTRA_PASSKEY = "android.bluetooth.device.extra.PASSKEY";
243
244 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700245 * Broadcast Action: This intent is used to broadcast the {@link UUID}
Nick Pellyaef439e2009-09-28 12:33:17 -0700246 * wrapped as a {@link android.os.ParcelUuid} of the remote device after it
247 * has been fetched. This intent is sent only when the UUIDs of the remote
248 * device are requested to be fetched using Service Discovery Protocol
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700249 * <p> Always contains the extra field {@link #EXTRA_DEVICE}
250 * <p> Always contains the extra filed {@link #EXTRA_UUID}
251 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
252 * @hide
253 */
254 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
255 public static final String ACTION_UUID =
256 "android.bleutooth.device.action.UUID";
257
258 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700259 * Broadcast Action: Indicates a failure to retrieve the name of a remote
260 * device.
261 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
262 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
263 * @hide
264 */
265 //TODO: is this actually useful?
266 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
267 public static final String ACTION_NAME_FAILED =
268 "android.bluetooth.device.action.NAME_FAILED";
269
270 /** @hide */
271 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
272 public static final String ACTION_PAIRING_REQUEST =
273 "android.bluetooth.device.action.PAIRING_REQUEST";
274 /** @hide */
275 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
276 public static final String ACTION_PAIRING_CANCEL =
277 "android.bluetooth.device.action.PAIRING_CANCEL";
Yue Lixina4433af2009-07-09 16:56:43 +0800278
Nick Pellyb24e11b2009-09-08 17:40:43 -0700279 /** A bond attempt succeeded
280 * @hide */
281 public static final int BOND_SUCCESS = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /** A bond attempt failed because pins did not match, or remote device did
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700283 * not respond to pin request in time
Nick Pelly45e27042009-08-19 11:00:00 -0700284 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 public static final int UNBOND_REASON_AUTH_FAILED = 1;
286 /** A bond attempt failed because the other side explicilty rejected
Nick Pelly45e27042009-08-19 11:00:00 -0700287 * bonding
288 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 public static final int UNBOND_REASON_AUTH_REJECTED = 2;
Nick Pelly45e27042009-08-19 11:00:00 -0700290 /** A bond attempt failed because we canceled the bonding process
291 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public static final int UNBOND_REASON_AUTH_CANCELED = 3;
Nick Pelly45e27042009-08-19 11:00:00 -0700293 /** A bond attempt failed because we could not contact the remote device
294 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
Nick Pelly45e27042009-08-19 11:00:00 -0700296 /** A bond attempt failed because a discovery is in progress
297 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5;
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700299 /** A bond attempt failed because of authentication timeout
300 * @hide */
301 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6;
302 /** A bond attempt failed because of repeated attempts
303 * @hide */
304 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7;
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700305 /** A bond attempt failed because we received an Authentication Cancel
306 * by remote end
307 * @hide */
308 public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8;
Nick Pelly45e27042009-08-19 11:00:00 -0700309 /** An existing bond was explicitly revoked
310 * @hide */
Jaikumar Ganeshe5d93b72009-10-08 02:27:52 -0700311 public static final int UNBOND_REASON_REMOVED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312
Nick Pelly45e27042009-08-19 11:00:00 -0700313 /** The user will be prompted to enter a pin
314 * @hide */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700315 public static final int PAIRING_VARIANT_PIN = 0;
Nick Pelly45e27042009-08-19 11:00:00 -0700316 /** The user will be prompted to enter a passkey
317 * @hide */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700318 public static final int PAIRING_VARIANT_PASSKEY = 1;
Nick Pelly45e27042009-08-19 11:00:00 -0700319 /** The user will be prompted to confirm the passkey displayed on the screen
320 * @hide */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700321 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2;
322 /** The user will be prompted to accept or deny the incoming pairing request
323 * @hide */
324 public static final int PAIRING_VARIANT_CONSENT = 3;
325 /** The user will be prompted to enter the passkey displayed on remote device
326 * @hide */
327 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4;
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700328
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700329 /**
330 * Used as an extra field in {@link #ACTION_UUID} intents,
Nick Pellyaef439e2009-09-28 12:33:17 -0700331 * Contains the {@link android.os.ParcelUuid}s of the remote device which
332 * is a parcelable version of {@link UUID}.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700333 * @hide
334 */
335 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID";
336
Nick Pelly16fb88a2009-10-07 07:44:03 +0200337 /**
338 * Lazy initialization. Guaranteed final after first object constructed, or
339 * getService() called.
340 * TODO: Unify implementation of sService amongst BluetoothFoo API's
341 */
342 private static IBluetooth sService;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700343
Nick Pellybd022f42009-08-14 18:33:38 -0700344 private final String mAddress;
345
Nick Pelly16fb88a2009-10-07 07:44:03 +0200346 /*package*/ static IBluetooth getService() {
347 synchronized (BluetoothDevice.class) {
348 if (sService == null) {
Nick Pellyf242b7b2009-10-08 00:12:45 +0200349 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
Nick Pelly16fb88a2009-10-07 07:44:03 +0200350 if (b == null) {
351 throw new RuntimeException("Bluetooth service not available");
352 }
353 sService = IBluetooth.Stub.asInterface(b);
354 }
355 }
356 return sService;
357 }
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
Nick Pellybd022f42009-08-14 18:33:38 -0700360 * Create a new BluetoothDevice
361 * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB",
362 * and is validated in this constructor.
363 * @param address valid Bluetooth MAC address
364 * @throws RuntimeException Bluetooth is not available on this platform
365 * @throws IllegalArgumentException address is invalid
366 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 */
Nick Pellybd022f42009-08-14 18:33:38 -0700368 /*package*/ BluetoothDevice(String address) {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200369 getService(); // ensures sService is initialized
Nick Pelly005b2282009-09-10 10:21:56 -0700370 if (!BluetoothAdapter.checkBluetoothAddress(address)) {
Nick Pellybd022f42009-08-14 18:33:38 -0700371 throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
372 }
373
374 mAddress = address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 }
376
Nick Pellybd022f42009-08-14 18:33:38 -0700377 @Override
378 public boolean equals(Object o) {
379 if (o instanceof BluetoothDevice) {
380 return mAddress.equals(((BluetoothDevice)o).getAddress());
381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 return false;
383 }
384
Nick Pellybd022f42009-08-14 18:33:38 -0700385 @Override
386 public int hashCode() {
387 return mAddress.hashCode();
The Android Open Source Project10592532009-03-18 17:39:46 -0700388 }
389
Nick Pelly45e27042009-08-19 11:00:00 -0700390 /**
391 * Returns a string representation of this BluetoothDevice.
392 * <p>Currently this is the Bluetooth hardware address, for example
393 * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress}
394 * if you explicitly require the Bluetooth hardware address in case the
395 * {@link #toString} representation changes in the future.
396 * @return string representation of this BluetoothDevice
397 */
Nick Pellybd022f42009-08-14 18:33:38 -0700398 @Override
399 public String toString() {
400 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402
Nick Pellybd022f42009-08-14 18:33:38 -0700403 public int describeContents() {
404 return 0;
405 }
406
407 public static final Parcelable.Creator<BluetoothDevice> CREATOR =
408 new Parcelable.Creator<BluetoothDevice>() {
409 public BluetoothDevice createFromParcel(Parcel in) {
410 return new BluetoothDevice(in.readString());
411 }
412 public BluetoothDevice[] newArray(int size) {
413 return new BluetoothDevice[size];
414 }
415 };
416
417 public void writeToParcel(Parcel out, int flags) {
418 out.writeString(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420
Nick Pelly45e27042009-08-19 11:00:00 -0700421 /**
422 * Returns the hardware address of this BluetoothDevice.
423 * <p> For example, "00:11:22:AA:BB:CC".
424 * @return Bluetooth hardware address as string
425 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 public String getAddress() {
Nick Pellybd022f42009-08-14 18:33:38 -0700427 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700431 * Get the friendly Bluetooth name of the remote device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 *
Nick Pelly45e27042009-08-19 11:00:00 -0700433 * <p>The local adapter will automatically retrieve remote names when
434 * performing a device scan, and will cache them. This method just returns
435 * the name for this device from the cache.
Nick Pellyde893f52009-09-08 13:15:33 -0700436 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 *
438 * @return the Bluetooth name, or null if there was a problem.
439 */
440 public String getName() {
441 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700442 return sService.getRemoteName(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 } catch (RemoteException e) {Log.e(TAG, "", e);}
444 return null;
445 }
446
447 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700448 * Start the bonding (pairing) process with the remote device.
449 * <p>This is an asynchronous call, it will return immediately. Register
450 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
451 * the bonding process completes, and its result.
452 * <p>Android system services will handle the necessary user interactions
453 * to confirm and complete the bonding process.
454 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 *
Nick Pelly005b2282009-09-10 10:21:56 -0700456 * @return false on immediate error, true if bonding will begin
Nick Pelly18b1e792009-09-24 11:14:15 -0700457 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 */
Nick Pellybd022f42009-08-14 18:33:38 -0700459 public boolean createBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700461 return sService.createBond(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 } catch (RemoteException e) {Log.e(TAG, "", e);}
463 return false;
464 }
465
466 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700467 * Cancel an in-progress bonding request started with {@link #createBond}.
468 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
469 *
470 * @return true on sucess, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700471 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
Nick Pellybd022f42009-08-14 18:33:38 -0700473 public boolean cancelBondProcess() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700475 return sService.cancelBondProcess(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 } catch (RemoteException e) {Log.e(TAG, "", e);}
477 return false;
478 }
479
480 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700481 * Remove bond (pairing) with the remote device.
482 * <p>Delete the link key associated with the remote device, and
483 * immediately terminate connections to that device that require
484 * authentication and encryption.
485 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 *
Nick Pelly005b2282009-09-10 10:21:56 -0700487 * @return true on sucess, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700488 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 */
Nick Pellybd022f42009-08-14 18:33:38 -0700490 public boolean removeBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700492 return sService.removeBond(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellybd022f42009-08-14 18:33:38 -0700494 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
497 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700498 * Get the bond state of the remote device.
499 * <p>Possible values for the bond state are:
500 * {@link #BOND_NONE},
501 * {@link #BOND_BONDING},
502 * {@link #BOND_BONDED}.
503 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 *
Nick Pelly005b2282009-09-10 10:21:56 -0700505 * @return the bond state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
Nick Pellybd022f42009-08-14 18:33:38 -0700507 public int getBondState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700509 return sService.getBondState(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700511 return BOND_NONE;
512 }
513
514 /**
515 * Get the Bluetooth class of the remote device.
516 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
517 *
518 * @return Bluetooth class object, or null on error
519 */
520 public BluetoothClass getBluetoothClass() {
521 try {
522 int classInt = sService.getRemoteClass(mAddress);
523 if (classInt == BluetoothClass.ERROR) return null;
524 return new BluetoothClass(classInt);
525 } catch (RemoteException e) {Log.e(TAG, "", e);}
526 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528
Lixin Yueefa1dd72009-08-31 15:55:13 +0800529 /**
530 * Get trust state of a remote device.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200531 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Lixin Yueefa1dd72009-08-31 15:55:13 +0800532 * @hide
533 */
534 public boolean getTrustState() {
535 try {
536 return sService.getTrustState(mAddress);
537 } catch (RemoteException e) {
538 Log.e(TAG, "", e);
539 }
540 return false;
541 }
542
543 /**
544 * Set trust state for a remote device.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200545 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Lixin Yueefa1dd72009-08-31 15:55:13 +0800546 * @param value the trust state value (true or false)
547 * @hide
548 */
549 public boolean setTrust(boolean value) {
550 try {
551 return sService.setTrust(mAddress, value);
552 } catch (RemoteException e) {
553 Log.e(TAG, "", e);
554 }
555 return false;
556 }
557
Nick Pelly45e27042009-08-19 11:00:00 -0700558 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700559 public ParcelUuid[] getUuids() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700561 return sService.getRemoteUuids(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 } catch (RemoteException e) {Log.e(TAG, "", e);}
563 return null;
564 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700565
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700566 /**
567 * Perform a SDP query on the remote device to get the UUIDs
568 * supported. This API is asynchronous and an Intent is sent,
569 * with the UUIDs supported by the remote end. If there is an error
570 * in getting the SDP records or if the process takes a long time,
571 * an Intent is sent with the UUIDs that is currently present in the
572 * cache. Clients should use the {@link getUuids} to get UUIDs
573 * is SDP is not to be performed.
574 *
575 * @return False if the sanity check fails, True if the process
576 * of initiating an ACL connection to the remote device
577 * was started.
578 * @hide
579 */
580 public boolean fetchUuidsWithSdp() {
581 try {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200582 return sService.fetchRemoteUuids(mAddress, null, null);
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700583 } catch (RemoteException e) {Log.e(TAG, "", e);}
584 return false;
585 }
586
Nick Pelly45e27042009-08-19 11:00:00 -0700587 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700588 public int getServiceChannel(ParcelUuid uuid) {
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700589 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700590 return sService.getRemoteServiceChannel(mAddress, uuid);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700591 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyb24e11b2009-09-08 17:40:43 -0700592 return BluetoothDevice.ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
594
Nick Pelly45e27042009-08-19 11:00:00 -0700595 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700596 public boolean setPin(byte[] pin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700598 return sService.setPin(mAddress, pin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 } catch (RemoteException e) {Log.e(TAG, "", e);}
600 return false;
601 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700602
Nick Pelly45e27042009-08-19 11:00:00 -0700603 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700604 public boolean setPasskey(int passkey) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700606 return sService.setPasskey(mAddress, passkey);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700607 } catch (RemoteException e) {Log.e(TAG, "", e);}
608 return false;
609 }
610
Nick Pelly45e27042009-08-19 11:00:00 -0700611 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700612 public boolean setPairingConfirmation(boolean confirm) {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700613 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700614 return sService.setPairingConfirmation(mAddress, confirm);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700615 } catch (RemoteException e) {Log.e(TAG, "", e);}
616 return false;
617 }
618
Nick Pelly45e27042009-08-19 11:00:00 -0700619 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700620 public boolean cancelPairingUserInput() {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700621 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700622 return sService.cancelPairingUserInput(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 } catch (RemoteException e) {Log.e(TAG, "", e);}
624 return false;
625 }
626
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -0800627 /** @hide */
628 public boolean isBluetoothDock() {
629 try {
630 return sService.isBluetoothDock(mAddress);
631 } catch (RemoteException e) {Log.e(TAG, "", e);}
632 return false;
633 }
634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700636 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
Nick Pelly16fb88a2009-10-07 07:44:03 +0200637 * outgoing connection to this remote device on given channel.
Nick Pelly45e27042009-08-19 11:00:00 -0700638 * <p>The remote device will be authenticated and communication on this
639 * socket will be encrypted.
640 * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing
641 * connection.
642 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pellycf440592009-09-08 10:12:06 -0700643 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellyde893f52009-09-08 13:15:33 -0700644 *
Nick Pelly45e27042009-08-19 11:00:00 -0700645 * @param channel RFCOMM channel to connect to
646 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
Nick Pellybd022f42009-08-14 18:33:38 -0700647 * @throws IOException on error, for example Bluetooth not available, or
Nick Pelly45e27042009-08-19 11:00:00 -0700648 * insufficient permissions
Nick Pelly16fb88a2009-10-07 07:44:03 +0200649 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700650 */
Nick Pelly45e27042009-08-19 11:00:00 -0700651 public BluetoothSocket createRfcommSocket(int channel) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200652 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel,
653 null);
654 }
655
656 /**
657 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
658 * outgoing connection to this remote device using SDP lookup of uuid.
659 * <p>This is designed to be used with {@link
660 * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer
661 * Bluetooth applications.
662 * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing
663 * connection. This will also perform an SDP lookup of the given uuid to
664 * determine which channel to connect to.
665 * <p>The remote device will be authenticated and communication on this
666 * socket will be encrypted.
Nick Pellyea5056e2010-02-24 11:19:10 -0800667 * <p>Hint: If you are connecting to a Bluetooth serial board then try
668 * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB.
669 * However if you are connecting to an Android peer then please generate
670 * your own unique UUID.
Nick Pelly16fb88a2009-10-07 07:44:03 +0200671 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
672 *
673 * @param uuid service record uuid to lookup RFCOMM channel
674 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
675 * @throws IOException on error, for example Bluetooth not available, or
676 * insufficient permissions
677 */
678 public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException {
679 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1,
680 new ParcelUuid(uuid));
Nick Pellybd022f42009-08-14 18:33:38 -0700681 }
682
683 /**
684 * Construct an insecure RFCOMM socket ready to start an outgoing
685 * connection.
686 * Call #connect on the returned #BluetoothSocket to begin the connection.
687 * The remote device will not be authenticated and communication on this
688 * socket will not be encrypted.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200689 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
690 *
Nick Pellybd022f42009-08-14 18:33:38 -0700691 * @param port remote port
692 * @return An RFCOMM BluetoothSocket
693 * @throws IOException On error, for example Bluetooth not available, or
694 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -0700695 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700696 */
697 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200698 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port,
699 null);
Nick Pellybd022f42009-08-14 18:33:38 -0700700 }
701
702 /**
703 * Construct a SCO socket ready to start an outgoing connection.
704 * Call #connect on the returned #BluetoothSocket to begin the connection.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200705 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
706 *
Nick Pellybd022f42009-08-14 18:33:38 -0700707 * @return a SCO BluetoothSocket
708 * @throws IOException on error, for example Bluetooth not available, or
709 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -0700710 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700711 */
712 public BluetoothSocket createScoSocket() throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200713 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null);
Nick Pellybd022f42009-08-14 18:33:38 -0700714 }
715
716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 * Check that a pin is valid and convert to byte array.
718 *
719 * Bluetooth pin's are 1 to 16 bytes of UTF8 characters.
720 * @param pin pin as java String
721 * @return the pin code as a UTF8 byte array, or null if it is an invalid
722 * Bluetooth pin.
Nick Pelly45e27042009-08-19 11:00:00 -0700723 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
725 public static byte[] convertPinToBytes(String pin) {
726 if (pin == null) {
727 return null;
728 }
729 byte[] pinBytes;
730 try {
731 pinBytes = pin.getBytes("UTF8");
732 } catch (UnsupportedEncodingException uee) {
733 Log.e(TAG, "UTF8 not supported?!?"); // this should not happen
734 return null;
735 }
736 if (pinBytes.length <= 0 || pinBytes.length > 16) {
737 return null;
738 }
739 return pinBytes;
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742}