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 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.annotation.RequiresPermission; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
| 22 | import android.annotation.SdkConstant.SdkConstantType; |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 28 | import android.os.ParcelUuid; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 29 | import android.os.Parcelable; |
Christine Hallstrom | 044b8f3 | 2015-11-17 12:24:07 -0800 | [diff] [blame] | 30 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.util.Log; |
| 33 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 34 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import java.io.UnsupportedEncodingException; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 36 | import java.util.UUID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 39 | * Represents a remote Bluetooth device. A {@link BluetoothDevice} lets you |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 40 | * create a connection with the respective device or query information about |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 41 | * it, such as the name, address, class, and bonding state. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 42 | * |
| 43 | * <p>This class is really just a thin wrapper for a Bluetooth hardware |
| 44 | * address. Objects of this class are immutable. Operations on this class |
| 45 | * are performed on the remote Bluetooth hardware address, using the |
| 46 | * {@link BluetoothAdapter} that was used to create this {@link |
| 47 | * BluetoothDevice}. |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 48 | * |
| 49 | * <p>To get a {@link BluetoothDevice}, use |
| 50 | * {@link BluetoothAdapter#getRemoteDevice(String) |
| 51 | * BluetoothAdapter.getRemoteDevice(String)} to create one representing a device |
| 52 | * of a known MAC address (which you can get through device discovery with |
| 53 | * {@link BluetoothAdapter}) or get one from the set of bonded devices |
| 54 | * returned by {@link BluetoothAdapter#getBondedDevices() |
| 55 | * BluetoothAdapter.getBondedDevices()}. You can then open a |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 56 | * {@link BluetoothSocket} for communication with the remote device, using |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 57 | * {@link #createRfcommSocketToServiceRecord(UUID)}. |
| 58 | * |
| 59 | * <p class="note"><strong>Note:</strong> |
| 60 | * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. |
| 61 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 62 | * <div class="special reference"> |
| 63 | * <h3>Developer Guides</h3> |
Hemal Patel | 65813df | 2016-08-17 13:18:14 -0700 | [diff] [blame] | 64 | * <p> |
| 65 | * For more information about using Bluetooth, read the <a href= |
| 66 | * "{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer |
| 67 | * guide. |
| 68 | * </p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 69 | * </div> |
| 70 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 71 | * {@see BluetoothAdapter} |
| 72 | * {@see BluetoothSocket} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 74 | public final class BluetoothDevice implements Parcelable { |
| 75 | private static final String TAG = "BluetoothDevice"; |
Ravi Nagarajan | 7077272 | 2012-04-23 04:06:49 -0700 | [diff] [blame] | 76 | private static final boolean DBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 78 | /** |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 79 | * Connection state bitmask as returned by getConnectionState. |
| 80 | */ |
| 81 | private static final int CONNECTION_STATE_DISCONNECTED = 0; |
| 82 | private static final int CONNECTION_STATE_CONNECTED = 1; |
| 83 | private static final int CONNECTION_STATE_ENCRYPTED_BREDR = 2; |
| 84 | private static final int CONNECTION_STATE_ENCRYPTED_LE = 4; |
| 85 | |
| 86 | /** |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 87 | * Sentinel error value for this class. Guaranteed to not equal any other |
| 88 | * integer constant in this class. Provided as a convenience for functions |
| 89 | * that require a sentinel error value, for example: |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 90 | * <p><code>Intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, |
| 91 | * BluetoothDevice.ERROR)</code> |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 92 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 93 | public static final int ERROR = Integer.MIN_VALUE; |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 94 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Broadcast Action: Remote device discovered. |
| 97 | * <p>Sent when a remote device is found during discovery. |
| 98 | * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link |
| 99 | * #EXTRA_CLASS}. Can contain the extra fields {@link #EXTRA_NAME} and/or |
| 100 | * {@link #EXTRA_RSSI} if they are available. |
Fyodor Kupolov | 72f491d | 2015-07-14 11:38:58 -0700 | [diff] [blame] | 101 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} and |
| 102 | * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} to receive. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 103 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 104 | // TODO: Change API to not broadcast RSSI if not available (incoming connection) |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 105 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 106 | public static final String ACTION_FOUND = |
| 107 | "android.bluetooth.device.action.FOUND"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 109 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 110 | * Broadcast Action: Bluetooth class of a remote device has changed. |
| 111 | * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link |
| 112 | * #EXTRA_CLASS}. |
| 113 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
Matthew Xie | b30f91e | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 114 | * {@see BluetoothClass} |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 115 | */ |
| 116 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 117 | public static final String ACTION_CLASS_CHANGED = |
| 118 | "android.bluetooth.device.action.CLASS_CHANGED"; |
| 119 | |
| 120 | /** |
| 121 | * Broadcast Action: Indicates a low level (ACL) connection has been |
| 122 | * established with a remote device. |
| 123 | * <p>Always contains the extra field {@link #EXTRA_DEVICE}. |
| 124 | * <p>ACL connections are managed automatically by the Android Bluetooth |
| 125 | * stack. |
| 126 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 127 | */ |
| 128 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 129 | public static final String ACTION_ACL_CONNECTED = |
| 130 | "android.bluetooth.device.action.ACL_CONNECTED"; |
| 131 | |
| 132 | /** |
| 133 | * Broadcast Action: Indicates that a low level (ACL) disconnection has |
| 134 | * been requested for a remote device, and it will soon be disconnected. |
| 135 | * <p>This is useful for graceful disconnection. Applications should use |
| 136 | * this intent as a hint to immediately terminate higher level connections |
| 137 | * (RFCOMM, L2CAP, or profile connections) to the remote device. |
| 138 | * <p>Always contains the extra field {@link #EXTRA_DEVICE}. |
| 139 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 140 | */ |
| 141 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 142 | public static final String ACTION_ACL_DISCONNECT_REQUESTED = |
| 143 | "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"; |
| 144 | |
| 145 | /** |
| 146 | * Broadcast Action: Indicates a low level (ACL) disconnection from a |
| 147 | * remote device. |
| 148 | * <p>Always contains the extra field {@link #EXTRA_DEVICE}. |
| 149 | * <p>ACL connections are managed automatically by the Android Bluetooth |
| 150 | * stack. |
| 151 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 152 | */ |
| 153 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 154 | public static final String ACTION_ACL_DISCONNECTED = |
| 155 | "android.bluetooth.device.action.ACL_DISCONNECTED"; |
| 156 | |
| 157 | /** |
| 158 | * Broadcast Action: Indicates the friendly name of a remote device has |
| 159 | * been retrieved for the first time, or changed since the last retrieval. |
| 160 | * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link |
| 161 | * #EXTRA_NAME}. |
| 162 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 163 | */ |
| 164 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 165 | public static final String ACTION_NAME_CHANGED = |
| 166 | "android.bluetooth.device.action.NAME_CHANGED"; |
| 167 | |
| 168 | /** |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 169 | * Broadcast Action: Indicates the alias of a remote device has been |
| 170 | * changed. |
| 171 | * <p>Always contains the extra field {@link #EXTRA_DEVICE}. |
| 172 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 173 | * |
| 174 | * @hide |
| 175 | */ |
| 176 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 177 | @UnsupportedAppUsage |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 178 | public static final String ACTION_ALIAS_CHANGED = |
| 179 | "android.bluetooth.device.action.ALIAS_CHANGED"; |
| 180 | |
| 181 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 182 | * Broadcast Action: Indicates a change in the bond state of a remote |
| 183 | * device. For example, if a device is bonded (paired). |
| 184 | * <p>Always contains the extra fields {@link #EXTRA_DEVICE}, {@link |
| 185 | * #EXTRA_BOND_STATE} and {@link #EXTRA_PREVIOUS_BOND_STATE}. |
| 186 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 187 | */ |
| 188 | // Note: When EXTRA_BOND_STATE is BOND_NONE then this will also |
| 189 | // contain a hidden extra field EXTRA_REASON with the result code. |
| 190 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 191 | public static final String ACTION_BOND_STATE_CHANGED = |
| 192 | "android.bluetooth.device.action.BOND_STATE_CHANGED"; |
| 193 | |
| 194 | /** |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 195 | * Broadcast Action: Indicates the battery level of a remote device has |
| 196 | * been retrieved for the first time, or changed since the last retrieval |
| 197 | * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link |
| 198 | * #EXTRA_BATTERY_LEVEL}. |
| 199 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 200 | * |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 201 | * @hide |
| 202 | */ |
| 203 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 204 | public static final String ACTION_BATTERY_LEVEL_CHANGED = |
| 205 | "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"; |
| 206 | |
| 207 | /** |
| 208 | * Used as an Integer extra field in {@link #ACTION_BATTERY_LEVEL_CHANGED} |
| 209 | * intent. It contains the most recently retrieved battery level information |
| 210 | * ranging from 0% to 100% for a remote device, {@link #BATTERY_LEVEL_UNKNOWN} |
| 211 | * when the valid is unknown or there is an error |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 212 | * |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 213 | * @hide |
| 214 | */ |
| 215 | public static final String EXTRA_BATTERY_LEVEL = |
| 216 | "android.bluetooth.device.extra.BATTERY_LEVEL"; |
| 217 | |
| 218 | /** |
| 219 | * Used as the unknown value for {@link #EXTRA_BATTERY_LEVEL} and {@link #getBatteryLevel()} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 220 | * |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 221 | * @hide |
| 222 | */ |
| 223 | public static final int BATTERY_LEVEL_UNKNOWN = -1; |
| 224 | |
| 225 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 226 | * Used as a Parcelable {@link BluetoothDevice} extra field in every intent |
| 227 | * broadcast by this class. It contains the {@link BluetoothDevice} that |
| 228 | * the intent applies to. |
| 229 | */ |
| 230 | public static final String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE"; |
| 231 | |
| 232 | /** |
| 233 | * Used as a String extra field in {@link #ACTION_NAME_CHANGED} and {@link |
| 234 | * #ACTION_FOUND} intents. It contains the friendly Bluetooth name. |
| 235 | */ |
| 236 | public static final String EXTRA_NAME = "android.bluetooth.device.extra.NAME"; |
| 237 | |
| 238 | /** |
| 239 | * Used as an optional short extra field in {@link #ACTION_FOUND} intents. |
| 240 | * Contains the RSSI value of the remote device as reported by the |
| 241 | * Bluetooth hardware. |
| 242 | */ |
| 243 | public static final String EXTRA_RSSI = "android.bluetooth.device.extra.RSSI"; |
| 244 | |
| 245 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 246 | * Used as a Parcelable {@link BluetoothClass} extra field in {@link |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 247 | * #ACTION_FOUND} and {@link #ACTION_CLASS_CHANGED} intents. |
| 248 | */ |
| 249 | public static final String EXTRA_CLASS = "android.bluetooth.device.extra.CLASS"; |
| 250 | |
| 251 | /** |
| 252 | * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents. |
| 253 | * Contains the bond state of the remote device. |
| 254 | * <p>Possible values are: |
| 255 | * {@link #BOND_NONE}, |
| 256 | * {@link #BOND_BONDING}, |
| 257 | * {@link #BOND_BONDED}. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 258 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 259 | public static final String EXTRA_BOND_STATE = "android.bluetooth.device.extra.BOND_STATE"; |
| 260 | /** |
| 261 | * Used as an int extra field in {@link #ACTION_BOND_STATE_CHANGED} intents. |
| 262 | * Contains the previous bond state of the remote device. |
| 263 | * <p>Possible values are: |
| 264 | * {@link #BOND_NONE}, |
| 265 | * {@link #BOND_BONDING}, |
| 266 | * {@link #BOND_BONDED}. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 267 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 268 | public static final String EXTRA_PREVIOUS_BOND_STATE = |
| 269 | "android.bluetooth.device.extra.PREVIOUS_BOND_STATE"; |
| 270 | /** |
| 271 | * Indicates the remote device is not bonded (paired). |
| 272 | * <p>There is no shared link key with the remote device, so communication |
| 273 | * (if it is allowed at all) will be unauthenticated and unencrypted. |
| 274 | */ |
| 275 | public static final int BOND_NONE = 10; |
| 276 | /** |
| 277 | * Indicates bonding (pairing) is in progress with the remote device. |
| 278 | */ |
| 279 | public static final int BOND_BONDING = 11; |
| 280 | /** |
| 281 | * Indicates the remote device is bonded (paired). |
| 282 | * <p>A shared link keys exists locally for the remote device, so |
| 283 | * communication can be authenticated and encrypted. |
| 284 | * <p><i>Being bonded (paired) with a remote device does not necessarily |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 285 | * mean the device is currently connected. It just means that the pending |
| 286 | * procedure was completed at some earlier time, and the link key is still |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 287 | * stored locally, ready to use on the next connection. |
| 288 | * </i> |
| 289 | */ |
| 290 | public static final int BOND_BONDED = 12; |
| 291 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} |
| 294 | * intents for unbond reason. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 295 | * |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 296 | * @hide |
| 297 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 298 | @UnsupportedAppUsage |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 299 | public static final String EXTRA_REASON = "android.bluetooth.device.extra.REASON"; |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} |
| 303 | * intents to indicate pairing method used. Possible values are: |
| 304 | * {@link #PAIRING_VARIANT_PIN}, |
| 305 | * {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION}, |
| 306 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 307 | public static final String EXTRA_PAIRING_VARIANT = |
| 308 | "android.bluetooth.device.extra.PAIRING_VARIANT"; |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 309 | |
| 310 | /** |
| 311 | * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} |
| 312 | * intents as the value of passkey. |
| 313 | */ |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 314 | public static final String EXTRA_PAIRING_KEY = "android.bluetooth.device.extra.PAIRING_KEY"; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 315 | |
| 316 | /** |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 317 | * Bluetooth device type, Unknown |
| 318 | */ |
| 319 | public static final int DEVICE_TYPE_UNKNOWN = 0; |
| 320 | |
| 321 | /** |
| 322 | * Bluetooth device type, Classic - BR/EDR devices |
| 323 | */ |
| 324 | public static final int DEVICE_TYPE_CLASSIC = 1; |
| 325 | |
| 326 | /** |
| 327 | * Bluetooth device type, Low Energy - LE-only |
| 328 | */ |
| 329 | public static final int DEVICE_TYPE_LE = 2; |
| 330 | |
| 331 | /** |
| 332 | * Bluetooth device type, Dual Mode - BR/EDR/LE |
| 333 | */ |
| 334 | public static final int DEVICE_TYPE_DUAL = 3; |
| 335 | |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 336 | |
| 337 | /** @hide */ |
| 338 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 339 | @UnsupportedAppUsage |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 340 | public static final String ACTION_SDP_RECORD = |
| 341 | "android.bluetooth.device.action.SDP_RECORD"; |
| 342 | |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 343 | /** |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 344 | * Broadcast Action: This intent is used to broadcast the {@link UUID} |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 345 | * wrapped as a {@link android.os.ParcelUuid} of the remote device after it |
| 346 | * has been fetched. This intent is sent only when the UUIDs of the remote |
| 347 | * device are requested to be fetched using Service Discovery Protocol |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 348 | * <p> Always contains the extra field {@link #EXTRA_DEVICE} |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 349 | * <p> Always contains the extra field {@link #EXTRA_UUID} |
Hansong Zhang | ff1c21c | 2018-11-12 14:32:03 -0800 | [diff] [blame] | 350 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} to receive. |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 351 | */ |
| 352 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 353 | public static final String ACTION_UUID = |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 354 | "android.bluetooth.device.action.UUID"; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 355 | |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 356 | /** @hide */ |
| 357 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 358 | public static final String ACTION_MAS_INSTANCE = |
| 359 | "android.bluetooth.device.action.MAS_INSTANCE"; |
| 360 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 361 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 362 | * Broadcast Action: Indicates a failure to retrieve the name of a remote |
| 363 | * device. |
| 364 | * <p>Always contains the extra field {@link #EXTRA_DEVICE}. |
| 365 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 366 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 367 | * @hide |
| 368 | */ |
| 369 | //TODO: is this actually useful? |
| 370 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 371 | public static final String ACTION_NAME_FAILED = |
| 372 | "android.bluetooth.device.action.NAME_FAILED"; |
| 373 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 374 | /** |
| 375 | * Broadcast Action: This intent is used to broadcast PAIRING REQUEST |
Matthew Xie | ac2c6c3 | 2013-10-21 14:56:33 -0700 | [diff] [blame] | 376 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} to |
Edward Jee | 430e361 | 2013-10-07 18:12:15 -0700 | [diff] [blame] | 377 | * receive. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 378 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 379 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 380 | public static final String ACTION_PAIRING_REQUEST = |
| 381 | "android.bluetooth.device.action.PAIRING_REQUEST"; |
| 382 | /** @hide */ |
| 383 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 384 | @UnsupportedAppUsage |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 385 | public static final String ACTION_PAIRING_CANCEL = |
| 386 | "android.bluetooth.device.action.PAIRING_CANCEL"; |
Yue Lixin | a4433af | 2009-07-09 16:56:43 +0800 | [diff] [blame] | 387 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 388 | /** @hide */ |
| 389 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 390 | public static final String ACTION_CONNECTION_ACCESS_REQUEST = |
| 391 | "android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST"; |
| 392 | |
| 393 | /** @hide */ |
| 394 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 395 | public static final String ACTION_CONNECTION_ACCESS_REPLY = |
| 396 | "android.bluetooth.device.action.CONNECTION_ACCESS_REPLY"; |
| 397 | |
| 398 | /** @hide */ |
| 399 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 400 | public static final String ACTION_CONNECTION_ACCESS_CANCEL = |
| 401 | "android.bluetooth.device.action.CONNECTION_ACCESS_CANCEL"; |
| 402 | |
| 403 | /** |
| 404 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intent. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 405 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 406 | * @hide |
| 407 | */ |
| 408 | public static final String EXTRA_ACCESS_REQUEST_TYPE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 409 | "android.bluetooth.device.extra.ACCESS_REQUEST_TYPE"; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 410 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 411 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 412 | public static final int REQUEST_TYPE_PROFILE_CONNECTION = 1; |
| 413 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 414 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 415 | public static final int REQUEST_TYPE_PHONEBOOK_ACCESS = 2; |
| 416 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 417 | /** @hide */ |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 418 | public static final int REQUEST_TYPE_MESSAGE_ACCESS = 3; |
| 419 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 420 | /** @hide */ |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 421 | public static final int REQUEST_TYPE_SIM_ACCESS = 4; |
| 422 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 423 | /** |
| 424 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents, |
| 425 | * Contains package name to return reply intent to. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 426 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 427 | * @hide |
| 428 | */ |
| 429 | public static final String EXTRA_PACKAGE_NAME = "android.bluetooth.device.extra.PACKAGE_NAME"; |
| 430 | |
| 431 | /** |
| 432 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents, |
| 433 | * Contains class name to return reply intent to. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 434 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 435 | * @hide |
| 436 | */ |
| 437 | public static final String EXTRA_CLASS_NAME = "android.bluetooth.device.extra.CLASS_NAME"; |
| 438 | |
| 439 | /** |
| 440 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intent. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 441 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 442 | * @hide |
| 443 | */ |
| 444 | public static final String EXTRA_CONNECTION_ACCESS_RESULT = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 445 | "android.bluetooth.device.extra.CONNECTION_ACCESS_RESULT"; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 446 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 447 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 448 | public static final int CONNECTION_ACCESS_YES = 1; |
| 449 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 450 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 451 | public static final int CONNECTION_ACCESS_NO = 2; |
| 452 | |
| 453 | /** |
| 454 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intents, |
| 455 | * Contains boolean to indicate if the allowed response is once-for-all so that |
| 456 | * next request will be granted without asking user again. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 457 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 458 | * @hide |
| 459 | */ |
| 460 | public static final String EXTRA_ALWAYS_ALLOWED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 461 | "android.bluetooth.device.extra.ALWAYS_ALLOWED"; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 462 | |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 463 | /** |
| 464 | * A bond attempt succeeded |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 465 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 466 | * @hide |
| 467 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 468 | public static final int BOND_SUCCESS = 0; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * 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] | 472 | * not respond to pin request in time |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 473 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 474 | * @hide |
| 475 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 476 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | public static final int UNBOND_REASON_AUTH_FAILED = 1; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 478 | |
| 479 | /** |
| 480 | * A bond attempt failed because the other side explicitly rejected |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 481 | * bonding |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 482 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 483 | * @hide |
| 484 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 485 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | public static final int UNBOND_REASON_AUTH_REJECTED = 2; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * A bond attempt failed because we canceled the bonding process |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 490 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 491 | * @hide |
| 492 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | public static final int UNBOND_REASON_AUTH_CANCELED = 3; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * A bond attempt failed because we could not contact the remote device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 497 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 498 | * @hide |
| 499 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 500 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 502 | |
| 503 | /** |
| 504 | * A bond attempt failed because a discovery is in progress |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 505 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 506 | * @hide |
| 507 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 508 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * A bond attempt failed because of authentication timeout |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 513 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 514 | * @hide |
| 515 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 516 | @UnsupportedAppUsage |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 517 | public static final int UNBOND_REASON_AUTH_TIMEOUT = 6; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * A bond attempt failed because of repeated attempts |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 521 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 522 | * @hide |
| 523 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 524 | @UnsupportedAppUsage |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 525 | public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 526 | |
| 527 | /** |
| 528 | * A bond attempt failed because we received an Authentication Cancel |
| 529 | * by remote end |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 530 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 531 | * @hide |
| 532 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 533 | @UnsupportedAppUsage |
Jaikumar Ganesh | e5d93b7 | 2009-10-08 02:27:52 -0700 | [diff] [blame] | 534 | public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 535 | |
| 536 | /** |
| 537 | * An existing bond was explicitly revoked |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 538 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 539 | * @hide |
| 540 | */ |
Jaikumar Ganesh | e5d93b7 | 2009-10-08 02:27:52 -0700 | [diff] [blame] | 541 | public static final int UNBOND_REASON_REMOVED = 9; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 543 | /** |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 544 | * The user will be prompted to enter a pin or |
Matthew Xie | ac2c6c3 | 2013-10-21 14:56:33 -0700 | [diff] [blame] | 545 | * an app will enter a pin for user. |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 546 | */ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 547 | public static final int PAIRING_VARIANT_PIN = 0; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * The user will be prompted to enter a passkey |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 551 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 552 | * @hide |
| 553 | */ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 554 | public static final int PAIRING_VARIANT_PASSKEY = 1; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 555 | |
| 556 | /** |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 557 | * The user will be prompted to confirm the passkey displayed on the screen or |
Matthew Xie | ac2c6c3 | 2013-10-21 14:56:33 -0700 | [diff] [blame] | 558 | * an app will confirm the passkey for the user. |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 559 | */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 560 | public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 561 | |
| 562 | /** |
| 563 | * The user will be prompted to accept or deny the incoming pairing request |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 564 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 565 | * @hide |
| 566 | */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 567 | public static final int PAIRING_VARIANT_CONSENT = 3; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 568 | |
| 569 | /** |
| 570 | * The user will be prompted to enter the passkey displayed on remote device |
| 571 | * This is used for Bluetooth 2.1 pairing. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 572 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 573 | * @hide |
| 574 | */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 575 | public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 576 | |
| 577 | /** |
| 578 | * The user will be prompted to enter the PIN displayed on remote device. |
| 579 | * This is used for Bluetooth 2.0 pairing. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 580 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 581 | * @hide |
| 582 | */ |
| 583 | public static final int PAIRING_VARIANT_DISPLAY_PIN = 5; |
| 584 | |
| 585 | /** |
| 586 | * The user will be prompted to accept or deny the OOB pairing request |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 587 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 588 | * @hide |
| 589 | */ |
| 590 | public static final int PAIRING_VARIANT_OOB_CONSENT = 6; |
| 591 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 592 | /** |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 593 | * The user will be prompted to enter a 16 digit pin or |
| 594 | * an app will enter a 16 digit pin for user. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 595 | * |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 596 | * @hide |
| 597 | */ |
| 598 | public static final int PAIRING_VARIANT_PIN_16_DIGITS = 7; |
| 599 | |
| 600 | /** |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 601 | * Used as an extra field in {@link #ACTION_UUID} intents, |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 602 | * Contains the {@link android.os.ParcelUuid}s of the remote device which |
| 603 | * is a parcelable version of {@link UUID}. |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 604 | */ |
| 605 | public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID"; |
| 606 | |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 607 | /** @hide */ |
| 608 | public static final String EXTRA_SDP_RECORD = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 609 | "android.bluetooth.device.extra.SDP_RECORD"; |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 610 | |
| 611 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 612 | @UnsupportedAppUsage |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 613 | public static final String EXTRA_SDP_SEARCH_STATUS = |
| 614 | "android.bluetooth.device.extra.SDP_SEARCH_STATUS"; |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 615 | /** |
| 616 | * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission}, |
| 617 | * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 618 | * |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 619 | * @hide |
| 620 | */ |
Selim Gurun | 17a26c7 | 2018-01-09 14:35:19 -0800 | [diff] [blame] | 621 | @SystemApi |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 622 | public static final int ACCESS_UNKNOWN = 0; |
| 623 | |
| 624 | /** |
| 625 | * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission}, |
| 626 | * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 627 | * |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 628 | * @hide |
| 629 | */ |
Selim Gurun | 17a26c7 | 2018-01-09 14:35:19 -0800 | [diff] [blame] | 630 | @SystemApi |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 631 | public static final int ACCESS_ALLOWED = 1; |
| 632 | |
| 633 | /** |
| 634 | * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission}, |
| 635 | * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 636 | * |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 637 | * @hide |
| 638 | */ |
Selim Gurun | 17a26c7 | 2018-01-09 14:35:19 -0800 | [diff] [blame] | 639 | @SystemApi |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 640 | public static final int ACCESS_REJECTED = 2; |
| 641 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 642 | /** |
| 643 | * No preferrence of physical transport for GATT connections to remote dual-mode devices |
| 644 | */ |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 645 | public static final int TRANSPORT_AUTO = 0; |
| 646 | |
| 647 | /** |
| 648 | * Prefer BR/EDR transport for GATT connections to remote dual-mode devices |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 649 | */ |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 650 | public static final int TRANSPORT_BREDR = 1; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 651 | |
| 652 | /** |
| 653 | * Prefer LE transport for GATT connections to remote dual-mode devices |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 654 | */ |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 655 | public static final int TRANSPORT_LE = 2; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 656 | |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 657 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 658 | * Bluetooth LE 1M PHY. Used to refer to LE 1M Physical Channel for advertising, scanning or |
| 659 | * connection. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 660 | */ |
| 661 | public static final int PHY_LE_1M = 1; |
| 662 | |
| 663 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 664 | * Bluetooth LE 2M PHY. Used to refer to LE 2M Physical Channel for advertising, scanning or |
| 665 | * connection. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 666 | */ |
| 667 | public static final int PHY_LE_2M = 2; |
| 668 | |
| 669 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 670 | * Bluetooth LE Coded PHY. Used to refer to LE Coded Physical Channel for advertising, scanning |
| 671 | * or connection. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 672 | */ |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 673 | public static final int PHY_LE_CODED = 3; |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 674 | |
| 675 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 676 | * Bluetooth LE 1M PHY mask. Used to specify LE 1M Physical Channel as one of many available |
| 677 | * options in a bitmask. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 678 | */ |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 679 | public static final int PHY_LE_1M_MASK = 1; |
| 680 | |
| 681 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 682 | * Bluetooth LE 2M PHY mask. Used to specify LE 2M Physical Channel as one of many available |
| 683 | * options in a bitmask. |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 684 | */ |
| 685 | public static final int PHY_LE_2M_MASK = 2; |
| 686 | |
| 687 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 688 | * Bluetooth LE Coded PHY mask. Used to specify LE Coded Physical Channel as one of many |
| 689 | * available options in a bitmask. |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 690 | */ |
| 691 | public static final int PHY_LE_CODED_MASK = 4; |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 692 | |
| 693 | /** |
| 694 | * No preferred coding when transmitting on the LE Coded PHY. |
| 695 | */ |
| 696 | public static final int PHY_OPTION_NO_PREFERRED = 0; |
| 697 | |
| 698 | /** |
| 699 | * Prefer the S=2 coding to be used when transmitting on the LE Coded PHY. |
| 700 | */ |
| 701 | public static final int PHY_OPTION_S2 = 1; |
| 702 | |
| 703 | /** |
| 704 | * Prefer the S=8 coding to be used when transmitting on the LE Coded PHY. |
| 705 | */ |
| 706 | public static final int PHY_OPTION_S8 = 2; |
| 707 | |
| 708 | |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 709 | /** @hide */ |
| 710 | public static final String EXTRA_MAS_INSTANCE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 711 | "android.bluetooth.device.extra.MAS_INSTANCE"; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 712 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 713 | /** |
| 714 | * Lazy initialization. Guaranteed final after first object constructed, or |
| 715 | * getService() called. |
| 716 | * TODO: Unify implementation of sService amongst BluetoothFoo API's |
| 717 | */ |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 718 | private static volatile IBluetooth sService; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 719 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 720 | private final String mAddress; |
| 721 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 722 | /*package*/ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 723 | @UnsupportedAppUsage |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 724 | static IBluetooth getService() { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 725 | synchronized (BluetoothDevice.class) { |
| 726 | if (sService == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 727 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 728 | sService = adapter.getBluetoothService(sStateChangeCallback); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | return sService; |
| 732 | } |
| 733 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 734 | static IBluetoothManagerCallback sStateChangeCallback = new IBluetoothManagerCallback.Stub() { |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 735 | |
| 736 | public void onBluetoothServiceUp(IBluetooth bluetoothService) |
| 737 | throws RemoteException { |
| 738 | synchronized (BluetoothDevice.class) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 739 | if (sService == null) { |
| 740 | sService = bluetoothService; |
| 741 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
| 745 | public void onBluetoothServiceDown() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 746 | throws RemoteException { |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 747 | synchronized (BluetoothDevice.class) { |
| 748 | sService = null; |
| 749 | } |
| 750 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 751 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 752 | public void onBrEdrDown() { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 753 | if (DBG) Log.d(TAG, "onBrEdrDown: reached BLE ON state"); |
| 754 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 755 | }; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 756 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 758 | * Create a new BluetoothDevice |
| 759 | * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", |
| 760 | * and is validated in this constructor. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 761 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 762 | * @param address valid Bluetooth MAC address |
| 763 | * @throws RuntimeException Bluetooth is not available on this platform |
| 764 | * @throws IllegalArgumentException address is invalid |
| 765 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 767 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 768 | /*package*/ BluetoothDevice(String address) { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 769 | getService(); // ensures sService is initialized |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 770 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 771 | throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); |
| 772 | } |
| 773 | |
| 774 | mAddress = address; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 777 | @Override |
| 778 | public boolean equals(Object o) { |
| 779 | if (o instanceof BluetoothDevice) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 780 | return mAddress.equals(((BluetoothDevice) o).getAddress()); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 781 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 785 | @Override |
| 786 | public int hashCode() { |
| 787 | return mAddress.hashCode(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 790 | /** |
| 791 | * Returns a string representation of this BluetoothDevice. |
| 792 | * <p>Currently this is the Bluetooth hardware address, for example |
| 793 | * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress} |
| 794 | * if you explicitly require the Bluetooth hardware address in case the |
| 795 | * {@link #toString} representation changes in the future. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 796 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 797 | * @return string representation of this BluetoothDevice |
| 798 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 799 | @Override |
| 800 | public String toString() { |
| 801 | return mAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 804 | @Override |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 805 | public int describeContents() { |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | public static final Parcelable.Creator<BluetoothDevice> CREATOR = |
| 810 | new Parcelable.Creator<BluetoothDevice>() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 811 | public BluetoothDevice createFromParcel(Parcel in) { |
| 812 | return new BluetoothDevice(in.readString()); |
| 813 | } |
| 814 | |
| 815 | public BluetoothDevice[] newArray(int size) { |
| 816 | return new BluetoothDevice[size]; |
| 817 | } |
| 818 | }; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 819 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 820 | @Override |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 821 | public void writeToParcel(Parcel out, int flags) { |
| 822 | out.writeString(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 825 | /** |
| 826 | * Returns the hardware address of this BluetoothDevice. |
| 827 | * <p> For example, "00:11:22:AA:BB:CC". |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 828 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 829 | * @return Bluetooth hardware address as string |
| 830 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | public String getAddress() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 832 | if (DBG) Log.d(TAG, "mAddress: " + mAddress); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 833 | return mAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 837 | * Get the friendly Bluetooth name of the remote device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 839 | * <p>The local adapter will automatically retrieve remote names when |
| 840 | * performing a device scan, and will cache them. This method just returns |
| 841 | * the name for this device from the cache. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | * |
| 843 | * @return the Bluetooth name, or null if there was a problem. |
| 844 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 845 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | public String getName() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 847 | final IBluetooth service = sService; |
| 848 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 849 | Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); |
| 850 | return null; |
| 851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | try { |
Hansong Zhang | 2e3bf46 | 2018-04-26 14:22:39 -0700 | [diff] [blame] | 853 | String name = service.getRemoteName(this); |
| 854 | if (name != null) { |
| 855 | return name.replaceAll("[\\t\\n\\r]+", " "); |
| 856 | } |
| 857 | return null; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 858 | } catch (RemoteException e) { |
| 859 | Log.e(TAG, "", e); |
| 860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | return null; |
| 862 | } |
| 863 | |
| 864 | /** |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 865 | * Get the Bluetooth device type of the remote device. |
| 866 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 867 | * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE} {@link |
| 868 | * #DEVICE_TYPE_DUAL}. {@link #DEVICE_TYPE_UNKNOWN} if it's not available |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 869 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 870 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 871 | public int getType() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 872 | final IBluetooth service = sService; |
| 873 | if (service == null) { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 874 | Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); |
| 875 | return DEVICE_TYPE_UNKNOWN; |
| 876 | } |
| 877 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 878 | return service.getRemoteType(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 879 | } catch (RemoteException e) { |
| 880 | Log.e(TAG, "", e); |
| 881 | } |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 882 | return DEVICE_TYPE_UNKNOWN; |
| 883 | } |
| 884 | |
| 885 | /** |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 886 | * Get the Bluetooth alias of the remote device. |
| 887 | * <p>Alias is the locally modified name of a remote device. |
| 888 | * |
| 889 | * @return the Bluetooth alias, or null if no alias or there was a problem |
| 890 | * @hide |
| 891 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 892 | @UnsupportedAppUsage |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 893 | public String getAlias() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 894 | final IBluetooth service = sService; |
| 895 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 896 | Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); |
| 897 | return null; |
| 898 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 899 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 900 | return service.getRemoteAlias(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 901 | } catch (RemoteException e) { |
| 902 | Log.e(TAG, "", e); |
| 903 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 904 | return null; |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Set the Bluetooth alias of the remote device. |
| 909 | * <p>Alias is the locally modified name of a remote device. |
| 910 | * <p>This methoid overwrites the alias. The changed |
| 911 | * alias is saved in the local storage so that the change |
| 912 | * is preserved over power cycle. |
| 913 | * |
| 914 | * @return true on success, false on error |
| 915 | * @hide |
| 916 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 917 | @UnsupportedAppUsage |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 918 | public boolean setAlias(String alias) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 919 | final IBluetooth service = sService; |
| 920 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 921 | Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); |
| 922 | return false; |
| 923 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 924 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 925 | return service.setRemoteAlias(this, alias); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 926 | } catch (RemoteException e) { |
| 927 | Log.e(TAG, "", e); |
| 928 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 929 | return false; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Get the Bluetooth alias of the remote device. |
| 934 | * If Alias is null, get the Bluetooth name instead. |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 935 | * |
| 936 | * @return the Bluetooth alias, or null if no alias or there was a problem |
| 937 | * @hide |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 938 | * @see #getAlias() |
| 939 | * @see #getName() |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 940 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 941 | @UnsupportedAppUsage |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 942 | public String getAliasName() { |
| 943 | String name = getAlias(); |
| 944 | if (name == null) { |
| 945 | name = getName(); |
| 946 | } |
| 947 | return name; |
| 948 | } |
| 949 | |
| 950 | /** |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 951 | * Get the most recent identified battery level of this Bluetooth device |
| 952 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 953 | * |
| 954 | * @return Battery level in percents from 0 to 100, or {@link #BATTERY_LEVEL_UNKNOWN} if |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 955 | * Bluetooth is disabled, or device is disconnected, or does not have any battery reporting |
| 956 | * service, or return value is invalid |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 957 | * @hide |
| 958 | */ |
| 959 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 960 | @UnsupportedAppUsage |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 961 | public int getBatteryLevel() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 962 | final IBluetooth service = sService; |
| 963 | if (service == null) { |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 964 | Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level"); |
| 965 | return BATTERY_LEVEL_UNKNOWN; |
| 966 | } |
| 967 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 968 | return service.getBatteryLevel(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 969 | } catch (RemoteException e) { |
| 970 | Log.e(TAG, "", e); |
| 971 | } |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 972 | return BATTERY_LEVEL_UNKNOWN; |
| 973 | } |
| 974 | |
| 975 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 976 | * Start the bonding (pairing) process with the remote device. |
| 977 | * <p>This is an asynchronous call, it will return immediately. Register |
| 978 | * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
| 979 | * the bonding process completes, and its result. |
| 980 | * <p>Android system services will handle the necessary user interactions |
| 981 | * to confirm and complete the bonding process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 983 | * @return false on immediate error, true if bonding will begin |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 985 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 986 | public boolean createBond() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 987 | final IBluetooth service = sService; |
| 988 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 989 | Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); |
| 990 | return false; |
| 991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 993 | Log.i(TAG, "createBond() for device " + getAddress() |
| 994 | + " called by pid: " + Process.myPid() |
| 995 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 996 | return service.createBond(this, TRANSPORT_AUTO); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 997 | } catch (RemoteException e) { |
| 998 | Log.e(TAG, "", e); |
| 999 | } |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * Start the bonding (pairing) process with the remote device using the |
| 1005 | * specified transport. |
| 1006 | * |
| 1007 | * <p>This is an asynchronous call, it will return immediately. Register |
| 1008 | * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
| 1009 | * the bonding process completes, and its result. |
| 1010 | * <p>Android system services will handle the necessary user interactions |
| 1011 | * to confirm and complete the bonding process. |
| 1012 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1013 | * |
| 1014 | * @param transport The transport to use for the pairing procedure. |
| 1015 | * @return false on immediate error, true if bonding will begin |
| 1016 | * @throws IllegalArgumentException if an invalid transport was specified |
| 1017 | * @hide |
| 1018 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1019 | @UnsupportedAppUsage |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1020 | public boolean createBond(int transport) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1021 | final IBluetooth service = sService; |
| 1022 | if (service == null) { |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1023 | Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); |
| 1024 | return false; |
| 1025 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1026 | if (TRANSPORT_AUTO > transport || transport > TRANSPORT_LE) { |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1027 | throw new IllegalArgumentException(transport + " is not a valid Bluetooth transport"); |
| 1028 | } |
| 1029 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1030 | Log.i(TAG, "createBond() for device " + getAddress() |
| 1031 | + " called by pid: " + Process.myPid() |
| 1032 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1033 | return service.createBond(this, transport); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1034 | } catch (RemoteException e) { |
| 1035 | Log.e(TAG, "", e); |
| 1036 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1041 | * Start the bonding (pairing) process with the remote device using the |
| 1042 | * Out Of Band mechanism. |
| 1043 | * |
| 1044 | * <p>This is an asynchronous call, it will return immediately. Register |
| 1045 | * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
| 1046 | * the bonding process completes, and its result. |
| 1047 | * |
| 1048 | * <p>Android system services will handle the necessary user interactions |
| 1049 | * to confirm and complete the bonding process. |
| 1050 | * |
| 1051 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1052 | * |
Jakub Pawlowski | 2fc6e6b | 2015-12-29 13:19:21 -0800 | [diff] [blame] | 1053 | * @param transport - Transport to use |
| 1054 | * @param oobData - Out Of Band data |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1055 | * @return false on immediate error, true if bonding will begin |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1056 | * @hide |
| 1057 | */ |
Jakub Pawlowski | 2fc6e6b | 2015-12-29 13:19:21 -0800 | [diff] [blame] | 1058 | public boolean createBondOutOfBand(int transport, OobData oobData) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1059 | final IBluetooth service = sService; |
| 1060 | if (service == null) { |
| 1061 | Log.w(TAG, "BT not enabled, createBondOutOfBand failed"); |
| 1062 | return false; |
| 1063 | } |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1064 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1065 | return service.createBondOutOfBand(this, transport, oobData); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1066 | } catch (RemoteException e) { |
| 1067 | Log.e(TAG, "", e); |
| 1068 | } |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1072 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1073 | @UnsupportedAppUsage |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1074 | public boolean isBondingInitiatedLocally() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1075 | final IBluetooth service = sService; |
| 1076 | if (service == null) { |
| 1077 | Log.w(TAG, "BT not enabled, isBondingInitiatedLocally failed"); |
| 1078 | return false; |
| 1079 | } |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1080 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1081 | return service.isBondingInitiatedLocally(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1082 | } catch (RemoteException e) { |
| 1083 | Log.e(TAG, "", e); |
| 1084 | } |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1085 | return false; |
| 1086 | } |
| 1087 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1088 | /** |
| 1089 | * Set the Out Of Band data for a remote device to be used later |
| 1090 | * in the pairing mechanism. Users can obtain this data through other |
| 1091 | * trusted channels |
| 1092 | * |
| 1093 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1094 | * |
| 1095 | * @param hash Simple Secure pairing hash |
| 1096 | * @param randomizer The random key obtained using OOB |
| 1097 | * @return false on error; true otherwise |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1098 | * @hide |
| 1099 | */ |
| 1100 | public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1101 | //TODO(BT) |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1102 | /* |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1103 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1104 | return sService.setDeviceOutOfBandData(this, hash, randomizer); |
| 1105 | } catch (RemoteException e) {Log.e(TAG, "", e);} */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1106 | return false; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1110 | * Cancel an in-progress bonding request started with {@link #createBond}. |
| 1111 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1112 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1113 | * @return true on success, false on error |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 1114 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | */ |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1116 | @SystemApi |
| 1117 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1118 | public boolean cancelBondProcess() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1119 | final IBluetooth service = sService; |
| 1120 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1121 | Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); |
| 1122 | return false; |
| 1123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1125 | Log.i(TAG, "cancelBondProcess() for device " + getAddress() |
| 1126 | + " called by pid: " + Process.myPid() |
| 1127 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1128 | return service.cancelBondProcess(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1129 | } catch (RemoteException e) { |
| 1130 | Log.e(TAG, "", e); |
| 1131 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1136 | * Remove bond (pairing) with the remote device. |
| 1137 | * <p>Delete the link key associated with the remote device, and |
| 1138 | * immediately terminate connections to that device that require |
| 1139 | * authentication and encryption. |
| 1140 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1142 | * @return true on success, false on error |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 1143 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1144 | */ |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1145 | @SystemApi |
| 1146 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1147 | public boolean removeBond() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1148 | final IBluetooth service = sService; |
| 1149 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1150 | Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); |
| 1151 | return false; |
| 1152 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1154 | Log.i(TAG, "removeBond() for device " + getAddress() |
| 1155 | + " called by pid: " + Process.myPid() |
| 1156 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1157 | return service.removeBond(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1158 | } catch (RemoteException e) { |
| 1159 | Log.e(TAG, "", e); |
| 1160 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1161 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1165 | * Get the bond state of the remote device. |
| 1166 | * <p>Possible values for the bond state are: |
| 1167 | * {@link #BOND_NONE}, |
| 1168 | * {@link #BOND_BONDING}, |
| 1169 | * {@link #BOND_BONDED}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1171 | * @return the bond state |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1173 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1174 | public int getBondState() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1175 | final IBluetooth service = sService; |
| 1176 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1177 | Log.e(TAG, "BT not enabled. Cannot get bond state"); |
| 1178 | return BOND_NONE; |
| 1179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1181 | return service.getBondState(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1182 | } catch (RemoteException e) { |
| 1183 | Log.e(TAG, "", e); |
Syed Ibrahim M | 305f240 | 2012-06-19 10:14:25 -0700 | [diff] [blame] | 1184 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1185 | return BOND_NONE; |
| 1186 | } |
| 1187 | |
| 1188 | /** |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1189 | * Returns whether there is an open connection to this device. |
| 1190 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 1191 | * |
| 1192 | * @return True if there is at least one open connection to this device. |
| 1193 | * @hide |
| 1194 | */ |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1195 | @SystemApi |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1196 | @RequiresPermission(android.Manifest.permission.BLUETOOTH) |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1197 | public boolean isConnected() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1198 | final IBluetooth service = sService; |
| 1199 | if (service == null) { |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1200 | // BT is not enabled, we cannot be connected. |
| 1201 | return false; |
| 1202 | } |
| 1203 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1204 | return service.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED; |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1205 | } catch (RemoteException e) { |
| 1206 | Log.e(TAG, "", e); |
| 1207 | return false; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Returns whether there is an open connection to this device |
| 1213 | * that has been encrypted. |
| 1214 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 1215 | * |
| 1216 | * @return True if there is at least one encrypted connection to this device. |
| 1217 | * @hide |
| 1218 | */ |
| 1219 | @SystemApi |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1220 | @RequiresPermission(android.Manifest.permission.BLUETOOTH) |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1221 | public boolean isEncrypted() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1222 | final IBluetooth service = sService; |
| 1223 | if (service == null) { |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1224 | // BT is not enabled, we cannot be connected. |
| 1225 | return false; |
| 1226 | } |
| 1227 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1228 | return service.getConnectionState(this) > CONNECTION_STATE_CONNECTED; |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1229 | } catch (RemoteException e) { |
| 1230 | Log.e(TAG, "", e); |
| 1231 | return false; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1236 | * Get the Bluetooth class of the remote device. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1237 | * |
| 1238 | * @return Bluetooth class object, or null on error |
| 1239 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1240 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1241 | public BluetoothClass getBluetoothClass() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1242 | final IBluetooth service = sService; |
| 1243 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1244 | Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); |
| 1245 | return null; |
| 1246 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1247 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1248 | int classInt = service.getRemoteClass(this); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1249 | if (classInt == BluetoothClass.ERROR) return null; |
| 1250 | return new BluetoothClass(classInt); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1251 | } catch (RemoteException e) { |
| 1252 | Log.e(TAG, "", e); |
| 1253 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1254 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1257 | /** |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 1258 | * Returns the supported features (UUIDs) of the remote device. |
| 1259 | * |
| 1260 | * <p>This method does not start a service discovery procedure to retrieve the UUIDs |
| 1261 | * from the remote device. Instead, the local cached copy of the service |
| 1262 | * UUIDs are returned. |
| 1263 | * <p>Use {@link #fetchUuidsWithSdp} if fresh UUIDs are desired. |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 1264 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1265 | * @return the supported features (UUIDs) of the remote device, or null on error |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 1266 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1267 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1268 | public ParcelUuid[] getUuids() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1269 | final IBluetooth service = sService; |
| 1270 | if (service == null || !isBluetoothEnabled()) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1271 | Log.e(TAG, "BT not enabled. Cannot get remote device Uuids"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1272 | return null; |
| 1273 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1275 | return service.getRemoteUuids(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1276 | } catch (RemoteException e) { |
| 1277 | Log.e(TAG, "", e); |
| 1278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | return null; |
| 1280 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1281 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1282 | /** |
| 1283 | * Perform a service discovery on the remote device to get the UUIDs supported. |
| 1284 | * |
| 1285 | * <p>This API is asynchronous and {@link #ACTION_UUID} intent is sent, |
| 1286 | * with the UUIDs supported by the remote end. If there is an error |
| 1287 | * in getting the SDP records or if the process takes a long time, |
| 1288 | * {@link #ACTION_UUID} intent is sent with the UUIDs that is currently |
| 1289 | * present in the cache. Clients should use the {@link #getUuids} to get UUIDs |
| 1290 | * if service discovery is not to be performed. |
| 1291 | * |
| 1292 | * @return False if the sanity check fails, True if the process of initiating an ACL connection |
| 1293 | * to the remote device was started. |
| 1294 | */ |
| 1295 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1296 | public boolean fetchUuidsWithSdp() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1297 | final IBluetooth service = sService; |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1298 | if (service == null || !isBluetoothEnabled()) { |
Zhihai Xu | 5d04f1c | 2014-01-13 16:54:38 -0800 | [diff] [blame] | 1299 | Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp"); |
| 1300 | return false; |
| 1301 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1302 | try { |
Zhihai Xu | 5d04f1c | 2014-01-13 16:54:38 -0800 | [diff] [blame] | 1303 | return service.fetchRemoteUuids(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1304 | } catch (RemoteException e) { |
| 1305 | Log.e(TAG, "", e); |
| 1306 | } |
| 1307 | return false; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1310 | /** |
| 1311 | * Perform a service discovery on the remote device to get the SDP records associated |
| 1312 | * with the specified UUID. |
| 1313 | * |
| 1314 | * <p>This API is asynchronous and {@link #ACTION_SDP_RECORD} intent is sent, |
| 1315 | * with the SDP records found on the remote end. If there is an error |
| 1316 | * in getting the SDP records or if the process takes a long time, |
| 1317 | * {@link #ACTION_SDP_RECORD} intent is sent with an status value in |
| 1318 | * {@link #EXTRA_SDP_SEARCH_STATUS} different from 0. |
| 1319 | * Detailed status error codes can be found by members of the Bluetooth package in |
| 1320 | * the AbstractionLayer class. |
| 1321 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 1322 | * The SDP record data will be stored in the intent as {@link #EXTRA_SDP_RECORD}. |
| 1323 | * The object type will match one of the SdpXxxRecord types, depending on the UUID searched |
| 1324 | * for. |
| 1325 | * |
| 1326 | * @return False if the sanity check fails, True if the process |
| 1327 | * of initiating an ACL connection to the remote device |
| 1328 | * was started. |
| 1329 | */ |
| 1330 | /** @hide */ |
| 1331 | public boolean sdpSearch(ParcelUuid uuid) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1332 | final IBluetooth service = sService; |
| 1333 | if (service == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1334 | Log.e(TAG, "BT not enabled. Cannot query remote device sdp records"); |
| 1335 | return false; |
| 1336 | } |
| 1337 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1338 | return service.sdpSearch(this, uuid); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1339 | } catch (RemoteException e) { |
| 1340 | Log.e(TAG, "", e); |
| 1341 | } |
| 1342 | return false; |
| 1343 | } |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 1344 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1345 | /** |
| 1346 | * Set the pin during pairing when the pairing method is {@link #PAIRING_VARIANT_PIN} |
Matthew Xie | ac2c6c3 | 2013-10-21 14:56:33 -0700 | [diff] [blame] | 1347 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1348 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1349 | * @return true pin has been set false for error |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1350 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1351 | public boolean setPin(byte[] pin) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1352 | final IBluetooth service = sService; |
| 1353 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1354 | Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); |
| 1355 | return false; |
| 1356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1358 | return service.setPin(this, true, pin.length, pin); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1359 | } catch (RemoteException e) { |
| 1360 | Log.e(TAG, "", e); |
| 1361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | return false; |
| 1363 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1364 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1365 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1366 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1367 | public boolean setPasskey(int passkey) { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1368 | //TODO(BT) |
| 1369 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1371 | return sService.setPasskey(this, true, 4, passkey); |
| 1372 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1373 | return false; |
| 1374 | } |
| 1375 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1376 | /** |
| 1377 | * Confirm passkey for {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION} pairing. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1378 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1379 | * @return true confirmation has been sent out false for error |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1380 | */ |
Marie Janssen | 93326cf | 2016-08-09 13:23:39 -0700 | [diff] [blame] | 1381 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1382 | public boolean setPairingConfirmation(boolean confirm) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1383 | final IBluetooth service = sService; |
| 1384 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1385 | Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); |
| 1386 | return false; |
| 1387 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1388 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1389 | return service.setPairingConfirmation(this, confirm); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1390 | } catch (RemoteException e) { |
| 1391 | Log.e(TAG, "", e); |
| 1392 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1393 | return false; |
| 1394 | } |
| 1395 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1396 | /** @hide */ |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1397 | public boolean setRemoteOutOfBandData() { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1398 | // TODO(BT) |
| 1399 | /* |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1400 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1401 | return sService.setRemoteOutOfBandData(this); |
| 1402 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1403 | return false; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1407 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1408 | public boolean cancelPairingUserInput() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1409 | final IBluetooth service = sService; |
| 1410 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1411 | Log.e(TAG, "BT not enabled. Cannot create pairing user input"); |
| 1412 | return false; |
| 1413 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1414 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1415 | return service.cancelBondProcess(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1416 | } catch (RemoteException e) { |
| 1417 | Log.e(TAG, "", e); |
| 1418 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | return false; |
| 1420 | } |
| 1421 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1422 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1423 | @UnsupportedAppUsage |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1424 | public boolean isBluetoothDock() { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1425 | // TODO(BT) |
| 1426 | /* |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1427 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1428 | return sService.isBluetoothDock(this); |
| 1429 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1430 | return false; |
| 1431 | } |
| 1432 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1433 | boolean isBluetoothEnabled() { |
| 1434 | boolean ret = false; |
| 1435 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1436 | if (adapter != null && adapter.isEnabled()) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1437 | ret = true; |
| 1438 | } |
| 1439 | return ret; |
| 1440 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | /** |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1443 | * Requires {@link android.Manifest.permission#BLUETOOTH}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1444 | * |
| 1445 | * @return Whether the phonebook access is allowed to this device. Can be {@link |
| 1446 | * #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1447 | * @hide |
| 1448 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1449 | @UnsupportedAppUsage |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1450 | public int getPhonebookAccessPermission() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1451 | final IBluetooth service = sService; |
| 1452 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1453 | return ACCESS_UNKNOWN; |
| 1454 | } |
| 1455 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1456 | return service.getPhonebookAccessPermission(this); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1457 | } catch (RemoteException e) { |
| 1458 | Log.e(TAG, "", e); |
| 1459 | } |
| 1460 | return ACCESS_UNKNOWN; |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * Sets whether the phonebook access is allowed to this device. |
| 1465 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1466 | * |
| 1467 | * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link |
| 1468 | * #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1469 | * @return Whether the value has been successfully set. |
| 1470 | * @hide |
| 1471 | */ |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1472 | @SystemApi |
| 1473 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1474 | public boolean setPhonebookAccessPermission(int value) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1475 | final IBluetooth service = sService; |
| 1476 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1477 | return false; |
| 1478 | } |
| 1479 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1480 | return service.setPhonebookAccessPermission(this, value); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1481 | } catch (RemoteException e) { |
| 1482 | Log.e(TAG, "", e); |
| 1483 | } |
| 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * Requires {@link android.Manifest.permission#BLUETOOTH}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1489 | * |
| 1490 | * @return Whether the message access is allowed to this device. Can be {@link #ACCESS_UNKNOWN}, |
| 1491 | * {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1492 | * @hide |
| 1493 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1494 | @UnsupportedAppUsage |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1495 | public int getMessageAccessPermission() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1496 | final IBluetooth service = sService; |
| 1497 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1498 | return ACCESS_UNKNOWN; |
| 1499 | } |
| 1500 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1501 | return service.getMessageAccessPermission(this); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1502 | } catch (RemoteException e) { |
| 1503 | Log.e(TAG, "", e); |
| 1504 | } |
| 1505 | return ACCESS_UNKNOWN; |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * Sets whether the message access is allowed to this device. |
| 1510 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1511 | * |
| 1512 | * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link |
| 1513 | * #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1514 | * @return Whether the value has been successfully set. |
| 1515 | * @hide |
| 1516 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1517 | @UnsupportedAppUsage |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1518 | public boolean setMessageAccessPermission(int value) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1519 | final IBluetooth service = sService; |
| 1520 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1521 | return false; |
| 1522 | } |
| 1523 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1524 | return service.setMessageAccessPermission(this, value); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1525 | } catch (RemoteException e) { |
| 1526 | Log.e(TAG, "", e); |
| 1527 | } |
| 1528 | return false; |
| 1529 | } |
| 1530 | |
| 1531 | /** |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1532 | * Requires {@link android.Manifest.permission#BLUETOOTH}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1533 | * |
| 1534 | * @return Whether the Sim access is allowed to this device. Can be {@link #ACCESS_UNKNOWN}, |
| 1535 | * {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}. |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1536 | * @hide |
| 1537 | */ |
| 1538 | public int getSimAccessPermission() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1539 | final IBluetooth service = sService; |
| 1540 | if (service == null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1541 | return ACCESS_UNKNOWN; |
| 1542 | } |
| 1543 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1544 | return service.getSimAccessPermission(this); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1545 | } catch (RemoteException e) { |
| 1546 | Log.e(TAG, "", e); |
| 1547 | } |
| 1548 | return ACCESS_UNKNOWN; |
| 1549 | } |
| 1550 | |
| 1551 | /** |
| 1552 | * Sets whether the Sim access is allowed to this device. |
| 1553 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1554 | * |
| 1555 | * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link |
| 1556 | * #ACCESS_REJECTED}. |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1557 | * @return Whether the value has been successfully set. |
| 1558 | * @hide |
| 1559 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1560 | @UnsupportedAppUsage |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1561 | public boolean setSimAccessPermission(int value) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1562 | final IBluetooth service = sService; |
| 1563 | if (service == null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1564 | return false; |
| 1565 | } |
| 1566 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1567 | return service.setSimAccessPermission(this, value); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1568 | } catch (RemoteException e) { |
| 1569 | Log.e(TAG, "", e); |
| 1570 | } |
| 1571 | return false; |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1574 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1575 | * Create an RFCOMM {@link BluetoothSocket} ready to start a secure |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1576 | * outgoing connection to this remote device on given channel. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1577 | * <p>The remote device will be authenticated and communication on this |
| 1578 | * socket will be encrypted. |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 1579 | * <p> Use this socket only if an authenticated socket link is possible. |
| 1580 | * Authentication refers to the authentication of the link key to |
| 1581 | * prevent man-in-the-middle type of attacks. |
| 1582 | * For example, for Bluetooth 2.1 devices, if any of the devices does not |
| 1583 | * have an input and output capability or just has the ability to |
| 1584 | * display a numeric key, a secure socket connection is not possible. |
| 1585 | * In such a case, use {#link createInsecureRfcommSocket}. |
| 1586 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 1587 | * Bluetooth Core Specification version 2.1 + EDR. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1588 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1589 | * connection. |
| 1590 | * <p>Valid RFCOMM channels are in range 1 to 30. |
Nick Pelly | cf44059 | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 1591 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1592 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1593 | * @param channel RFCOMM channel to connect to |
| 1594 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1595 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1596 | * permissions |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1597 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1598 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1599 | @UnsupportedAppUsage |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1600 | public BluetoothSocket createRfcommSocket(int channel) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1601 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1602 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1603 | throw new IOException(); |
| 1604 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1605 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel, |
| 1606 | null); |
| 1607 | } |
| 1608 | |
| 1609 | /** |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 1610 | * Create an L2cap {@link BluetoothSocket} ready to start a secure |
| 1611 | * outgoing connection to this remote device on given channel. |
| 1612 | * <p>The remote device will be authenticated and communication on this |
| 1613 | * socket will be encrypted. |
| 1614 | * <p> Use this socket only if an authenticated socket link is possible. |
| 1615 | * Authentication refers to the authentication of the link key to |
| 1616 | * prevent man-in-the-middle type of attacks. |
| 1617 | * For example, for Bluetooth 2.1 devices, if any of the devices does not |
| 1618 | * have an input and output capability or just has the ability to |
| 1619 | * display a numeric key, a secure socket connection is not possible. |
| 1620 | * In such a case, use {#link createInsecureRfcommSocket}. |
| 1621 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 1622 | * Bluetooth Core Specification version 2.1 + EDR. |
| 1623 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
| 1624 | * connection. |
| 1625 | * <p>Valid L2CAP PSM channels are in range 1 to 2^16. |
| 1626 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1627 | * |
| 1628 | * @param channel L2cap PSM/channel to connect to |
| 1629 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1630 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1631 | * permissions |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 1632 | * @hide |
| 1633 | */ |
| 1634 | public BluetoothSocket createL2capSocket(int channel) throws IOException { |
| 1635 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, true, true, this, channel, |
| 1636 | null); |
| 1637 | } |
| 1638 | |
| 1639 | /** |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 1640 | * Create an L2cap {@link BluetoothSocket} ready to start an insecure |
| 1641 | * outgoing connection to this remote device on given channel. |
| 1642 | * <p>The remote device will be not authenticated and communication on this |
| 1643 | * socket will not be encrypted. |
| 1644 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
| 1645 | * connection. |
| 1646 | * <p>Valid L2CAP PSM channels are in range 1 to 2^16. |
| 1647 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1648 | * |
| 1649 | * @param channel L2cap PSM/channel to connect to |
| 1650 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1651 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1652 | * permissions |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 1653 | * @hide |
| 1654 | */ |
| 1655 | public BluetoothSocket createInsecureL2capSocket(int channel) throws IOException { |
| 1656 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, false, false, this, channel, |
| 1657 | null); |
| 1658 | } |
| 1659 | |
| 1660 | /** |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1661 | * Create an RFCOMM {@link BluetoothSocket} ready to start a secure |
| 1662 | * outgoing connection to this remote device using SDP lookup of uuid. |
| 1663 | * <p>This is designed to be used with {@link |
| 1664 | * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer |
| 1665 | * Bluetooth applications. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1666 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1667 | * connection. This will also perform an SDP lookup of the given uuid to |
| 1668 | * determine which channel to connect to. |
| 1669 | * <p>The remote device will be authenticated and communication on this |
| 1670 | * socket will be encrypted. |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 1671 | * <p> Use this socket only if an authenticated socket link is possible. |
| 1672 | * Authentication refers to the authentication of the link key to |
| 1673 | * prevent man-in-the-middle type of attacks. |
| 1674 | * For example, for Bluetooth 2.1 devices, if any of the devices does not |
| 1675 | * have an input and output capability or just has the ability to |
| 1676 | * display a numeric key, a secure socket connection is not possible. |
| 1677 | * In such a case, use {#link createInsecureRfcommSocketToServiceRecord}. |
| 1678 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 1679 | * Bluetooth Core Specification version 2.1 + EDR. |
Nick Pelly | ea5056e | 2010-02-24 11:19:10 -0800 | [diff] [blame] | 1680 | * <p>Hint: If you are connecting to a Bluetooth serial board then try |
| 1681 | * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. |
| 1682 | * However if you are connecting to an Android peer then please generate |
| 1683 | * your own unique UUID. |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1684 | * |
| 1685 | * @param uuid service record uuid to lookup RFCOMM channel |
| 1686 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1687 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1688 | * permissions |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1689 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1690 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1691 | public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1692 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1693 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1694 | throw new IOException(); |
| 1695 | } |
| 1696 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1697 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1, |
| 1698 | new ParcelUuid(uuid)); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | /** |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1702 | * Create an RFCOMM {@link BluetoothSocket} socket ready to start an insecure |
| 1703 | * outgoing connection to this remote device using SDP lookup of uuid. |
| 1704 | * <p> The communication channel will not have an authenticated link key |
| 1705 | * i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1 |
| 1706 | * devices, the link key will be encrypted, as encryption is mandatory. |
| 1707 | * For legacy devices (pre Bluetooth 2.1 devices) the link key will |
| 1708 | * be not be encrypted. Use {@link #createRfcommSocketToServiceRecord} if an |
| 1709 | * encrypted and authenticated communication channel is desired. |
| 1710 | * <p>This is designed to be used with {@link |
| 1711 | * BluetoothAdapter#listenUsingInsecureRfcommWithServiceRecord} for peer-peer |
| 1712 | * Bluetooth applications. |
| 1713 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
| 1714 | * connection. This will also perform an SDP lookup of the given uuid to |
| 1715 | * determine which channel to connect to. |
| 1716 | * <p>The remote device will be authenticated and communication on this |
| 1717 | * socket will be encrypted. |
| 1718 | * <p>Hint: If you are connecting to a Bluetooth serial board then try |
| 1719 | * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. |
| 1720 | * However if you are connecting to an Android peer then please generate |
| 1721 | * your own unique UUID. |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1722 | * |
| 1723 | * @param uuid service record uuid to lookup RFCOMM channel |
| 1724 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1725 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1726 | * permissions |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1727 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1728 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1729 | public BluetoothSocket createInsecureRfcommSocketToServiceRecord(UUID uuid) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1730 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1731 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1732 | throw new IOException(); |
| 1733 | } |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1734 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, -1, |
| 1735 | new ParcelUuid(uuid)); |
| 1736 | } |
| 1737 | |
| 1738 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1739 | * Construct an insecure RFCOMM socket ready to start an outgoing |
| 1740 | * connection. |
| 1741 | * Call #connect on the returned #BluetoothSocket to begin the connection. |
| 1742 | * The remote device will not be authenticated and communication on this |
| 1743 | * socket will not be encrypted. |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 1744 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 1745 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1746 | * @param port remote port |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1747 | * @return An RFCOMM BluetoothSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1748 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 1749 | * permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1750 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1751 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1752 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1753 | public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1754 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1755 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1756 | throw new IOException(); |
| 1757 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1758 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, |
| 1759 | null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /** |
| 1763 | * Construct a SCO socket ready to start an outgoing connection. |
| 1764 | * Call #connect on the returned #BluetoothSocket to begin the connection. |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 1765 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 1766 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1767 | * @return a SCO BluetoothSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1768 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1769 | * permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1770 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1771 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1772 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1773 | public BluetoothSocket createScoSocket() throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1774 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1775 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1776 | throw new IOException(); |
| 1777 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1778 | return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | * Check that a pin is valid and convert to byte array. |
| 1783 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1784 | * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1785 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | * @param pin pin as java String |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1787 | * @return the pin code as a UTF-8 byte array, or null if it is an invalid Bluetooth pin. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1788 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1790 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | public static byte[] convertPinToBytes(String pin) { |
| 1792 | if (pin == null) { |
| 1793 | return null; |
| 1794 | } |
| 1795 | byte[] pinBytes; |
| 1796 | try { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1797 | pinBytes = pin.getBytes("UTF-8"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | } catch (UnsupportedEncodingException uee) { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1799 | Log.e(TAG, "UTF-8 not supported?!?"); // this should not happen |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | return null; |
| 1801 | } |
| 1802 | if (pinBytes.length <= 0 || pinBytes.length > 16) { |
| 1803 | return null; |
| 1804 | } |
| 1805 | return pinBytes; |
| 1806 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1808 | /** |
| 1809 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 1810 | * The callback is used to deliver results to Caller, such as connection status as well |
| 1811 | * as any further GATT client operations. |
| 1812 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 1813 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1814 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1815 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1816 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 1817 | * automatically connect as soon as the remote device becomes available (true). |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1818 | * @throws IllegalArgumentException if callback is null |
| 1819 | */ |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 1820 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1821 | BluetoothGattCallback callback) { |
| 1822 | return (connectGatt(context, autoConnect, callback, TRANSPORT_AUTO)); |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | /** |
| 1826 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 1827 | * The callback is used to deliver results to Caller, such as connection status as well |
| 1828 | * as any further GATT client operations. |
| 1829 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 1830 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1831 | * |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 1832 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1833 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 1834 | * automatically connect as soon as the remote device becomes available (true). |
| 1835 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 1836 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 1837 | * BluetoothDevice#TRANSPORT_LE} |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 1838 | * @throws IllegalArgumentException if callback is null |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 1839 | */ |
| 1840 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1841 | BluetoothGattCallback callback, int transport) { |
| 1842 | return (connectGatt(context, autoConnect, callback, transport, PHY_LE_1M_MASK)); |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | /** |
| 1846 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 1847 | * The callback is used to deliver results to Caller, such as connection status as well |
| 1848 | * as any further GATT client operations. |
| 1849 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 1850 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1851 | * |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 1852 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1853 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 1854 | * automatically connect as soon as the remote device becomes available (true). |
| 1855 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 1856 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 1857 | * BluetoothDevice#TRANSPORT_LE} |
| 1858 | * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link |
| 1859 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, and {@link |
| 1860 | * BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect} |
| 1861 | * is set to true. |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1862 | * @throws NullPointerException if callback is null |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 1863 | */ |
| 1864 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1865 | BluetoothGattCallback callback, int transport, int phy) { |
| 1866 | return connectGatt(context, autoConnect, callback, transport, phy, null); |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /** |
| 1870 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 1871 | * The callback is used to deliver results to Caller, such as connection status as well |
| 1872 | * as any further GATT client operations. |
| 1873 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 1874 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1875 | * |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1876 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1877 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 1878 | * automatically connect as soon as the remote device becomes available (true). |
| 1879 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 1880 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 1881 | * BluetoothDevice#TRANSPORT_LE} |
| 1882 | * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link |
| 1883 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, an d{@link |
| 1884 | * BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect} |
| 1885 | * is set to true. |
| 1886 | * @param handler The handler to use for the callback. If {@code null}, callbacks will happen on |
| 1887 | * an un-specified background thread. |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1888 | * @throws NullPointerException if callback is null |
| 1889 | */ |
| 1890 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1891 | BluetoothGattCallback callback, int transport, int phy, |
| 1892 | Handler handler) { |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1893 | return connectGatt(context, autoConnect, callback, transport, false, phy, handler); |
| 1894 | } |
| 1895 | |
| 1896 | /** |
| 1897 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 1898 | * The callback is used to deliver results to Caller, such as connection status as well |
| 1899 | * as any further GATT client operations. |
| 1900 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 1901 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1902 | * |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1903 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1904 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 1905 | * automatically connect as soon as the remote device becomes available (true). |
| 1906 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 1907 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 1908 | * BluetoothDevice#TRANSPORT_LE} |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1909 | * @param opportunistic Whether this GATT client is opportunistic. An opportunistic GATT client |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1910 | * does not hold a GATT connection. It automatically disconnects when no other GATT connections |
| 1911 | * are active for the remote device. |
| 1912 | * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link |
| 1913 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, an d{@link |
| 1914 | * BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect} |
| 1915 | * is set to true. |
| 1916 | * @param handler The handler to use for the callback. If {@code null}, callbacks will happen on |
| 1917 | * an un-specified background thread. |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1918 | * @return A BluetoothGatt instance. You can use BluetoothGatt to conduct GATT client |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1919 | * operations. |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1920 | * @hide |
| 1921 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1922 | @UnsupportedAppUsage |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1923 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1924 | BluetoothGattCallback callback, int transport, |
| 1925 | boolean opportunistic, int phy, Handler handler) { |
| 1926 | if (callback == null) { |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1927 | throw new NullPointerException("callback is null"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1928 | } |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1929 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1930 | // TODO(Bluetooth) check whether platform support BLE |
| 1931 | // Do the check here or in GattServer? |
| 1932 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 1933 | IBluetoothManager managerService = adapter.getBluetoothManager(); |
| 1934 | try { |
| 1935 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | 32ab77b | 2013-05-08 19:26:57 -0700 | [diff] [blame] | 1936 | if (iGatt == null) { |
| 1937 | // BLE is not supported |
| 1938 | return null; |
| 1939 | } |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 1940 | BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, opportunistic, phy); |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 1941 | gatt.connect(autoConnect, callback, handler); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1942 | return gatt; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1943 | } catch (RemoteException e) { |
| 1944 | Log.e(TAG, "", e); |
| 1945 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1946 | return null; |
| 1947 | } |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1948 | |
| 1949 | /** |
| 1950 | * Create a Bluetooth L2CAP Connection-oriented Channel (CoC) {@link BluetoothSocket} that can |
| 1951 | * be used to start a secure outgoing connection to the remote device with the same dynamic |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 1952 | * protocol/service multiplexer (PSM) value. The supported Bluetooth transport is LE only. |
| 1953 | * <p>This is designed to be used with {@link BluetoothAdapter#listenUsingL2capChannel()} for |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1954 | * peer-peer Bluetooth applications. |
| 1955 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing connection. |
| 1956 | * <p>Application using this API is responsible for obtaining PSM value from remote device. |
| 1957 | * <p>The remote device will be authenticated and communication on this socket will be |
| 1958 | * encrypted. |
| 1959 | * <p> Use this socket if an authenticated socket link is possible. Authentication refers |
| 1960 | * to the authentication of the link key to prevent man-in-the-middle type of attacks. When a |
| 1961 | * secure socket connection is not possible, use {#link createInsecureLeL2capCocSocket(int, |
| 1962 | * int)}. |
| 1963 | * |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1964 | * @param psm dynamic PSM value from remote device |
| 1965 | * @return a CoC #BluetoothSocket ready for an outgoing connection |
| 1966 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1967 | * permissions |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1968 | */ |
| 1969 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 1970 | public BluetoothSocket createL2capChannel(int psm) throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1971 | if (!isBluetoothEnabled()) { |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 1972 | Log.e(TAG, "createL2capChannel: Bluetooth is not enabled"); |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1973 | throw new IOException(); |
| 1974 | } |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 1975 | if (DBG) Log.d(TAG, "createL2capChannel: psm=" + psm); |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1976 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP_LE, -1, true, true, this, psm, |
| 1977 | null); |
| 1978 | } |
| 1979 | |
| 1980 | /** |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 1981 | * TODO: Remove this hidden method once all the SL4A and other tests are updated to use the new |
| 1982 | * API name, createL2capChannel. |
| 1983 | * @hide |
| 1984 | */ |
| 1985 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1986 | public BluetoothSocket createL2capCocSocket(int transport, int psm) throws IOException { |
| 1987 | Log.e(TAG, "createL2capCocSocket: PLEASE USE THE OFFICIAL API, createL2capChannel"); |
| 1988 | return createL2capChannel(psm); |
| 1989 | } |
| 1990 | |
| 1991 | /** |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1992 | * Create a Bluetooth L2CAP Connection-oriented Channel (CoC) {@link BluetoothSocket} that can |
| 1993 | * be used to start a secure outgoing connection to the remote device with the same dynamic |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 1994 | * protocol/service multiplexer (PSM) value. The supported Bluetooth transport is LE only. |
| 1995 | * <p>This is designed to be used with {@link |
| 1996 | * BluetoothAdapter#listenUsingInsecureL2capChannel()} for peer-peer Bluetooth applications. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 1997 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing connection. |
| 1998 | * <p>Application using this API is responsible for obtaining PSM value from remote device. |
| 1999 | * <p> The communication channel may not have an authenticated link key, i.e. it may be subject |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2000 | * to man-in-the-middle attacks. Use {@link #createL2capChannel(int)} if an encrypted and |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2001 | * authenticated communication channel is possible. |
| 2002 | * |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2003 | * @param psm dynamic PSM value from remote device |
| 2004 | * @return a CoC #BluetoothSocket ready for an outgoing connection |
| 2005 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2006 | * permissions |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2007 | */ |
| 2008 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 2009 | public BluetoothSocket createInsecureL2capChannel(int psm) throws IOException { |
| 2010 | if (!isBluetoothEnabled()) { |
| 2011 | Log.e(TAG, "createInsecureL2capChannel: Bluetooth is not enabled"); |
| 2012 | throw new IOException(); |
| 2013 | } |
| 2014 | if (DBG) { |
| 2015 | Log.d(TAG, "createInsecureL2capChannel: psm=" + psm); |
| 2016 | } |
| 2017 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP_LE, -1, false, false, this, psm, |
| 2018 | null); |
| 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * TODO: Remove this hidden method once all the SL4A and other tests are updated to use the new |
| 2023 | * API name, createInsecureL2capChannel. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2024 | * @hide |
| 2025 | */ |
| 2026 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 2027 | public BluetoothSocket createInsecureL2capCocSocket(int transport, int psm) throws IOException { |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2028 | Log.e(TAG, "createL2capCocSocket: PLEASE USE THE OFFICIAL API, createInsecureL2capChannel"); |
| 2029 | return createInsecureL2capChannel(psm); |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2030 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | } |