blob: ce975c22794273e34d985c6eb627d57df255ad53 [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.content.Context;
22import android.os.IBinder;
23import android.os.Parcel;
24import android.os.Parcelable;
Nick Pellyaef439e2009-09-28 12:33:17 -070025import android.os.ParcelUuid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.RemoteException;
Nick Pellybd022f42009-08-14 18:33:38 -070027import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.util.Log;
29
Nick Pellybd022f42009-08-14 18:33:38 -070030import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import java.io.UnsupportedEncodingException;
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -070032import java.util.UUID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34/**
Nick Pellybd022f42009-08-14 18:33:38 -070035 * Represents a remote Bluetooth device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 *
Nick Pelly45e27042009-08-19 11:00:00 -070037 * <p>Use {@link BluetoothAdapter#getRemoteDevice} to create a {@link
38 * BluetoothDevice}.
39 *
40 * <p>This class is really just a thin wrapper for a Bluetooth hardware
41 * address. Objects of this class are immutable. Operations on this class
42 * are performed on the remote Bluetooth hardware address, using the
43 * {@link BluetoothAdapter} that was used to create this {@link
44 * BluetoothDevice}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Nick Pellybd022f42009-08-14 18:33:38 -070046public final class BluetoothDevice implements Parcelable {
47 private static final String TAG = "BluetoothDevice";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Nick Pellyb24e11b2009-09-08 17:40:43 -070049 /**
50 * Sentinel error value for this class. Guaranteed to not equal any other
51 * integer constant in this class. Provided as a convenience for functions
52 * that require a sentinel error value, for example:
Nick Pelly005b2282009-09-10 10:21:56 -070053 * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
54 * BluetoothDevice.ERROR)</code>
Nick Pellyb24e11b2009-09-08 17:40:43 -070055 */
Nick Pelly005b2282009-09-10 10:21:56 -070056 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070057
Nick Pelly005b2282009-09-10 10:21:56 -070058 /**
59 * Broadcast Action: Remote device discovered.
60 * <p>Sent when a remote device is found during discovery.
61 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
62 * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or
63 * {@link #EXTRA_RSSI} if they are available.
64 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
65 */
66 // TODO: Change API to not broadcast RSSI if not available (incoming connection)
67 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
68 public static final String ACTION_FOUND =
69 "android.bluetooth.device.action.FOUND";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Nick Pelly005b2282009-09-10 10:21:56 -070071 /**
72 * Broadcast Action: Remote device disappeared.
73 * <p>Sent when a remote device that was found in the last discovery is not
74 * found in the current discovery.
75 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
76 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
77 * @hide
78 */
79 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
80 public static final String ACTION_DISAPPEARED =
81 "android.bluetooth.device.action.DISAPPEARED";
82
83 /**
84 * Broadcast Action: Bluetooth class of a remote device has changed.
85 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
86 * #EXTRA_CLASS}.
87 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
88 * @see {@link BluetoothClass}
89 */
90 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
91 public static final String ACTION_CLASS_CHANGED =
92 "android.bluetooth.device.action.CLASS_CHANGED";
93
94 /**
95 * Broadcast Action: Indicates a low level (ACL) connection has been
96 * established with a remote device.
97 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
98 * <p>ACL connections are managed automatically by the Android Bluetooth
99 * stack.
100 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
101 */
102 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
103 public static final String ACTION_ACL_CONNECTED =
104 "android.bluetooth.device.action.ACL_CONNECTED";
105
106 /**
107 * Broadcast Action: Indicates that a low level (ACL) disconnection has
108 * been requested for a remote device, and it will soon be disconnected.
109 * <p>This is useful for graceful disconnection. Applications should use
110 * this intent as a hint to immediately terminate higher level connections
111 * (RFCOMM, L2CAP, or profile connections) to the remote device.
112 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
113 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
114 */
115 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
116 public static final String ACTION_ACL_DISCONNECT_REQUESTED =
117 "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED";
118
119 /**
120 * Broadcast Action: Indicates a low level (ACL) disconnection from a
121 * remote device.
122 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
123 * <p>ACL connections are managed automatically by the Android Bluetooth
124 * stack.
125 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
126 */
127 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
128 public static final String ACTION_ACL_DISCONNECTED =
129 "android.bluetooth.device.action.ACL_DISCONNECTED";
130
131 /**
132 * Broadcast Action: Indicates the friendly name of a remote device has
133 * been retrieved for the first time, or changed since the last retrieval.
134 * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link
135 * #EXTRA_NAME}.
136 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
137 */
138 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
139 public static final String ACTION_NAME_CHANGED =
140 "android.bluetooth.device.action.NAME_CHANGED";
141
142 /**
143 * Broadcast Action: Indicates a change in the bond state of a remote
144 * device. For example, if a device is bonded (paired).
145 * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link
146 * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}.
147 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
148 */
149 // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also
150 // contain a hidden extra field EXTRA_REASON with the result code.
151 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
152 public static final String ACTION_BOND_STATE_CHANGED =
153 "android.bluetooth.device.action.BOND_STATE_CHANGED";
154
155 /**
156 * Used as a Parcelable {@link BluetoothDevice} extra field in every intent
157 * broadcast by this class. It contains the {@link BluetoothDevice} that
158 * the intent applies to.
159 */
160 public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
161
162 /**
163 * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link
164 * #ACTION_FOUND} intents. It contains the friendly Bluetooth name.
165 */
166 public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME";
167
168 /**
169 * Used as an optional short extra field in {@link #ACTION_FOUND} intents.
170 * Contains the RSSI value of the remote device as reported by the
171 * Bluetooth hardware.
172 */
173 public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI";
174
175 /**
176 * Used as an Parcelable {@link BluetoothClass} extra field in {@link
177 * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents.
178 */
179 public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS";
180
181 /**
182 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
183 * Contains the bond state of the remote device.
184 * <p>Possible values are:
185 * {@link #BOND_NONE},
186 * {@link #BOND_BONDING},
187 * {@link #BOND_BONDED}.
188 */
189 public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE";
190 /**
191 * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents.
192 * Contains the previous bond state of the remote device.
193 * <p>Possible values are:
194 * {@link #BOND_NONE},
195 * {@link #BOND_BONDING},
196 * {@link #BOND_BONDED}.
197 */
198 public static final String EXTRA_PREVIOUS_BOND_STATE =
199 "android.bluetooth.device.extra.PREVIOUS_BOND_STATE";
200 /**
201 * Indicates the remote device is not bonded (paired).
202 * <p>There is no shared link key with the remote device, so communication
203 * (if it is allowed at all) will be unauthenticated and unencrypted.
204 */
205 public static final int BOND_NONE = 10;
206 /**
207 * Indicates bonding (pairing) is in progress with the remote device.
208 */
209 public static final int BOND_BONDING = 11;
210 /**
211 * Indicates the remote device is bonded (paired).
212 * <p>A shared link keys exists locally for the remote device, so
213 * communication can be authenticated and encrypted.
214 * <p><i>Being bonded (paired) with a remote device does not necessarily
215 * mean the device is currently connected. It just means that the ponding
216 * procedure was compeleted at some earlier time, and the link key is still
217 * stored locally, ready to use on the next connection.
218 * </i>
219 */
220 public static final int BOND_BONDED = 12;
221
222 /** @hide */
223 public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON";
224 /** @hide */
225 public static final String EXTRA_PAIRING_VARIANT =
226 "android.bluetooth.device.extra.PAIRING_VARIANT";
227 /** @hide */
228 public static final String EXTRA_PASSKEY = "android.bluetooth.device.extra.PASSKEY";
229
230 /**
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700231 * Broadcast Action: This intent is used to broadcast the {@link UUID}
Nick Pellyaef439e2009-09-28 12:33:17 -0700232 * wrapped as a {@link android.os.ParcelUuid} of the remote device after it
233 * has been fetched. This intent is sent only when the UUIDs of the remote
234 * device are requested to be fetched using Service Discovery Protocol
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700235 * <p> Always contains the extra field {@link #EXTRA_DEVICE}
236 * <p> Always contains the extra filed {@link #EXTRA_UUID}
237 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
238 * @hide
239 */
240 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
241 public static final String ACTION_UUID =
242 "android.bleutooth.device.action.UUID";
243
244 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700245 * Broadcast Action: Indicates a failure to retrieve the name of a remote
246 * device.
247 * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
248 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
249 * @hide
250 */
251 //TODO: is this actually useful?
252 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
253 public static final String ACTION_NAME_FAILED =
254 "android.bluetooth.device.action.NAME_FAILED";
255
256 /** @hide */
257 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
258 public static final String ACTION_PAIRING_REQUEST =
259 "android.bluetooth.device.action.PAIRING_REQUEST";
260 /** @hide */
261 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
262 public static final String ACTION_PAIRING_CANCEL =
263 "android.bluetooth.device.action.PAIRING_CANCEL";
Yue Lixina4433af2009-07-09 16:56:43 +0800264
Nick Pellyb24e11b2009-09-08 17:40:43 -0700265 /** A bond attempt succeeded
266 * @hide */
267 public static final int BOND_SUCCESS = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /** A bond attempt failed because pins did not match, or remote device did
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700269 * not respond to pin request in time
Nick Pelly45e27042009-08-19 11:00:00 -0700270 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public static final int UNBOND_REASON_AUTH_FAILED = 1;
272 /** A bond attempt failed because the other side explicilty rejected
Nick Pelly45e27042009-08-19 11:00:00 -0700273 * bonding
274 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 public static final int UNBOND_REASON_AUTH_REJECTED = 2;
Nick Pelly45e27042009-08-19 11:00:00 -0700276 /** A bond attempt failed because we canceled the bonding process
277 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 public static final int UNBOND_REASON_AUTH_CANCELED = 3;
Nick Pelly45e27042009-08-19 11:00:00 -0700279 /** A bond attempt failed because we could not contact the remote device
280 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4;
Nick Pelly45e27042009-08-19 11:00:00 -0700282 /** A bond attempt failed because a discovery is in progress
283 * @hide */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5;
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700285 /** A bond attempt failed because of authentication timeout
286 * @hide */
287 public static final int UNBOND_REASON_AUTH_TIMEOUT = 6;
288 /** A bond attempt failed because of repeated attempts
289 * @hide */
290 public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7;
Nick Pelly45e27042009-08-19 11:00:00 -0700291 /** An existing bond was explicitly revoked
292 * @hide */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700293 public static final int UNBOND_REASON_REMOVED = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
Nick Pelly45e27042009-08-19 11:00:00 -0700295 /** The user will be prompted to enter a pin
296 * @hide */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700297 public static final int PAIRING_VARIANT_PIN = 0;
Nick Pelly45e27042009-08-19 11:00:00 -0700298 /** The user will be prompted to enter a passkey
299 * @hide */
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700300 public static final int PAIRING_VARIANT_PASSKEY = 1;
Nick Pelly45e27042009-08-19 11:00:00 -0700301 /** The user will be prompted to confirm the passkey displayed on the screen
302 * @hide */
Jaikumar Ganesh32d85712009-09-10 22:00:05 -0700303 public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2;
304 /** The user will be prompted to accept or deny the incoming pairing request
305 * @hide */
306 public static final int PAIRING_VARIANT_CONSENT = 3;
307 /** The user will be prompted to enter the passkey displayed on remote device
308 * @hide */
309 public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4;
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700310
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700311 /**
312 * Used as an extra field in {@link #ACTION_UUID} intents,
Nick Pellyaef439e2009-09-28 12:33:17 -0700313 * Contains the {@link android.os.ParcelUuid}s of the remote device which
314 * is a parcelable version of {@link UUID}.
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700315 * @hide
316 */
317 public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID";
318
Nick Pelly16fb88a2009-10-07 07:44:03 +0200319 /**
320 * Lazy initialization. Guaranteed final after first object constructed, or
321 * getService() called.
322 * TODO: Unify implementation of sService amongst BluetoothFoo API's
323 */
324 private static IBluetooth sService;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700325
Nick Pellybd022f42009-08-14 18:33:38 -0700326 private final String mAddress;
327
Nick Pelly16fb88a2009-10-07 07:44:03 +0200328 /*package*/ static IBluetooth getService() {
329 synchronized (BluetoothDevice.class) {
330 if (sService == null) {
331 IBinder b = ServiceManager.getService(Context.BLUETOOTH_SERVICE);
332 if (b == null) {
333 throw new RuntimeException("Bluetooth service not available");
334 }
335 sService = IBluetooth.Stub.asInterface(b);
336 }
337 }
338 return sService;
339 }
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 /**
Nick Pellybd022f42009-08-14 18:33:38 -0700342 * Create a new BluetoothDevice
343 * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB",
344 * and is validated in this constructor.
345 * @param address valid Bluetooth MAC address
346 * @throws RuntimeException Bluetooth is not available on this platform
347 * @throws IllegalArgumentException address is invalid
348 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Nick Pellybd022f42009-08-14 18:33:38 -0700350 /*package*/ BluetoothDevice(String address) {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200351 getService(); // ensures sService is initialized
Nick Pelly005b2282009-09-10 10:21:56 -0700352 if (!BluetoothAdapter.checkBluetoothAddress(address)) {
Nick Pellybd022f42009-08-14 18:33:38 -0700353 throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
354 }
355
356 mAddress = address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
Nick Pellybd022f42009-08-14 18:33:38 -0700359 @Override
360 public boolean equals(Object o) {
361 if (o instanceof BluetoothDevice) {
362 return mAddress.equals(((BluetoothDevice)o).getAddress());
363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 return false;
365 }
366
Nick Pellybd022f42009-08-14 18:33:38 -0700367 @Override
368 public int hashCode() {
369 return mAddress.hashCode();
The Android Open Source Project10592532009-03-18 17:39:46 -0700370 }
371
Nick Pelly45e27042009-08-19 11:00:00 -0700372 /**
373 * Returns a string representation of this BluetoothDevice.
374 * <p>Currently this is the Bluetooth hardware address, for example
375 * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress}
376 * if you explicitly require the Bluetooth hardware address in case the
377 * {@link #toString} representation changes in the future.
378 * @return string representation of this BluetoothDevice
379 */
Nick Pellybd022f42009-08-14 18:33:38 -0700380 @Override
381 public String toString() {
382 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384
Nick Pellybd022f42009-08-14 18:33:38 -0700385 public int describeContents() {
386 return 0;
387 }
388
389 public static final Parcelable.Creator<BluetoothDevice> CREATOR =
390 new Parcelable.Creator<BluetoothDevice>() {
391 public BluetoothDevice createFromParcel(Parcel in) {
392 return new BluetoothDevice(in.readString());
393 }
394 public BluetoothDevice[] newArray(int size) {
395 return new BluetoothDevice[size];
396 }
397 };
398
399 public void writeToParcel(Parcel out, int flags) {
400 out.writeString(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402
Nick Pelly45e27042009-08-19 11:00:00 -0700403 /**
404 * Returns the hardware address of this BluetoothDevice.
405 * <p> For example, "00:11:22:AA:BB:CC".
406 * @return Bluetooth hardware address as string
407 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 public String getAddress() {
Nick Pellybd022f42009-08-14 18:33:38 -0700409 return mAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411
412 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700413 * Get the friendly Bluetooth name of the remote device.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 *
Nick Pelly45e27042009-08-19 11:00:00 -0700415 * <p>The local adapter will automatically retrieve remote names when
416 * performing a device scan, and will cache them. This method just returns
417 * the name for this device from the cache.
Nick Pellyde893f52009-09-08 13:15:33 -0700418 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 *
420 * @return the Bluetooth name, or null if there was a problem.
421 */
422 public String getName() {
423 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700424 return sService.getRemoteName(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 } catch (RemoteException e) {Log.e(TAG, "", e);}
426 return null;
427 }
428
429 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700430 * Start the bonding (pairing) process with the remote device.
431 * <p>This is an asynchronous call, it will return immediately. Register
432 * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
433 * the bonding process completes, and its result.
434 * <p>Android system services will handle the necessary user interactions
435 * to confirm and complete the bonding process.
436 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 *
Nick Pelly005b2282009-09-10 10:21:56 -0700438 * @return false on immediate error, true if bonding will begin
Nick Pelly18b1e792009-09-24 11:14:15 -0700439 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 */
Nick Pellybd022f42009-08-14 18:33:38 -0700441 public boolean createBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700443 return sService.createBond(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 } catch (RemoteException e) {Log.e(TAG, "", e);}
445 return false;
446 }
447
448 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700449 * Cancel an in-progress bonding request started with {@link #createBond}.
450 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
451 *
452 * @return true on sucess, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700453 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 */
Nick Pellybd022f42009-08-14 18:33:38 -0700455 public boolean cancelBondProcess() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700457 return sService.cancelBondProcess(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 } catch (RemoteException e) {Log.e(TAG, "", e);}
459 return false;
460 }
461
462 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700463 * Remove bond (pairing) with the remote device.
464 * <p>Delete the link key associated with the remote device, and
465 * immediately terminate connections to that device that require
466 * authentication and encryption.
467 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 *
Nick Pelly005b2282009-09-10 10:21:56 -0700469 * @return true on sucess, false on error
Nick Pelly18b1e792009-09-24 11:14:15 -0700470 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 */
Nick Pellybd022f42009-08-14 18:33:38 -0700472 public boolean removeBond() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700474 return sService.removeBond(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellybd022f42009-08-14 18:33:38 -0700476 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478
479 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700480 * Get the bond state of the remote device.
481 * <p>Possible values for the bond state are:
482 * {@link #BOND_NONE},
483 * {@link #BOND_BONDING},
484 * {@link #BOND_BONDED}.
485 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 *
Nick Pelly005b2282009-09-10 10:21:56 -0700487 * @return the bond state
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 */
Nick Pellybd022f42009-08-14 18:33:38 -0700489 public int getBondState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700491 return sService.getBondState(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700493 return BOND_NONE;
494 }
495
496 /**
497 * Get the Bluetooth class of the remote device.
498 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
499 *
500 * @return Bluetooth class object, or null on error
501 */
502 public BluetoothClass getBluetoothClass() {
503 try {
504 int classInt = sService.getRemoteClass(mAddress);
505 if (classInt == BluetoothClass.ERROR) return null;
506 return new BluetoothClass(classInt);
507 } catch (RemoteException e) {Log.e(TAG, "", e);}
508 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510
Lixin Yueefa1dd72009-08-31 15:55:13 +0800511 /**
512 * Get trust state of a remote device.
513 * @hide
514 */
515 public boolean getTrustState() {
516 try {
517 return sService.getTrustState(mAddress);
518 } catch (RemoteException e) {
519 Log.e(TAG, "", e);
520 }
521 return false;
522 }
523
524 /**
525 * Set trust state for a remote device.
526 * @param value the trust state value (true or false)
527 * @hide
528 */
529 public boolean setTrust(boolean value) {
530 try {
531 return sService.setTrust(mAddress, value);
532 } catch (RemoteException e) {
533 Log.e(TAG, "", e);
534 }
535 return false;
536 }
537
Nick Pelly45e27042009-08-19 11:00:00 -0700538 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700539 public ParcelUuid[] getUuids() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700541 return sService.getRemoteUuids(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 } catch (RemoteException e) {Log.e(TAG, "", e);}
543 return null;
544 }
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700545
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700546 /**
547 * Perform a SDP query on the remote device to get the UUIDs
548 * supported. This API is asynchronous and an Intent is sent,
549 * with the UUIDs supported by the remote end. If there is an error
550 * in getting the SDP records or if the process takes a long time,
551 * an Intent is sent with the UUIDs that is currently present in the
552 * cache. Clients should use the {@link getUuids} to get UUIDs
553 * is SDP is not to be performed.
554 *
555 * @return False if the sanity check fails, True if the process
556 * of initiating an ACL connection to the remote device
557 * was started.
558 * @hide
559 */
560 public boolean fetchUuidsWithSdp() {
561 try {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200562 return sService.fetchRemoteUuids(mAddress, null, null);
Jaikumar Ganesh1caa6d12009-09-18 11:32:54 -0700563 } catch (RemoteException e) {Log.e(TAG, "", e);}
564 return false;
565 }
566
Nick Pelly45e27042009-08-19 11:00:00 -0700567 /** @hide */
Jaikumar Ganeshdd0463a2009-09-16 12:30:02 -0700568 public int getServiceChannel(ParcelUuid uuid) {
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700569 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700570 return sService.getRemoteServiceChannel(mAddress, uuid);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700571 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyb24e11b2009-09-08 17:40:43 -0700572 return BluetoothDevice.ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574
Nick Pelly45e27042009-08-19 11:00:00 -0700575 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700576 public boolean setPin(byte[] pin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700578 return sService.setPin(mAddress, pin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 } catch (RemoteException e) {Log.e(TAG, "", e);}
580 return false;
581 }
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700582
Nick Pelly45e27042009-08-19 11:00:00 -0700583 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700584 public boolean setPasskey(int passkey) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700586 return sService.setPasskey(mAddress, passkey);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700587 } catch (RemoteException e) {Log.e(TAG, "", e);}
588 return false;
589 }
590
Nick Pelly45e27042009-08-19 11:00:00 -0700591 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700592 public boolean setPairingConfirmation(boolean confirm) {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700593 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700594 return sService.setPairingConfirmation(mAddress, confirm);
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700595 } catch (RemoteException e) {Log.e(TAG, "", e);}
596 return false;
597 }
598
Nick Pelly45e27042009-08-19 11:00:00 -0700599 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700600 public boolean cancelPairingUserInput() {
Jaikumar Ganeshb0eca412009-07-16 18:26:28 -0700601 try {
Nick Pellybd022f42009-08-14 18:33:38 -0700602 return sService.cancelPairingUserInput(mAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 } catch (RemoteException e) {Log.e(TAG, "", e);}
604 return false;
605 }
606
607 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700608 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
Nick Pelly16fb88a2009-10-07 07:44:03 +0200609 * outgoing connection to this remote device on given channel.
Nick Pelly45e27042009-08-19 11:00:00 -0700610 * <p>The remote device will be authenticated and communication on this
611 * socket will be encrypted.
612 * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing
613 * connection.
614 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pellycf440592009-09-08 10:12:06 -0700615 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellyde893f52009-09-08 13:15:33 -0700616 *
Nick Pelly45e27042009-08-19 11:00:00 -0700617 * @param channel RFCOMM channel to connect to
618 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
Nick Pellybd022f42009-08-14 18:33:38 -0700619 * @throws IOException on error, for example Bluetooth not available, or
Nick Pelly45e27042009-08-19 11:00:00 -0700620 * insufficient permissions
Nick Pelly16fb88a2009-10-07 07:44:03 +0200621 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700622 */
Nick Pelly45e27042009-08-19 11:00:00 -0700623 public BluetoothSocket createRfcommSocket(int channel) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200624 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel,
625 null);
626 }
627
628 /**
629 * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
630 * outgoing connection to this remote device using SDP lookup of uuid.
631 * <p>This is designed to be used with {@link
632 * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer
633 * Bluetooth applications.
634 * <p>Use {@link BluetoothSocket#connect} to intiate the outgoing
635 * connection. This will also perform an SDP lookup of the given uuid to
636 * determine which channel to connect to.
637 * <p>The remote device will be authenticated and communication on this
638 * socket will be encrypted.
639 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
640 *
641 * @param uuid service record uuid to lookup RFCOMM channel
642 * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection
643 * @throws IOException on error, for example Bluetooth not available, or
644 * insufficient permissions
645 */
646 public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException {
647 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1,
648 new ParcelUuid(uuid));
Nick Pellybd022f42009-08-14 18:33:38 -0700649 }
650
651 /**
652 * Construct an insecure RFCOMM socket ready to start an outgoing
653 * connection.
654 * Call #connect on the returned #BluetoothSocket to begin the connection.
655 * The remote device will not be authenticated and communication on this
656 * socket will not be encrypted.
657 * @param port remote port
658 * @return An RFCOMM BluetoothSocket
659 * @throws IOException On error, for example Bluetooth not available, or
660 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -0700661 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700662 */
663 public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200664 return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port,
665 null);
Nick Pellybd022f42009-08-14 18:33:38 -0700666 }
667
668 /**
669 * Construct a SCO socket ready to start an outgoing connection.
670 * Call #connect on the returned #BluetoothSocket to begin the connection.
671 * @return a SCO BluetoothSocket
672 * @throws IOException on error, for example Bluetooth not available, or
673 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -0700674 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700675 */
676 public BluetoothSocket createScoSocket() throws IOException {
Nick Pelly16fb88a2009-10-07 07:44:03 +0200677 return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null);
Nick Pellybd022f42009-08-14 18:33:38 -0700678 }
679
680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * Check that a pin is valid and convert to byte array.
682 *
683 * Bluetooth pin's are 1 to 16 bytes of UTF8 characters.
684 * @param pin pin as java String
685 * @return the pin code as a UTF8 byte array, or null if it is an invalid
686 * Bluetooth pin.
Nick Pelly45e27042009-08-19 11:00:00 -0700687 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static byte[] convertPinToBytes(String pin) {
690 if (pin == null) {
691 return null;
692 }
693 byte[] pinBytes;
694 try {
695 pinBytes = pin.getBytes("UTF8");
696 } catch (UnsupportedEncodingException uee) {
697 Log.e(TAG, "UTF8 not supported?!?"); // this should not happen
698 return null;
699 }
700 if (pinBytes.length <= 0 || pinBytes.length > 16) {
701 return null;
702 }
703 return pinBytes;
704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706}