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