blob: 9c23746494a4f1c68d54d5c90a5f3174ff4951b7 [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/**
Nick Pellybd022f42009-08-14 18:33:38 -070034 * Represents a remote Bluetooth device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 *
Nick Pelly45e27042009-08-19 11:00:00 -070036 * <p>Use {@link BluetoothAdapter#getRemoteDevice} to create a {@link
37 * BluetoothDevice}.
38 *
39 * <p>This class is really just a thin wrapper for a Bluetooth hardware
40 * address. Objects of this class are immutable. Operations on this class
41 * are performed on the remote Bluetooth hardware address, using the
42 * {@link BluetoothAdapter} that was used to create this {@link
43 * BluetoothDevice}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
Nick Pellybd022f42009-08-14 18:33:38 -070045public final class BluetoothDevice implements Parcelable {
46 private static final String TAG = "BluetoothDevice";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Nick Pellyb24e11b2009-09-08 17:40:43 -070048 /**
49 * Sentinel error value for this class. Guaranteed to not equal any other
50 * integer constant in this class. Provided as a convenience for functions
51 * that require a sentinel error value, for example:
Nick Pelly005b2282009-09-10 10:21:56 -070052 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
53 * BluetoothDevice.ERROR)</code>
Nick Pellyb24e11b2009-09-08 17:40:43 -070054 */
Nick Pelly005b2282009-09-10 10:21:56 -070055 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070056
Nick Pelly005b2282009-09-10 10:21:56 -070057 /**
58 * Broadcast Action: Remote device discovered.
59 * <p>Sent when a remote device is found during discovery.
60 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
61 * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or
62 * {@link #EXTRA_RSSI} if they are available.
63 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
64 */
65 // TODO: Change API to not broadcast RSSI if not available (incoming connection)
66 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
67 public static final String ACTION_FOUND =
68 "android.bluetooth.device.action.FOUND";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Nick Pelly005b2282009-09-10 10:21:56 -070070 /**
71 * Broadcast Action: Remote device disappeared.
72 * <p>Sent when a remote device that was found in the last discovery is not
73 * found in the current discovery.
74 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
75 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
76 * @hide
77 */
78 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
79 public static final String ACTION_DISAPPEARED =
80 "android.bluetooth.device.action.DISAPPEARED";
81
82 /**
83 * Broadcast Action: Bluetooth class of a remote device has changed.
84 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
85 * #EXTRA_CLASS}.
86 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
87 * @see {@link BluetoothClass}
88 */
89 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
90 public static final String ACTION_CLASS_CHANGED =
91 "android.bluetooth.device.action.CLASS_CHANGED";
92
93 /**
94 * Broadcast Action: Indicates a low level (ACL) connection has been
95 * established with a remote device.
96 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
97 * <p>ACL connections are managed automatically by the Android Bluetooth
98 * stack.
99 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
100 */
101 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
102 public static final String ACTION_ACL_CONNECTED =
103 "android.bluetooth.device.action.ACL_CONNECTED";
104
105 /**
106 * Broadcast Action: Indicates that a low level (ACL) disconnection has
107 * been requested for a remote device, and it will soon be disconnected.
108 * <p>This is useful for graceful disconnection. Applications should use
109 * this intent as a hint to immediately terminate higher level connections
110 * (RFCOMM, L2CAP, or profile connections) to the remote device.
111 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
112 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
113 */
114 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
115 public static final String ACTION_ACL_DISCONNECT_REQUESTED =
116 "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED";
117
118 /**
119 * Broadcast Action: Indicates a low level (ACL) disconnection from a
120 * remote device.
121 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
122 * <p>ACL connections are managed automatically by the Android Bluetooth
123 * stack.
124 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
125 */
126 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
127 public static final String ACTION_ACL_DISCONNECTED =
128 "android.bluetooth.device.action.ACL_DISCONNECTED";
129
130 /**
131 * Broadcast Action: Indicates the friendly name of a remote device has
132 * been retrieved for the first time, or changed since the last retrieval.
133 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
134 * #EXTRA_NAME}.
135 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
136 */
137 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
138 public static final String ACTION_NAME_CHANGED =
139 "android.bluetooth.device.action.NAME_CHANGED";
140
141 /**
142 * Broadcast Action: Indicates a change in the bond state of a remote
143 * device. For example, if a device is bonded (paired).
144 * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link
145 * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}.
146 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
147 */
148 // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also
149 // contain a hidden extra field EXTRA_REASON with the result code.
150 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
151 public static final String ACTION_BOND_STATE_CHANGED =
152 "android.bluetooth.device.action.BOND_STATE_CHANGED";
153
154 /**
155 * Used as a Parcelable {@link BluetoothDevice} extra field in every intent
156 * broadcast by this class. It contains the {@link BluetoothDevice} that
157 * the intent applies to.
158 */
159 public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
160
161 /**
162 * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link
163 * #ACTION_FOUND} intents. It contains the friendly Bluetooth name.
164 */
165 public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME";
166
167 /**
168 * Used as an optional short extra field in {@link #ACTION_FOUND} intents.
169 * Contains the RSSI value of the remote device as reported by the
170 * Bluetooth hardware.
171 */
172 public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI";
173
174 /**
175 * Used as an Parcelable {@link BluetoothClass} extra field in {@link
176 * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents.
177 */
178 public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS";
179
180 /**
181 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
182 * Contains the bond state of the remote device.
183 * <p>Possible values are:
184 * {@link #BOND_NONE},
185 * {@link #BOND_BONDING},
186 * {@link #BOND_BONDED}.
187 */
188 public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE";
189 /**
190 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
191 * Contains the previous bond state of the remote device.
192 * <p>Possible values are:
193 * {@link #BOND_NONE},
194 * {@link #BOND_BONDING},
195 * {@link #BOND_BONDED}.
196 */
197 public static final String EXTRA_PREVIOUS_BOND_STATE =
198 "android.bluetooth.device.extra.PREVIOUS_BOND_STATE";
199 /**
200 * Indicates the remote device is not bonded (paired).
201 * <p>There is no shared link key with the remote device, so communication
202 * (if it is allowed at all) will be unauthenticated and unencrypted.
203 */
204 public static final int BOND_NONE = 10;
205 /**
206 * Indicates bonding (pairing) is in progress with the remote device.
207 */
208 public static final int BOND_BONDING = 11;
209 /**
210 * Indicates the remote device is bonded (paired).
211 * <p>A shared link keys exists locally for the remote device, so
212 * communication can be authenticated and encrypted.
213 * <p><i>Being bonded (paired) with a remote device does not necessarily
214 * mean the device is currently connected. It just means that the ponding
215 * procedure was compeleted at some earlier time, and the link key is still
216 * stored locally, ready to use on the next connection.
217 * </i>
218 */
219 public static final int BOND_BONDED = 12;
220
221 /** @hide */
222 public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON";
223 /** @hide */
224 public static final String EXTRA_PAIRING_VARIANT =
225 "android.bluetooth.device.extra.PAIRING_VARIANT";
226 /** @hide */
227 public static final String EXTRA_PASSKEY = "android.bluetooth.device.extra.PASSKEY";
228
229 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700230 * Broadcast Action: This intent is used to broadcast the {@link UUID}
Nick Pellyaef439e2009-09-28 12:33:17 -0700231 * wrapped as a {@link android.os.ParcelUuid} of the remote device after it
232 * has been fetched. This intent is sent only when the UUIDs of the remote
233 * device are requested to be fetched using Service Discovery Protocol
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700234 * <p> Always contains the extra field {@link #EXTRA_DEVICE}
235 * <p> Always contains the extra filed {@link #EXTRA_UUID}
236 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
237 * @hide
238 */
239 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
240 public static final String ACTION_UUID =
241 "android.bleutooth.device.action.UUID";
242
243 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700244 * Broadcast Action: Indicates a failure to retrieve the name of a remote
245 * device.
246 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
247 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
248 * @hide
249 */
250 //TODO: is this actually useful?
251 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
252 public static final String ACTION_NAME_FAILED =
253 "android.bluetooth.device.action.NAME_FAILED";
254
255 /** @hide */
256 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
257 public static final String ACTION_PAIRING_REQUEST =
258 "android.bluetooth.device.action.PAIRING_REQUEST";
259 /** @hide */
260 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
261 public static final String ACTION_PAIRING_CANCEL =
262 "android.bluetooth.device.action.PAIRING_CANCEL";
Yue Lixina4433af2009-07-09 16:56:43 +0800263
Nick Pellyb24e11b2009-09-08 17:40:43 -0700264 /** A bond attempt succeeded
265 * @hide */
266 public static final int BOND_SUCCESS = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /** A bond attempt failed because pins did not match, or remote device did
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700268 * not respond to pin request in time
Nick Pelly45e27042009-08-19 11:00:00 -0700269 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public static final int UNBOND_REASON_AUTH_FAILED = 1;
271 /** A bond attempt failed because the other side explicilty rejected
Nick Pelly45e27042009-08-19 11:00:00 -0700272 * bonding
273 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public static final int UNBOND_REASON_AUTH_REJECTED = 2;
Nick Pelly45e27042009-08-19 11:00:00 -0700275 /** A bond attempt failed because we canceled the bonding process
276 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 public static final int UNBOND_REASON_AUTH_CANCELED = 3;
Nick Pelly45e27042009-08-19 11:00:00 -0700278 /** A bond attempt failed because we could not contact the remote device
279 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
Nick Pelly45e27042009-08-19 11:00:00 -0700281 /** A bond attempt failed because a discovery is in progress
282 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5;
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700284 /** A bond attempt failed because of authentication timeout
285 * @hide */
286 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6;
287 /** A bond attempt failed because of repeated attempts
288 * @hide */
289 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7;
Nick Pelly45e27042009-08-19 11:00:00 -0700290 /** An existing bond was explicitly revoked
291 * @hide */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700292 public static final int UNBOND_REASON_REMOVED = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Nick Pelly45e27042009-08-19 11:00:00 -0700294 /** The user will be prompted to enter a pin
295 * @hide */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700296 public static final int PAIRING_VARIANT_PIN = 0;
Nick Pelly45e27042009-08-19 11:00:00 -0700297 /** The user will be prompted to enter a passkey
298 * @hide */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700299 public static final int PAIRING_VARIANT_PASSKEY = 1;
Nick Pelly45e27042009-08-19 11:00:00 -0700300 /** The user will be prompted to confirm the passkey displayed on the screen
301 * @hide */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700302 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2;
303 /** The user will be prompted to accept or deny the incoming pairing request
304 * @hide */
305 public static final int PAIRING_VARIANT_CONSENT = 3;
306 /** The user will be prompted to enter the passkey displayed on remote device
307 * @hide */
308 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4;
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700309
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700310 /**
311 * Used as an extra field in {@link #ACTION_UUID} intents,
Nick Pellyaef439e2009-09-28 12:33:17 -0700312 * Contains the {@link android.os.ParcelUuid}s of the remote device which
313 * is a parcelable version of {@link UUID}.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700314 * @hide
315 */
316 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID";
317
Nick Pelly16fb88a2009-10-07 07:44:03 +0200318 /**
319 * Lazy initialization. Guaranteed final after first object constructed, or
320 * getService() called.
321 * TODO: Unify implementation of sService amongst BluetoothFoo API's
322 */
323 private static IBluetooth sService;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700324
Nick Pellybd022f42009-08-14 18:33:38 -0700325 private final String mAddress;
326
Nick Pelly16fb88a2009-10-07 07:44:03 +0200327 /*package*/ static IBluetooth getService() {
328 synchronized (BluetoothDevice.class) {
329 if (sService == null) {
Nick Pellyf242b7b2009-10-08 00:12:45 +0200330 IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
Nick Pelly16fb88a2009-10-07 07:44:03 +0200331 if (b == null) {
332 throw new RuntimeException("Bluetooth service not available");
333 }
334 sService = IBluetooth.Stub.asInterface(b);
335 }
336 }
337 return sService;
338 }
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
Nick Pellybd022f42009-08-14 18:33:38 -0700341 * Create a new BluetoothDevice
342 * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB",
343 * and is validated in this constructor.
344 * @param address valid Bluetooth MAC address
345 * @throws RuntimeException Bluetooth is not available on this platform
346 * @throws IllegalArgumentException address is invalid
347 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 */
Nick Pellybd022f42009-08-14 18:33:38 -0700349 /*package*/ BluetoothDevice(String address) {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200350 getService(); // ensures sService is initialized
Nick Pelly005b2282009-09-10 10:21:56 -0700351 if (!BluetoothAdapter.checkBluetoothAddress(address)) {
Nick Pellybd022f42009-08-14 18:33:38 -0700352 throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
353 }
354
355 mAddress = address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
357
Nick Pellybd022f42009-08-14 18:33:38 -0700358 @Override
359 public boolean equals(Object o) {
360 if (o instanceof BluetoothDevice) {
361 return mAddress.equals(((BluetoothDevice)o).getAddress());
362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 return false;
364 }
365
Nick Pellybd022f42009-08-14 18:33:38 -0700366 @Override
367 public int hashCode() {
368 return mAddress.hashCode();
The Android Open Source Project10592532009-03-18 17:39:46 -0700369 }
370
Nick Pelly45e27042009-08-19 11:00:00 -0700371 /**
372 * Returns a string representation of this BluetoothDevice.
373 * <p>Currently this is the Bluetooth hardware address, for example
374 * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress}
375 * if you explicitly require the Bluetooth hardware address in case the
376 * {@link #toString} representation changes in the future.
377 * @return string representation of this BluetoothDevice
378 */
Nick Pellybd022f42009-08-14 18:33:38 -0700379 @Override
380 public String toString() {
381 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
Nick Pellybd022f42009-08-14 18:33:38 -0700384 public int describeContents() {
385 return 0;
386 }
387
388 public static final Parcelable.Creator<BluetoothDevice> CREATOR =
389 new Parcelable.Creator<BluetoothDevice>() {
390 public BluetoothDevice createFromParcel(Parcel in) {
391 return new BluetoothDevice(in.readString());
392 }
393 public BluetoothDevice[] newArray(int size) {
394 return new BluetoothDevice[size];
395 }
396 };
397
398 public void writeToParcel(Parcel out, int flags) {
399 out.writeString(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
401
Nick Pelly45e27042009-08-19 11:00:00 -0700402 /**
403 * Returns the hardware address of this BluetoothDevice.
404 * <p> For example, "00:11:22:AA:BB:CC".
405 * @return Bluetooth hardware address as string
406 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 public String getAddress() {
Nick Pellybd022f42009-08-14 18:33:38 -0700408 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410
411 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700412 * Get the friendly Bluetooth name of the remote device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 *
Nick Pelly45e27042009-08-19 11:00:00 -0700414 * <p>The local adapter will automatically retrieve remote names when
415 * performing a device scan, and will cache them. This method just returns
416 * the name for this device from the cache.
Nick Pellyde893f52009-09-08 13:15:33 -0700417 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 *
419 * @return the Bluetooth name, or null if there was a problem.
420 */
421 public String getName() {
422 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700423 return sService.getRemoteName(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 } catch (RemoteException e) {Log.e(TAG, "", e);}
425 return null;
426 }
427
428 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700429 * Start the bonding (pairing) process with the remote device.
430 * <p>This is an asynchronous call, it will return immediately. Register
431 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
432 * the bonding process completes, and its result.
433 * <p>Android system services will handle the necessary user interactions
434 * to confirm and complete the bonding process.
435 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 *
Nick Pelly005b2282009-09-10 10:21:56 -0700437 * @return false on immediate error, true if bonding will begin
Nick Pelly18b1e792009-09-24 11:14:15 -0700438 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 */
Nick Pellybd022f42009-08-14 18:33:38 -0700440 public boolean createBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700442 return sService.createBond(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 } catch (RemoteException e) {Log.e(TAG, "", e);}
444 return false;
445 }
446
447 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700448 * Cancel an in-progress bonding request started with {@link #createBond}.
449 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
450 *
451 * @return true on sucess, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700452 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
Nick Pellybd022f42009-08-14 18:33:38 -0700454 public boolean cancelBondProcess() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700456 return sService.cancelBondProcess(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 } catch (RemoteException e) {Log.e(TAG, "", e);}
458 return false;
459 }
460
461 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700462 * Remove bond (pairing) with the remote device.
463 * <p>Delete the link key associated with the remote device, and
464 * immediately terminate connections to that device that require
465 * authentication and encryption.
466 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 *
Nick Pelly005b2282009-09-10 10:21:56 -0700468 * @return true on sucess, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700469 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Nick Pellybd022f42009-08-14 18:33:38 -0700471 public boolean removeBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700473 return sService.removeBond(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellybd022f42009-08-14 18:33:38 -0700475 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477
478 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700479 * Get the bond state of the remote device.
480 * <p>Possible values for the bond state are:
481 * {@link #BOND_NONE},
482 * {@link #BOND_BONDING},
483 * {@link #BOND_BONDED}.
484 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 *
Nick Pelly005b2282009-09-10 10:21:56 -0700486 * @return the bond state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 */
Nick Pellybd022f42009-08-14 18:33:38 -0700488 public int getBondState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700490 return sService.getBondState(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700492 return BOND_NONE;
493 }
494
495 /**
496 * Get the Bluetooth class of the remote device.
497 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
498 *
499 * @return Bluetooth class object, or null on error
500 */
501 public BluetoothClass getBluetoothClass() {
502 try {
503 int classInt = sService.getRemoteClass(mAddress);
504 if (classInt == BluetoothClass.ERROR) return null;
505 return new BluetoothClass(classInt);
506 } catch (RemoteException e) {Log.e(TAG, "", e);}
507 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
Lixin Yueefa1dd72009-08-31 15:55:13 +0800510 /**
511 * Get trust state of a remote device.
512 * @hide
513 */
514 public boolean getTrustState() {
515 try {
516 return sService.getTrustState(mAddress);
517 } catch (RemoteException e) {
518 Log.e(TAG, "", e);
519 }
520 return false;
521 }
522
523 /**
524 * Set trust state for a remote device.
525 * @param value the trust state value (true or false)
526 * @hide
527 */
528 public boolean setTrust(boolean value) {
529 try {
530 return sService.setTrust(mAddress, value);
531 } catch (RemoteException e) {
532 Log.e(TAG, "", e);
533 }
534 return false;
535 }
536
Nick Pelly45e27042009-08-19 11:00:00 -0700537 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700538 public ParcelUuid[] getUuids() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700540 return sService.getRemoteUuids(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 } catch (RemoteException e) {Log.e(TAG, "", e);}
542 return null;
543 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700544
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700545 /**
546 * Perform a SDP query on the remote device to get the UUIDs
547 * supported. This API is asynchronous and an Intent is sent,
548 * with the UUIDs supported by the remote end. If there is an error
549 * in getting the SDP records or if the process takes a long time,
550 * an Intent is sent with the UUIDs that is currently present in the
551 * cache. Clients should use the {@link getUuids} to get UUIDs
552 * is SDP is not to be performed.
553 *
554 * @return False if the sanity check fails, True if the process
555 * of initiating an ACL connection to the remote device
556 * was started.
557 * @hide
558 */
559 public boolean fetchUuidsWithSdp() {
560 try {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200561 return sService.fetchRemoteUuids(mAddress, null, null);
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700562 } catch (RemoteException e) {Log.e(TAG, "", e);}
563 return false;
564 }
565
Nick Pelly45e27042009-08-19 11:00:00 -0700566 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700567 public int getServiceChannel(ParcelUuid uuid) {
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700568 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700569 return sService.getRemoteServiceChannel(mAddress, uuid);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700570 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyb24e11b2009-09-08 17:40:43 -0700571 return BluetoothDevice.ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573
Nick Pelly45e27042009-08-19 11:00:00 -0700574 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700575 public boolean setPin(byte[] pin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700577 return sService.setPin(mAddress, pin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 } catch (RemoteException e) {Log.e(TAG, "", e);}
579 return false;
580 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700581
Nick Pelly45e27042009-08-19 11:00:00 -0700582 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700583 public boolean setPasskey(int passkey) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700585 return sService.setPasskey(mAddress, passkey);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700586 } catch (RemoteException e) {Log.e(TAG, "", e);}
587 return false;
588 }
589
Nick Pelly45e27042009-08-19 11:00:00 -0700590 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700591 public boolean setPairingConfirmation(boolean confirm) {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700592 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700593 return sService.setPairingConfirmation(mAddress, confirm);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700594 } catch (RemoteException e) {Log.e(TAG, "", e);}
595 return false;
596 }
597
Nick Pelly45e27042009-08-19 11:00:00 -0700598 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700599 public boolean cancelPairingUserInput() {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700600 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700601 return sService.cancelPairingUserInput(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 } catch (RemoteException e) {Log.e(TAG, "", e);}
603 return false;
604 }
605
606 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700607 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
Nick Pelly16fb88a2009-10-07 07:44:03 +0200608 * outgoing connection to this remote device on given channel.
Nick Pelly45e27042009-08-19 11:00:00 -0700609 * <p>The remote device will be authenticated and communication on this
610 * socket will be encrypted.
611 * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing
612 * connection.
613 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pellycf440592009-09-08 10:12:06 -0700614 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellyde893f52009-09-08 13:15:33 -0700615 *
Nick Pelly45e27042009-08-19 11:00:00 -0700616 * @param channel RFCOMM channel to connect to
617 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
Nick Pellybd022f42009-08-14 18:33:38 -0700618 * @throws IOException on error, for example Bluetooth not available, or
Nick Pelly45e27042009-08-19 11:00:00 -0700619 * insufficient permissions
Nick Pelly16fb88a2009-10-07 07:44:03 +0200620 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700621 */
Nick Pelly45e27042009-08-19 11:00:00 -0700622 public BluetoothSocket createRfcommSocket(int channel) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200623 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel,
624 null);
625 }
626
627 /**
628 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
629 * outgoing connection to this remote device using SDP lookup of uuid.
630 * <p>This is designed to be used with {@link
631 * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer
632 * Bluetooth applications.
633 * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing
634 * connection. This will also perform an SDP lookup of the given uuid to
635 * determine which channel to connect to.
636 * <p>The remote device will be authenticated and communication on this
637 * socket will be encrypted.
638 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
639 *
640 * @param uuid service record uuid to lookup RFCOMM channel
641 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
642 * @throws IOException on error, for example Bluetooth not available, or
643 * insufficient permissions
644 */
645 public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException {
646 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1,
647 new ParcelUuid(uuid));
Nick Pellybd022f42009-08-14 18:33:38 -0700648 }
649
650 /**
651 * Construct an insecure RFCOMM socket ready to start an outgoing
652 * connection.
653 * Call #connect on the returned #BluetoothSocket to begin the connection.
654 * The remote device will not be authenticated and communication on this
655 * socket will not be encrypted.
656 * @param port remote port
657 * @return An RFCOMM BluetoothSocket
658 * @throws IOException On error, for example Bluetooth not available, or
659 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -0700660 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700661 */
662 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200663 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port,
664 null);
Nick Pellybd022f42009-08-14 18:33:38 -0700665 }
666
667 /**
668 * Construct a SCO socket ready to start an outgoing connection.
669 * Call #connect on the returned #BluetoothSocket to begin the connection.
670 * @return a SCO BluetoothSocket
671 * @throws IOException on error, for example Bluetooth not available, or
672 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -0700673 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700674 */
675 public BluetoothSocket createScoSocket() throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200676 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null);
Nick Pellybd022f42009-08-14 18:33:38 -0700677 }
678
679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 * Check that a pin is valid and convert to byte array.
681 *
682 * Bluetooth pin's are 1 to 16 bytes of UTF8 characters.
683 * @param pin pin as java String
684 * @return the pin code as a UTF8 byte array, or null if it is an invalid
685 * Bluetooth pin.
Nick Pelly45e27042009-08-19 11:00:00 -0700686 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 */
688 public static byte[] convertPinToBytes(String pin) {
689 if (pin == null) {
690 return null;
691 }
692 byte[] pinBytes;
693 try {
694 pinBytes = pin.getBytes("UTF8");
695 } catch (UnsupportedEncodingException uee) {
696 Log.e(TAG, "UTF8 not supported?!?"); // this should not happen
697 return null;
698 }
699 if (pinBytes.length <= 0 || pinBytes.length > 16) {
700 return null;
701 }
702 return pinBytes;
703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705}