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 | /** |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 344 | * Maximum length of a metadata entry, this is to avoid exploding Bluetooth |
| 345 | * disk usage |
| 346 | * @hide |
| 347 | */ |
| 348 | @SystemApi |
| 349 | public static final int METADATA_MAX_LENGTH = 2048; |
| 350 | |
| 351 | /** |
| 352 | * Manufacturer name of this Bluetooth device |
| 353 | * @hide |
| 354 | */ |
| 355 | @SystemApi |
| 356 | public static final int METADATA_MANUFACTURER_NAME = 0; |
| 357 | |
| 358 | /** |
| 359 | * Model name of this Bluetooth device |
| 360 | * @hide |
| 361 | */ |
| 362 | @SystemApi |
| 363 | public static final int METADATA_MODEL_NAME = 1; |
| 364 | |
| 365 | /** |
| 366 | * Software version of this Bluetooth device |
| 367 | * @hide |
| 368 | */ |
| 369 | @SystemApi |
| 370 | public static final int METADATA_SOFTWARE_VERSION = 2; |
| 371 | |
| 372 | /** |
| 373 | * Hardware version of this Bluetooth device |
| 374 | * @hide |
| 375 | */ |
| 376 | @SystemApi |
| 377 | public static final int METADATA_HARDWARE_VERSION = 3; |
| 378 | |
| 379 | /** |
| 380 | * Package name of the companion app, if any |
| 381 | * @hide |
| 382 | */ |
| 383 | @SystemApi |
| 384 | public static final int METADATA_COMPANION_APP = 4; |
| 385 | |
| 386 | /** |
| 387 | * URI to the main icon shown on the settings UI |
| 388 | * @hide |
| 389 | */ |
| 390 | @SystemApi |
| 391 | public static final int METADATA_MAIN_ICON = 5; |
| 392 | |
| 393 | /** |
| 394 | * Whether this device is an untethered headset with left, right and case |
| 395 | * @hide |
| 396 | */ |
| 397 | @SystemApi |
| 398 | public static final int METADATA_IS_UNTHETHERED_HEADSET = 6; |
| 399 | |
| 400 | /** |
| 401 | * URI to icon of the left headset |
| 402 | * @hide |
| 403 | */ |
| 404 | @SystemApi |
| 405 | public static final int METADATA_UNTHETHERED_LEFT_ICON = 7; |
| 406 | |
| 407 | /** |
| 408 | * URI to icon of the right headset |
| 409 | * @hide |
| 410 | */ |
| 411 | @SystemApi |
| 412 | public static final int METADATA_UNTHETHERED_RIGHT_ICON = 8; |
| 413 | |
| 414 | /** |
| 415 | * URI to icon of the headset charging case |
| 416 | * @hide |
| 417 | */ |
| 418 | @SystemApi |
| 419 | public static final int METADATA_UNTHETHERED_CASE_ICON = 9; |
| 420 | |
| 421 | /** |
| 422 | * Battery level (0-100), {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN} |
| 423 | * is invalid, of the left headset |
| 424 | * @hide |
| 425 | */ |
| 426 | @SystemApi |
| 427 | public static final int METADATA_UNTHETHERED_LEFT_BATTERY = 10; |
| 428 | |
| 429 | /** |
| 430 | * Battery level (0-100), {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN} |
| 431 | * is invalid, of the right headset |
| 432 | * @hide |
| 433 | */ |
| 434 | @SystemApi |
| 435 | public static final int METADATA_UNTHETHERED_RIGHT_BATTERY = 11; |
| 436 | |
| 437 | /** |
| 438 | * Battery level (0-100), {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN} |
| 439 | * is invalid, of the headset charging case |
| 440 | * @hide |
| 441 | */ |
| 442 | @SystemApi |
| 443 | public static final int METADATA_UNTHETHERED_CASE_BATTERY = 12; |
| 444 | |
| 445 | /** |
| 446 | * Whether the left headset is charging |
| 447 | * @hide |
| 448 | */ |
| 449 | @SystemApi |
| 450 | public static final int METADATA_UNTHETHERED_LEFT_CHARGING = 13; |
| 451 | |
| 452 | /** |
| 453 | * Whether the right headset is charging |
| 454 | * @hide |
| 455 | */ |
| 456 | @SystemApi |
| 457 | public static final int METADATA_UNTHETHERED_RIGHT_CHARGING = 14; |
| 458 | |
| 459 | /** |
| 460 | * Whether the headset charging case is charging |
| 461 | * @hide |
| 462 | */ |
| 463 | @SystemApi |
| 464 | public static final int METADATA_UNTHETHERED_CASE_CHARGING = 15; |
| 465 | |
| 466 | /** |
| 467 | * URI to the enhanced settings UI slice, null or empty String means |
| 468 | * the UI does not exist |
| 469 | * @hide |
| 470 | */ |
| 471 | @SystemApi |
| 472 | public static final int METADATA_ENHANCED_SETTINGS_UI_URI = 16; |
| 473 | |
| 474 | /** |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 475 | * Broadcast Action: This intent is used to broadcast the {@link UUID} |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 476 | * wrapped as a {@link android.os.ParcelUuid} of the remote device after it |
| 477 | * has been fetched. This intent is sent only when the UUIDs of the remote |
| 478 | * device are requested to be fetched using Service Discovery Protocol |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 479 | * <p> Always contains the extra field {@link #EXTRA_DEVICE} |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 480 | * <p> Always contains the extra field {@link #EXTRA_UUID} |
Hansong Zhang | ff1c21c | 2018-11-12 14:32:03 -0800 | [diff] [blame] | 481 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} to receive. |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 482 | */ |
| 483 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 484 | public static final String ACTION_UUID = |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 485 | "android.bluetooth.device.action.UUID"; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 486 | |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 487 | /** @hide */ |
| 488 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 489 | public static final String ACTION_MAS_INSTANCE = |
| 490 | "android.bluetooth.device.action.MAS_INSTANCE"; |
| 491 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 492 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 493 | * Broadcast Action: Indicates a failure to retrieve the name of a remote |
| 494 | * device. |
| 495 | * <p>Always contains the extra field {@link #EXTRA_DEVICE}. |
| 496 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 497 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 498 | * @hide |
| 499 | */ |
| 500 | //TODO: is this actually useful? |
| 501 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 502 | public static final String ACTION_NAME_FAILED = |
| 503 | "android.bluetooth.device.action.NAME_FAILED"; |
| 504 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 505 | /** |
| 506 | * Broadcast Action: This intent is used to broadcast PAIRING REQUEST |
Matthew Xie | ac2c6c3 | 2013-10-21 14:56:33 -0700 | [diff] [blame] | 507 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} to |
Edward Jee | 430e361 | 2013-10-07 18:12:15 -0700 | [diff] [blame] | 508 | * receive. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 509 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 510 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 511 | public static final String ACTION_PAIRING_REQUEST = |
| 512 | "android.bluetooth.device.action.PAIRING_REQUEST"; |
| 513 | /** @hide */ |
| 514 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 515 | @UnsupportedAppUsage |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 516 | public static final String ACTION_PAIRING_CANCEL = |
| 517 | "android.bluetooth.device.action.PAIRING_CANCEL"; |
Yue Lixin | a4433af | 2009-07-09 16:56:43 +0800 | [diff] [blame] | 518 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 519 | /** @hide */ |
| 520 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 521 | public static final String ACTION_CONNECTION_ACCESS_REQUEST = |
| 522 | "android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST"; |
| 523 | |
| 524 | /** @hide */ |
| 525 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 526 | public static final String ACTION_CONNECTION_ACCESS_REPLY = |
| 527 | "android.bluetooth.device.action.CONNECTION_ACCESS_REPLY"; |
| 528 | |
| 529 | /** @hide */ |
| 530 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 531 | public static final String ACTION_CONNECTION_ACCESS_CANCEL = |
| 532 | "android.bluetooth.device.action.CONNECTION_ACCESS_CANCEL"; |
| 533 | |
| 534 | /** |
Ugo Yu | bfbe0ee | 2019-01-13 02:50:07 +0800 | [diff] [blame] | 535 | * Intent to broadcast silence mode changed. |
| 536 | * Alway contains the extra field {@link #EXTRA_DEVICE} |
| 537 | * Alway contains the extra field {@link #EXTRA_SILENCE_ENABLED} |
| 538 | * |
| 539 | * @hide |
| 540 | */ |
| 541 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 542 | @SystemApi |
| 543 | public static final String ACTION_SILENCE_MODE_CHANGED = |
| 544 | "android.bluetooth.device.action.SILENCE_MODE_CHANGED"; |
| 545 | |
| 546 | /** |
| 547 | * Used as an extra field in {@link #ACTION_SILENCE_MODE_CHANGED} intent, |
| 548 | * contains whether device is in silence mode as boolean. |
| 549 | * |
| 550 | * @hide |
| 551 | */ |
| 552 | @SystemApi |
| 553 | public static final String EXTRA_SILENCE_ENABLED = |
| 554 | "android.bluetooth.device.extra.SILENCE_ENABLED"; |
| 555 | |
| 556 | /** |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 557 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intent. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 558 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 559 | * @hide |
| 560 | */ |
| 561 | public static final String EXTRA_ACCESS_REQUEST_TYPE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 562 | "android.bluetooth.device.extra.ACCESS_REQUEST_TYPE"; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 563 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 564 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 565 | public static final int REQUEST_TYPE_PROFILE_CONNECTION = 1; |
| 566 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 567 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 568 | public static final int REQUEST_TYPE_PHONEBOOK_ACCESS = 2; |
| 569 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 570 | /** @hide */ |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 571 | public static final int REQUEST_TYPE_MESSAGE_ACCESS = 3; |
| 572 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 573 | /** @hide */ |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 574 | public static final int REQUEST_TYPE_SIM_ACCESS = 4; |
| 575 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 576 | /** |
| 577 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents, |
| 578 | * Contains package name to return reply intent to. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 579 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 580 | * @hide |
| 581 | */ |
| 582 | public static final String EXTRA_PACKAGE_NAME = "android.bluetooth.device.extra.PACKAGE_NAME"; |
| 583 | |
| 584 | /** |
| 585 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REQUEST} intents, |
| 586 | * Contains class name to return reply intent to. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 587 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 588 | * @hide |
| 589 | */ |
| 590 | public static final String EXTRA_CLASS_NAME = "android.bluetooth.device.extra.CLASS_NAME"; |
| 591 | |
| 592 | /** |
| 593 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intent. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 594 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 595 | * @hide |
| 596 | */ |
| 597 | public static final String EXTRA_CONNECTION_ACCESS_RESULT = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 598 | "android.bluetooth.device.extra.CONNECTION_ACCESS_RESULT"; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 599 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 600 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 601 | public static final int CONNECTION_ACCESS_YES = 1; |
| 602 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 603 | /** @hide */ |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 604 | public static final int CONNECTION_ACCESS_NO = 2; |
| 605 | |
| 606 | /** |
| 607 | * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intents, |
| 608 | * Contains boolean to indicate if the allowed response is once-for-all so that |
| 609 | * next request will be granted without asking user again. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 610 | * |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 611 | * @hide |
| 612 | */ |
| 613 | public static final String EXTRA_ALWAYS_ALLOWED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 614 | "android.bluetooth.device.extra.ALWAYS_ALLOWED"; |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 615 | |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 616 | /** |
| 617 | * A bond attempt succeeded |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 618 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 619 | * @hide |
| 620 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 621 | public static final int BOND_SUCCESS = 0; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 622 | |
| 623 | /** |
| 624 | * 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] | 625 | * not respond to pin request in time |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 626 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 627 | * @hide |
| 628 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 629 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | public static final int UNBOND_REASON_AUTH_FAILED = 1; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 631 | |
| 632 | /** |
| 633 | * A bond attempt failed because the other side explicitly rejected |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 634 | * bonding |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 635 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 636 | * @hide |
| 637 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 638 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | public static final int UNBOND_REASON_AUTH_REJECTED = 2; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 640 | |
| 641 | /** |
| 642 | * A bond attempt failed because we canceled the bonding process |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 643 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 644 | * @hide |
| 645 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | public static final int UNBOND_REASON_AUTH_CANCELED = 3; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 647 | |
| 648 | /** |
| 649 | * A bond attempt failed because we could not contact the remote device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 650 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 651 | * @hide |
| 652 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 653 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | public static final int UNBOND_REASON_REMOTE_DEVICE_DOWN = 4; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 655 | |
| 656 | /** |
| 657 | * A bond attempt failed because a discovery is in progress |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 658 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 659 | * @hide |
| 660 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 661 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | public static final int UNBOND_REASON_DISCOVERY_IN_PROGRESS = 5; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 663 | |
| 664 | /** |
| 665 | * A bond attempt failed because of authentication timeout |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 666 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 667 | * @hide |
| 668 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 669 | @UnsupportedAppUsage |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 670 | public static final int UNBOND_REASON_AUTH_TIMEOUT = 6; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 671 | |
| 672 | /** |
| 673 | * A bond attempt failed because of repeated attempts |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 674 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 675 | * @hide |
| 676 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 677 | @UnsupportedAppUsage |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 678 | public static final int UNBOND_REASON_REPEATED_ATTEMPTS = 7; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 679 | |
| 680 | /** |
| 681 | * A bond attempt failed because we received an Authentication Cancel |
| 682 | * by remote end |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 683 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 684 | * @hide |
| 685 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 686 | @UnsupportedAppUsage |
Jaikumar Ganesh | e5d93b7 | 2009-10-08 02:27:52 -0700 | [diff] [blame] | 687 | public static final int UNBOND_REASON_REMOTE_AUTH_CANCELED = 8; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 688 | |
| 689 | /** |
| 690 | * An existing bond was explicitly revoked |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 691 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 692 | * @hide |
| 693 | */ |
Jaikumar Ganesh | e5d93b7 | 2009-10-08 02:27:52 -0700 | [diff] [blame] | 694 | public static final int UNBOND_REASON_REMOVED = 9; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 696 | /** |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 697 | * The user will be prompted to enter a pin or |
Matthew Xie | ac2c6c3 | 2013-10-21 14:56:33 -0700 | [diff] [blame] | 698 | * an app will enter a pin for user. |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 699 | */ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 700 | public static final int PAIRING_VARIANT_PIN = 0; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 701 | |
| 702 | /** |
| 703 | * The user will be prompted to enter a passkey |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 704 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 705 | * @hide |
| 706 | */ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 707 | public static final int PAIRING_VARIANT_PASSKEY = 1; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 708 | |
| 709 | /** |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 710 | * 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] | 711 | * an app will confirm the passkey for the user. |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 712 | */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 713 | public static final int PAIRING_VARIANT_PASSKEY_CONFIRMATION = 2; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 714 | |
| 715 | /** |
| 716 | * 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] | 717 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 718 | * @hide |
| 719 | */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 720 | public static final int PAIRING_VARIANT_CONSENT = 3; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 721 | |
| 722 | /** |
| 723 | * The user will be prompted to enter the passkey displayed on remote device |
| 724 | * This is used for Bluetooth 2.1 pairing. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 725 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 726 | * @hide |
| 727 | */ |
Jaikumar Ganesh | 32d8571 | 2009-09-10 22:00:05 -0700 | [diff] [blame] | 728 | public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4; |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 729 | |
| 730 | /** |
| 731 | * The user will be prompted to enter the PIN displayed on remote device. |
| 732 | * This is used for Bluetooth 2.0 pairing. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 733 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 734 | * @hide |
| 735 | */ |
| 736 | public static final int PAIRING_VARIANT_DISPLAY_PIN = 5; |
| 737 | |
| 738 | /** |
| 739 | * 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] | 740 | * |
Jaikumar Ganesh | c88b0c6 | 2011-01-05 13:49:00 -0800 | [diff] [blame] | 741 | * @hide |
| 742 | */ |
| 743 | public static final int PAIRING_VARIANT_OOB_CONSENT = 6; |
| 744 | |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 745 | /** |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 746 | * The user will be prompted to enter a 16 digit pin or |
| 747 | * an app will enter a 16 digit pin for user. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 748 | * |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 749 | * @hide |
| 750 | */ |
| 751 | public static final int PAIRING_VARIANT_PIN_16_DIGITS = 7; |
| 752 | |
| 753 | /** |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 754 | * Used as an extra field in {@link #ACTION_UUID} intents, |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 755 | * Contains the {@link android.os.ParcelUuid}s of the remote device which |
| 756 | * is a parcelable version of {@link UUID}. |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 757 | */ |
| 758 | public static final String EXTRA_UUID = "android.bluetooth.device.extra.UUID"; |
| 759 | |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 760 | /** @hide */ |
| 761 | public static final String EXTRA_SDP_RECORD = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 762 | "android.bluetooth.device.extra.SDP_RECORD"; |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 763 | |
| 764 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 765 | @UnsupportedAppUsage |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 766 | public static final String EXTRA_SDP_SEARCH_STATUS = |
| 767 | "android.bluetooth.device.extra.SDP_SEARCH_STATUS"; |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 768 | /** |
| 769 | * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission}, |
| 770 | * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 771 | * |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 772 | * @hide |
| 773 | */ |
Selim Gurun | 17a26c7 | 2018-01-09 14:35:19 -0800 | [diff] [blame] | 774 | @SystemApi |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 775 | public static final int ACCESS_UNKNOWN = 0; |
| 776 | |
| 777 | /** |
| 778 | * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission}, |
| 779 | * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 780 | * |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 781 | * @hide |
| 782 | */ |
Selim Gurun | 17a26c7 | 2018-01-09 14:35:19 -0800 | [diff] [blame] | 783 | @SystemApi |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 784 | public static final int ACCESS_ALLOWED = 1; |
| 785 | |
| 786 | /** |
| 787 | * For {@link #getPhonebookAccessPermission}, {@link #setPhonebookAccessPermission}, |
| 788 | * {@link #getMessageAccessPermission} and {@link #setMessageAccessPermission}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 789 | * |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 790 | * @hide |
| 791 | */ |
Selim Gurun | 17a26c7 | 2018-01-09 14:35:19 -0800 | [diff] [blame] | 792 | @SystemApi |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 793 | public static final int ACCESS_REJECTED = 2; |
| 794 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 795 | /** |
| 796 | * No preferrence of physical transport for GATT connections to remote dual-mode devices |
| 797 | */ |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 798 | public static final int TRANSPORT_AUTO = 0; |
| 799 | |
| 800 | /** |
| 801 | * 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] | 802 | */ |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 803 | public static final int TRANSPORT_BREDR = 1; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 804 | |
| 805 | /** |
| 806 | * Prefer LE transport for GATT connections to remote dual-mode devices |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 807 | */ |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 808 | public static final int TRANSPORT_LE = 2; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 809 | |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 810 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 811 | * Bluetooth LE 1M PHY. Used to refer to LE 1M Physical Channel for advertising, scanning or |
| 812 | * connection. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 813 | */ |
| 814 | public static final int PHY_LE_1M = 1; |
| 815 | |
| 816 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 817 | * Bluetooth LE 2M PHY. Used to refer to LE 2M Physical Channel for advertising, scanning or |
| 818 | * connection. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 819 | */ |
| 820 | public static final int PHY_LE_2M = 2; |
| 821 | |
| 822 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 823 | * Bluetooth LE Coded PHY. Used to refer to LE Coded Physical Channel for advertising, scanning |
| 824 | * or connection. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 825 | */ |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 826 | public static final int PHY_LE_CODED = 3; |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 827 | |
| 828 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 829 | * Bluetooth LE 1M PHY mask. Used to specify LE 1M Physical Channel as one of many available |
| 830 | * options in a bitmask. |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 831 | */ |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 832 | public static final int PHY_LE_1M_MASK = 1; |
| 833 | |
| 834 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 835 | * Bluetooth LE 2M PHY mask. Used to specify LE 2M Physical Channel as one of many available |
| 836 | * options in a bitmask. |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 837 | */ |
| 838 | public static final int PHY_LE_2M_MASK = 2; |
| 839 | |
| 840 | /** |
Jakub Pawlowski | 91fe308 | 2017-04-21 04:26:12 -0700 | [diff] [blame] | 841 | * Bluetooth LE Coded PHY mask. Used to specify LE Coded Physical Channel as one of many |
| 842 | * available options in a bitmask. |
Jakub Pawlowski | a515137 | 2017-04-12 08:51:22 -0700 | [diff] [blame] | 843 | */ |
| 844 | public static final int PHY_LE_CODED_MASK = 4; |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 845 | |
| 846 | /** |
| 847 | * No preferred coding when transmitting on the LE Coded PHY. |
| 848 | */ |
| 849 | public static final int PHY_OPTION_NO_PREFERRED = 0; |
| 850 | |
| 851 | /** |
| 852 | * Prefer the S=2 coding to be used when transmitting on the LE Coded PHY. |
| 853 | */ |
| 854 | public static final int PHY_OPTION_S2 = 1; |
| 855 | |
| 856 | /** |
| 857 | * Prefer the S=8 coding to be used when transmitting on the LE Coded PHY. |
| 858 | */ |
| 859 | public static final int PHY_OPTION_S8 = 2; |
| 860 | |
| 861 | |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 862 | /** @hide */ |
| 863 | public static final String EXTRA_MAS_INSTANCE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 864 | "android.bluetooth.device.extra.MAS_INSTANCE"; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 865 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 866 | /** |
| 867 | * Lazy initialization. Guaranteed final after first object constructed, or |
| 868 | * getService() called. |
| 869 | * TODO: Unify implementation of sService amongst BluetoothFoo API's |
| 870 | */ |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 871 | private static volatile IBluetooth sService; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 872 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 873 | private final String mAddress; |
| 874 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 875 | /*package*/ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 876 | @UnsupportedAppUsage |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 877 | static IBluetooth getService() { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 878 | synchronized (BluetoothDevice.class) { |
| 879 | if (sService == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 880 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 881 | sService = adapter.getBluetoothService(sStateChangeCallback); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | return sService; |
| 885 | } |
| 886 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 887 | static IBluetoothManagerCallback sStateChangeCallback = new IBluetoothManagerCallback.Stub() { |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 888 | |
| 889 | public void onBluetoothServiceUp(IBluetooth bluetoothService) |
| 890 | throws RemoteException { |
| 891 | synchronized (BluetoothDevice.class) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 892 | if (sService == null) { |
| 893 | sService = bluetoothService; |
| 894 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | public void onBluetoothServiceDown() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 899 | throws RemoteException { |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 900 | synchronized (BluetoothDevice.class) { |
| 901 | sService = null; |
| 902 | } |
| 903 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 904 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 905 | public void onBrEdrDown() { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 906 | if (DBG) Log.d(TAG, "onBrEdrDown: reached BLE ON state"); |
| 907 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 908 | }; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 911 | * Create a new BluetoothDevice |
| 912 | * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", |
| 913 | * and is validated in this constructor. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 914 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 915 | * @param address valid Bluetooth MAC address |
| 916 | * @throws RuntimeException Bluetooth is not available on this platform |
| 917 | * @throws IllegalArgumentException address is invalid |
| 918 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 920 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 921 | /*package*/ BluetoothDevice(String address) { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 922 | getService(); // ensures sService is initialized |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 923 | if (!BluetoothAdapter.checkBluetoothAddress(address)) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 924 | throw new IllegalArgumentException(address + " is not a valid Bluetooth address"); |
| 925 | } |
| 926 | |
| 927 | mAddress = address; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | } |
| 929 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 930 | @Override |
| 931 | public boolean equals(Object o) { |
| 932 | if (o instanceof BluetoothDevice) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 933 | return mAddress.equals(((BluetoothDevice) o).getAddress()); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 934 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | return false; |
| 936 | } |
| 937 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 938 | @Override |
| 939 | public int hashCode() { |
| 940 | return mAddress.hashCode(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 943 | /** |
| 944 | * Returns a string representation of this BluetoothDevice. |
| 945 | * <p>Currently this is the Bluetooth hardware address, for example |
| 946 | * "00:11:22:AA:BB:CC". However, you should always use {@link #getAddress} |
| 947 | * if you explicitly require the Bluetooth hardware address in case the |
| 948 | * {@link #toString} representation changes in the future. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 949 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 950 | * @return string representation of this BluetoothDevice |
| 951 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 952 | @Override |
| 953 | public String toString() { |
| 954 | return mAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 957 | @Override |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 958 | public int describeContents() { |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | public static final Parcelable.Creator<BluetoothDevice> CREATOR = |
| 963 | new Parcelable.Creator<BluetoothDevice>() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 964 | public BluetoothDevice createFromParcel(Parcel in) { |
| 965 | return new BluetoothDevice(in.readString()); |
| 966 | } |
| 967 | |
| 968 | public BluetoothDevice[] newArray(int size) { |
| 969 | return new BluetoothDevice[size]; |
| 970 | } |
| 971 | }; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 972 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 973 | @Override |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 974 | public void writeToParcel(Parcel out, int flags) { |
| 975 | out.writeString(mAddress); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 978 | /** |
| 979 | * Returns the hardware address of this BluetoothDevice. |
| 980 | * <p> For example, "00:11:22:AA:BB:CC". |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 981 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 982 | * @return Bluetooth hardware address as string |
| 983 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | public String getAddress() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 985 | if (DBG) Log.d(TAG, "mAddress: " + mAddress); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 986 | return mAddress; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 990 | * Get the friendly Bluetooth name of the remote device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 992 | * <p>The local adapter will automatically retrieve remote names when |
| 993 | * performing a device scan, and will cache them. This method just returns |
| 994 | * the name for this device from the cache. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | * |
| 996 | * @return the Bluetooth name, or null if there was a problem. |
| 997 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 998 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | public String getName() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1000 | final IBluetooth service = sService; |
| 1001 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1002 | Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); |
| 1003 | return null; |
| 1004 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | try { |
Hansong Zhang | 2e3bf46 | 2018-04-26 14:22:39 -0700 | [diff] [blame] | 1006 | String name = service.getRemoteName(this); |
| 1007 | if (name != null) { |
| 1008 | return name.replaceAll("[\\t\\n\\r]+", " "); |
| 1009 | } |
| 1010 | return null; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1011 | } catch (RemoteException e) { |
| 1012 | Log.e(TAG, "", e); |
| 1013 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | return null; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 1018 | * Get the Bluetooth device type of the remote device. |
| 1019 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1020 | * @return the device type {@link #DEVICE_TYPE_CLASSIC}, {@link #DEVICE_TYPE_LE} {@link |
| 1021 | * #DEVICE_TYPE_DUAL}. {@link #DEVICE_TYPE_UNKNOWN} if it's not available |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 1022 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1023 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 1024 | public int getType() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1025 | final IBluetooth service = sService; |
| 1026 | if (service == null) { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 1027 | Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); |
| 1028 | return DEVICE_TYPE_UNKNOWN; |
| 1029 | } |
| 1030 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1031 | return service.getRemoteType(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1032 | } catch (RemoteException e) { |
| 1033 | Log.e(TAG, "", e); |
| 1034 | } |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 1035 | return DEVICE_TYPE_UNKNOWN; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1039 | * Get the Bluetooth alias of the remote device. |
| 1040 | * <p>Alias is the locally modified name of a remote device. |
| 1041 | * |
| 1042 | * @return the Bluetooth alias, or null if no alias or there was a problem |
| 1043 | * @hide |
| 1044 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1045 | @UnsupportedAppUsage |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1046 | public String getAlias() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1047 | final IBluetooth service = sService; |
| 1048 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1049 | Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); |
| 1050 | return null; |
| 1051 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1052 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1053 | return service.getRemoteAlias(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1054 | } catch (RemoteException e) { |
| 1055 | Log.e(TAG, "", e); |
| 1056 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1057 | return null; |
| 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * Set the Bluetooth alias of the remote device. |
| 1062 | * <p>Alias is the locally modified name of a remote device. |
| 1063 | * <p>This methoid overwrites the alias. The changed |
| 1064 | * alias is saved in the local storage so that the change |
| 1065 | * is preserved over power cycle. |
| 1066 | * |
| 1067 | * @return true on success, false on error |
| 1068 | * @hide |
| 1069 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1070 | @UnsupportedAppUsage |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1071 | public boolean setAlias(String alias) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1072 | final IBluetooth service = sService; |
| 1073 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1074 | Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); |
| 1075 | return false; |
| 1076 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1077 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1078 | return service.setRemoteAlias(this, alias); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1079 | } catch (RemoteException e) { |
| 1080 | Log.e(TAG, "", e); |
| 1081 | } |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * Get the Bluetooth alias of the remote device. |
| 1087 | * If Alias is null, get the Bluetooth name instead. |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1088 | * |
| 1089 | * @return the Bluetooth alias, or null if no alias or there was a problem |
| 1090 | * @hide |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1091 | * @see #getAlias() |
| 1092 | * @see #getName() |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1093 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1094 | @UnsupportedAppUsage |
Matthew Xie | 269e81a | 2011-07-26 18:36:49 -0700 | [diff] [blame] | 1095 | public String getAliasName() { |
| 1096 | String name = getAlias(); |
| 1097 | if (name == null) { |
| 1098 | name = getName(); |
| 1099 | } |
| 1100 | return name; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 1104 | * Get the most recent identified battery level of this Bluetooth device |
| 1105 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1106 | * |
| 1107 | * @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] | 1108 | * Bluetooth is disabled, or device is disconnected, or does not have any battery reporting |
| 1109 | * service, or return value is invalid |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 1110 | * @hide |
| 1111 | */ |
| 1112 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1113 | @UnsupportedAppUsage |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 1114 | public int getBatteryLevel() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1115 | final IBluetooth service = sService; |
| 1116 | if (service == null) { |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 1117 | Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level"); |
| 1118 | return BATTERY_LEVEL_UNKNOWN; |
| 1119 | } |
| 1120 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1121 | return service.getBatteryLevel(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1122 | } catch (RemoteException e) { |
| 1123 | Log.e(TAG, "", e); |
| 1124 | } |
Jack He | 1d312bf | 2017-06-16 19:43:58 -0700 | [diff] [blame] | 1125 | return BATTERY_LEVEL_UNKNOWN; |
| 1126 | } |
| 1127 | |
| 1128 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1129 | * Start the bonding (pairing) process with the remote device. |
| 1130 | * <p>This is an asynchronous call, it will return immediately. Register |
| 1131 | * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
| 1132 | * the bonding process completes, and its result. |
| 1133 | * <p>Android system services will handle the necessary user interactions |
| 1134 | * to confirm and complete the bonding process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1136 | * @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] | 1137 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1138 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1139 | public boolean createBond() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1140 | final IBluetooth service = sService; |
| 1141 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1142 | Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); |
| 1143 | return false; |
| 1144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1146 | Log.i(TAG, "createBond() for device " + getAddress() |
| 1147 | + " called by pid: " + Process.myPid() |
| 1148 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1149 | return service.createBond(this, TRANSPORT_AUTO); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1150 | } catch (RemoteException e) { |
| 1151 | Log.e(TAG, "", e); |
| 1152 | } |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * Start the bonding (pairing) process with the remote device using the |
| 1158 | * specified transport. |
| 1159 | * |
| 1160 | * <p>This is an asynchronous call, it will return immediately. Register |
| 1161 | * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
| 1162 | * the bonding process completes, and its result. |
| 1163 | * <p>Android system services will handle the necessary user interactions |
| 1164 | * to confirm and complete the bonding process. |
| 1165 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1166 | * |
| 1167 | * @param transport The transport to use for the pairing procedure. |
| 1168 | * @return false on immediate error, true if bonding will begin |
| 1169 | * @throws IllegalArgumentException if an invalid transport was specified |
| 1170 | * @hide |
| 1171 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1172 | @UnsupportedAppUsage |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1173 | public boolean createBond(int transport) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1174 | final IBluetooth service = sService; |
| 1175 | if (service == null) { |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1176 | Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); |
| 1177 | return false; |
| 1178 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1179 | if (TRANSPORT_AUTO > transport || transport > TRANSPORT_LE) { |
Andre Eisenbach | 57210c7 | 2014-08-04 17:51:43 -0700 | [diff] [blame] | 1180 | throw new IllegalArgumentException(transport + " is not a valid Bluetooth transport"); |
| 1181 | } |
| 1182 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1183 | Log.i(TAG, "createBond() for device " + getAddress() |
| 1184 | + " called by pid: " + Process.myPid() |
| 1185 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1186 | return service.createBond(this, transport); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1187 | } catch (RemoteException e) { |
| 1188 | Log.e(TAG, "", e); |
| 1189 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | /** |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1194 | * Start the bonding (pairing) process with the remote device using the |
| 1195 | * Out Of Band mechanism. |
| 1196 | * |
| 1197 | * <p>This is an asynchronous call, it will return immediately. Register |
| 1198 | * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
| 1199 | * the bonding process completes, and its result. |
| 1200 | * |
| 1201 | * <p>Android system services will handle the necessary user interactions |
| 1202 | * to confirm and complete the bonding process. |
| 1203 | * |
| 1204 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1205 | * |
Jakub Pawlowski | 2fc6e6b | 2015-12-29 13:19:21 -0800 | [diff] [blame] | 1206 | * @param transport - Transport to use |
| 1207 | * @param oobData - Out Of Band data |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1208 | * @return false on immediate error, true if bonding will begin |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1209 | * @hide |
| 1210 | */ |
Jakub Pawlowski | 2fc6e6b | 2015-12-29 13:19:21 -0800 | [diff] [blame] | 1211 | public boolean createBondOutOfBand(int transport, OobData oobData) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1212 | final IBluetooth service = sService; |
| 1213 | if (service == null) { |
| 1214 | Log.w(TAG, "BT not enabled, createBondOutOfBand failed"); |
| 1215 | return false; |
| 1216 | } |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1217 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1218 | return service.createBondOutOfBand(this, transport, oobData); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1219 | } catch (RemoteException e) { |
| 1220 | Log.e(TAG, "", e); |
| 1221 | } |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
| 1224 | |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1225 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1226 | @UnsupportedAppUsage |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1227 | public boolean isBondingInitiatedLocally() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1228 | final IBluetooth service = sService; |
| 1229 | if (service == null) { |
| 1230 | Log.w(TAG, "BT not enabled, isBondingInitiatedLocally failed"); |
| 1231 | return false; |
| 1232 | } |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1233 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1234 | return service.isBondingInitiatedLocally(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1235 | } catch (RemoteException e) { |
| 1236 | Log.e(TAG, "", e); |
| 1237 | } |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 1238 | return false; |
| 1239 | } |
| 1240 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1241 | /** |
| 1242 | * Set the Out Of Band data for a remote device to be used later |
| 1243 | * in the pairing mechanism. Users can obtain this data through other |
| 1244 | * trusted channels |
| 1245 | * |
| 1246 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1247 | * |
| 1248 | * @param hash Simple Secure pairing hash |
| 1249 | * @param randomizer The random key obtained using OOB |
| 1250 | * @return false on error; true otherwise |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1251 | * @hide |
| 1252 | */ |
| 1253 | public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1254 | //TODO(BT) |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1255 | /* |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1256 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1257 | return sService.setDeviceOutOfBandData(this, hash, randomizer); |
| 1258 | } catch (RemoteException e) {Log.e(TAG, "", e);} */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1259 | return false; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1263 | * Cancel an in-progress bonding request started with {@link #createBond}. |
| 1264 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 1265 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1266 | * @return true on success, false on error |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 1267 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | */ |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1269 | @SystemApi |
| 1270 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1271 | public boolean cancelBondProcess() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1272 | final IBluetooth service = sService; |
| 1273 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1274 | Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); |
| 1275 | return false; |
| 1276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1278 | Log.i(TAG, "cancelBondProcess() for device " + getAddress() |
| 1279 | + " called by pid: " + Process.myPid() |
| 1280 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1281 | return service.cancelBondProcess(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1282 | } catch (RemoteException e) { |
| 1283 | Log.e(TAG, "", e); |
| 1284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1289 | * Remove bond (pairing) with the remote device. |
| 1290 | * <p>Delete the link key associated with the remote device, and |
| 1291 | * immediately terminate connections to that device that require |
| 1292 | * authentication and encryption. |
| 1293 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1295 | * @return true on success, false on error |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 1296 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | */ |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1298 | @SystemApi |
| 1299 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1300 | public boolean removeBond() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1301 | final IBluetooth service = sService; |
| 1302 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1303 | Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); |
| 1304 | return false; |
| 1305 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1307 | Log.i(TAG, "removeBond() for device " + getAddress() |
| 1308 | + " called by pid: " + Process.myPid() |
| 1309 | + " tid: " + Process.myTid()); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1310 | return service.removeBond(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1311 | } catch (RemoteException e) { |
| 1312 | Log.e(TAG, "", e); |
| 1313 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1314 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1318 | * Get the bond state of the remote device. |
| 1319 | * <p>Possible values for the bond state are: |
| 1320 | * {@link #BOND_NONE}, |
| 1321 | * {@link #BOND_BONDING}, |
| 1322 | * {@link #BOND_BONDED}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1324 | * @return the bond state |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1326 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1327 | public int getBondState() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1328 | final IBluetooth service = sService; |
| 1329 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1330 | Log.e(TAG, "BT not enabled. Cannot get bond state"); |
| 1331 | return BOND_NONE; |
| 1332 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1334 | return service.getBondState(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1335 | } catch (RemoteException e) { |
| 1336 | Log.e(TAG, "", e); |
Syed Ibrahim M | 305f240 | 2012-06-19 10:14:25 -0700 | [diff] [blame] | 1337 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1338 | return BOND_NONE; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1342 | * Returns whether there is an open connection to this device. |
| 1343 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 1344 | * |
| 1345 | * @return True if there is at least one open connection to this device. |
| 1346 | * @hide |
| 1347 | */ |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1348 | @SystemApi |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1349 | @RequiresPermission(android.Manifest.permission.BLUETOOTH) |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1350 | public boolean isConnected() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1351 | final IBluetooth service = sService; |
| 1352 | if (service == null) { |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1353 | // BT is not enabled, we cannot be connected. |
| 1354 | return false; |
| 1355 | } |
| 1356 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1357 | return service.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED; |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1358 | } catch (RemoteException e) { |
| 1359 | Log.e(TAG, "", e); |
| 1360 | return false; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Returns whether there is an open connection to this device |
| 1366 | * that has been encrypted. |
| 1367 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 1368 | * |
| 1369 | * @return True if there is at least one encrypted connection to this device. |
| 1370 | * @hide |
| 1371 | */ |
| 1372 | @SystemApi |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1373 | @RequiresPermission(android.Manifest.permission.BLUETOOTH) |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1374 | public boolean isEncrypted() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1375 | final IBluetooth service = sService; |
| 1376 | if (service == null) { |
Andre Eisenbach | 2b8696e | 2015-02-05 20:06:33 -0800 | [diff] [blame] | 1377 | // BT is not enabled, we cannot be connected. |
| 1378 | return false; |
| 1379 | } |
| 1380 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1381 | return service.getConnectionState(this) > CONNECTION_STATE_CONNECTED; |
Jay Civelli | 174928c | 2014-05-08 09:24:08 -0700 | [diff] [blame] | 1382 | } catch (RemoteException e) { |
| 1383 | Log.e(TAG, "", e); |
| 1384 | return false; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1389 | * Get the Bluetooth class of the remote device. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1390 | * |
| 1391 | * @return Bluetooth class object, or null on error |
| 1392 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1393 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1394 | public BluetoothClass getBluetoothClass() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1395 | final IBluetooth service = sService; |
| 1396 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1397 | Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); |
| 1398 | return null; |
| 1399 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1400 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1401 | int classInt = service.getRemoteClass(this); |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1402 | if (classInt == BluetoothClass.ERROR) return null; |
| 1403 | return new BluetoothClass(classInt); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1404 | } catch (RemoteException e) { |
| 1405 | Log.e(TAG, "", e); |
| 1406 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1407 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Lixin Yue | efa1dd7 | 2009-08-31 15:55:13 +0800 | [diff] [blame] | 1410 | /** |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 1411 | * Returns the supported features (UUIDs) of the remote device. |
| 1412 | * |
| 1413 | * <p>This method does not start a service discovery procedure to retrieve the UUIDs |
| 1414 | * from the remote device. Instead, the local cached copy of the service |
| 1415 | * UUIDs are returned. |
| 1416 | * <p>Use {@link #fetchUuidsWithSdp} if fresh UUIDs are desired. |
Matthew Xie | ad23210 | 2011-11-08 10:58:12 -0800 | [diff] [blame] | 1417 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1418 | * @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] | 1419 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1420 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1421 | public ParcelUuid[] getUuids() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1422 | final IBluetooth service = sService; |
| 1423 | if (service == null || !isBluetoothEnabled()) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1424 | Log.e(TAG, "BT not enabled. Cannot get remote device Uuids"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1425 | return null; |
| 1426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1428 | return service.getRemoteUuids(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1429 | } catch (RemoteException e) { |
| 1430 | Log.e(TAG, "", e); |
| 1431 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | return null; |
| 1433 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 1434 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1435 | /** |
| 1436 | * Perform a service discovery on the remote device to get the UUIDs supported. |
| 1437 | * |
| 1438 | * <p>This API is asynchronous and {@link #ACTION_UUID} intent is sent, |
| 1439 | * with the UUIDs supported by the remote end. If there is an error |
| 1440 | * in getting the SDP records or if the process takes a long time, |
| 1441 | * {@link #ACTION_UUID} intent is sent with the UUIDs that is currently |
| 1442 | * present in the cache. Clients should use the {@link #getUuids} to get UUIDs |
| 1443 | * if service discovery is not to be performed. |
| 1444 | * |
| 1445 | * @return False if the sanity check fails, True if the process of initiating an ACL connection |
| 1446 | * to the remote device was started. |
| 1447 | */ |
| 1448 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1449 | public boolean fetchUuidsWithSdp() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1450 | final IBluetooth service = sService; |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1451 | if (service == null || !isBluetoothEnabled()) { |
Zhihai Xu | 5d04f1c | 2014-01-13 16:54:38 -0800 | [diff] [blame] | 1452 | Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp"); |
| 1453 | return false; |
| 1454 | } |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1455 | try { |
Zhihai Xu | 5d04f1c | 2014-01-13 16:54:38 -0800 | [diff] [blame] | 1456 | return service.fetchRemoteUuids(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1457 | } catch (RemoteException e) { |
| 1458 | Log.e(TAG, "", e); |
| 1459 | } |
| 1460 | return false; |
Jaikumar Ganesh | 1caa6d1 | 2009-09-18 11:32:54 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1463 | /** |
| 1464 | * Perform a service discovery on the remote device to get the SDP records associated |
| 1465 | * with the specified UUID. |
| 1466 | * |
| 1467 | * <p>This API is asynchronous and {@link #ACTION_SDP_RECORD} intent is sent, |
| 1468 | * with the SDP records found on the remote end. If there is an error |
| 1469 | * in getting the SDP records or if the process takes a long time, |
| 1470 | * {@link #ACTION_SDP_RECORD} intent is sent with an status value in |
| 1471 | * {@link #EXTRA_SDP_SEARCH_STATUS} different from 0. |
| 1472 | * Detailed status error codes can be found by members of the Bluetooth package in |
| 1473 | * the AbstractionLayer class. |
| 1474 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 1475 | * The SDP record data will be stored in the intent as {@link #EXTRA_SDP_RECORD}. |
| 1476 | * The object type will match one of the SdpXxxRecord types, depending on the UUID searched |
| 1477 | * for. |
| 1478 | * |
| 1479 | * @return False if the sanity check fails, True if the process |
| 1480 | * of initiating an ACL connection to the remote device |
| 1481 | * was started. |
| 1482 | */ |
| 1483 | /** @hide */ |
| 1484 | public boolean sdpSearch(ParcelUuid uuid) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1485 | final IBluetooth service = sService; |
| 1486 | if (service == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1487 | Log.e(TAG, "BT not enabled. Cannot query remote device sdp records"); |
| 1488 | return false; |
| 1489 | } |
| 1490 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1491 | return service.sdpSearch(this, uuid); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1492 | } catch (RemoteException e) { |
| 1493 | Log.e(TAG, "", e); |
| 1494 | } |
| 1495 | return false; |
| 1496 | } |
Hemant Gupta | 8949bfb | 2013-08-16 14:57:55 +0530 | [diff] [blame] | 1497 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1498 | /** |
| 1499 | * 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] | 1500 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1501 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1502 | * @return true pin has been set false for error |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1503 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1504 | public boolean setPin(byte[] pin) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1505 | final IBluetooth service = sService; |
| 1506 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1507 | Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); |
| 1508 | return false; |
| 1509 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1511 | return service.setPin(this, true, pin.length, pin); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1512 | } catch (RemoteException e) { |
| 1513 | Log.e(TAG, "", e); |
| 1514 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | return false; |
| 1516 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1517 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1518 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1519 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1520 | public boolean setPasskey(int passkey) { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1521 | //TODO(BT) |
| 1522 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1524 | return sService.setPasskey(this, true, 4, passkey); |
| 1525 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1526 | return false; |
| 1527 | } |
| 1528 | |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1529 | /** |
| 1530 | * Confirm passkey for {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION} pairing. |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1531 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1532 | * @return true confirmation has been sent out false for error |
Matthew Xie | 091fc2b | 2013-09-23 23:23:13 -0700 | [diff] [blame] | 1533 | */ |
Marie Janssen | 93326cf | 2016-08-09 13:23:39 -0700 | [diff] [blame] | 1534 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1535 | public boolean setPairingConfirmation(boolean confirm) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1536 | final IBluetooth service = sService; |
| 1537 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1538 | Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); |
| 1539 | return false; |
| 1540 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1541 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1542 | return service.setPairingConfirmation(this, confirm); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1543 | } catch (RemoteException e) { |
| 1544 | Log.e(TAG, "", e); |
| 1545 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1546 | return false; |
| 1547 | } |
| 1548 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1549 | /** @hide */ |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1550 | public boolean setRemoteOutOfBandData() { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1551 | // TODO(BT) |
| 1552 | /* |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1553 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1554 | return sService.setRemoteOutOfBandData(this); |
| 1555 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1556 | return false; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1560 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1561 | public boolean cancelPairingUserInput() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1562 | final IBluetooth service = sService; |
| 1563 | if (service == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1564 | Log.e(TAG, "BT not enabled. Cannot create pairing user input"); |
| 1565 | return false; |
| 1566 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 1567 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1568 | return service.cancelBondProcess(this); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1569 | } catch (RemoteException e) { |
| 1570 | Log.e(TAG, "", e); |
| 1571 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | return false; |
| 1573 | } |
| 1574 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1575 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1576 | @UnsupportedAppUsage |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1577 | public boolean isBluetoothDock() { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1578 | // TODO(BT) |
| 1579 | /* |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1580 | try { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1581 | return sService.isBluetoothDock(this); |
| 1582 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 1583 | return false; |
| 1584 | } |
| 1585 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1586 | boolean isBluetoothEnabled() { |
| 1587 | boolean ret = false; |
| 1588 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1589 | if (adapter != null && adapter.isEnabled()) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1590 | ret = true; |
| 1591 | } |
| 1592 | return ret; |
| 1593 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | /** |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1596 | * Requires {@link android.Manifest.permission#BLUETOOTH}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1597 | * |
| 1598 | * @return Whether the phonebook access is allowed to this device. Can be {@link |
| 1599 | * #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1600 | * @hide |
| 1601 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1602 | @UnsupportedAppUsage |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1603 | public int getPhonebookAccessPermission() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1604 | final IBluetooth service = sService; |
| 1605 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1606 | return ACCESS_UNKNOWN; |
| 1607 | } |
| 1608 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1609 | return service.getPhonebookAccessPermission(this); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1610 | } catch (RemoteException e) { |
| 1611 | Log.e(TAG, "", e); |
| 1612 | } |
| 1613 | return ACCESS_UNKNOWN; |
| 1614 | } |
| 1615 | |
| 1616 | /** |
Ugo Yu | bfbe0ee | 2019-01-13 02:50:07 +0800 | [diff] [blame] | 1617 | * Set the Bluetooth device silence mode. |
| 1618 | * |
| 1619 | * When the {@link BluetoothDevice} enters silence mode, and the {@link BluetoothDevice} |
| 1620 | * is an active device (for A2DP or HFP), the active device for that profile |
| 1621 | * will be set to null. |
| 1622 | * If the {@link BluetoothDevice} exits silence mode while the A2DP or HFP |
| 1623 | * active device is null, the {@link BluetoothDevice} will be set as the |
| 1624 | * active device for that profile. |
| 1625 | * If the {@link BluetoothDevice} is disconnected, it exits silence mode. |
| 1626 | * If the {@link BluetoothDevice} is set as the active device for A2DP or |
| 1627 | * HFP, while silence mode is enabled, then the device will exit silence mode. |
| 1628 | * If the {@link BluetoothDevice} is in silence mode, AVRCP position change |
| 1629 | * event and HFP AG indicators will be disabled. |
| 1630 | * If the {@link BluetoothDevice} is not connected with A2DP or HFP, it cannot |
| 1631 | * enter silence mode. |
| 1632 | * |
| 1633 | * <p> Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
| 1634 | * |
| 1635 | * @param silence true to enter silence mode, false to exit |
| 1636 | * @return true on success, false on error. |
| 1637 | * @hide |
| 1638 | */ |
| 1639 | @SystemApi |
| 1640 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 1641 | public boolean setSilenceMode(boolean silence) { |
| 1642 | final IBluetooth service = sService; |
| 1643 | if (service == null) { |
| 1644 | return false; |
| 1645 | } |
| 1646 | try { |
| 1647 | if (getSilenceMode() == silence) { |
| 1648 | return true; |
| 1649 | } |
| 1650 | return service.setSilenceMode(this, silence); |
| 1651 | } catch (RemoteException e) { |
| 1652 | Log.e(TAG, "setSilenceMode fail", e); |
| 1653 | return false; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * Get the device silence mode status |
| 1659 | * |
| 1660 | * <p> Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
| 1661 | * |
| 1662 | * @return true on device in silence mode, otherwise false. |
| 1663 | * @hide |
| 1664 | */ |
| 1665 | @SystemApi |
| 1666 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 1667 | public boolean getSilenceMode() { |
| 1668 | final IBluetooth service = sService; |
| 1669 | if (service == null) { |
| 1670 | return false; |
| 1671 | } |
| 1672 | try { |
| 1673 | return service.getSilenceMode(this); |
| 1674 | } catch (RemoteException e) { |
| 1675 | Log.e(TAG, "getSilenceMode fail", e); |
| 1676 | return false; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | /** |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1681 | * Sets whether the phonebook access is allowed to this device. |
| 1682 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1683 | * |
| 1684 | * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link |
| 1685 | * #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1686 | * @return Whether the value has been successfully set. |
| 1687 | * @hide |
| 1688 | */ |
Selim Gurun | 4029fa6 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 1689 | @SystemApi |
| 1690 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1691 | public boolean setPhonebookAccessPermission(int value) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1692 | final IBluetooth service = sService; |
| 1693 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1694 | return false; |
| 1695 | } |
| 1696 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1697 | return service.setPhonebookAccessPermission(this, value); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1698 | } catch (RemoteException e) { |
| 1699 | Log.e(TAG, "", e); |
| 1700 | } |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * Requires {@link android.Manifest.permission#BLUETOOTH}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1706 | * |
| 1707 | * @return Whether the message access is allowed to this device. Can be {@link #ACCESS_UNKNOWN}, |
| 1708 | * {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1709 | * @hide |
| 1710 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1711 | @UnsupportedAppUsage |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1712 | public int getMessageAccessPermission() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1713 | final IBluetooth service = sService; |
| 1714 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1715 | return ACCESS_UNKNOWN; |
| 1716 | } |
| 1717 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1718 | return service.getMessageAccessPermission(this); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1719 | } catch (RemoteException e) { |
| 1720 | Log.e(TAG, "", e); |
| 1721 | } |
| 1722 | return ACCESS_UNKNOWN; |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * Sets whether the message access is allowed to this device. |
| 1727 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1728 | * |
| 1729 | * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link |
| 1730 | * #ACCESS_REJECTED}. |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1731 | * @return Whether the value has been successfully set. |
| 1732 | * @hide |
| 1733 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1734 | @UnsupportedAppUsage |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1735 | public boolean setMessageAccessPermission(int value) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1736 | final IBluetooth service = sService; |
| 1737 | if (service == null) { |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1738 | return false; |
| 1739 | } |
| 1740 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1741 | return service.setMessageAccessPermission(this, value); |
Edward Jee | 8dd30aa | 2014-09-05 00:29:14 -0700 | [diff] [blame] | 1742 | } catch (RemoteException e) { |
| 1743 | Log.e(TAG, "", e); |
| 1744 | } |
| 1745 | return false; |
| 1746 | } |
| 1747 | |
| 1748 | /** |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1749 | * Requires {@link android.Manifest.permission#BLUETOOTH}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1750 | * |
| 1751 | * @return Whether the Sim access is allowed to this device. Can be {@link #ACCESS_UNKNOWN}, |
| 1752 | * {@link #ACCESS_ALLOWED} or {@link #ACCESS_REJECTED}. |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1753 | * @hide |
| 1754 | */ |
| 1755 | public int getSimAccessPermission() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1756 | final IBluetooth service = sService; |
| 1757 | if (service == null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1758 | return ACCESS_UNKNOWN; |
| 1759 | } |
| 1760 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1761 | return service.getSimAccessPermission(this); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1762 | } catch (RemoteException e) { |
| 1763 | Log.e(TAG, "", e); |
| 1764 | } |
| 1765 | return ACCESS_UNKNOWN; |
| 1766 | } |
| 1767 | |
| 1768 | /** |
| 1769 | * Sets whether the Sim access is allowed to this device. |
| 1770 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_PRIVILEGED}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1771 | * |
| 1772 | * @param value Can be {@link #ACCESS_UNKNOWN}, {@link #ACCESS_ALLOWED} or {@link |
| 1773 | * #ACCESS_REJECTED}. |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1774 | * @return Whether the value has been successfully set. |
| 1775 | * @hide |
| 1776 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1777 | @UnsupportedAppUsage |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1778 | public boolean setSimAccessPermission(int value) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1779 | final IBluetooth service = sService; |
| 1780 | if (service == null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1781 | return false; |
| 1782 | } |
| 1783 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1784 | return service.setSimAccessPermission(this, value); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1785 | } catch (RemoteException e) { |
| 1786 | Log.e(TAG, "", e); |
| 1787 | } |
| 1788 | return false; |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 1791 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1792 | * Create an RFCOMM {@link BluetoothSocket} ready to start a secure |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1793 | * outgoing connection to this remote device on given channel. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1794 | * <p>The remote device will be authenticated and communication on this |
| 1795 | * socket will be encrypted. |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 1796 | * <p> Use this socket only if an authenticated socket link is possible. |
| 1797 | * Authentication refers to the authentication of the link key to |
| 1798 | * prevent man-in-the-middle type of attacks. |
| 1799 | * For example, for Bluetooth 2.1 devices, if any of the devices does not |
| 1800 | * have an input and output capability or just has the ability to |
| 1801 | * display a numeric key, a secure socket connection is not possible. |
| 1802 | * In such a case, use {#link createInsecureRfcommSocket}. |
| 1803 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 1804 | * Bluetooth Core Specification version 2.1 + EDR. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1805 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1806 | * connection. |
| 1807 | * <p>Valid RFCOMM channels are in range 1 to 30. |
Nick Pelly | cf44059 | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 1808 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1809 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1810 | * @param channel RFCOMM channel to connect to |
| 1811 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1812 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1813 | * permissions |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1814 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1815 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1816 | @UnsupportedAppUsage |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1817 | public BluetoothSocket createRfcommSocket(int channel) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1818 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1819 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1820 | throw new IOException(); |
| 1821 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1822 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, channel, |
| 1823 | null); |
| 1824 | } |
| 1825 | |
| 1826 | /** |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 1827 | * Create an L2cap {@link BluetoothSocket} ready to start a secure |
| 1828 | * outgoing connection to this remote device on given channel. |
| 1829 | * <p>The remote device will be authenticated and communication on this |
| 1830 | * socket will be encrypted. |
| 1831 | * <p> Use this socket only if an authenticated socket link is possible. |
| 1832 | * Authentication refers to the authentication of the link key to |
| 1833 | * prevent man-in-the-middle type of attacks. |
| 1834 | * For example, for Bluetooth 2.1 devices, if any of the devices does not |
| 1835 | * have an input and output capability or just has the ability to |
| 1836 | * display a numeric key, a secure socket connection is not possible. |
| 1837 | * In such a case, use {#link createInsecureRfcommSocket}. |
| 1838 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 1839 | * Bluetooth Core Specification version 2.1 + EDR. |
| 1840 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
| 1841 | * connection. |
| 1842 | * <p>Valid L2CAP PSM channels are in range 1 to 2^16. |
| 1843 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1844 | * |
| 1845 | * @param channel L2cap PSM/channel to connect to |
| 1846 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1847 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1848 | * permissions |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 1849 | * @hide |
| 1850 | */ |
| 1851 | public BluetoothSocket createL2capSocket(int channel) throws IOException { |
| 1852 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, true, true, this, channel, |
| 1853 | null); |
| 1854 | } |
| 1855 | |
| 1856 | /** |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 1857 | * Create an L2cap {@link BluetoothSocket} ready to start an insecure |
| 1858 | * outgoing connection to this remote device on given channel. |
| 1859 | * <p>The remote device will be not authenticated and communication on this |
| 1860 | * socket will not be encrypted. |
| 1861 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
| 1862 | * connection. |
| 1863 | * <p>Valid L2CAP PSM channels are in range 1 to 2^16. |
| 1864 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1865 | * |
| 1866 | * @param channel L2cap PSM/channel to connect to |
| 1867 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1868 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1869 | * permissions |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 1870 | * @hide |
| 1871 | */ |
| 1872 | public BluetoothSocket createInsecureL2capSocket(int channel) throws IOException { |
| 1873 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, false, false, this, channel, |
| 1874 | null); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1878 | * Create an RFCOMM {@link BluetoothSocket} ready to start a secure |
| 1879 | * outgoing connection to this remote device using SDP lookup of uuid. |
| 1880 | * <p>This is designed to be used with {@link |
| 1881 | * BluetoothAdapter#listenUsingRfcommWithServiceRecord} for peer-peer |
| 1882 | * Bluetooth applications. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1883 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1884 | * connection. This will also perform an SDP lookup of the given uuid to |
| 1885 | * determine which channel to connect to. |
| 1886 | * <p>The remote device will be authenticated and communication on this |
| 1887 | * socket will be encrypted. |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 1888 | * <p> Use this socket only if an authenticated socket link is possible. |
| 1889 | * Authentication refers to the authentication of the link key to |
| 1890 | * prevent man-in-the-middle type of attacks. |
| 1891 | * For example, for Bluetooth 2.1 devices, if any of the devices does not |
| 1892 | * have an input and output capability or just has the ability to |
| 1893 | * display a numeric key, a secure socket connection is not possible. |
| 1894 | * In such a case, use {#link createInsecureRfcommSocketToServiceRecord}. |
| 1895 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 1896 | * Bluetooth Core Specification version 2.1 + EDR. |
Nick Pelly | ea5056e | 2010-02-24 11:19:10 -0800 | [diff] [blame] | 1897 | * <p>Hint: If you are connecting to a Bluetooth serial board then try |
| 1898 | * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. |
| 1899 | * However if you are connecting to an Android peer then please generate |
| 1900 | * your own unique UUID. |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1901 | * |
| 1902 | * @param uuid service record uuid to lookup RFCOMM channel |
| 1903 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1904 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1905 | * permissions |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1906 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1907 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1908 | public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1909 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1910 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1911 | throw new IOException(); |
| 1912 | } |
| 1913 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1914 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, -1, |
| 1915 | new ParcelUuid(uuid)); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /** |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1919 | * Create an RFCOMM {@link BluetoothSocket} socket ready to start an insecure |
| 1920 | * outgoing connection to this remote device using SDP lookup of uuid. |
| 1921 | * <p> The communication channel will not have an authenticated link key |
| 1922 | * i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1 |
| 1923 | * devices, the link key will be encrypted, as encryption is mandatory. |
| 1924 | * For legacy devices (pre Bluetooth 2.1 devices) the link key will |
| 1925 | * be not be encrypted. Use {@link #createRfcommSocketToServiceRecord} if an |
| 1926 | * encrypted and authenticated communication channel is desired. |
| 1927 | * <p>This is designed to be used with {@link |
| 1928 | * BluetoothAdapter#listenUsingInsecureRfcommWithServiceRecord} for peer-peer |
| 1929 | * Bluetooth applications. |
| 1930 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing |
| 1931 | * connection. This will also perform an SDP lookup of the given uuid to |
| 1932 | * determine which channel to connect to. |
| 1933 | * <p>The remote device will be authenticated and communication on this |
| 1934 | * socket will be encrypted. |
| 1935 | * <p>Hint: If you are connecting to a Bluetooth serial board then try |
| 1936 | * using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. |
| 1937 | * However if you are connecting to an Android peer then please generate |
| 1938 | * your own unique UUID. |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1939 | * |
| 1940 | * @param uuid service record uuid to lookup RFCOMM channel |
| 1941 | * @return a RFCOMM BluetoothServerSocket ready for an outgoing connection |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1942 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1943 | * permissions |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1944 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1945 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1946 | public BluetoothSocket createInsecureRfcommSocketToServiceRecord(UUID uuid) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1947 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1948 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1949 | throw new IOException(); |
| 1950 | } |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1951 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, -1, |
| 1952 | new ParcelUuid(uuid)); |
| 1953 | } |
| 1954 | |
| 1955 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1956 | * Construct an insecure RFCOMM socket ready to start an outgoing |
| 1957 | * connection. |
| 1958 | * Call #connect on the returned #BluetoothSocket to begin the connection. |
| 1959 | * The remote device will not be authenticated and communication on this |
| 1960 | * socket will not be encrypted. |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 1961 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 1962 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1963 | * @param port remote port |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1964 | * @return An RFCOMM BluetoothSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1965 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 1966 | * permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1967 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1968 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1969 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1970 | public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1971 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1972 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1973 | throw new IOException(); |
| 1974 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1975 | return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port, |
| 1976 | null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /** |
| 1980 | * Construct a SCO socket ready to start an outgoing connection. |
| 1981 | * Call #connect on the returned #BluetoothSocket to begin the connection. |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 1982 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 1983 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1984 | * @return a SCO BluetoothSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1985 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 1986 | * permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1987 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1988 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1989 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1990 | public BluetoothSocket createScoSocket() throws IOException { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1991 | if (!isBluetoothEnabled()) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1992 | Log.e(TAG, "Bluetooth is not enabled"); |
| 1993 | throw new IOException(); |
| 1994 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 1995 | return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1, null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | * Check that a pin is valid and convert to byte array. |
| 2000 | * |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 2001 | * Bluetooth pin's are 1 to 16 bytes of UTF-8 characters. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2002 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2003 | * @param pin pin as java String |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2004 | * @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] | 2005 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2007 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | public static byte[] convertPinToBytes(String pin) { |
| 2009 | if (pin == null) { |
| 2010 | return null; |
| 2011 | } |
| 2012 | byte[] pinBytes; |
| 2013 | try { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 2014 | pinBytes = pin.getBytes("UTF-8"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | } catch (UnsupportedEncodingException uee) { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 2016 | 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] | 2017 | return null; |
| 2018 | } |
| 2019 | if (pinBytes.length <= 0 || pinBytes.length > 16) { |
| 2020 | return null; |
| 2021 | } |
| 2022 | return pinBytes; |
| 2023 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2025 | /** |
| 2026 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 2027 | * The callback is used to deliver results to Caller, such as connection status as well |
| 2028 | * as any further GATT client operations. |
| 2029 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 2030 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2031 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2032 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2033 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 2034 | * automatically connect as soon as the remote device becomes available (true). |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2035 | * @throws IllegalArgumentException if callback is null |
| 2036 | */ |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 2037 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2038 | BluetoothGattCallback callback) { |
| 2039 | return (connectGatt(context, autoConnect, callback, TRANSPORT_AUTO)); |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /** |
| 2043 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 2044 | * The callback is used to deliver results to Caller, such as connection status as well |
| 2045 | * as any further GATT client operations. |
| 2046 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 2047 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2048 | * |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 2049 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2050 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 2051 | * automatically connect as soon as the remote device becomes available (true). |
| 2052 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 2053 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 2054 | * BluetoothDevice#TRANSPORT_LE} |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 2055 | * @throws IllegalArgumentException if callback is null |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 2056 | */ |
| 2057 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2058 | BluetoothGattCallback callback, int transport) { |
| 2059 | return (connectGatt(context, autoConnect, callback, transport, PHY_LE_1M_MASK)); |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | /** |
| 2063 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 2064 | * The callback is used to deliver results to Caller, such as connection status as well |
| 2065 | * as any further GATT client operations. |
| 2066 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 2067 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2068 | * |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 2069 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2070 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 2071 | * automatically connect as soon as the remote device becomes available (true). |
| 2072 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 2073 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 2074 | * BluetoothDevice#TRANSPORT_LE} |
| 2075 | * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link |
| 2076 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, and {@link |
| 2077 | * BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect} |
| 2078 | * is set to true. |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2079 | * @throws NullPointerException if callback is null |
Jakub Pawlowski | 409cee6 | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 2080 | */ |
| 2081 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2082 | BluetoothGattCallback callback, int transport, int phy) { |
| 2083 | return connectGatt(context, autoConnect, callback, transport, phy, null); |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 2088 | * The callback is used to deliver results to Caller, such as connection status as well |
| 2089 | * as any further GATT client operations. |
| 2090 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 2091 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2092 | * |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2093 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2094 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 2095 | * automatically connect as soon as the remote device becomes available (true). |
| 2096 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 2097 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 2098 | * BluetoothDevice#TRANSPORT_LE} |
| 2099 | * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link |
| 2100 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, an d{@link |
| 2101 | * BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect} |
| 2102 | * is set to true. |
| 2103 | * @param handler The handler to use for the callback. If {@code null}, callbacks will happen on |
| 2104 | * an un-specified background thread. |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2105 | * @throws NullPointerException if callback is null |
| 2106 | */ |
| 2107 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2108 | BluetoothGattCallback callback, int transport, int phy, |
| 2109 | Handler handler) { |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2110 | return connectGatt(context, autoConnect, callback, transport, false, phy, handler); |
| 2111 | } |
| 2112 | |
| 2113 | /** |
| 2114 | * Connect to GATT Server hosted by this device. Caller acts as GATT client. |
| 2115 | * The callback is used to deliver results to Caller, such as connection status as well |
| 2116 | * as any further GATT client operations. |
| 2117 | * The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct |
| 2118 | * GATT client operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2119 | * |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2120 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2121 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 2122 | * automatically connect as soon as the remote device becomes available (true). |
| 2123 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 2124 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 2125 | * BluetoothDevice#TRANSPORT_LE} |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2126 | * @param opportunistic Whether this GATT client is opportunistic. An opportunistic GATT client |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2127 | * does not hold a GATT connection. It automatically disconnects when no other GATT connections |
| 2128 | * are active for the remote device. |
| 2129 | * @param phy preferred PHY for connections to remote LE device. Bitwise OR of any of {@link |
| 2130 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, an d{@link |
| 2131 | * BluetoothDevice#PHY_LE_CODED_MASK}. This option does not take effect if {@code autoConnect} |
| 2132 | * is set to true. |
| 2133 | * @param handler The handler to use for the callback. If {@code null}, callbacks will happen on |
| 2134 | * an un-specified background thread. |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2135 | * @return A BluetoothGatt instance. You can use BluetoothGatt to conduct GATT client |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2136 | * operations. |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2137 | * @hide |
| 2138 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2139 | @UnsupportedAppUsage |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2140 | public BluetoothGatt connectGatt(Context context, boolean autoConnect, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2141 | BluetoothGattCallback callback, int transport, |
| 2142 | boolean opportunistic, int phy, Handler handler) { |
| 2143 | if (callback == null) { |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2144 | throw new NullPointerException("callback is null"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2145 | } |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2146 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2147 | // TODO(Bluetooth) check whether platform support BLE |
| 2148 | // Do the check here or in GattServer? |
| 2149 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 2150 | IBluetoothManager managerService = adapter.getBluetoothManager(); |
| 2151 | try { |
| 2152 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | 32ab77b | 2013-05-08 19:26:57 -0700 | [diff] [blame] | 2153 | if (iGatt == null) { |
| 2154 | // BLE is not supported |
| 2155 | return null; |
| 2156 | } |
Jack He | 13f52c8 | 2017-07-05 14:55:35 -0700 | [diff] [blame] | 2157 | BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport, opportunistic, phy); |
Jakub Pawlowski | b0f6474 | 2017-04-21 03:49:00 -0700 | [diff] [blame] | 2158 | gatt.connect(autoConnect, callback, handler); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2159 | return gatt; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2160 | } catch (RemoteException e) { |
| 2161 | Log.e(TAG, "", e); |
| 2162 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2163 | return null; |
| 2164 | } |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2165 | |
| 2166 | /** |
| 2167 | * Create a Bluetooth L2CAP Connection-oriented Channel (CoC) {@link BluetoothSocket} that can |
| 2168 | * 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] | 2169 | * protocol/service multiplexer (PSM) value. The supported Bluetooth transport is LE only. |
| 2170 | * <p>This is designed to be used with {@link BluetoothAdapter#listenUsingL2capChannel()} for |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2171 | * peer-peer Bluetooth applications. |
| 2172 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing connection. |
| 2173 | * <p>Application using this API is responsible for obtaining PSM value from remote device. |
| 2174 | * <p>The remote device will be authenticated and communication on this socket will be |
| 2175 | * encrypted. |
| 2176 | * <p> Use this socket if an authenticated socket link is possible. Authentication refers |
| 2177 | * to the authentication of the link key to prevent man-in-the-middle type of attacks. When a |
| 2178 | * secure socket connection is not possible, use {#link createInsecureLeL2capCocSocket(int, |
| 2179 | * int)}. |
| 2180 | * |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2181 | * @param psm dynamic PSM value from remote device |
| 2182 | * @return a CoC #BluetoothSocket ready for an outgoing connection |
| 2183 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2184 | * permissions |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2185 | */ |
| 2186 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2187 | public BluetoothSocket createL2capChannel(int psm) throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2188 | if (!isBluetoothEnabled()) { |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2189 | Log.e(TAG, "createL2capChannel: Bluetooth is not enabled"); |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2190 | throw new IOException(); |
| 2191 | } |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2192 | if (DBG) Log.d(TAG, "createL2capChannel: psm=" + psm); |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2193 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP_LE, -1, true, true, this, psm, |
| 2194 | null); |
| 2195 | } |
| 2196 | |
| 2197 | /** |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2198 | * TODO: Remove this hidden method once all the SL4A and other tests are updated to use the new |
| 2199 | * API name, createL2capChannel. |
| 2200 | * @hide |
| 2201 | */ |
| 2202 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 2203 | public BluetoothSocket createL2capCocSocket(int transport, int psm) throws IOException { |
| 2204 | Log.e(TAG, "createL2capCocSocket: PLEASE USE THE OFFICIAL API, createL2capChannel"); |
| 2205 | return createL2capChannel(psm); |
| 2206 | } |
| 2207 | |
| 2208 | /** |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2209 | * Create a Bluetooth L2CAP Connection-oriented Channel (CoC) {@link BluetoothSocket} that can |
| 2210 | * 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] | 2211 | * protocol/service multiplexer (PSM) value. The supported Bluetooth transport is LE only. |
| 2212 | * <p>This is designed to be used with {@link |
| 2213 | * BluetoothAdapter#listenUsingInsecureL2capChannel()} for peer-peer Bluetooth applications. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2214 | * <p>Use {@link BluetoothSocket#connect} to initiate the outgoing connection. |
| 2215 | * <p>Application using this API is responsible for obtaining PSM value from remote device. |
| 2216 | * <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] | 2217 | * 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] | 2218 | * authenticated communication channel is possible. |
| 2219 | * |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2220 | * @param psm dynamic PSM value from remote device |
| 2221 | * @return a CoC #BluetoothSocket ready for an outgoing connection |
| 2222 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2223 | * permissions |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2224 | */ |
| 2225 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 2226 | public BluetoothSocket createInsecureL2capChannel(int psm) throws IOException { |
| 2227 | if (!isBluetoothEnabled()) { |
| 2228 | Log.e(TAG, "createInsecureL2capChannel: Bluetooth is not enabled"); |
| 2229 | throw new IOException(); |
| 2230 | } |
| 2231 | if (DBG) { |
| 2232 | Log.d(TAG, "createInsecureL2capChannel: psm=" + psm); |
| 2233 | } |
| 2234 | return new BluetoothSocket(BluetoothSocket.TYPE_L2CAP_LE, -1, false, false, this, psm, |
| 2235 | null); |
| 2236 | } |
| 2237 | |
| 2238 | /** |
| 2239 | * TODO: Remove this hidden method once all the SL4A and other tests are updated to use the new |
| 2240 | * API name, createInsecureL2capChannel. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2241 | * @hide |
| 2242 | */ |
| 2243 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 2244 | public BluetoothSocket createInsecureL2capCocSocket(int transport, int psm) throws IOException { |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 2245 | Log.e(TAG, "createL2capCocSocket: PLEASE USE THE OFFICIAL API, createInsecureL2capChannel"); |
| 2246 | return createInsecureL2capChannel(psm); |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2247 | } |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 2248 | |
| 2249 | /** |
| 2250 | * Set a keyed metadata of this {@link BluetoothDevice} to a |
| 2251 | * {@link String} value. |
| 2252 | * Only bonded devices's metadata will be persisted across Bluetooth |
| 2253 | * restart. |
| 2254 | * Metadata will be removed when the device's bond state is moved to |
| 2255 | * {@link #BOND_NONE}. |
| 2256 | * |
| 2257 | * @param key must be within the list of BluetoothDevice.METADATA_* |
| 2258 | * @param value the string data to set for key. Must be less than |
| 2259 | * {@link BluetoothAdapter#METADATA_MAX_LENGTH} characters in length |
| 2260 | * @return true on success, false on error |
| 2261 | * @hide |
| 2262 | */ |
| 2263 | @SystemApi |
| 2264 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 2265 | public boolean setMetadata(int key, String value) { |
| 2266 | final IBluetooth service = sService; |
| 2267 | if (service == null) { |
| 2268 | Log.e(TAG, "Bluetooth is not enabled. Cannot set metadata"); |
| 2269 | return false; |
| 2270 | } |
| 2271 | if (value.length() > METADATA_MAX_LENGTH) { |
| 2272 | throw new IllegalArgumentException("value length is " + value.length() |
| 2273 | + ", should not over " + METADATA_MAX_LENGTH); |
| 2274 | } |
| 2275 | try { |
| 2276 | return service.setMetadata(this, key, value); |
| 2277 | } catch (RemoteException e) { |
| 2278 | Log.e(TAG, "setMetadata fail", e); |
| 2279 | return false; |
| 2280 | } |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * Get a keyed metadata for this {@link BluetoothDevice} as {@link String} |
| 2285 | * |
| 2286 | * @param key must be within the list of BluetoothDevice.METADATA_* |
| 2287 | * @return Metadata of the key as string, null on error or not found |
| 2288 | * @hide |
| 2289 | */ |
| 2290 | @SystemApi |
| 2291 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 2292 | public String getMetadata(int key) { |
| 2293 | final IBluetooth service = sService; |
| 2294 | if (service == null) { |
| 2295 | Log.e(TAG, "Bluetooth is not enabled. Cannot get metadata"); |
| 2296 | return null; |
| 2297 | } |
| 2298 | try { |
| 2299 | return service.getMetadata(this, key); |
| 2300 | } catch (RemoteException e) { |
| 2301 | Log.e(TAG, "getMetadata fail", e); |
| 2302 | return null; |
| 2303 | } |
| 2304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | } |