The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package android.bluetooth; |
| 18 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 24 | import android.os.ParcelUuid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 26 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 29 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import java.io.UnsupportedEncodingException; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 31 | import java.util.UUID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 34 | * Represents a remote Bluetooth device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 36 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 45 | public final class BluetoothDevice implements Parcelable { |
| 46 | private static final String TAG = "BluetoothDevice"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 48 | /** |
| 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 Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 52 | * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, |
| 53 | * BluetoothDevice.ERROR)</code> |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 54 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 55 | public static final int ERROR = Integer.MIN_VALUE; |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 56 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 57 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 70 | /** |
| 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 Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 230 | * Broadcast Action: This intent is used to broadcast the {@link UUID} |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 231 | * 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 Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 234 | * <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 Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 244 | * 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 Lixin | a4433af | 2009-07-09 16:56:43 +0800 | [diff] [blame] | 263 | |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 264 | /** A bond attempt succeeded |
| 265 | * @hide */ |
| 266 | public static final int BOND_SUCCESS = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | /** A bond attempt failed because pins did not match, or remote device did |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 268 | * not respond to pin request in time |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 269 | * @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | public static final int UNBOND_REASON_AUTH_FAILED = 1; |
| 271 | /** A bond attempt failed because the other side explicilty rejected |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 272 | * bonding |
| 273 | * @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | public static final int UNBOND_REASON_AUTH_REJECTED = 2; |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 275 | /** A bond attempt failed because we canceled the bonding process |
| 276 | * @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | public static final int UNBOND_REASON_AUTH_CANCELED = 3; |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 278 | /** A bond attempt failed because we could not contact the remote device |
| 279 | * @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4; |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 281 | /** A bond attempt failed because a discovery is in progress |
| 282 | * @hide */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5; |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 284 | /** 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 Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 290 | /** An existing bond was explicitly revoked |
| 291 | * @hide */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 292 | public static final int UNBOND_REASON_REMOVED = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 294 | /** The user will be prompted to enter a pin |
| 295 | * @hide */ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 296 | public static final int PAIRING_VARIANT_PIN = 0; |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 297 | /** The user will be prompted to enter a passkey |
| 298 | * @hide */ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 299 | public static final int PAIRING_VARIANT_PASSKEY = 1; |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 300 | /** The user will be prompted to confirm the passkey displayed on the screen |
| 301 | * @hide */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 302 | 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 Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 309 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Used as an extra field in {@link #ACTION_UUID} intents, |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 312 | * Contains the {@link android.os.ParcelUuid}s of the remote device which |
| 313 | * is a parcelable version of {@link UUID}. |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 314 | * @hide |
| 315 | */ |
| 316 | public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID"; |
| 317 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 318 | /** |
| 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 Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 324 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 325 | private final String mAddress; |
| 326 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 327 | /*package*/ static IBluetooth getService() { |
| 328 | synchronized (BluetoothDevice.class) { |
| 329 | if (sService == null) { |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame^] | 330 | IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 331 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 341 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 349 | /*package*/ BluetoothDevice(String address) { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 350 | getService(); // ensures sService is initialized |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 351 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 352 | throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); |
| 353 | } |
| 354 | |
| 355 | mAddress = address; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 358 | @Override |
| 359 | public boolean equals(Object o) { |
| 360 | if (o instanceof BluetoothDevice) { |
| 361 | return mAddress.equals(((BluetoothDevice)o).getAddress()); |
| 362 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 366 | @Override |
| 367 | public int hashCode() { |
| 368 | return mAddress.hashCode(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 371 | /** |
| 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 Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 379 | @Override |
| 380 | public String toString() { |
| 381 | return mAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 384 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 402 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | public String getAddress() { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 408 | return mAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 412 | * Get the friendly Bluetooth name of the remote device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 414 | * <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 Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 417 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | * |
| 419 | * @return the Bluetooth name, or null if there was a problem. |
| 420 | */ |
| 421 | public String getName() { |
| 422 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 423 | return sService.getRemoteName(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 425 | return null; |
| 426 | } |
| 427 | |
| 428 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 429 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 437 | * @return false on immediate error, true if bonding will begin |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 438 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 440 | public boolean createBond() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 442 | return sService.createBond(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 448 | * 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 Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 452 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 454 | public boolean cancelBondProcess() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 456 | return sService.cancelBondProcess(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 462 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 468 | * @return true on sucess, false on error |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 469 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 471 | public boolean removeBond() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 473 | return sService.removeBond(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 475 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 479 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 486 | * @return the bond state |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 488 | public int getBondState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 490 | return sService.getBondState(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 492 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 510 | /** |
| 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 Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 537 | /** @hide */ |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 538 | public ParcelUuid[] getUuids() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 540 | return sService.getRemoteUuids(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 542 | return null; |
| 543 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 544 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 545 | /** |
| 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 Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 561 | return sService.fetchRemoteUuids(mAddress, null, null); |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 562 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 563 | return false; |
| 564 | } |
| 565 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 566 | /** @hide */ |
Jaikumar Ganesh | dd0463a | 2009-09-16 12:30:02 -0700 | [diff] [blame] | 567 | public int getServiceChannel(ParcelUuid uuid) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 568 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 569 | return sService.getRemoteServiceChannel(mAddress, uuid); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 570 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 571 | return BluetoothDevice.ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | } |
| 573 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 574 | /** @hide */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 575 | public boolean setPin(byte[] pin) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 577 | return sService.setPin(mAddress, pin); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 579 | return false; |
| 580 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 581 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 582 | /** @hide */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 583 | public boolean setPasskey(int passkey) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 585 | return sService.setPasskey(mAddress, passkey); |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 586 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 587 | return false; |
| 588 | } |
| 589 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 590 | /** @hide */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 591 | public boolean setPairingConfirmation(boolean confirm) { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 592 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 593 | return sService.setPairingConfirmation(mAddress, confirm); |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 594 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 595 | return false; |
| 596 | } |
| 597 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 598 | /** @hide */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 599 | public boolean cancelPairingUserInput() { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 600 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 601 | return sService.cancelPairingUserInput(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 607 | * Create an RFCOMM {@link BluetoothSocket} ready to start a secure |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 608 | * outgoing connection to this remote device on given channel. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 609 | * <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 Pelly | cf44059 | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 614 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 615 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 616 | * @param channel RFCOMM channel to connect to |
| 617 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 618 | * @throws IOException on error, for example Bluetooth not available, or |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 619 | * insufficient permissions |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 620 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 621 | */ |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 622 | public BluetoothSocket createRfcommSocket(int channel) throws IOException { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 623 | 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 Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 648 | } |
| 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 Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 660 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 661 | */ |
| 662 | public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 663 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, |
| 664 | null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 665 | } |
| 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 Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 673 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 674 | */ |
| 675 | public BluetoothSocket createScoSocket() throws IOException { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 676 | return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | * 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 Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 686 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | */ |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } |