Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1 | /* |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2 | * Copyright 2009-2016 The Android Open Source Project |
| 3 | * Copyright 2015 Samsung LSI |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package android.bluetooth; |
| 19 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 20 | import android.Manifest; |
| 21 | import android.annotation.IntDef; |
Stanley Tng | 97341da | 2019-03-12 10:19:49 -0700 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 24 | import android.annotation.RequiresPermission; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 25 | import android.annotation.SdkConstant; |
| 26 | import android.annotation.SdkConstant.SdkConstantType; |
Wei Wang | d91f193 | 2015-03-19 15:09:56 -0700 | [diff] [blame] | 27 | import android.annotation.SystemApi; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 28 | import android.annotation.UnsupportedAppUsage; |
Svetoslav Ganov | b1e2da7 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 29 | import android.app.ActivityThread; |
Wei Wang | 6d81118 | 2014-05-22 12:10:25 -0700 | [diff] [blame] | 30 | import android.bluetooth.le.BluetoothLeAdvertiser; |
| 31 | import android.bluetooth.le.BluetoothLeScanner; |
Jakub Pawlowski | ac7fcfa | 2017-01-16 07:21:01 -0800 | [diff] [blame] | 32 | import android.bluetooth.le.PeriodicAdvertisingManager; |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 33 | import android.bluetooth.le.ScanCallback; |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 34 | import android.bluetooth.le.ScanFilter; |
| 35 | import android.bluetooth.le.ScanRecord; |
Wei Wang | 9fb1791 | 2014-07-01 15:10:06 -0700 | [diff] [blame] | 36 | import android.bluetooth.le.ScanResult; |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 37 | import android.bluetooth.le.ScanSettings; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 38 | import android.content.Context; |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 39 | import android.os.BatteryStats; |
Wei Wang | 46ab723 | 2015-05-07 16:25:33 -0700 | [diff] [blame] | 40 | import android.os.Binder; |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 41 | import android.os.IBinder; |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 42 | import android.os.ParcelUuid; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 43 | import android.os.RemoteException; |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 44 | import android.os.ResultReceiver; |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 45 | import android.os.ServiceManager; |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 46 | import android.os.SynchronousResultReceiver; |
Ajay Panicker | b6e1daf | 2016-02-29 16:09:14 -0800 | [diff] [blame] | 47 | import android.os.SystemProperties; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 49 | import android.util.Pair; |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 50 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 51 | import java.io.IOException; |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 52 | import java.lang.annotation.Retention; |
| 53 | import java.lang.annotation.RetentionPolicy; |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 54 | import java.util.ArrayList; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 55 | import java.util.Arrays; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 56 | import java.util.Collections; |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 57 | import java.util.HashMap; |
Wei Wang | 18c7693 | 2013-10-29 21:05:37 -0700 | [diff] [blame] | 58 | import java.util.HashSet; |
Wei Wang | 9fb1791 | 2014-07-01 15:10:06 -0700 | [diff] [blame] | 59 | import java.util.List; |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 60 | import java.util.Locale; |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 61 | import java.util.Map; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 62 | import java.util.Set; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 63 | import java.util.UUID; |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 64 | import java.util.concurrent.Executor; |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 65 | import java.util.concurrent.TimeoutException; |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 66 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 69 | * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter} |
| 70 | * lets you perform fundamental Bluetooth tasks, such as initiate |
| 71 | * device discovery, query a list of bonded (paired) devices, |
| 72 | * instantiate a {@link BluetoothDevice} using a known MAC address, and create |
| 73 | * a {@link BluetoothServerSocket} to listen for connection requests from other |
Matthew Xie | b30f91e | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 74 | * devices, and start a scan for Bluetooth LE devices. |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 75 | * |
| 76 | * <p>To get a {@link BluetoothAdapter} representing the local Bluetooth |
Marie Janssen | 553c8c7 | 2017-01-12 16:00:30 -0800 | [diff] [blame] | 77 | * adapter, call the {@link BluetoothManager#getAdapter} function on {@link BluetoothManager}. |
| 78 | * On JELLY_BEAN_MR1 and below you will need to use the static {@link #getDefaultAdapter} |
| 79 | * method instead. |
| 80 | * </p><p> |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 81 | * Fundamentally, this is your starting point for all |
| 82 | * Bluetooth actions. Once you have the local adapter, you can get a set of |
| 83 | * {@link BluetoothDevice} objects representing all paired devices with |
| 84 | * {@link #getBondedDevices()}; start device discovery with |
| 85 | * {@link #startDiscovery()}; or create a {@link BluetoothServerSocket} to |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 86 | * listen for incoming RFComm connection requests with {@link |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 87 | * #listenUsingRfcommWithServiceRecord(String, UUID)}; listen for incoming L2CAP Connection-oriented |
| 88 | * Channels (CoC) connection requests with {@link #listenUsingL2capChannel()}; or start a scan for |
Matthew Xie | b30f91e | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 89 | * Bluetooth LE devices with {@link #startLeScan(LeScanCallback callback)}. |
Marie Janssen | 553c8c7 | 2017-01-12 16:00:30 -0800 | [diff] [blame] | 90 | * </p> |
| 91 | * <p>This class is thread safe.</p> |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 92 | * <p class="note"><strong>Note:</strong> |
| 93 | * Most methods require the {@link android.Manifest.permission#BLUETOOTH} |
| 94 | * permission and some also require the |
| 95 | * {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
Marie Janssen | 553c8c7 | 2017-01-12 16:00:30 -0800 | [diff] [blame] | 96 | * </p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 97 | * <div class="special reference"> |
| 98 | * <h3>Developer Guides</h3> |
Hemal Patel | 65813df | 2016-08-17 13:18:14 -0700 | [diff] [blame] | 99 | * <p> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 100 | * For more information about using Bluetooth, read the <a href= |
Hemal Patel | 65813df | 2016-08-17 13:18:14 -0700 | [diff] [blame] | 101 | * "{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer |
| 102 | * guide. |
| 103 | * </p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 104 | * </div> |
| 105 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 106 | * {@see BluetoothDevice} |
| 107 | * {@see BluetoothServerSocket} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 108 | */ |
| 109 | public final class BluetoothAdapter { |
| 110 | private static final String TAG = "BluetoothAdapter"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 111 | private static final boolean DBG = true; |
Matthew Xie | 3b6214f | 2012-08-29 00:12:29 -0700 | [diff] [blame] | 112 | private static final boolean VDBG = false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 113 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 114 | /** |
Svet Ganov | 408abf7 | 2015-05-12 19:13:36 -0700 | [diff] [blame] | 115 | * Default MAC address reported to a client that does not have the |
| 116 | * android.permission.LOCAL_MAC_ADDRESS permission. |
| 117 | * |
| 118 | * @hide |
| 119 | */ |
| 120 | public static final String DEFAULT_MAC_ADDRESS = "02:00:00:00:00:00"; |
| 121 | |
| 122 | /** |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 123 | * Sentinel error value for this class. Guaranteed to not equal any other |
| 124 | * integer constant in this class. Provided as a convenience for functions |
| 125 | * that require a sentinel error value, for example: |
| 126 | * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, |
| 127 | * BluetoothAdapter.ERROR)</code> |
| 128 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 129 | public static final int ERROR = Integer.MIN_VALUE; |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 132 | * Broadcast Action: The state of the local Bluetooth adapter has been |
| 133 | * changed. |
| 134 | * <p>For example, Bluetooth has been turned on or off. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 135 | * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 136 | * #EXTRA_PREVIOUS_STATE} containing the new and old states |
| 137 | * respectively. |
| 138 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 139 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 140 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String |
| 141 | ACTION_STATE_CHANGED = "android.bluetooth.adapter.action.STATE_CHANGED"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 142 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 143 | /** |
| 144 | * Used as an int extra field in {@link #ACTION_STATE_CHANGED} |
| 145 | * intents to request the current power state. Possible values are: |
| 146 | * {@link #STATE_OFF}, |
| 147 | * {@link #STATE_TURNING_ON}, |
| 148 | * {@link #STATE_ON}, |
| 149 | * {@link #STATE_TURNING_OFF}, |
| 150 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 151 | public static final String EXTRA_STATE = "android.bluetooth.adapter.extra.STATE"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Used as an int extra field in {@link #ACTION_STATE_CHANGED} |
| 154 | * intents to request the previous power state. Possible values are: |
| 155 | * {@link #STATE_OFF}, |
| 156 | * {@link #STATE_TURNING_ON}, |
| 157 | * {@link #STATE_ON}, |
Etan Cohen | fd840dd | 2015-04-23 18:25:08 -0700 | [diff] [blame] | 158 | * {@link #STATE_TURNING_OFF} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 159 | */ |
| 160 | public static final String EXTRA_PREVIOUS_STATE = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 161 | "android.bluetooth.adapter.extra.PREVIOUS_STATE"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 162 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 163 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 164 | @IntDef(prefix = { "STATE_" }, value = { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 165 | STATE_OFF, |
| 166 | STATE_TURNING_ON, |
| 167 | STATE_ON, |
| 168 | STATE_TURNING_OFF, |
| 169 | STATE_BLE_TURNING_ON, |
| 170 | STATE_BLE_ON, |
| 171 | STATE_BLE_TURNING_OFF |
| 172 | }) |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 173 | @Retention(RetentionPolicy.SOURCE) |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 174 | public @interface AdapterState {} |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 175 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Indicates the local Bluetooth adapter is off. |
| 178 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 179 | public static final int STATE_OFF = 10; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Indicates the local Bluetooth adapter is turning on. However local |
| 182 | * clients should wait for {@link #STATE_ON} before attempting to |
| 183 | * use the adapter. |
| 184 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 185 | public static final int STATE_TURNING_ON = 11; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Indicates the local Bluetooth adapter is on, and ready for use. |
| 188 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 189 | public static final int STATE_ON = 12; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Indicates the local Bluetooth adapter is turning off. Local clients |
| 192 | * should immediately attempt graceful disconnection of any remote links. |
| 193 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 194 | public static final int STATE_TURNING_OFF = 13; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 197 | * Indicates the local Bluetooth adapter is turning Bluetooth LE mode on. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 198 | * |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 199 | * @hide |
| 200 | */ |
| 201 | public static final int STATE_BLE_TURNING_ON = 14; |
| 202 | |
| 203 | /** |
| 204 | * Indicates the local Bluetooth adapter is in LE only mode. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 205 | * |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 206 | * @hide |
| 207 | */ |
| 208 | public static final int STATE_BLE_ON = 15; |
| 209 | |
| 210 | /** |
| 211 | * Indicates the local Bluetooth adapter is turning off LE only mode. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 212 | * |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 213 | * @hide |
| 214 | */ |
| 215 | public static final int STATE_BLE_TURNING_OFF = 16; |
| 216 | |
| 217 | /** |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 218 | * UUID of the GATT Read Characteristics for LE_PSM value. |
| 219 | * |
| 220 | * @hide |
| 221 | */ |
| 222 | public static final UUID LE_PSM_CHARACTERISTIC_UUID = |
| 223 | UUID.fromString("2d410339-82b6-42aa-b34e-e2e01df8cc1a"); |
| 224 | |
| 225 | /** |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 226 | * Human-readable string helper for AdapterState |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 227 | * |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 228 | * @hide |
| 229 | */ |
| 230 | public static String nameForState(@AdapterState int state) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 231 | switch (state) { |
| 232 | case STATE_OFF: |
| 233 | return "OFF"; |
| 234 | case STATE_TURNING_ON: |
| 235 | return "TURNING_ON"; |
| 236 | case STATE_ON: |
| 237 | return "ON"; |
| 238 | case STATE_TURNING_OFF: |
| 239 | return "TURNING_OFF"; |
| 240 | case STATE_BLE_TURNING_ON: |
| 241 | return "BLE_TURNING_ON"; |
| 242 | case STATE_BLE_ON: |
| 243 | return "BLE_ON"; |
| 244 | case STATE_BLE_TURNING_OFF: |
| 245 | return "BLE_TURNING_OFF"; |
| 246 | default: |
| 247 | return "?!?!? (" + state + ")"; |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 252 | * Activity Action: Show a system activity that requests discoverable mode. |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 253 | * This activity will also request the user to turn on Bluetooth if it |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 254 | * is not currently enabled. |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 255 | * <p>Discoverable mode is equivalent to {@link |
| 256 | * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see |
| 257 | * this Bluetooth adapter when they perform a discovery. |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 258 | * <p>For privacy, Android is not discoverable by default. |
| 259 | * <p>The sender of this Intent can optionally use extra field {@link |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 260 | * #EXTRA_DISCOVERABLE_DURATION} to request the duration of |
| 261 | * discoverability. Currently the default duration is 120 seconds, and |
| 262 | * maximum duration is capped at 300 seconds for each request. |
| 263 | * <p>Notification of the result of this activity is posted using the |
| 264 | * {@link android.app.Activity#onActivityResult} callback. The |
| 265 | * <code>resultCode</code> |
Michael Chan | cdd2864 | 2009-11-05 18:29:01 -0800 | [diff] [blame] | 266 | * will be the duration (in seconds) of discoverability or |
| 267 | * {@link android.app.Activity#RESULT_CANCELED} if the user rejected |
| 268 | * discoverability or an error has occurred. |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 269 | * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED} |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 270 | * for global notification whenever the scan mode changes. For example, an |
| 271 | * application can be notified when the device has ended discoverability. |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 272 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 273 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 274 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String |
| 275 | ACTION_REQUEST_DISCOVERABLE = "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE"; |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Used as an optional int extra field in {@link |
| 279 | * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration |
| 280 | * for discoverability in seconds. The current default is 120 seconds, and |
| 281 | * requests over 300 seconds will be capped. These values could change. |
| 282 | */ |
| 283 | public static final String EXTRA_DISCOVERABLE_DURATION = |
| 284 | "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION"; |
| 285 | |
| 286 | /** |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 287 | * Activity Action: Show a system activity that allows the user to turn on |
| 288 | * Bluetooth. |
| 289 | * <p>This system activity will return once Bluetooth has completed turning |
| 290 | * on, or the user has decided not to turn Bluetooth on. |
| 291 | * <p>Notification of the result of this activity is posted using the |
| 292 | * {@link android.app.Activity#onActivityResult} callback. The |
| 293 | * <code>resultCode</code> |
Michael Chan | cdd2864 | 2009-11-05 18:29:01 -0800 | [diff] [blame] | 294 | * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been |
| 295 | * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user |
| 296 | * has rejected the request or an error has occurred. |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 297 | * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED} |
| 298 | * for global notification whenever Bluetooth is turned on or off. |
| 299 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 300 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 301 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String |
| 302 | ACTION_REQUEST_ENABLE = "android.bluetooth.adapter.action.REQUEST_ENABLE"; |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
Svetoslav Ganov | b1e2da7 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 305 | * Activity Action: Show a system activity that allows the user to turn off |
| 306 | * Bluetooth. This is used only if permission review is enabled which is for |
| 307 | * apps targeting API less than 23 require a permission review before any of |
| 308 | * the app's components can run. |
| 309 | * <p>This system activity will return once Bluetooth has completed turning |
| 310 | * off, or the user has decided not to turn Bluetooth off. |
| 311 | * <p>Notification of the result of this activity is posted using the |
| 312 | * {@link android.app.Activity#onActivityResult} callback. The |
| 313 | * <code>resultCode</code> |
| 314 | * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been |
| 315 | * turned off or {@link android.app.Activity#RESULT_CANCELED} if the user |
| 316 | * has rejected the request or an error has occurred. |
| 317 | * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED} |
| 318 | * for global notification whenever Bluetooth is turned on or off. |
| 319 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 320 | * |
| 321 | * @hide |
| 322 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 323 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String |
| 324 | ACTION_REQUEST_DISABLE = "android.bluetooth.adapter.action.REQUEST_DISABLE"; |
Svetoslav Ganov | b1e2da7 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
Wei Wang | d91f193 | 2015-03-19 15:09:56 -0700 | [diff] [blame] | 327 | * Activity Action: Show a system activity that allows user to enable BLE scans even when |
| 328 | * Bluetooth is turned off.<p> |
| 329 | * |
| 330 | * Notification of result of this activity is posted using |
| 331 | * {@link android.app.Activity#onActivityResult}. The <code>resultCode</code> will be |
| 332 | * {@link android.app.Activity#RESULT_OK} if BLE scan always available setting is turned on or |
| 333 | * {@link android.app.Activity#RESULT_CANCELED} if the user has rejected the request or an |
| 334 | * error occurred. |
| 335 | * |
| 336 | * @hide |
| 337 | */ |
| 338 | @SystemApi |
| 339 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 340 | public static final String ACTION_REQUEST_BLE_SCAN_ALWAYS_AVAILABLE = |
| 341 | "android.bluetooth.adapter.action.REQUEST_BLE_SCAN_ALWAYS_AVAILABLE"; |
| 342 | |
| 343 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 344 | * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter |
| 345 | * has changed. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 346 | * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 347 | * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes |
| 348 | * respectively. |
| 349 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 350 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 351 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String |
| 352 | ACTION_SCAN_MODE_CHANGED = "android.bluetooth.adapter.action.SCAN_MODE_CHANGED"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED} |
| 356 | * intents to request the current scan mode. Possible values are: |
| 357 | * {@link #SCAN_MODE_NONE}, |
| 358 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 359 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}, |
| 360 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 361 | public static final String EXTRA_SCAN_MODE = "android.bluetooth.adapter.extra.SCAN_MODE"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 362 | /** |
| 363 | * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED} |
| 364 | * intents to request the previous scan mode. Possible values are: |
| 365 | * {@link #SCAN_MODE_NONE}, |
| 366 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 367 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}, |
| 368 | */ |
| 369 | public static final String EXTRA_PREVIOUS_SCAN_MODE = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 370 | "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 371 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 372 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 373 | @IntDef(prefix = { "SCAN_" }, value = { |
| 374 | SCAN_MODE_NONE, |
| 375 | SCAN_MODE_CONNECTABLE, |
| 376 | SCAN_MODE_CONNECTABLE_DISCOVERABLE |
| 377 | }) |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 378 | @Retention(RetentionPolicy.SOURCE) |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 379 | public @interface ScanMode {} |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 380 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 381 | /** |
| 382 | * Indicates that both inquiry scan and page scan are disabled on the local |
| 383 | * Bluetooth adapter. Therefore this device is neither discoverable |
| 384 | * nor connectable from remote Bluetooth devices. |
| 385 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 386 | public static final int SCAN_MODE_NONE = 20; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 387 | /** |
| 388 | * Indicates that inquiry scan is disabled, but page scan is enabled on the |
| 389 | * local Bluetooth adapter. Therefore this device is not discoverable from |
| 390 | * remote Bluetooth devices, but is connectable from remote devices that |
| 391 | * have previously discovered this device. |
| 392 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 393 | public static final int SCAN_MODE_CONNECTABLE = 21; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 394 | /** |
| 395 | * Indicates that both inquiry scan and page scan are enabled on the local |
| 396 | * Bluetooth adapter. Therefore this device is both discoverable and |
| 397 | * connectable from remote Bluetooth devices. |
| 398 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 399 | public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 400 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 401 | /** |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 402 | * Device only has a display. |
| 403 | * |
| 404 | * @hide |
| 405 | */ |
| 406 | public static final int IO_CAPABILITY_OUT = 0; |
| 407 | |
| 408 | /** |
| 409 | * Device has a display and the ability to input Yes/No. |
| 410 | * |
| 411 | * @hide |
| 412 | */ |
| 413 | public static final int IO_CAPABILITY_IO = 1; |
| 414 | |
| 415 | /** |
| 416 | * Device only has a keyboard for entry but no display. |
| 417 | * |
| 418 | * @hide |
| 419 | */ |
| 420 | public static final int IO_CAPABILITY_IN = 2; |
| 421 | |
| 422 | /** |
| 423 | * Device has no Input or Output capability. |
| 424 | * |
| 425 | * @hide |
| 426 | */ |
| 427 | public static final int IO_CAPABILITY_NONE = 3; |
| 428 | |
| 429 | /** |
| 430 | * Device has a display and a full keyboard. |
| 431 | * |
| 432 | * @hide |
| 433 | */ |
| 434 | public static final int IO_CAPABILITY_KBDISP = 4; |
| 435 | |
| 436 | /** |
| 437 | * Maximum range value for Input/Output capabilities. |
| 438 | * |
| 439 | * <p>This should be updated when adding a new Input/Output capability. Other code |
| 440 | * like validation depends on this being accurate. |
| 441 | * |
| 442 | * @hide |
| 443 | */ |
| 444 | public static final int IO_CAPABILITY_MAX = 5; |
| 445 | |
| 446 | /** |
| 447 | * The Input/Output capability of the device is unknown. |
| 448 | * |
| 449 | * @hide |
| 450 | */ |
| 451 | public static final int IO_CAPABILITY_UNKNOWN = 255; |
| 452 | |
Pulkit Bhuwalka | 8023814 | 2018-04-20 16:56:53 -0700 | [diff] [blame] | 453 | /** @hide */ |
| 454 | @IntDef({IO_CAPABILITY_OUT, IO_CAPABILITY_IO, IO_CAPABILITY_IN, IO_CAPABILITY_NONE, |
| 455 | IO_CAPABILITY_KBDISP}) |
| 456 | @Retention(RetentionPolicy.SOURCE) |
| 457 | public @interface IoCapability {} |
| 458 | |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 459 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 460 | * Broadcast Action: The local Bluetooth adapter has started the remote |
| 461 | * device discovery process. |
| 462 | * <p>This usually involves an inquiry scan of about 12 seconds, followed |
| 463 | * by a page scan of each new device to retrieve its Bluetooth name. |
| 464 | * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as |
| 465 | * remote Bluetooth devices are found. |
| 466 | * <p>Device discovery is a heavyweight procedure. New connections to |
| 467 | * remote Bluetooth devices should not be attempted while discovery is in |
| 468 | * progress, and existing connections will experience limited bandwidth |
| 469 | * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing |
| 470 | * discovery. |
| 471 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 472 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 473 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String |
| 474 | ACTION_DISCOVERY_STARTED = "android.bluetooth.adapter.action.DISCOVERY_STARTED"; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 475 | /** |
| 476 | * Broadcast Action: The local Bluetooth adapter has finished the device |
| 477 | * discovery process. |
| 478 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 479 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 480 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String |
| 481 | ACTION_DISCOVERY_FINISHED = "android.bluetooth.adapter.action.DISCOVERY_FINISHED"; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 482 | |
| 483 | /** |
| 484 | * Broadcast Action: The local Bluetooth adapter has changed its friendly |
| 485 | * Bluetooth name. |
| 486 | * <p>This name is visible to remote Bluetooth devices. |
| 487 | * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing |
| 488 | * the name. |
| 489 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 490 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 491 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String |
| 492 | ACTION_LOCAL_NAME_CHANGED = "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED"; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 493 | /** |
| 494 | * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED} |
| 495 | * intents to request the local Bluetooth name. |
| 496 | */ |
| 497 | public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME"; |
| 498 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 499 | /** |
| 500 | * Intent used to broadcast the change in connection state of the local |
| 501 | * Bluetooth adapter to a profile of the remote device. When the adapter is |
| 502 | * not connected to any profiles of any remote devices and it attempts a |
Ricardo Loo Foronda | f710a23 | 2018-01-24 17:49:18 -0800 | [diff] [blame] | 503 | * connection to a profile this intent will be sent. Once connected, this intent |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 504 | * will not be sent for any more connection attempts to any profiles of any |
| 505 | * remote device. When the adapter disconnects from the last profile its |
| 506 | * connected to of any remote device, this intent will be sent. |
| 507 | * |
| 508 | * <p> This intent is useful for applications that are only concerned about |
| 509 | * whether the local adapter is connected to any profile of any device and |
| 510 | * are not really concerned about which profile. For example, an application |
| 511 | * which displays an icon to display whether Bluetooth is connected or not |
| 512 | * can use this intent. |
| 513 | * |
| 514 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | 0b5b35f | 2011-02-01 16:47:11 -0800 | [diff] [blame] | 515 | * {@link #EXTRA_CONNECTION_STATE} - The current connection state. |
| 516 | * {@link #EXTRA_PREVIOUS_CONNECTION_STATE}- The previous connection state. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 517 | * {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. |
| 518 | * |
Jaikumar Ganesh | 0b5b35f | 2011-02-01 16:47:11 -0800 | [diff] [blame] | 519 | * {@link #EXTRA_CONNECTION_STATE} or {@link #EXTRA_PREVIOUS_CONNECTION_STATE} |
| 520 | * can be any of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 521 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 522 | * |
| 523 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 524 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 525 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String |
| 526 | ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 527 | "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED"; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 531 | * |
| 532 | * This extra represents the current connection state. |
| 533 | */ |
| 534 | public static final String EXTRA_CONNECTION_STATE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 535 | "android.bluetooth.adapter.extra.CONNECTION_STATE"; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 539 | * |
| 540 | * This extra represents the previous connection state. |
| 541 | */ |
| 542 | public static final String EXTRA_PREVIOUS_CONNECTION_STATE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 543 | "android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE"; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 544 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 545 | /** |
| 546 | * Broadcast Action: The Bluetooth adapter state has changed in LE only mode. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 547 | * |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 548 | * @hide |
| 549 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 550 | @SystemApi public static final String ACTION_BLE_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 551 | "android.bluetooth.adapter.action.BLE_STATE_CHANGED"; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 552 | |
| 553 | /** |
Stanley Tng | dd749b0 | 2017-04-17 22:35:45 -0700 | [diff] [blame] | 554 | * Intent used to broadcast the change in the Bluetooth address |
| 555 | * of the local Bluetooth adapter. |
| 556 | * <p>Always contains the extra field {@link |
| 557 | * #EXTRA_BLUETOOTH_ADDRESS} containing the Bluetooth address. |
| 558 | * |
| 559 | * Note: only system level processes are allowed to send this |
| 560 | * defined broadcast. |
| 561 | * |
| 562 | * @hide |
| 563 | */ |
| 564 | public static final String ACTION_BLUETOOTH_ADDRESS_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 565 | "android.bluetooth.adapter.action.BLUETOOTH_ADDRESS_CHANGED"; |
Stanley Tng | dd749b0 | 2017-04-17 22:35:45 -0700 | [diff] [blame] | 566 | |
| 567 | /** |
| 568 | * Used as a String extra field in {@link |
| 569 | * #ACTION_BLUETOOTH_ADDRESS_CHANGED} intent to store the local |
| 570 | * Bluetooth address. |
| 571 | * |
| 572 | * @hide |
| 573 | */ |
| 574 | public static final String EXTRA_BLUETOOTH_ADDRESS = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 575 | "android.bluetooth.adapter.extra.BLUETOOTH_ADDRESS"; |
Stanley Tng | dd749b0 | 2017-04-17 22:35:45 -0700 | [diff] [blame] | 576 | |
| 577 | /** |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 578 | * Broadcast Action: The notifys Bluetooth ACL connected event. This will be |
| 579 | * by BLE Always on enabled application to know the ACL_CONNECTED event |
| 580 | * when Bluetooth state in STATE_BLE_ON. This denotes GATT connection |
| 581 | * as Bluetooth LE is the only feature available in STATE_BLE_ON |
| 582 | * |
| 583 | * This is counterpart of {@link BluetoothDevice#ACTION_ACL_CONNECTED} which |
| 584 | * works in Bluetooth state STATE_ON |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 585 | * |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 586 | * @hide |
| 587 | */ |
| 588 | public static final String ACTION_BLE_ACL_CONNECTED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 589 | "android.bluetooth.adapter.action.BLE_ACL_CONNECTED"; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 590 | |
| 591 | /** |
| 592 | * Broadcast Action: The notifys Bluetooth ACL connected event. This will be |
| 593 | * by BLE Always on enabled application to know the ACL_DISCONNECTED event |
| 594 | * when Bluetooth state in STATE_BLE_ON. This denotes GATT disconnection as Bluetooth |
| 595 | * LE is the only feature available in STATE_BLE_ON |
| 596 | * |
| 597 | * This is counterpart of {@link BluetoothDevice#ACTION_ACL_DISCONNECTED} which |
| 598 | * works in Bluetooth state STATE_ON |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 599 | * |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 600 | * @hide |
| 601 | */ |
| 602 | public static final String ACTION_BLE_ACL_DISCONNECTED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 603 | "android.bluetooth.adapter.action.BLE_ACL_DISCONNECTED"; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 604 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 605 | /** The profile is in disconnected state */ |
Jack He | bd020a6 | 2018-03-02 13:11:27 -0800 | [diff] [blame] | 606 | public static final int STATE_DISCONNECTED = BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTED; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 607 | /** The profile is in connecting state */ |
Jack He | bd020a6 | 2018-03-02 13:11:27 -0800 | [diff] [blame] | 608 | public static final int STATE_CONNECTING = BluetoothProtoEnums.CONNECTION_STATE_CONNECTING; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 609 | /** The profile is in connected state */ |
Jack He | bd020a6 | 2018-03-02 13:11:27 -0800 | [diff] [blame] | 610 | public static final int STATE_CONNECTED = BluetoothProtoEnums.CONNECTION_STATE_CONNECTED; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 611 | /** The profile is in disconnecting state */ |
Jack He | bd020a6 | 2018-03-02 13:11:27 -0800 | [diff] [blame] | 612 | public static final int STATE_DISCONNECTING = |
| 613 | BluetoothProtoEnums.CONNECTION_STATE_DISCONNECTING; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 614 | |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 615 | /** @hide */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 616 | public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager"; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 617 | private final IBinder mToken; |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 618 | |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 619 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 620 | /** |
| 621 | * When creating a ServerSocket using listenUsingRfcommOn() or |
| 622 | * listenUsingL2capOn() use SOCKET_CHANNEL_AUTO_STATIC to create |
| 623 | * a ServerSocket that auto assigns a channel number to the first |
| 624 | * bluetooth socket. |
| 625 | * The channel number assigned to this first Bluetooth Socket will |
| 626 | * be stored in the ServerSocket, and reused for subsequent Bluetooth |
| 627 | * sockets. |
| 628 | * |
| 629 | * @hide |
| 630 | */ |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 631 | public static final int SOCKET_CHANNEL_AUTO_STATIC_NO_SDP = -2; |
| 632 | |
| 633 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 634 | private static final int ADDRESS_LENGTH = 17; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 635 | |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 636 | /** |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 637 | * Lazily initialized singleton. Guaranteed final after first object |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 638 | * constructed. |
| 639 | */ |
| 640 | private static BluetoothAdapter sAdapter; |
| 641 | |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 642 | private static BluetoothLeScanner sBluetoothLeScanner; |
| 643 | private static BluetoothLeAdvertiser sBluetoothLeAdvertiser; |
Jakub Pawlowski | ac7fcfa | 2017-01-16 07:21:01 -0800 | [diff] [blame] | 644 | private static PeriodicAdvertisingManager sPeriodicAdvertisingManager; |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 645 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 646 | private final IBluetoothManager mManagerService; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 647 | @UnsupportedAppUsage |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 648 | private IBluetooth mService; |
Zach Johnson | 57e6e90 | 2018-12-12 17:11:25 -0800 | [diff] [blame] | 649 | private Context mContext; |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 650 | private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 651 | |
Wei Wang | f305589 | 2014-03-11 22:22:41 -0700 | [diff] [blame] | 652 | private final Object mLock = new Object(); |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 653 | private final Map<LeScanCallback, ScanCallback> mLeScanClients; |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 654 | private static final Map<BluetoothDevice, List<Pair<OnMetadataChangedListener, Executor>>> |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 655 | sMetadataListeners = new HashMap<>(); |
| 656 | |
| 657 | /** |
| 658 | * Bluetooth metadata listener. Overrides the default BluetoothMetadataListener |
| 659 | * implementation. |
| 660 | */ |
| 661 | private static final IBluetoothMetadataListener sBluetoothMetadataListener = |
| 662 | new IBluetoothMetadataListener.Stub() { |
| 663 | @Override |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 664 | public void onMetadataChanged(BluetoothDevice device, int key, byte[] value) { |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 665 | synchronized (sMetadataListeners) { |
| 666 | if (sMetadataListeners.containsKey(device)) { |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 667 | List<Pair<OnMetadataChangedListener, Executor>> list = |
| 668 | sMetadataListeners.get(device); |
| 669 | for (Pair<OnMetadataChangedListener, Executor> pair : list) { |
| 670 | OnMetadataChangedListener listener = pair.first; |
| 671 | Executor executor = pair.second; |
| 672 | executor.execute(() -> { |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 673 | listener.onMetadataChanged(device, key, value); |
| 674 | }); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | return; |
| 679 | } |
| 680 | }; |
Matthew Xie | 484867a | 2011-08-25 16:45:58 -0700 | [diff] [blame] | 681 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 682 | /** |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 683 | * Get a handle to the default local Bluetooth adapter. |
| 684 | * <p>Currently Android only supports one Bluetooth adapter, but the API |
| 685 | * could be extended to support more. This will always return the default |
| 686 | * adapter. |
Marie Janssen | 553c8c7 | 2017-01-12 16:00:30 -0800 | [diff] [blame] | 687 | * </p> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 688 | * |
| 689 | * @return the default local adapter, or null if Bluetooth is not supported on this hardware |
| 690 | * platform |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 691 | */ |
| 692 | public static synchronized BluetoothAdapter getDefaultAdapter() { |
| 693 | if (sAdapter == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 694 | IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE); |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 695 | if (b != null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 696 | IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b); |
| 697 | sAdapter = new BluetoothAdapter(managerService); |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 698 | } else { |
| 699 | Log.e(TAG, "Bluetooth binder is null"); |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | return sAdapter; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 707 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 708 | BluetoothAdapter(IBluetoothManager managerService) { |
| 709 | |
| 710 | if (managerService == null) { |
| 711 | throw new IllegalArgumentException("bluetooth manager service is null"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 712 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 713 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 714 | mServiceLock.writeLock().lock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 715 | mService = managerService.registerAdapter(mManagerCallback); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 716 | } catch (RemoteException e) { |
| 717 | Log.e(TAG, "", e); |
| 718 | } finally { |
| 719 | mServiceLock.writeLock().unlock(); |
| 720 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 721 | mManagerService = managerService; |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 722 | mLeScanClients = new HashMap<LeScanCallback, ScanCallback>(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 723 | mToken = new Binder(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 727 | * Get a {@link BluetoothDevice} object for the given Bluetooth hardware |
| 728 | * address. |
| 729 | * <p>Valid Bluetooth hardware addresses must be upper case, in a format |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 730 | * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is |
| 731 | * available to validate a Bluetooth address. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 732 | * <p>A {@link BluetoothDevice} will always be returned for a valid |
| 733 | * hardware address, even if this adapter has never seen that device. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 734 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 735 | * @param address valid Bluetooth MAC address |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 736 | * @throws IllegalArgumentException if address is invalid |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 737 | */ |
| 738 | public BluetoothDevice getRemoteDevice(String address) { |
| 739 | return new BluetoothDevice(address); |
| 740 | } |
| 741 | |
| 742 | /** |
Nick Pelly | 75596b4 | 2011-12-07 15:03:55 -0800 | [diff] [blame] | 743 | * Get a {@link BluetoothDevice} object for the given Bluetooth hardware |
| 744 | * address. |
| 745 | * <p>Valid Bluetooth hardware addresses must be 6 bytes. This method |
| 746 | * expects the address in network byte order (MSB first). |
| 747 | * <p>A {@link BluetoothDevice} will always be returned for a valid |
| 748 | * hardware address, even if this adapter has never seen that device. |
| 749 | * |
| 750 | * @param address Bluetooth MAC address (6 bytes) |
| 751 | * @throws IllegalArgumentException if address is invalid |
| 752 | */ |
| 753 | public BluetoothDevice getRemoteDevice(byte[] address) { |
| 754 | if (address == null || address.length != 6) { |
| 755 | throw new IllegalArgumentException("Bluetooth address must have 6 bytes"); |
| 756 | } |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 757 | return new BluetoothDevice( |
| 758 | String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X", address[0], address[1], |
| 759 | address[2], address[3], address[4], address[5])); |
Nick Pelly | 75596b4 | 2011-12-07 15:03:55 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** |
Prerepa Viswanadham | 248edc3 | 2014-10-15 16:36:01 -0700 | [diff] [blame] | 763 | * Returns a {@link BluetoothLeAdvertiser} object for Bluetooth LE Advertising operations. |
| 764 | * Will return null if Bluetooth is turned off or if Bluetooth LE Advertising is not |
| 765 | * supported on this device. |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 766 | * <p> |
| 767 | * Use {@link #isMultipleAdvertisementSupported()} to check whether LE Advertising is supported |
| 768 | * on this device before calling this method. |
Wei Wang | adf6aff | 2014-05-20 06:30:20 +0000 | [diff] [blame] | 769 | */ |
| 770 | public BluetoothLeAdvertiser getBluetoothLeAdvertiser() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 771 | if (!getLeAccess()) { |
| 772 | return null; |
| 773 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 774 | synchronized (mLock) { |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 775 | if (sBluetoothLeAdvertiser == null) { |
| 776 | sBluetoothLeAdvertiser = new BluetoothLeAdvertiser(mManagerService); |
| 777 | } |
| 778 | } |
| 779 | return sBluetoothLeAdvertiser; |
Wei Wang | adf6aff | 2014-05-20 06:30:20 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /** |
Jakub Pawlowski | ac7fcfa | 2017-01-16 07:21:01 -0800 | [diff] [blame] | 783 | * Returns a {@link PeriodicAdvertisingManager} object for Bluetooth LE Periodic Advertising |
| 784 | * operations. Will return null if Bluetooth is turned off or if Bluetooth LE Periodic |
| 785 | * Advertising is not supported on this device. |
| 786 | * <p> |
| 787 | * Use {@link #isLePeriodicAdvertisingSupported()} to check whether LE Periodic Advertising is |
| 788 | * supported on this device before calling this method. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 789 | * |
Jakub Pawlowski | a537b3f | 2017-03-29 17:00:44 -0700 | [diff] [blame] | 790 | * @hide |
Jakub Pawlowski | ac7fcfa | 2017-01-16 07:21:01 -0800 | [diff] [blame] | 791 | */ |
| 792 | public PeriodicAdvertisingManager getPeriodicAdvertisingManager() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 793 | if (!getLeAccess()) { |
| 794 | return null; |
Jakub Pawlowski | ac7fcfa | 2017-01-16 07:21:01 -0800 | [diff] [blame] | 795 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 796 | |
| 797 | if (!isLePeriodicAdvertisingSupported()) { |
| 798 | return null; |
| 799 | } |
| 800 | |
| 801 | synchronized (mLock) { |
| 802 | if (sPeriodicAdvertisingManager == null) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 803 | sPeriodicAdvertisingManager = new PeriodicAdvertisingManager(mManagerService); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | return sPeriodicAdvertisingManager; |
Jakub Pawlowski | ac7fcfa | 2017-01-16 07:21:01 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /** |
Wei Wang | adf6aff | 2014-05-20 06:30:20 +0000 | [diff] [blame] | 810 | * Returns a {@link BluetoothLeScanner} object for Bluetooth LE scan operations. |
| 811 | */ |
| 812 | public BluetoothLeScanner getBluetoothLeScanner() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 813 | if (!getLeAccess()) { |
| 814 | return null; |
| 815 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 816 | synchronized (mLock) { |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 817 | if (sBluetoothLeScanner == null) { |
| 818 | sBluetoothLeScanner = new BluetoothLeScanner(mManagerService); |
| 819 | } |
| 820 | } |
| 821 | return sBluetoothLeScanner; |
Wei Wang | adf6aff | 2014-05-20 06:30:20 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 825 | * Return true if Bluetooth is currently enabled and ready for use. |
| 826 | * <p>Equivalent to: |
| 827 | * <code>getBluetoothState() == STATE_ON</code> |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 828 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 829 | * @return true if the local adapter is turned on |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 830 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 831 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 832 | public boolean isEnabled() { |
| 833 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 834 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 835 | if (mService != null) { |
| 836 | return mService.isEnabled(); |
| 837 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 838 | } catch (RemoteException e) { |
| 839 | Log.e(TAG, "", e); |
| 840 | } finally { |
| 841 | mServiceLock.readLock().unlock(); |
| 842 | } |
| 843 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 844 | return false; |
| 845 | } |
| 846 | |
| 847 | /** |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 848 | * Return true if Bluetooth LE(Always BLE On feature) is currently |
| 849 | * enabled and ready for use |
| 850 | * <p>This returns true if current state is either STATE_ON or STATE_BLE_ON |
| 851 | * |
| 852 | * @return true if the local Bluetooth LE adapter is turned on |
| 853 | * @hide |
| 854 | */ |
Wei Wang | f9e3e9e | 2015-06-01 12:14:28 -0700 | [diff] [blame] | 855 | @SystemApi |
| 856 | public boolean isLeEnabled() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 857 | final int state = getLeState(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 858 | if (DBG) { |
| 859 | Log.d(TAG, "isLeEnabled(): " + BluetoothAdapter.nameForState(state)); |
| 860 | } |
Dan Harms | 2d7e187 | 2019-08-06 09:18:02 -0700 | [diff] [blame] | 861 | return (state == BluetoothAdapter.STATE_ON |
| 862 | || state == BluetoothAdapter.STATE_BLE_ON |
| 863 | || state == BluetoothAdapter.STATE_TURNING_ON |
| 864 | || state == BluetoothAdapter.STATE_TURNING_OFF); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /** |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 868 | * Turns off Bluetooth LE which was earlier turned on by calling enableBLE(). |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 869 | * |
| 870 | * <p> If the internal Adapter state is STATE_BLE_ON, this would trigger the transition |
| 871 | * to STATE_OFF and completely shut-down Bluetooth |
| 872 | * |
| 873 | * <p> If the Adapter state is STATE_ON, This would unregister the existance of |
| 874 | * special Bluetooth LE application and hence the further turning off of Bluetooth |
| 875 | * from UI would ensure the complete turn-off of Bluetooth rather than staying back |
| 876 | * BLE only state |
| 877 | * |
| 878 | * <p>This is an asynchronous call: it will return immediately, and |
| 879 | * clients should listen for {@link #ACTION_BLE_STATE_CHANGED} |
| 880 | * to be notified of subsequent adapter state changes If this call returns |
| 881 | * true, then the adapter state will immediately transition from {@link |
| 882 | * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time |
| 883 | * later transition to either {@link #STATE_BLE_ON} or {@link |
| 884 | * #STATE_OFF} based on the existance of the further Always BLE ON enabled applications |
| 885 | * If this call returns false then there was an |
| 886 | * immediate problem that will prevent the QAdapter from being turned off - |
| 887 | * such as the QAadapter already being turned off. |
| 888 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 889 | * @return true to indicate success, or false on immediate error |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 890 | * @hide |
| 891 | */ |
Wei Wang | f9e3e9e | 2015-06-01 12:14:28 -0700 | [diff] [blame] | 892 | @SystemApi |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 893 | public boolean disableBLE() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 894 | if (!isBleScanAlwaysAvailable()) { |
| 895 | return false; |
| 896 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 897 | |
| 898 | int state = getLeState(); |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 899 | if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_BLE_ON) { |
| 900 | String packageName = ActivityThread.currentPackageName(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 901 | if (DBG) { |
| 902 | Log.d(TAG, "disableBLE(): de-registering " + packageName); |
| 903 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 904 | try { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 905 | mManagerService.updateBleAppCount(mToken, false, packageName); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 906 | } catch (RemoteException e) { |
| 907 | Log.e(TAG, "", e); |
| 908 | } |
| 909 | return true; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 912 | if (DBG) { |
| 913 | Log.d(TAG, "disableBLE(): Already disabled"); |
| 914 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | |
| 918 | /** |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 919 | * Applications who want to only use Bluetooth Low Energy (BLE) can call enableBLE. |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 920 | * |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 921 | * enableBLE registers the existence of an app using only LE functions. |
| 922 | * |
| 923 | * enableBLE may enable Bluetooth to an LE only mode so that an app can use |
| 924 | * LE related features (BluetoothGatt or BluetoothGattServer classes) |
| 925 | * |
| 926 | * If the user disables Bluetooth while an app is registered to use LE only features, |
| 927 | * Bluetooth will remain on in LE only mode for the app. |
| 928 | * |
| 929 | * When Bluetooth is in LE only mode, it is not shown as ON to the UI. |
| 930 | * |
| 931 | * <p>This is an asynchronous call: it returns immediately, and |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 932 | * clients should listen for {@link #ACTION_BLE_STATE_CHANGED} |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 933 | * to be notified of adapter state changes. |
| 934 | * |
| 935 | * If this call returns * true, then the adapter state is either in a mode where |
| 936 | * LE is available, or will transition from {@link #STATE_OFF} to {@link #STATE_BLE_TURNING_ON}, |
| 937 | * and some time later transition to either {@link #STATE_OFF} or {@link #STATE_BLE_ON}. |
| 938 | * |
| 939 | * If this call returns false then there was an immediate problem that prevents the |
| 940 | * adapter from being turned on - such as Airplane mode. |
| 941 | * |
| 942 | * {@link #ACTION_BLE_STATE_CHANGED} returns the Bluetooth Adapter's various |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 943 | * states, It includes all the classic Bluetooth Adapter states along with |
| 944 | * internal BLE only states |
| 945 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 946 | * @return true to indicate Bluetooth LE will be available, or false on immediate error |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 947 | * @hide |
| 948 | */ |
Wei Wang | f9e3e9e | 2015-06-01 12:14:28 -0700 | [diff] [blame] | 949 | @SystemApi |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 950 | public boolean enableBLE() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 951 | if (!isBleScanAlwaysAvailable()) { |
| 952 | return false; |
| 953 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 954 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 955 | try { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 956 | String packageName = ActivityThread.currentPackageName(); |
| 957 | mManagerService.updateBleAppCount(mToken, true, packageName); |
Marie Janssen | 6a383a7 | 2016-10-25 10:47:51 -0700 | [diff] [blame] | 958 | if (isLeEnabled()) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 959 | if (DBG) { |
| 960 | Log.d(TAG, "enableBLE(): Bluetooth already enabled"); |
| 961 | } |
Marie Janssen | 6a383a7 | 2016-10-25 10:47:51 -0700 | [diff] [blame] | 962 | return true; |
| 963 | } |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 964 | if (DBG) { |
| 965 | Log.d(TAG, "enableBLE(): Calling enable"); |
| 966 | } |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 967 | return mManagerService.enable(packageName); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 968 | } catch (RemoteException e) { |
| 969 | Log.e(TAG, "", e); |
| 970 | } |
| 971 | |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 976 | * Get the current state of the local Bluetooth adapter. |
| 977 | * <p>Possible return values are |
| 978 | * {@link #STATE_OFF}, |
| 979 | * {@link #STATE_TURNING_ON}, |
| 980 | * {@link #STATE_ON}, |
| 981 | * {@link #STATE_TURNING_OFF}. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 982 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 983 | * @return current state of Bluetooth adapter |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 984 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 985 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 986 | @AdapterState |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 987 | public int getState() { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 988 | int state = BluetoothAdapter.STATE_OFF; |
| 989 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 990 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 991 | mServiceLock.readLock().lock(); |
| 992 | if (mService != null) { |
| 993 | state = mService.getState(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 994 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 995 | } catch (RemoteException e) { |
| 996 | Log.e(TAG, "", e); |
| 997 | } finally { |
| 998 | mServiceLock.readLock().unlock(); |
| 999 | } |
| 1000 | |
| 1001 | // Consider all internal states as OFF |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1002 | if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1003 | || state == BluetoothAdapter.STATE_BLE_TURNING_OFF) { |
| 1004 | if (VDBG) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1005 | Log.d(TAG, "Consider " + BluetoothAdapter.nameForState(state) + " state as OFF"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1006 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1007 | state = BluetoothAdapter.STATE_OFF; |
| 1008 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1009 | if (VDBG) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1010 | Log.d(TAG, "" + hashCode() + ": getState(). Returning " + BluetoothAdapter.nameForState( |
| 1011 | state)); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1012 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1013 | return state; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /** |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1017 | * Get the current state of the local Bluetooth adapter |
| 1018 | * <p>This returns current internal state of Adapter including LE ON/OFF |
| 1019 | * |
| 1020 | * <p>Possible return values are |
| 1021 | * {@link #STATE_OFF}, |
| 1022 | * {@link #STATE_BLE_TURNING_ON}, |
| 1023 | * {@link #STATE_BLE_ON}, |
| 1024 | * {@link #STATE_TURNING_ON}, |
| 1025 | * {@link #STATE_ON}, |
| 1026 | * {@link #STATE_TURNING_OFF}, |
| 1027 | * {@link #STATE_BLE_TURNING_OFF}. |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1028 | * |
| 1029 | * @return current state of Bluetooth adapter |
| 1030 | * @hide |
| 1031 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1032 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1033 | @AdapterState |
Andrei Onea | f950c08 | 2019-06-17 11:26:14 +0100 | [diff] [blame] | 1034 | @UnsupportedAppUsage(publicAlternatives = "Use {@link #getState()} instead to determine " |
| 1035 | + "whether you can use BLE & BT classic.") |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1036 | public int getLeState() { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1037 | int state = BluetoothAdapter.STATE_OFF; |
| 1038 | |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1039 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1040 | mServiceLock.readLock().lock(); |
| 1041 | if (mService != null) { |
| 1042 | state = mService.getState(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1043 | } |
| 1044 | } catch (RemoteException e) { |
| 1045 | Log.e(TAG, "", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1046 | } finally { |
| 1047 | mServiceLock.readLock().unlock(); |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1048 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1049 | |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1050 | if (VDBG) { |
| 1051 | Log.d(TAG, "getLeState() returning " + BluetoothAdapter.nameForState(state)); |
| 1052 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1053 | return state; |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | boolean getLeAccess() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1057 | if (getLeState() == STATE_ON) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1058 | return true; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1059 | } else if (getLeState() == STATE_BLE_ON) { |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1060 | return true; // TODO: FILTER SYSTEM APPS HERE <-- |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1061 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 1062 | |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | /** |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 1067 | * Turn on the local Bluetooth adapter—do not use without explicit |
| 1068 | * user action to turn on Bluetooth. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1069 | * <p>This powers on the underlying Bluetooth hardware, and starts all |
| 1070 | * Bluetooth system services. |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 1071 | * <p class="caution"><strong>Bluetooth should never be enabled without |
| 1072 | * direct user consent</strong>. If you want to turn on Bluetooth in order |
| 1073 | * to create a wireless connection, you should use the {@link |
| 1074 | * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests |
| 1075 | * user permission to turn on Bluetooth. The {@link #enable()} method is |
| 1076 | * provided only for applications that include a user interface for changing |
| 1077 | * system settings, such as a "power manager" app.</p> |
Brad Fitzpatrick | 3219ab4 | 2009-09-25 16:31:39 +0400 | [diff] [blame] | 1078 | * <p>This is an asynchronous call: it will return immediately, and |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1079 | * clients should listen for {@link #ACTION_STATE_CHANGED} |
| 1080 | * to be notified of subsequent adapter state changes. If this call returns |
| 1081 | * true, then the adapter state will immediately transition from {@link |
| 1082 | * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time |
| 1083 | * later transition to either {@link #STATE_OFF} or {@link |
| 1084 | * #STATE_ON}. If this call returns false then there was an |
| 1085 | * immediate problem that will prevent the adapter from being turned on - |
| 1086 | * such as Airplane mode, or the adapter is already turned on. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1087 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1088 | * @return true to indicate adapter startup has begun, or false on immediate error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1089 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1090 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1091 | public boolean enable() { |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 1092 | if (isEnabled()) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1093 | if (DBG) { |
| 1094 | Log.d(TAG, "enable(): BT already enabled!"); |
| 1095 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1096 | return true; |
| 1097 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1098 | try { |
Svetoslav Ganov | b1e2da7 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 1099 | return mManagerService.enable(ActivityThread.currentPackageName()); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1100 | } catch (RemoteException e) { |
| 1101 | Log.e(TAG, "", e); |
| 1102 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 1107 | * Turn off the local Bluetooth adapter—do not use without explicit |
| 1108 | * user action to turn off Bluetooth. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1109 | * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth |
| 1110 | * system services, and powers down the underlying Bluetooth hardware. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1111 | * <p class="caution"><strong>Bluetooth should never be disabled without |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 1112 | * direct user consent</strong>. The {@link #disable()} method is |
| 1113 | * provided only for applications that include a user interface for changing |
| 1114 | * system settings, such as a "power manager" app.</p> |
Brad Fitzpatrick | 3219ab4 | 2009-09-25 16:31:39 +0400 | [diff] [blame] | 1115 | * <p>This is an asynchronous call: it will return immediately, and |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1116 | * clients should listen for {@link #ACTION_STATE_CHANGED} |
| 1117 | * to be notified of subsequent adapter state changes. If this call returns |
| 1118 | * true, then the adapter state will immediately transition from {@link |
| 1119 | * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time |
| 1120 | * later transition to either {@link #STATE_OFF} or {@link |
| 1121 | * #STATE_ON}. If this call returns false then there was an |
| 1122 | * immediate problem that will prevent the adapter from being turned off - |
| 1123 | * such as the adapter already being turned off. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1124 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1125 | * @return true to indicate adapter shutdown has begun, or false on immediate error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1126 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1127 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1128 | public boolean disable() { |
| 1129 | try { |
Svetoslav Ganov | b1e2da7 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 1130 | return mManagerService.disable(ActivityThread.currentPackageName(), true); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1131 | } catch (RemoteException e) { |
| 1132 | Log.e(TAG, "", e); |
| 1133 | } |
Jaikumar Ganesh | e21a4ac | 2012-03-06 17:15:16 -0800 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * Turn off the local Bluetooth adapter and don't persist the setting. |
| 1139 | * |
| 1140 | * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 1141 | * permission |
| 1142 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1143 | * @return true to indicate adapter shutdown has begun, or false on immediate error |
Jaikumar Ganesh | e21a4ac | 2012-03-06 17:15:16 -0800 | [diff] [blame] | 1144 | * @hide |
| 1145 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1146 | @UnsupportedAppUsage |
Jaikumar Ganesh | e21a4ac | 2012-03-06 17:15:16 -0800 | [diff] [blame] | 1147 | public boolean disable(boolean persist) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1148 | |
Jaikumar Ganesh | e21a4ac | 2012-03-06 17:15:16 -0800 | [diff] [blame] | 1149 | try { |
Svetoslav Ganov | b1e2da7 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 1150 | return mManagerService.disable(ActivityThread.currentPackageName(), persist); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1151 | } catch (RemoteException e) { |
| 1152 | Log.e(TAG, "", e); |
| 1153 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1154 | return false; |
| 1155 | } |
| 1156 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * Returns the hardware address of the local Bluetooth adapter. |
| 1159 | * <p>For example, "00:11:22:AA:BB:CC". |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1160 | * |
| 1161 | * @return Bluetooth hardware address as string |
| 1162 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1163 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1164 | public String getAddress() { |
| 1165 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1166 | return mManagerService.getAddress(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1167 | } catch (RemoteException e) { |
| 1168 | Log.e(TAG, "", e); |
| 1169 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1170 | return null; |
| 1171 | } |
| 1172 | |
| 1173 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1174 | * Get the friendly Bluetooth name of the local Bluetooth adapter. |
| 1175 | * <p>This name is visible to remote Bluetooth devices. |
| 1176 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1177 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1178 | * @return the Bluetooth name, or null on error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1179 | */ |
| 1180 | public String getName() { |
| 1181 | try { |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 1182 | return mManagerService.getName(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1183 | } catch (RemoteException e) { |
| 1184 | Log.e(TAG, "", e); |
| 1185 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1186 | return null; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
Ajay Panicker | cda6e87 | 2015-07-28 16:52:09 -0700 | [diff] [blame] | 1190 | * Factory reset bluetooth settings. |
| 1191 | * |
Ajay Panicker | cda6e87 | 2015-07-28 16:52:09 -0700 | [diff] [blame] | 1192 | * @return true to indicate that the config file was successfully cleared |
Ajay Panicker | cda6e87 | 2015-07-28 16:52:09 -0700 | [diff] [blame] | 1193 | * @hide |
| 1194 | */ |
Rahul Sabnis | 644d3dd | 2019-10-25 13:15:12 -0700 | [diff] [blame] | 1195 | @SystemApi |
| 1196 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Ajay Panicker | cda6e87 | 2015-07-28 16:52:09 -0700 | [diff] [blame] | 1197 | public boolean factoryReset() { |
| 1198 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1199 | mServiceLock.readLock().lock(); |
Ajay Panicker | cda6e87 | 2015-07-28 16:52:09 -0700 | [diff] [blame] | 1200 | if (mService != null) { |
| 1201 | return mService.factoryReset(); |
| 1202 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1203 | SystemProperties.set("persist.bluetooth.factoryreset", "true"); |
| 1204 | } catch (RemoteException e) { |
| 1205 | Log.e(TAG, "", e); |
| 1206 | } finally { |
| 1207 | mServiceLock.readLock().unlock(); |
| 1208 | } |
Ajay Panicker | cda6e87 | 2015-07-28 16:52:09 -0700 | [diff] [blame] | 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 1213 | * Get the UUIDs supported by the local Bluetooth adapter. |
| 1214 | * |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 1215 | * @return the UUIDs supported by the local Bluetooth Adapter. |
| 1216 | * @hide |
| 1217 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1218 | @UnsupportedAppUsage |
Rahul Sabnis | 644d3dd | 2019-10-25 13:15:12 -0700 | [diff] [blame] | 1219 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1220 | public @NonNull ParcelUuid[] getUuids() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1221 | if (getState() != STATE_ON) { |
| 1222 | return null; |
| 1223 | } |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 1224 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1225 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1226 | if (mService != null) { |
| 1227 | return mService.getUuids(); |
| 1228 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1229 | } catch (RemoteException e) { |
| 1230 | Log.e(TAG, "", e); |
| 1231 | } finally { |
| 1232 | mServiceLock.readLock().unlock(); |
| 1233 | } |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 1234 | return null; |
| 1235 | } |
| 1236 | |
| 1237 | /** |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 1238 | * Set the friendly Bluetooth name of the local Bluetooth adapter. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1239 | * <p>This name is visible to remote Bluetooth devices. |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 1240 | * <p>Valid Bluetooth names are a maximum of 248 bytes using UTF-8 |
| 1241 | * encoding, although many remote devices can only display the first |
| 1242 | * 40 characters, and some may be limited to just 20. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 1243 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1244 | * will return false. After turning on Bluetooth, |
| 1245 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1246 | * to get the updated value. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1247 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1248 | * @param name a valid Bluetooth name |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1249 | * @return true if the name was set, false otherwise |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1250 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1251 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1252 | public boolean setName(String name) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1253 | if (getState() != STATE_ON) { |
| 1254 | return false; |
| 1255 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1256 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1257 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1258 | if (mService != null) { |
| 1259 | return mService.setName(name); |
| 1260 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1261 | } catch (RemoteException e) { |
| 1262 | Log.e(TAG, "", e); |
| 1263 | } finally { |
| 1264 | mServiceLock.readLock().unlock(); |
| 1265 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
Pulkit Bhuwalka | d01fa6e | 2017-09-20 15:10:45 -0700 | [diff] [blame] | 1270 | * Returns the {@link BluetoothClass} Bluetooth Class of Device (CoD) of the local Bluetooth |
| 1271 | * adapter. |
| 1272 | * |
| 1273 | * @return {@link BluetoothClass} Bluetooth CoD of local Bluetooth device. |
| 1274 | * |
| 1275 | * @hide |
| 1276 | */ |
| 1277 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 1278 | public BluetoothClass getBluetoothClass() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1279 | if (getState() != STATE_ON) { |
| 1280 | return null; |
| 1281 | } |
Pulkit Bhuwalka | d01fa6e | 2017-09-20 15:10:45 -0700 | [diff] [blame] | 1282 | try { |
| 1283 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1284 | if (mService != null) { |
| 1285 | return mService.getBluetoothClass(); |
| 1286 | } |
Pulkit Bhuwalka | d01fa6e | 2017-09-20 15:10:45 -0700 | [diff] [blame] | 1287 | } catch (RemoteException e) { |
| 1288 | Log.e(TAG, "", e); |
| 1289 | } finally { |
| 1290 | mServiceLock.readLock().unlock(); |
| 1291 | } |
| 1292 | return null; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
Pulkit Bhuwalka | 547853d | 2017-09-25 17:55:12 -0700 | [diff] [blame] | 1296 | * Sets the {@link BluetoothClass} Bluetooth Class of Device (CoD) of the local Bluetooth |
| 1297 | * adapter. |
| 1298 | * |
| 1299 | * <p>Note: This value persists across system reboot. |
Pulkit Bhuwalka | 66d6123 | 2017-08-16 21:52:04 -0700 | [diff] [blame] | 1300 | * |
| 1301 | * @param bluetoothClass {@link BluetoothClass} to set the local Bluetooth adapter to. |
| 1302 | * @return true if successful, false if unsuccessful. |
| 1303 | * |
| 1304 | * @hide |
| 1305 | */ |
| 1306 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 1307 | public boolean setBluetoothClass(BluetoothClass bluetoothClass) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1308 | if (getState() != STATE_ON) { |
| 1309 | return false; |
| 1310 | } |
Pulkit Bhuwalka | 66d6123 | 2017-08-16 21:52:04 -0700 | [diff] [blame] | 1311 | try { |
| 1312 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1313 | if (mService != null) { |
| 1314 | return mService.setBluetoothClass(bluetoothClass); |
| 1315 | } |
Pulkit Bhuwalka | 66d6123 | 2017-08-16 21:52:04 -0700 | [diff] [blame] | 1316 | } catch (RemoteException e) { |
| 1317 | Log.e(TAG, "", e); |
| 1318 | } finally { |
| 1319 | mServiceLock.readLock().unlock(); |
| 1320 | } |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
| 1324 | /** |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 1325 | * Returns the Input/Output capability of the device for classic Bluetooth. |
| 1326 | * |
| 1327 | * @return Input/Output capability of the device. One of {@link #IO_CAPABILITY_OUT}, |
| 1328 | * {@link #IO_CAPABILITY_IO}, {@link #IO_CAPABILITY_IN}, {@link #IO_CAPABILITY_NONE}, |
| 1329 | * {@link #IO_CAPABILITY_KBDISP} or {@link #IO_CAPABILITY_UNKNOWN}. |
| 1330 | * |
| 1331 | * @hide |
| 1332 | */ |
| 1333 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Pulkit Bhuwalka | 8023814 | 2018-04-20 16:56:53 -0700 | [diff] [blame] | 1334 | @IoCapability |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 1335 | public int getIoCapability() { |
| 1336 | if (getState() != STATE_ON) return BluetoothAdapter.IO_CAPABILITY_UNKNOWN; |
| 1337 | try { |
| 1338 | mServiceLock.readLock().lock(); |
| 1339 | if (mService != null) return mService.getIoCapability(); |
| 1340 | } catch (RemoteException e) { |
| 1341 | Log.e(TAG, e.getMessage(), e); |
| 1342 | } finally { |
| 1343 | mServiceLock.readLock().unlock(); |
| 1344 | } |
| 1345 | return BluetoothAdapter.IO_CAPABILITY_UNKNOWN; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * Sets the Input/Output capability of the device for classic Bluetooth. |
| 1350 | * |
| 1351 | * <p>Changing the Input/Output capability of a device only takes effect on restarting the |
| 1352 | * Bluetooth stack. You would need to restart the stack using {@link BluetoothAdapter#disable()} |
| 1353 | * and {@link BluetoothAdapter#enable()} to see the changes. |
| 1354 | * |
| 1355 | * @param capability Input/Output capability of the device. One of {@link #IO_CAPABILITY_OUT}, |
| 1356 | * {@link #IO_CAPABILITY_IO}, {@link #IO_CAPABILITY_IN}, |
| 1357 | * {@link #IO_CAPABILITY_NONE} or {@link #IO_CAPABILITY_KBDISP}. |
| 1358 | * |
| 1359 | * @hide |
| 1360 | */ |
| 1361 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Pulkit Bhuwalka | 8023814 | 2018-04-20 16:56:53 -0700 | [diff] [blame] | 1362 | public boolean setIoCapability(@IoCapability int capability) { |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 1363 | if (getState() != STATE_ON) return false; |
| 1364 | try { |
| 1365 | mServiceLock.readLock().lock(); |
| 1366 | if (mService != null) return mService.setIoCapability(capability); |
| 1367 | } catch (RemoteException e) { |
| 1368 | Log.e(TAG, e.getMessage(), e); |
| 1369 | } finally { |
| 1370 | mServiceLock.readLock().unlock(); |
| 1371 | } |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * Returns the Input/Output capability of the device for BLE operations. |
| 1377 | * |
| 1378 | * @return Input/Output capability of the device. One of {@link #IO_CAPABILITY_OUT}, |
| 1379 | * {@link #IO_CAPABILITY_IO}, {@link #IO_CAPABILITY_IN}, {@link #IO_CAPABILITY_NONE}, |
| 1380 | * {@link #IO_CAPABILITY_KBDISP} or {@link #IO_CAPABILITY_UNKNOWN}. |
| 1381 | * |
| 1382 | * @hide |
| 1383 | */ |
| 1384 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Pulkit Bhuwalka | 8023814 | 2018-04-20 16:56:53 -0700 | [diff] [blame] | 1385 | @IoCapability |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 1386 | public int getLeIoCapability() { |
| 1387 | if (getState() != STATE_ON) return BluetoothAdapter.IO_CAPABILITY_UNKNOWN; |
| 1388 | try { |
| 1389 | mServiceLock.readLock().lock(); |
| 1390 | if (mService != null) return mService.getLeIoCapability(); |
| 1391 | } catch (RemoteException e) { |
| 1392 | Log.e(TAG, e.getMessage(), e); |
| 1393 | } finally { |
| 1394 | mServiceLock.readLock().unlock(); |
| 1395 | } |
| 1396 | return BluetoothAdapter.IO_CAPABILITY_UNKNOWN; |
| 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * Sets the Input/Output capability of the device for BLE operations. |
| 1401 | * |
| 1402 | * <p>Changing the Input/Output capability of a device only takes effect on restarting the |
| 1403 | * Bluetooth stack. You would need to restart the stack using {@link BluetoothAdapter#disable()} |
| 1404 | * and {@link BluetoothAdapter#enable()} to see the changes. |
| 1405 | * |
| 1406 | * @param capability Input/Output capability of the device. One of {@link #IO_CAPABILITY_OUT}, |
| 1407 | * {@link #IO_CAPABILITY_IO}, {@link #IO_CAPABILITY_IN}, |
| 1408 | * {@link #IO_CAPABILITY_NONE} or {@link #IO_CAPABILITY_KBDISP}. |
| 1409 | * |
| 1410 | * @hide |
| 1411 | */ |
| 1412 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Pulkit Bhuwalka | 8023814 | 2018-04-20 16:56:53 -0700 | [diff] [blame] | 1413 | public boolean setLeIoCapability(@IoCapability int capability) { |
Pulkit Bhuwalka | 979698b | 2018-03-28 13:51:43 -0700 | [diff] [blame] | 1414 | if (getState() != STATE_ON) return false; |
| 1415 | try { |
| 1416 | mServiceLock.readLock().lock(); |
| 1417 | if (mService != null) return mService.setLeIoCapability(capability); |
| 1418 | } catch (RemoteException e) { |
| 1419 | Log.e(TAG, e.getMessage(), e); |
| 1420 | } finally { |
| 1421 | mServiceLock.readLock().unlock(); |
| 1422 | } |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | /** |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 1427 | * Get the current Bluetooth scan mode of the local Bluetooth adapter. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1428 | * <p>The Bluetooth scan mode determines if the local adapter is |
| 1429 | * connectable and/or discoverable from remote Bluetooth devices. |
| 1430 | * <p>Possible values are: |
| 1431 | * {@link #SCAN_MODE_NONE}, |
| 1432 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 1433 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 1434 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1435 | * will return {@link #SCAN_MODE_NONE}. After turning on Bluetooth, |
| 1436 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1437 | * to get the updated value. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1438 | * |
| 1439 | * @return scan mode |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1440 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1441 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1442 | @ScanMode |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1443 | public int getScanMode() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1444 | if (getState() != STATE_ON) { |
| 1445 | return SCAN_MODE_NONE; |
| 1446 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1447 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1448 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1449 | if (mService != null) { |
| 1450 | return mService.getScanMode(); |
| 1451 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1452 | } catch (RemoteException e) { |
| 1453 | Log.e(TAG, "", e); |
| 1454 | } finally { |
| 1455 | mServiceLock.readLock().unlock(); |
| 1456 | } |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1457 | return SCAN_MODE_NONE; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1461 | * Set the Bluetooth scan mode of the local Bluetooth adapter. |
| 1462 | * <p>The Bluetooth scan mode determines if the local adapter is |
| 1463 | * connectable and/or discoverable from remote Bluetooth devices. |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 1464 | * <p>For privacy reasons, discoverable mode is automatically turned off |
| 1465 | * after <code>duration</code> seconds. For example, 120 seconds should be |
| 1466 | * enough for a remote device to initiate and complete its discovery |
| 1467 | * process. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1468 | * <p>Valid scan mode values are: |
| 1469 | * {@link #SCAN_MODE_NONE}, |
| 1470 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 1471 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 1472 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1473 | * will return false. After turning on Bluetooth, |
| 1474 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1475 | * to get the updated value. |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 1476 | * <p>Applications cannot set the scan mode. They should use |
| 1477 | * <code>startActivityForResult( |
| 1478 | * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE}) |
| 1479 | * </code>instead. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1480 | * |
| 1481 | * @param mode valid scan mode |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1482 | * @param duration time in seconds to apply scan mode, only used for {@link |
| 1483 | * #SCAN_MODE_CONNECTABLE_DISCOVERABLE} |
| 1484 | * @return true if the scan mode was set, false otherwise |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 1485 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1486 | */ |
Rahul Sabnis | 644d3dd | 2019-10-25 13:15:12 -0700 | [diff] [blame] | 1487 | @SystemApi |
| 1488 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1489 | public boolean setScanMode(@ScanMode int mode, int duration) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1490 | if (getState() != STATE_ON) { |
| 1491 | return false; |
| 1492 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1493 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1494 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1495 | if (mService != null) { |
| 1496 | return mService.setScanMode(mode, duration); |
| 1497 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1498 | } catch (RemoteException e) { |
| 1499 | Log.e(TAG, "", e); |
| 1500 | } finally { |
| 1501 | mServiceLock.readLock().unlock(); |
| 1502 | } |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 1503 | return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Rahul Sabnis | 644d3dd | 2019-10-25 13:15:12 -0700 | [diff] [blame] | 1506 | /** |
| 1507 | * Set the Bluetooth scan mode of the local Bluetooth adapter. |
| 1508 | * <p>The Bluetooth scan mode determines if the local adapter is |
| 1509 | * connectable and/or discoverable from remote Bluetooth devices. |
| 1510 | * <p>For privacy reasons, discoverable mode is automatically turned off |
| 1511 | * after <code>duration</code> seconds. For example, 120 seconds should be |
| 1512 | * enough for a remote device to initiate and complete its discovery |
| 1513 | * process. |
| 1514 | * <p>Valid scan mode values are: |
| 1515 | * {@link #SCAN_MODE_NONE}, |
| 1516 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 1517 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. |
| 1518 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1519 | * will return false. After turning on Bluetooth, |
| 1520 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1521 | * to get the updated value. |
| 1522 | * <p>Applications cannot set the scan mode. They should use |
| 1523 | * <code>startActivityForResult( |
| 1524 | * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE}) |
| 1525 | * </code>instead. |
| 1526 | * |
| 1527 | * @param mode valid scan mode |
| 1528 | * @return true if the scan mode was set, false otherwise |
| 1529 | * @hide |
| 1530 | */ |
| 1531 | @SystemApi |
| 1532 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 1533 | public boolean setScanMode(@ScanMode int mode) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1534 | if (getState() != STATE_ON) { |
| 1535 | return false; |
| 1536 | } |
Srikanth Uppala | 827de2d | 2012-04-04 03:33:26 -0700 | [diff] [blame] | 1537 | /* getDiscoverableTimeout() to use the latest from NV than use 0 */ |
| 1538 | return setScanMode(mode, getDiscoverableTimeout()); |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1542 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1543 | public int getDiscoverableTimeout() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1544 | if (getState() != STATE_ON) { |
| 1545 | return -1; |
| 1546 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1547 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1548 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1549 | if (mService != null) { |
| 1550 | return mService.getDiscoverableTimeout(); |
| 1551 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1552 | } catch (RemoteException e) { |
| 1553 | Log.e(TAG, "", e); |
| 1554 | } finally { |
| 1555 | mServiceLock.readLock().unlock(); |
| 1556 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1557 | return -1; |
| 1558 | } |
| 1559 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1560 | /** @hide */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1561 | @UnsupportedAppUsage |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1562 | public void setDiscoverableTimeout(int timeout) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1563 | if (getState() != STATE_ON) { |
| 1564 | return; |
| 1565 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1566 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1567 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1568 | if (mService != null) { |
| 1569 | mService.setDiscoverableTimeout(timeout); |
| 1570 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1571 | } catch (RemoteException e) { |
| 1572 | Log.e(TAG, "", e); |
| 1573 | } finally { |
| 1574 | mServiceLock.readLock().unlock(); |
| 1575 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1578 | /** |
Marie Janssen | 23a4833 | 2017-01-24 14:09:59 -0800 | [diff] [blame] | 1579 | * Get the end time of the latest remote device discovery process. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1580 | * |
| 1581 | * @return the latest time that the bluetooth adapter was/will be in discovery mode, in |
| 1582 | * milliseconds since the epoch. This time can be in the future if {@link #startDiscovery()} has |
| 1583 | * been called recently. |
Marie Janssen | 23a4833 | 2017-01-24 14:09:59 -0800 | [diff] [blame] | 1584 | * @hide |
| 1585 | */ |
Rahul Sabnis | 644d3dd | 2019-10-25 13:15:12 -0700 | [diff] [blame] | 1586 | @SystemApi |
| 1587 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Marie Janssen | 23a4833 | 2017-01-24 14:09:59 -0800 | [diff] [blame] | 1588 | public long getDiscoveryEndMillis() { |
| 1589 | try { |
| 1590 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1591 | if (mService != null) { |
| 1592 | return mService.getDiscoveryEndMillis(); |
| 1593 | } |
Marie Janssen | 23a4833 | 2017-01-24 14:09:59 -0800 | [diff] [blame] | 1594 | } catch (RemoteException e) { |
| 1595 | Log.e(TAG, "", e); |
| 1596 | } finally { |
| 1597 | mServiceLock.readLock().unlock(); |
| 1598 | } |
| 1599 | return -1; |
| 1600 | } |
| 1601 | |
| 1602 | /** |
Zach Johnson | 57e6e90 | 2018-12-12 17:11:25 -0800 | [diff] [blame] | 1603 | * Set the context for this BluetoothAdapter (only called from BluetoothManager) |
| 1604 | * @hide |
| 1605 | */ |
| 1606 | public void setContext(Context context) { |
| 1607 | mContext = context; |
| 1608 | } |
| 1609 | |
| 1610 | private String getOpPackageName() { |
| 1611 | // Workaround for legacy API for getting a BluetoothAdapter not |
| 1612 | // passing a context |
| 1613 | if (mContext != null) { |
| 1614 | return mContext.getOpPackageName(); |
| 1615 | } |
| 1616 | return ActivityThread.currentOpPackageName(); |
| 1617 | } |
| 1618 | |
| 1619 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1620 | * Start the remote device discovery process. |
| 1621 | * <p>The discovery process usually involves an inquiry scan of about 12 |
| 1622 | * seconds, followed by a page scan of each new device to retrieve its |
| 1623 | * Bluetooth name. |
| 1624 | * <p>This is an asynchronous call, it will return immediately. Register |
| 1625 | * for {@link #ACTION_DISCOVERY_STARTED} and {@link |
| 1626 | * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the |
| 1627 | * discovery starts and completes. Register for {@link |
| 1628 | * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices |
| 1629 | * are found. |
| 1630 | * <p>Device discovery is a heavyweight procedure. New connections to |
| 1631 | * remote Bluetooth devices should not be attempted while discovery is in |
| 1632 | * progress, and existing connections will experience limited bandwidth |
| 1633 | * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 1634 | * discovery. Discovery is not managed by the Activity, |
| 1635 | * but is run as a system service, so an application should always call |
| 1636 | * {@link BluetoothAdapter#cancelDiscovery()} even if it |
| 1637 | * did not directly request a discovery, just to be sure. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1638 | * <p>Device discovery will only find remote devices that are currently |
| 1639 | * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are |
| 1640 | * not discoverable by default, and need to be entered into a special mode. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 1641 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1642 | * will return false. After turning on Bluetooth, |
| 1643 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1644 | * to get the updated value. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1645 | * |
| 1646 | * @return true on success, false on error |
| 1647 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1648 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1649 | public boolean startDiscovery() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1650 | if (getState() != STATE_ON) { |
| 1651 | return false; |
| 1652 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1653 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1654 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1655 | if (mService != null) { |
Zach Johnson | 57e6e90 | 2018-12-12 17:11:25 -0800 | [diff] [blame] | 1656 | return mService.startDiscovery(getOpPackageName()); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1657 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1658 | } catch (RemoteException e) { |
| 1659 | Log.e(TAG, "", e); |
| 1660 | } finally { |
| 1661 | mServiceLock.readLock().unlock(); |
| 1662 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1663 | return false; |
| 1664 | } |
| 1665 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1666 | /** |
| 1667 | * Cancel the current device discovery process. |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 1668 | * <p>Because discovery is a heavyweight procedure for the Bluetooth |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 1669 | * adapter, this method should always be called before attempting to connect |
| 1670 | * to a remote device with {@link |
| 1671 | * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by |
| 1672 | * the Activity, but is run as a system service, so an application should |
| 1673 | * always call cancel discovery even if it did not directly request a |
| 1674 | * discovery, just to be sure. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 1675 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1676 | * will return false. After turning on Bluetooth, |
| 1677 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1678 | * to get the updated value. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1679 | * |
| 1680 | * @return true on success, false on error |
| 1681 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1682 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1683 | public boolean cancelDiscovery() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1684 | if (getState() != STATE_ON) { |
| 1685 | return false; |
| 1686 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1687 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1688 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1689 | if (mService != null) { |
| 1690 | return mService.cancelDiscovery(); |
| 1691 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1692 | } catch (RemoteException e) { |
| 1693 | Log.e(TAG, "", e); |
| 1694 | } finally { |
| 1695 | mServiceLock.readLock().unlock(); |
| 1696 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1697 | return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1700 | /** |
| 1701 | * Return true if the local Bluetooth adapter is currently in the device |
| 1702 | * discovery process. |
| 1703 | * <p>Device discovery is a heavyweight procedure. New connections to |
| 1704 | * remote Bluetooth devices should not be attempted while discovery is in |
| 1705 | * progress, and existing connections will experience limited bandwidth |
| 1706 | * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing |
| 1707 | * discovery. |
| 1708 | * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED} |
| 1709 | * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery |
| 1710 | * starts or completes. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 1711 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 1712 | * will return false. After turning on Bluetooth, |
| 1713 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 1714 | * to get the updated value. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1715 | * |
| 1716 | * @return true if discovering |
| 1717 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 1718 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1719 | public boolean isDiscovering() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1720 | if (getState() != STATE_ON) { |
| 1721 | return false; |
| 1722 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1723 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1724 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1725 | if (mService != null) { |
| 1726 | return mService.isDiscovering(); |
| 1727 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1728 | } catch (RemoteException e) { |
| 1729 | Log.e(TAG, "", e); |
| 1730 | } finally { |
| 1731 | mServiceLock.readLock().unlock(); |
| 1732 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | /** |
Rahul Sabnis | 0d33ef9 | 2019-11-13 16:21:12 -0800 | [diff] [blame] | 1737 | * Connects all enabled and supported bluetooth profiles between the local and remote device |
| 1738 | * |
| 1739 | * @param device is the remote device with which to connect these profiles |
| 1740 | * @return true if all profiles successfully connected, false if an error occurred |
| 1741 | * |
| 1742 | * @hide |
| 1743 | */ |
| 1744 | @SystemApi |
| 1745 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 1746 | public boolean connectAllEnabledProfiles(@NonNull BluetoothDevice device) { |
| 1747 | try { |
| 1748 | mServiceLock.readLock().lock(); |
| 1749 | if (mService != null) { |
| 1750 | return mService.connectAllEnabledProfiles(device); |
| 1751 | } |
| 1752 | } catch (RemoteException e) { |
| 1753 | Log.e(TAG, "", e); |
| 1754 | } finally { |
| 1755 | mServiceLock.readLock().unlock(); |
| 1756 | } |
| 1757 | |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
| 1761 | /** |
| 1762 | * Disconnects all enabled and supported bluetooth profiles between the local and remote device |
| 1763 | * |
| 1764 | * @param device is the remote device with which to disconnect these profiles |
| 1765 | * @return true if all profiles successfully disconnected, false if an error occurred |
| 1766 | * |
| 1767 | * @hide |
| 1768 | */ |
| 1769 | @SystemApi |
| 1770 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 1771 | public boolean disconnectAllEnabledProfiles(@NonNull BluetoothDevice device) { |
| 1772 | try { |
| 1773 | mServiceLock.readLock().lock(); |
| 1774 | if (mService != null) { |
| 1775 | return mService.disconnectAllEnabledProfiles(device); |
| 1776 | } |
| 1777 | } catch (RemoteException e) { |
| 1778 | Log.e(TAG, "", e); |
| 1779 | } finally { |
| 1780 | mServiceLock.readLock().unlock(); |
| 1781 | } |
| 1782 | |
| 1783 | return false; |
| 1784 | } |
| 1785 | |
| 1786 | /** |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1787 | * Return true if the multi advertisement is supported by the chipset |
| 1788 | * |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1789 | * @return true if Multiple Advertisement feature is supported |
| 1790 | */ |
| 1791 | public boolean isMultipleAdvertisementSupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1792 | if (getState() != STATE_ON) { |
| 1793 | return false; |
| 1794 | } |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1795 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1796 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1797 | if (mService != null) { |
| 1798 | return mService.isMultiAdvertisementSupported(); |
| 1799 | } |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1800 | } catch (RemoteException e) { |
| 1801 | Log.e(TAG, "failed to get isMultipleAdvertisementSupported, error: ", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1802 | } finally { |
| 1803 | mServiceLock.readLock().unlock(); |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1804 | } |
| 1805 | return false; |
| 1806 | } |
| 1807 | |
| 1808 | /** |
Wei Wang | d91f193 | 2015-03-19 15:09:56 -0700 | [diff] [blame] | 1809 | * Returns {@code true} if BLE scan is always available, {@code false} otherwise. <p> |
| 1810 | * |
| 1811 | * If this returns {@code true}, application can issue {@link BluetoothLeScanner#startScan} and |
| 1812 | * fetch scan results even when Bluetooth is turned off.<p> |
| 1813 | * |
| 1814 | * To change this setting, use {@link #ACTION_REQUEST_BLE_SCAN_ALWAYS_AVAILABLE}. |
| 1815 | * |
| 1816 | * @hide |
| 1817 | */ |
| 1818 | @SystemApi |
| 1819 | public boolean isBleScanAlwaysAvailable() { |
Wei Wang | 67d8416 | 2015-04-26 17:04:29 -0700 | [diff] [blame] | 1820 | try { |
| 1821 | return mManagerService.isBleScanAlwaysAvailable(); |
| 1822 | } catch (RemoteException e) { |
| 1823 | Log.e(TAG, "remote expection when calling isBleScanAlwaysAvailable", e); |
| 1824 | return false; |
| 1825 | } |
Wei Wang | d91f193 | 2015-03-19 15:09:56 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /** |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1829 | * Return true if offloaded filters are supported |
| 1830 | * |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1831 | * @return true if chipset supports on-chip filtering |
| 1832 | */ |
| 1833 | public boolean isOffloadedFilteringSupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1834 | if (!getLeAccess()) { |
| 1835 | return false; |
| 1836 | } |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1837 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1838 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1839 | if (mService != null) { |
| 1840 | return mService.isOffloadedFilteringSupported(); |
| 1841 | } |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1842 | } catch (RemoteException e) { |
| 1843 | Log.e(TAG, "failed to get isOffloadedFilteringSupported, error: ", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1844 | } finally { |
| 1845 | mServiceLock.readLock().unlock(); |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1846 | } |
| 1847 | return false; |
| 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * Return true if offloaded scan batching is supported |
| 1852 | * |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1853 | * @return true if chipset supports on-chip scan batching |
| 1854 | */ |
| 1855 | public boolean isOffloadedScanBatchingSupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1856 | if (!getLeAccess()) { |
| 1857 | return false; |
| 1858 | } |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1859 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1860 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1861 | if (mService != null) { |
| 1862 | return mService.isOffloadedScanBatchingSupported(); |
| 1863 | } |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1864 | } catch (RemoteException e) { |
| 1865 | Log.e(TAG, "failed to get isOffloadedScanBatchingSupported, error: ", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 1866 | } finally { |
| 1867 | mServiceLock.readLock().unlock(); |
Prerepa Viswanadham | 0c116e1 | 2014-07-02 12:30:38 -0700 | [diff] [blame] | 1868 | } |
| 1869 | return false; |
| 1870 | } |
| 1871 | |
| 1872 | /** |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1873 | * Return true if LE 2M PHY feature is supported. |
| 1874 | * |
| 1875 | * @return true if chipset supports LE 2M PHY feature |
| 1876 | */ |
| 1877 | public boolean isLe2MPhySupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1878 | if (!getLeAccess()) { |
| 1879 | return false; |
| 1880 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1881 | try { |
| 1882 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1883 | if (mService != null) { |
| 1884 | return mService.isLe2MPhySupported(); |
| 1885 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1886 | } catch (RemoteException e) { |
| 1887 | Log.e(TAG, "failed to get isExtendedAdvertisingSupported, error: ", e); |
| 1888 | } finally { |
| 1889 | mServiceLock.readLock().unlock(); |
| 1890 | } |
| 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | /** |
| 1895 | * Return true if LE Coded PHY feature is supported. |
| 1896 | * |
| 1897 | * @return true if chipset supports LE Coded PHY feature |
| 1898 | */ |
| 1899 | public boolean isLeCodedPhySupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1900 | if (!getLeAccess()) { |
| 1901 | return false; |
| 1902 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1903 | try { |
| 1904 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1905 | if (mService != null) { |
| 1906 | return mService.isLeCodedPhySupported(); |
| 1907 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1908 | } catch (RemoteException e) { |
| 1909 | Log.e(TAG, "failed to get isLeCodedPhySupported, error: ", e); |
| 1910 | } finally { |
| 1911 | mServiceLock.readLock().unlock(); |
| 1912 | } |
| 1913 | return false; |
| 1914 | } |
| 1915 | |
| 1916 | /** |
Jakub Pawlowski | bc2991e | 2017-03-10 16:07:59 -0800 | [diff] [blame] | 1917 | * Return true if LE Extended Advertising feature is supported. |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1918 | * |
Jakub Pawlowski | bc2991e | 2017-03-10 16:07:59 -0800 | [diff] [blame] | 1919 | * @return true if chipset supports LE Extended Advertising feature |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1920 | */ |
| 1921 | public boolean isLeExtendedAdvertisingSupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1922 | if (!getLeAccess()) { |
| 1923 | return false; |
| 1924 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1925 | try { |
| 1926 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1927 | if (mService != null) { |
| 1928 | return mService.isLeExtendedAdvertisingSupported(); |
| 1929 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1930 | } catch (RemoteException e) { |
| 1931 | Log.e(TAG, "failed to get isLeExtendedAdvertisingSupported, error: ", e); |
| 1932 | } finally { |
| 1933 | mServiceLock.readLock().unlock(); |
| 1934 | } |
| 1935 | return false; |
| 1936 | } |
| 1937 | |
| 1938 | /** |
| 1939 | * Return true if LE Periodic Advertising feature is supported. |
| 1940 | * |
| 1941 | * @return true if chipset supports LE Periodic Advertising feature |
| 1942 | */ |
| 1943 | public boolean isLePeriodicAdvertisingSupported() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1944 | if (!getLeAccess()) { |
| 1945 | return false; |
| 1946 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1947 | try { |
| 1948 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1949 | if (mService != null) { |
| 1950 | return mService.isLePeriodicAdvertisingSupported(); |
| 1951 | } |
Jakub Pawlowski | 48091bb | 2017-02-07 18:05:39 -0800 | [diff] [blame] | 1952 | } catch (RemoteException e) { |
| 1953 | Log.e(TAG, "failed to get isLePeriodicAdvertisingSupported, error: ", e); |
| 1954 | } finally { |
| 1955 | mServiceLock.readLock().unlock(); |
| 1956 | } |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
| 1960 | /** |
Jakub Pawlowski | 76cceb1 | 2017-04-21 04:59:55 -0700 | [diff] [blame] | 1961 | * Return the maximum LE advertising data length in bytes, |
| 1962 | * if LE Extended Advertising feature is supported, 0 otherwise. |
Jakub Pawlowski | ad10aff | 2017-03-16 18:22:36 -0700 | [diff] [blame] | 1963 | * |
| 1964 | * @return the maximum LE advertising data length. |
| 1965 | */ |
| 1966 | public int getLeMaximumAdvertisingDataLength() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1967 | if (!getLeAccess()) { |
| 1968 | return 0; |
| 1969 | } |
Jakub Pawlowski | ad10aff | 2017-03-16 18:22:36 -0700 | [diff] [blame] | 1970 | try { |
| 1971 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 1972 | if (mService != null) { |
| 1973 | return mService.getLeMaximumAdvertisingDataLength(); |
| 1974 | } |
Jakub Pawlowski | ad10aff | 2017-03-16 18:22:36 -0700 | [diff] [blame] | 1975 | } catch (RemoteException e) { |
| 1976 | Log.e(TAG, "failed to get getLeMaximumAdvertisingDataLength, error: ", e); |
| 1977 | } finally { |
| 1978 | mServiceLock.readLock().unlock(); |
| 1979 | } |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | /** |
Stanley Tng | 61dbd81 | 2019-01-13 16:04:31 -0800 | [diff] [blame] | 1984 | * Return true if Hearing Aid Profile is supported. |
| 1985 | * |
| 1986 | * @return true if phone supports Hearing Aid Profile |
| 1987 | */ |
| 1988 | private boolean isHearingAidProfileSupported() { |
| 1989 | try { |
| 1990 | return mManagerService.isHearingAidProfileSupported(); |
| 1991 | } catch (RemoteException e) { |
| 1992 | Log.e(TAG, "remote expection when calling isHearingAidProfileSupported", e); |
| 1993 | return false; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | /** |
Pavlin Radoslavov | a513792 | 2018-01-17 02:09:53 -0800 | [diff] [blame] | 1998 | * Get the maximum number of connected audio devices. |
| 1999 | * |
| 2000 | * @return the maximum number of connected audio devices |
| 2001 | * @hide |
| 2002 | */ |
| 2003 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 2004 | public int getMaxConnectedAudioDevices() { |
| 2005 | try { |
| 2006 | mServiceLock.readLock().lock(); |
| 2007 | if (mService != null) { |
| 2008 | return mService.getMaxConnectedAudioDevices(); |
| 2009 | } |
| 2010 | } catch (RemoteException e) { |
| 2011 | Log.e(TAG, "failed to get getMaxConnectedAudioDevices, error: ", e); |
| 2012 | } finally { |
| 2013 | mServiceLock.readLock().unlock(); |
| 2014 | } |
| 2015 | return 1; |
| 2016 | } |
| 2017 | |
| 2018 | /** |
Prerepa Viswanadham | e593d0a | 2015-04-07 14:36:53 -0700 | [diff] [blame] | 2019 | * Return true if hardware has entries available for matching beacons |
| 2020 | * |
| 2021 | * @return true if there are hw entries available for matching beacons |
| 2022 | * @hide |
| 2023 | */ |
| 2024 | public boolean isHardwareTrackingFiltersAvailable() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2025 | if (!getLeAccess()) { |
| 2026 | return false; |
| 2027 | } |
Prerepa Viswanadham | e593d0a | 2015-04-07 14:36:53 -0700 | [diff] [blame] | 2028 | try { |
Prerepa Viswanadham | db1dbb8 | 2015-04-09 17:14:50 -0700 | [diff] [blame] | 2029 | IBluetoothGatt iGatt = mManagerService.getBluetoothGatt(); |
| 2030 | if (iGatt == null) { |
| 2031 | // BLE is not supported |
| 2032 | return false; |
Prerepa Viswanadham | e593d0a | 2015-04-07 14:36:53 -0700 | [diff] [blame] | 2033 | } |
Prerepa Viswanadham | db1dbb8 | 2015-04-09 17:14:50 -0700 | [diff] [blame] | 2034 | return (iGatt.numHwTrackFiltersAvailable() != 0); |
Prerepa Viswanadham | e593d0a | 2015-04-07 14:36:53 -0700 | [diff] [blame] | 2035 | } catch (RemoteException e) { |
| 2036 | Log.e(TAG, "", e); |
| 2037 | } |
| 2038 | return false; |
| 2039 | } |
| 2040 | |
| 2041 | /** |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2042 | * Return the record of {@link BluetoothActivityEnergyInfo} object that |
| 2043 | * has the activity and energy info. This can be used to ascertain what |
| 2044 | * the controller has been up to, since the last sample. |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2045 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2046 | * @param updateType Type of info, cached vs refreshed. |
| 2047 | * @return a record with {@link BluetoothActivityEnergyInfo} or null if report is unavailable or |
| 2048 | * unsupported |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2049 | * @hide |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2050 | * @deprecated use the asynchronous {@link #requestControllerActivityEnergyInfo(ResultReceiver)} |
| 2051 | * instead. |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2052 | */ |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 2053 | @Deprecated |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2054 | public BluetoothActivityEnergyInfo getControllerActivityEnergyInfo(int updateType) { |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 2055 | SynchronousResultReceiver receiver = new SynchronousResultReceiver(); |
Adam Lesinski | 991357f | 2016-05-10 14:00:03 -0700 | [diff] [blame] | 2056 | requestControllerActivityEnergyInfo(receiver); |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2057 | try { |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 2058 | SynchronousResultReceiver.Result result = receiver.awaitResult(1000); |
| 2059 | if (result.bundle != null) { |
| 2060 | return result.bundle.getParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY); |
| 2061 | } |
| 2062 | } catch (TimeoutException e) { |
| 2063 | Log.e(TAG, "getControllerActivityEnergyInfo timed out"); |
| 2064 | } |
| 2065 | return null; |
| 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * Request the record of {@link BluetoothActivityEnergyInfo} object that |
| 2070 | * has the activity and energy info. This can be used to ascertain what |
| 2071 | * the controller has been up to, since the last sample. |
| 2072 | * |
| 2073 | * A null value for the activity info object may be sent if the bluetooth service is |
| 2074 | * unreachable or the device does not support reporting such information. |
| 2075 | * |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 2076 | * @param result The callback to which to send the activity info. |
| 2077 | * @hide |
| 2078 | */ |
Adam Lesinski | 991357f | 2016-05-10 14:00:03 -0700 | [diff] [blame] | 2079 | public void requestControllerActivityEnergyInfo(ResultReceiver result) { |
Adam Lesinski | 010bf37 | 2016-04-11 12:18:18 -0700 | [diff] [blame] | 2080 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2081 | mServiceLock.readLock().lock(); |
| 2082 | if (mService != null) { |
| 2083 | mService.requestActivityInfo(result); |
| 2084 | result = null; |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2085 | } |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2086 | } catch (RemoteException e) { |
| 2087 | Log.e(TAG, "getControllerActivityEnergyInfoCallback: " + e); |
Adam Lesinski | 991357f | 2016-05-10 14:00:03 -0700 | [diff] [blame] | 2088 | } finally { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2089 | mServiceLock.readLock().unlock(); |
Adam Lesinski | 991357f | 2016-05-10 14:00:03 -0700 | [diff] [blame] | 2090 | if (result != null) { |
| 2091 | // Only send an immediate result if we failed. |
| 2092 | result.send(0, null); |
| 2093 | } |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2094 | } |
Prerepa Viswanadham | 8caac74 | 2014-07-22 17:00:09 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 2098 | * Return the set of {@link BluetoothDevice} objects that are bonded |
| 2099 | * (paired) to the local adapter. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 2100 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 2101 | * will return an empty set. After turning on Bluetooth, |
| 2102 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 2103 | * to get the updated value. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2104 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 2105 | * @return unmodifiable set of {@link BluetoothDevice}, or null on error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2106 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 2107 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2108 | public Set<BluetoothDevice> getBondedDevices() { |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 2109 | if (getState() != STATE_ON) { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 2110 | return toDeviceSet(new BluetoothDevice[0]); |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 2111 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2112 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2113 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2114 | if (mService != null) { |
| 2115 | return toDeviceSet(mService.getBondedDevices()); |
| 2116 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2117 | return toDeviceSet(new BluetoothDevice[0]); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2118 | } catch (RemoteException e) { |
| 2119 | Log.e(TAG, "", e); |
| 2120 | } finally { |
| 2121 | mServiceLock.readLock().unlock(); |
| 2122 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2123 | return null; |
| 2124 | } |
| 2125 | |
| 2126 | /** |
Bryce Lee | b1301fa | 2016-10-09 12:54:42 -0700 | [diff] [blame] | 2127 | * Gets the currently supported profiles by the adapter. |
| 2128 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2129 | * <p> This can be used to check whether a profile is supported before attempting |
Bryce Lee | b1301fa | 2016-10-09 12:54:42 -0700 | [diff] [blame] | 2130 | * to connect to its respective proxy. |
| 2131 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2132 | * @return a list of integers indicating the ids of supported profiles as defined in {@link |
| 2133 | * BluetoothProfile}. |
Bryce Lee | b1301fa | 2016-10-09 12:54:42 -0700 | [diff] [blame] | 2134 | * @hide |
| 2135 | */ |
Rahul Sabnis | 644d3dd | 2019-10-25 13:15:12 -0700 | [diff] [blame] | 2136 | public @NonNull List<Integer> getSupportedProfiles() { |
Bryce Lee | b1301fa | 2016-10-09 12:54:42 -0700 | [diff] [blame] | 2137 | final ArrayList<Integer> supportedProfiles = new ArrayList<Integer>(); |
| 2138 | |
| 2139 | try { |
| 2140 | synchronized (mManagerCallback) { |
| 2141 | if (mService != null) { |
| 2142 | final long supportedProfilesBitMask = mService.getSupportedProfiles(); |
| 2143 | |
| 2144 | for (int i = 0; i <= BluetoothProfile.MAX_PROFILE_ID; i++) { |
| 2145 | if ((supportedProfilesBitMask & (1 << i)) != 0) { |
| 2146 | supportedProfiles.add(i); |
| 2147 | } |
| 2148 | } |
Stanley Tng | 61dbd81 | 2019-01-13 16:04:31 -0800 | [diff] [blame] | 2149 | } else { |
| 2150 | // Bluetooth is disabled. Just fill in known supported Profiles |
| 2151 | if (isHearingAidProfileSupported()) { |
| 2152 | supportedProfiles.add(BluetoothProfile.HEARING_AID); |
| 2153 | } |
Bryce Lee | b1301fa | 2016-10-09 12:54:42 -0700 | [diff] [blame] | 2154 | } |
| 2155 | } |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 2156 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2157 | Log.e(TAG, "getSupportedProfiles:", e); |
Marie Janssen | cb21ad7 | 2016-12-13 10:51:02 -0800 | [diff] [blame] | 2158 | } |
Bryce Lee | b1301fa | 2016-10-09 12:54:42 -0700 | [diff] [blame] | 2159 | return supportedProfiles; |
| 2160 | } |
| 2161 | |
| 2162 | /** |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2163 | * Get the current connection state of the local Bluetooth adapter. |
| 2164 | * This can be used to check whether the local Bluetooth adapter is connected |
| 2165 | * to any profile of any other remote Bluetooth Device. |
| 2166 | * |
| 2167 | * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 2168 | * intent to get the connection state of the adapter. |
| 2169 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2170 | * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED}, {@link |
| 2171 | * #STATE_CONNECTING} or {@link #STATE_DISCONNECTED} |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2172 | * @hide |
| 2173 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2174 | @UnsupportedAppUsage |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2175 | public int getConnectionState() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2176 | if (getState() != STATE_ON) { |
| 2177 | return BluetoothAdapter.STATE_DISCONNECTED; |
| 2178 | } |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2179 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2180 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2181 | if (mService != null) { |
| 2182 | return mService.getAdapterConnectionState(); |
| 2183 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2184 | } catch (RemoteException e) { |
| 2185 | Log.e(TAG, "getConnectionState:", e); |
| 2186 | } finally { |
| 2187 | mServiceLock.readLock().unlock(); |
| 2188 | } |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 2189 | return BluetoothAdapter.STATE_DISCONNECTED; |
| 2190 | } |
| 2191 | |
| 2192 | /** |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2193 | * Get the current connection state of a profile. |
| 2194 | * This function can be used to check whether the local Bluetooth adapter |
| 2195 | * is connected to any remote device for a specific profile. |
Jack He | 6b73b32 | 2019-01-03 16:23:41 -0800 | [diff] [blame] | 2196 | * Profile can be one of {@link BluetoothProfile#HEADSET}, {@link BluetoothProfile#A2DP}. |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2197 | * |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2198 | * <p> Return value can be one of |
Jaikumar Ganesh | 9354790 | 2011-08-23 12:21:55 -0700 | [diff] [blame] | 2199 | * {@link BluetoothProfile#STATE_DISCONNECTED}, |
| 2200 | * {@link BluetoothProfile#STATE_CONNECTING}, |
| 2201 | * {@link BluetoothProfile#STATE_CONNECTED}, |
| 2202 | * {@link BluetoothProfile#STATE_DISCONNECTING} |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2203 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 2204 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2205 | public int getProfileConnectionState(int profile) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2206 | if (getState() != STATE_ON) { |
| 2207 | return BluetoothProfile.STATE_DISCONNECTED; |
| 2208 | } |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2209 | try { |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2210 | mServiceLock.readLock().lock(); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2211 | if (mService != null) { |
| 2212 | return mService.getProfileConnectionState(profile); |
| 2213 | } |
Jaikumar Ganesh | 9354790 | 2011-08-23 12:21:55 -0700 | [diff] [blame] | 2214 | } catch (RemoteException e) { |
| 2215 | Log.e(TAG, "getProfileConnectionState:", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2216 | } finally { |
| 2217 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 9354790 | 2011-08-23 12:21:55 -0700 | [diff] [blame] | 2218 | } |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 2219 | return BluetoothProfile.STATE_DISCONNECTED; |
| 2220 | } |
| 2221 | |
| 2222 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2223 | * Create a listening, secure RFCOMM Bluetooth socket. |
| 2224 | * <p>A remote device connecting to this socket will be authenticated and |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2225 | * communication on this socket will be encrypted. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2226 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2227 | * connections from a listening {@link BluetoothServerSocket}. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2228 | * <p>Valid RFCOMM channels are in range 1 to 30. |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2229 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2230 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2231 | * @param channel RFCOMM channel to listen on |
| 2232 | * @return a listening RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2233 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2234 | * permissions, or channel in use. |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2235 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2236 | */ |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2237 | public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException { |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 2238 | return listenUsingRfcommOn(channel, false, false); |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * Create a listening, secure RFCOMM Bluetooth socket. |
| 2243 | * <p>A remote device connecting to this socket will be authenticated and |
| 2244 | * communication on this socket will be encrypted. |
| 2245 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 2246 | * connections from a listening {@link BluetoothServerSocket}. |
| 2247 | * <p>Valid RFCOMM channels are in range 1 to 30. |
| 2248 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 2249 | * <p>To auto assign a channel without creating a SDP record use |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2250 | * {@link #SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as channel number. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2251 | * |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2252 | * @param channel RFCOMM channel to listen on |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2253 | * @param mitm enforce man-in-the-middle protection for authentication. |
| 2254 | * @param min16DigitPin enforce a pin key length og minimum 16 digit for sec mode 2 |
| 2255 | * connections. |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2256 | * @return a listening RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2257 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2258 | * permissions, or channel in use. |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2259 | * @hide |
| 2260 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2261 | @UnsupportedAppUsage |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 2262 | public BluetoothServerSocket listenUsingRfcommOn(int channel, boolean mitm, |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2263 | boolean min16DigitPin) throws IOException { |
| 2264 | BluetoothServerSocket socket = |
| 2265 | new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, true, true, channel, mitm, |
| 2266 | min16DigitPin); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2267 | int errno = socket.mSocket.bindListen(); |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2268 | if (channel == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2269 | socket.setChannel(socket.mSocket.getPort()); |
| 2270 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2271 | if (errno != 0) { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 2272 | //TODO(BT): Throw the same exception error code |
| 2273 | // that the previous code was using. |
| 2274 | //socket.mSocket.throwErrnoNative(errno); |
| 2275 | throw new IOException("Error: " + errno); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2276 | } |
| 2277 | return socket; |
| 2278 | } |
| 2279 | |
| 2280 | /** |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2281 | * Create a listening, secure RFCOMM Bluetooth socket with Service Record. |
| 2282 | * <p>A remote device connecting to this socket will be authenticated and |
| 2283 | * communication on this socket will be encrypted. |
| 2284 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 2285 | * connections from a listening {@link BluetoothServerSocket}. |
| 2286 | * <p>The system will assign an unused RFCOMM channel to listen on. |
| 2287 | * <p>The system will also register a Service Discovery |
| 2288 | * Protocol (SDP) record with the local SDP server containing the specified |
| 2289 | * UUID, service name, and auto-assigned channel. Remote Bluetooth devices |
| 2290 | * can use the same UUID to query our SDP server and discover which channel |
| 2291 | * to connect to. This SDP record will be removed when this socket is |
| 2292 | * closed, or if this application closes unexpectedly. |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 2293 | * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to |
| 2294 | * connect to this socket from another device using the same {@link UUID}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2295 | * |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2296 | * @param name service name for SDP record |
| 2297 | * @param uuid uuid for SDP record |
| 2298 | * @return a listening RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2299 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2300 | * permissions, or channel in use. |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2301 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 2302 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 2303 | public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid) |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2304 | throws IOException { |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2305 | return createNewRfcommSocketAndRecord(name, uuid, true, true); |
| 2306 | } |
| 2307 | |
| 2308 | /** |
| 2309 | * Create a listening, insecure RFCOMM Bluetooth socket with Service Record. |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2310 | * <p>The link key is not required to be authenticated, i.e the communication may be |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2311 | * vulnerable to Man In the Middle attacks. For Bluetooth 2.1 devices, |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2312 | * the link will be encrypted, as encryption is mandartory. |
| 2313 | * For legacy devices (pre Bluetooth 2.1 devices) the link will not |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2314 | * be encrypted. Use {@link #listenUsingRfcommWithServiceRecord}, if an |
| 2315 | * encrypted and authenticated communication channel is desired. |
| 2316 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 2317 | * connections from a listening {@link BluetoothServerSocket}. |
| 2318 | * <p>The system will assign an unused RFCOMM channel to listen on. |
| 2319 | * <p>The system will also register a Service Discovery |
| 2320 | * Protocol (SDP) record with the local SDP server containing the specified |
| 2321 | * UUID, service name, and auto-assigned channel. Remote Bluetooth devices |
| 2322 | * can use the same UUID to query our SDP server and discover which channel |
| 2323 | * to connect to. This SDP record will be removed when this socket is |
| 2324 | * closed, or if this application closes unexpectedly. |
| 2325 | * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to |
| 2326 | * connect to this socket from another device using the same {@link UUID}. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2327 | * |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2328 | * @param name service name for SDP record |
| 2329 | * @param uuid uuid for SDP record |
| 2330 | * @return a listening RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2331 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2332 | * permissions, or channel in use. |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2333 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 2334 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2335 | public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid) |
| 2336 | throws IOException { |
| 2337 | return createNewRfcommSocketAndRecord(name, uuid, false, false); |
| 2338 | } |
| 2339 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2340 | /** |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2341 | * Create a listening, encrypted, |
| 2342 | * RFCOMM Bluetooth socket with Service Record. |
| 2343 | * <p>The link will be encrypted, but the link key is not required to be authenticated |
| 2344 | * i.e the communication is vulnerable to Man In the Middle attacks. Use |
| 2345 | * {@link #listenUsingRfcommWithServiceRecord}, to ensure an authenticated link key. |
| 2346 | * <p> Use this socket if authentication of link key is not possible. |
| 2347 | * For example, for Bluetooth 2.1 devices, if any of the devices does not have |
| 2348 | * an input and output capability or just has the ability to display a numeric key, |
| 2349 | * a secure socket connection is not possible and this socket can be used. |
| 2350 | * Use {@link #listenUsingInsecureRfcommWithServiceRecord}, if encryption is not required. |
| 2351 | * For Bluetooth 2.1 devices, the link will be encrypted, as encryption is mandartory. |
| 2352 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 2353 | * Bluetooth Core Specification version 2.1 + EDR. |
| 2354 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 2355 | * connections from a listening {@link BluetoothServerSocket}. |
| 2356 | * <p>The system will assign an unused RFCOMM channel to listen on. |
| 2357 | * <p>The system will also register a Service Discovery |
| 2358 | * Protocol (SDP) record with the local SDP server containing the specified |
| 2359 | * UUID, service name, and auto-assigned channel. Remote Bluetooth devices |
| 2360 | * can use the same UUID to query our SDP server and discover which channel |
| 2361 | * to connect to. This SDP record will be removed when this socket is |
| 2362 | * closed, or if this application closes unexpectedly. |
| 2363 | * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to |
| 2364 | * connect to this socket from another device using the same {@link UUID}. |
| 2365 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2366 | * |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2367 | * @param name service name for SDP record |
| 2368 | * @param uuid uuid for SDP record |
| 2369 | * @return a listening RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2370 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 2371 | * permissions, or channel in use. |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2372 | * @hide |
| 2373 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2374 | @UnsupportedAppUsage |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2375 | public BluetoothServerSocket listenUsingEncryptedRfcommWithServiceRecord(String name, UUID uuid) |
| 2376 | throws IOException { |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2377 | return createNewRfcommSocketAndRecord(name, uuid, false, true); |
| 2378 | } |
| 2379 | |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 2380 | |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 2381 | private BluetoothServerSocket createNewRfcommSocketAndRecord(String name, UUID uuid, |
| 2382 | boolean auth, boolean encrypt) throws IOException { |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2383 | BluetoothServerSocket socket; |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2384 | socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, auth, encrypt, |
| 2385 | new ParcelUuid(uuid)); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 2386 | socket.setServiceName(name); |
| 2387 | int errno = socket.mSocket.bindListen(); |
| 2388 | if (errno != 0) { |
| 2389 | //TODO(BT): Throw the same exception error code |
| 2390 | // that the previous code was using. |
| 2391 | //socket.mSocket.throwErrnoNative(errno); |
| 2392 | throw new IOException("Error: " + errno); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2393 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2394 | return socket; |
| 2395 | } |
| 2396 | |
| 2397 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2398 | * Construct an unencrypted, unauthenticated, RFCOMM server socket. |
| 2399 | * Call #accept to retrieve connections to this socket. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2400 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2401 | * @return An RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2402 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 2403 | * permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2404 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2405 | */ |
| 2406 | public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2407 | BluetoothServerSocket socket = |
| 2408 | new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, false, false, port); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2409 | int errno = socket.mSocket.bindListen(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2410 | if (port == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2411 | socket.setChannel(socket.mSocket.getPort()); |
| 2412 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2413 | if (errno != 0) { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 2414 | //TODO(BT): Throw the same exception error code |
| 2415 | // that the previous code was using. |
| 2416 | //socket.mSocket.throwErrnoNative(errno); |
| 2417 | throw new IOException("Error: " + errno); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2418 | } |
| 2419 | return socket; |
| 2420 | } |
| 2421 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2422 | /** |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2423 | * Construct an encrypted, RFCOMM server socket. |
| 2424 | * Call #accept to retrieve connections to this socket. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2425 | * |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2426 | * @return An RFCOMM BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2427 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 2428 | * permissions. |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2429 | * @hide |
| 2430 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2431 | public BluetoothServerSocket listenUsingEncryptedRfcommOn(int port) throws IOException { |
| 2432 | BluetoothServerSocket socket = |
| 2433 | new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, false, true, port); |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2434 | int errno = socket.mSocket.bindListen(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2435 | if (port == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2436 | socket.setChannel(socket.mSocket.getPort()); |
| 2437 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 2438 | if (errno < 0) { |
| 2439 | //TODO(BT): Throw the same exception error code |
| 2440 | // that the previous code was using. |
| 2441 | //socket.mSocket.throwErrnoNative(errno); |
| 2442 | throw new IOException("Error: " + errno); |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 2443 | } |
| 2444 | return socket; |
| 2445 | } |
| 2446 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2447 | /** |
| 2448 | * Construct a SCO server socket. |
| 2449 | * Call #accept to retrieve connections to this socket. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2450 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2451 | * @return A SCO BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2452 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 2453 | * permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 2454 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2455 | */ |
| 2456 | public static BluetoothServerSocket listenUsingScoOn() throws IOException { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2457 | BluetoothServerSocket socket = |
| 2458 | new BluetoothServerSocket(BluetoothSocket.TYPE_SCO, false, false, -1); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 2459 | int errno = socket.mSocket.bindListen(); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 2460 | if (errno < 0) { |
| 2461 | //TODO(BT): Throw the same exception error code |
| 2462 | // that the previous code was using. |
| 2463 | //socket.mSocket.throwErrnoNative(errno); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2464 | } |
| 2465 | return socket; |
| 2466 | } |
| 2467 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 2468 | /** |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2469 | * Construct an encrypted, authenticated, L2CAP server socket. |
| 2470 | * Call #accept to retrieve connections to this socket. |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2471 | * <p>To auto assign a port without creating a SDP record use |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2472 | * {@link #SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as port number. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2473 | * |
| 2474 | * @param port the PSM to listen on |
| 2475 | * @param mitm enforce man-in-the-middle protection for authentication. |
| 2476 | * @param min16DigitPin enforce a pin key length og minimum 16 digit for sec mode 2 |
| 2477 | * connections. |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2478 | * @return An L2CAP BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2479 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 2480 | * permissions. |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2481 | * @hide |
| 2482 | */ |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 2483 | public BluetoothServerSocket listenUsingL2capOn(int port, boolean mitm, boolean min16DigitPin) |
| 2484 | throws IOException { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2485 | BluetoothServerSocket socket = |
| 2486 | new BluetoothServerSocket(BluetoothSocket.TYPE_L2CAP, true, true, port, mitm, |
| 2487 | min16DigitPin); |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2488 | int errno = socket.mSocket.bindListen(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2489 | if (port == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2490 | int assignedChannel = socket.mSocket.getPort(); |
| 2491 | if (DBG) Log.d(TAG, "listenUsingL2capOn: set assigned channel to " + assignedChannel); |
| 2492 | socket.setChannel(assignedChannel); |
Casper Bonde | 238e0f9 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 2493 | } |
| 2494 | if (errno != 0) { |
| 2495 | //TODO(BT): Throw the same exception error code |
| 2496 | // that the previous code was using. |
| 2497 | //socket.mSocket.throwErrnoNative(errno); |
| 2498 | throw new IOException("Error: " + errno); |
| 2499 | } |
| 2500 | return socket; |
| 2501 | } |
| 2502 | |
| 2503 | /** |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2504 | * Construct an encrypted, authenticated, L2CAP server socket. |
| 2505 | * Call #accept to retrieve connections to this socket. |
| 2506 | * <p>To auto assign a port without creating a SDP record use |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2507 | * {@link #SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as port number. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2508 | * |
| 2509 | * @param port the PSM to listen on |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2510 | * @return An L2CAP BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2511 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 2512 | * permissions. |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2513 | * @hide |
| 2514 | */ |
| 2515 | public BluetoothServerSocket listenUsingL2capOn(int port) throws IOException { |
Casper Bonde | 3b3d1fe | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 2516 | return listenUsingL2capOn(port, false, false); |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2517 | } |
| 2518 | |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 2519 | |
| 2520 | /** |
| 2521 | * Construct an insecure L2CAP server socket. |
| 2522 | * Call #accept to retrieve connections to this socket. |
| 2523 | * <p>To auto assign a port without creating a SDP record use |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2524 | * {@link #SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as port number. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2525 | * |
| 2526 | * @param port the PSM to listen on |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 2527 | * @return An L2CAP BluetoothServerSocket |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2528 | * @throws IOException On error, for example Bluetooth not available, or insufficient |
| 2529 | * permissions. |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 2530 | * @hide |
| 2531 | */ |
| 2532 | public BluetoothServerSocket listenUsingInsecureL2capOn(int port) throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2533 | Log.d(TAG, "listenUsingInsecureL2capOn: port=" + port); |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2534 | BluetoothServerSocket socket = |
| 2535 | new BluetoothServerSocket(BluetoothSocket.TYPE_L2CAP, false, false, port, false, |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2536 | false); |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 2537 | int errno = socket.mSocket.bindListen(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2538 | if (port == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 2539 | int assignedChannel = socket.mSocket.getPort(); |
| 2540 | if (DBG) { |
| 2541 | Log.d(TAG, "listenUsingInsecureL2capOn: set assigned channel to " |
| 2542 | + assignedChannel); |
| 2543 | } |
| 2544 | socket.setChannel(assignedChannel); |
Hemant Gupta | ef3e0db | 2017-02-03 16:38:59 +0530 | [diff] [blame] | 2545 | } |
| 2546 | if (errno != 0) { |
| 2547 | //TODO(BT): Throw the same exception error code |
| 2548 | // that the previous code was using. |
| 2549 | //socket.mSocket.throwErrnoNative(errno); |
| 2550 | throw new IOException("Error: " + errno); |
| 2551 | } |
| 2552 | return socket; |
| 2553 | |
| 2554 | } |
| 2555 | |
Casper Bonde | 2328423 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 2556 | /** |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 2557 | * Read the local Out of Band Pairing Data |
| 2558 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 2559 | * |
| 2560 | * @return Pair<byte[], byte[]> of Hash and Randomizer |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 2561 | * @hide |
| 2562 | */ |
| 2563 | public Pair<byte[], byte[]> readOutOfBandData() { |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 2564 | return null; |
| 2565 | } |
| 2566 | |
Scott Main | 299ae67 | 2011-01-19 21:13:18 -0800 | [diff] [blame] | 2567 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2568 | * Get the profile proxy object associated with the profile. |
| 2569 | * |
Jack He | 6b73b32 | 2019-01-03 16:23:41 -0800 | [diff] [blame] | 2570 | * <p>Profile can be one of {@link BluetoothProfile#HEADSET}, {@link BluetoothProfile#A2DP}, |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 2571 | * {@link BluetoothProfile#GATT}, {@link BluetoothProfile#HEARING_AID}, or {@link |
| 2572 | * BluetoothProfile#GATT_SERVER}. Clients must implement {@link |
| 2573 | * BluetoothProfile.ServiceListener} to get notified of the connection status and to get the |
| 2574 | * proxy object. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2575 | * |
| 2576 | * @param context Context of the application |
| 2577 | * @param listener The service Listener for connection callbacks. |
Jack He | 6b73b32 | 2019-01-03 16:23:41 -0800 | [diff] [blame] | 2578 | * @param profile The Bluetooth profile; either {@link BluetoothProfile#HEADSET}, |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 2579 | * {@link BluetoothProfile#A2DP}, {@link BluetoothProfile#GATT}, {@link |
| 2580 | * BluetoothProfile#HEARING_AID} or {@link BluetoothProfile#GATT_SERVER}. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2581 | * @return true on success, false on error |
| 2582 | */ |
| 2583 | public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2584 | int profile) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2585 | if (context == null || listener == null) { |
| 2586 | return false; |
| 2587 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2588 | |
| 2589 | if (profile == BluetoothProfile.HEADSET) { |
| 2590 | BluetoothHeadset headset = new BluetoothHeadset(context, listener); |
| 2591 | return true; |
| 2592 | } else if (profile == BluetoothProfile.A2DP) { |
| 2593 | BluetoothA2dp a2dp = new BluetoothA2dp(context, listener); |
| 2594 | return true; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 2595 | } else if (profile == BluetoothProfile.A2DP_SINK) { |
| 2596 | BluetoothA2dpSink a2dpSink = new BluetoothA2dpSink(context, listener); |
| 2597 | return true; |
Mike Lockwood | 94b59de | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 2598 | } else if (profile == BluetoothProfile.AVRCP_CONTROLLER) { |
| 2599 | BluetoothAvrcpController avrcp = new BluetoothAvrcpController(context, listener); |
| 2600 | return true; |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 2601 | } else if (profile == BluetoothProfile.HID_HOST) { |
| 2602 | BluetoothHidHost iDev = new BluetoothHidHost(context, listener); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 2603 | return true; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 2604 | } else if (profile == BluetoothProfile.PAN) { |
| 2605 | BluetoothPan pan = new BluetoothPan(context, listener); |
| 2606 | return true; |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 2607 | } else if (profile == BluetoothProfile.HEALTH) { |
Jack He | 6b73b32 | 2019-01-03 16:23:41 -0800 | [diff] [blame] | 2608 | Log.e(TAG, "getProfileProxy(): BluetoothHealth is deprecated"); |
| 2609 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 2610 | } else if (profile == BluetoothProfile.MAP) { |
| 2611 | BluetoothMap map = new BluetoothMap(context, listener); |
| 2612 | return true; |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 2613 | } else if (profile == BluetoothProfile.HEADSET_CLIENT) { |
| 2614 | BluetoothHeadsetClient headsetClient = new BluetoothHeadsetClient(context, listener); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 2615 | return true; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 2616 | } else if (profile == BluetoothProfile.SAP) { |
| 2617 | BluetoothSap sap = new BluetoothSap(context, listener); |
| 2618 | return true; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 2619 | } else if (profile == BluetoothProfile.PBAP_CLIENT) { |
| 2620 | BluetoothPbapClient pbapClient = new BluetoothPbapClient(context, listener); |
| 2621 | return true; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 2622 | } else if (profile == BluetoothProfile.MAP_CLIENT) { |
| 2623 | BluetoothMapClient mapClient = new BluetoothMapClient(context, listener); |
| 2624 | return true; |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 2625 | } else if (profile == BluetoothProfile.HID_DEVICE) { |
| 2626 | BluetoothHidDevice hidDevice = new BluetoothHidDevice(context, listener); |
Ivan Podogov | 5297dba | 2016-12-30 14:35:09 +0000 | [diff] [blame] | 2627 | return true; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 2628 | } else if (profile == BluetoothProfile.HEARING_AID) { |
Stanley Tng | 61dbd81 | 2019-01-13 16:04:31 -0800 | [diff] [blame] | 2629 | if (isHearingAidProfileSupported()) { |
| 2630 | BluetoothHearingAid hearingAid = new BluetoothHearingAid(context, listener); |
| 2631 | return true; |
| 2632 | } |
| 2633 | return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2634 | } else { |
| 2635 | return false; |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | /** |
| 2640 | * Close the connection of the profile proxy to the Service. |
| 2641 | * |
| 2642 | * <p> Clients should call this when they are no longer using |
| 2643 | * the proxy obtained from {@link #getProfileProxy}. |
Jack He | 6b73b32 | 2019-01-03 16:23:41 -0800 | [diff] [blame] | 2644 | * Profile can be one of {@link BluetoothProfile#HEADSET} or {@link BluetoothProfile#A2DP} |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2645 | * |
| 2646 | * @param profile |
| 2647 | * @param proxy Profile proxy object |
| 2648 | */ |
| 2649 | public void closeProfileProxy(int profile, BluetoothProfile proxy) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2650 | if (proxy == null) { |
| 2651 | return; |
| 2652 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 2653 | |
| 2654 | switch (profile) { |
| 2655 | case BluetoothProfile.HEADSET: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2656 | BluetoothHeadset headset = (BluetoothHeadset) proxy; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2657 | headset.close(); |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 2658 | break; |
| 2659 | case BluetoothProfile.A2DP: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2660 | BluetoothA2dp a2dp = (BluetoothA2dp) proxy; |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 2661 | a2dp.close(); |
| 2662 | break; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 2663 | case BluetoothProfile.A2DP_SINK: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2664 | BluetoothA2dpSink a2dpSink = (BluetoothA2dpSink) proxy; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 2665 | a2dpSink.close(); |
| 2666 | break; |
Mike Lockwood | 94b59de | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 2667 | case BluetoothProfile.AVRCP_CONTROLLER: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2668 | BluetoothAvrcpController avrcp = (BluetoothAvrcpController) proxy; |
Mike Lockwood | 94b59de | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 2669 | avrcp.close(); |
| 2670 | break; |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 2671 | case BluetoothProfile.HID_HOST: |
| 2672 | BluetoothHidHost iDev = (BluetoothHidHost) proxy; |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 2673 | iDev.close(); |
| 2674 | break; |
| 2675 | case BluetoothProfile.PAN: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2676 | BluetoothPan pan = (BluetoothPan) proxy; |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 2677 | pan.close(); |
| 2678 | break; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2679 | case BluetoothProfile.GATT: |
| 2680 | BluetoothGatt gatt = (BluetoothGatt) proxy; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 2681 | gatt.close(); |
| 2682 | break; |
| 2683 | case BluetoothProfile.GATT_SERVER: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2684 | BluetoothGattServer gattServer = (BluetoothGattServer) proxy; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 2685 | gattServer.close(); |
| 2686 | break; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 2687 | case BluetoothProfile.MAP: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2688 | BluetoothMap map = (BluetoothMap) proxy; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 2689 | map.close(); |
| 2690 | break; |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 2691 | case BluetoothProfile.HEADSET_CLIENT: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2692 | BluetoothHeadsetClient headsetClient = (BluetoothHeadsetClient) proxy; |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 2693 | headsetClient.close(); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 2694 | break; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 2695 | case BluetoothProfile.SAP: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2696 | BluetoothSap sap = (BluetoothSap) proxy; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 2697 | sap.close(); |
| 2698 | break; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 2699 | case BluetoothProfile.PBAP_CLIENT: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2700 | BluetoothPbapClient pbapClient = (BluetoothPbapClient) proxy; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 2701 | pbapClient.close(); |
| 2702 | break; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 2703 | case BluetoothProfile.MAP_CLIENT: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2704 | BluetoothMapClient mapClient = (BluetoothMapClient) proxy; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 2705 | mapClient.close(); |
| 2706 | break; |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 2707 | case BluetoothProfile.HID_DEVICE: |
| 2708 | BluetoothHidDevice hidDevice = (BluetoothHidDevice) proxy; |
| 2709 | hidDevice.close(); |
Ivan Podogov | 5297dba | 2016-12-30 14:35:09 +0000 | [diff] [blame] | 2710 | break; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 2711 | case BluetoothProfile.HEARING_AID: |
| 2712 | BluetoothHearingAid hearingAid = (BluetoothHearingAid) proxy; |
| 2713 | hearingAid.close(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 2714 | } |
| 2715 | } |
| 2716 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 2717 | private final IBluetoothManagerCallback mManagerCallback = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2718 | new IBluetoothManagerCallback.Stub() { |
| 2719 | public void onBluetoothServiceUp(IBluetooth bluetoothService) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2720 | if (DBG) { |
| 2721 | Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService); |
| 2722 | } |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2723 | |
Pavlin Radoslavov | e957a8a | 2016-05-24 15:28:41 -0700 | [diff] [blame] | 2724 | mServiceLock.writeLock().lock(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2725 | mService = bluetoothService; |
Pavlin Radoslavov | e957a8a | 2016-05-24 15:28:41 -0700 | [diff] [blame] | 2726 | mServiceLock.writeLock().unlock(); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2727 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2728 | synchronized (mProxyServiceStateCallbacks) { |
| 2729 | for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks) { |
| 2730 | try { |
| 2731 | if (cb != null) { |
| 2732 | cb.onBluetoothServiceUp(bluetoothService); |
| 2733 | } else { |
| 2734 | Log.d(TAG, "onBluetoothServiceUp: cb is null!"); |
| 2735 | } |
| 2736 | } catch (Exception e) { |
| 2737 | Log.e(TAG, "", e); |
Pavlin Radoslavov | eb50a39 | 2016-05-22 22:16:41 -0700 | [diff] [blame] | 2738 | } |
Matthew Xie | 60675b2 | 2015-02-06 14:09:54 -0800 | [diff] [blame] | 2739 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 2740 | } |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 2741 | synchronized (sMetadataListeners) { |
| 2742 | sMetadataListeners.forEach((device, pair) -> { |
| 2743 | try { |
| 2744 | mService.registerMetadataListener(sBluetoothMetadataListener, |
| 2745 | device); |
| 2746 | } catch (RemoteException e) { |
| 2747 | Log.e(TAG, "Failed to register metadata listener", e); |
| 2748 | } |
| 2749 | }); |
| 2750 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2751 | } |
Nitin Arora | d055adb | 2015-03-02 15:03:51 -0800 | [diff] [blame] | 2752 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2753 | public void onBluetoothServiceDown() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2754 | if (DBG) { |
| 2755 | Log.d(TAG, "onBluetoothServiceDown: " + mService); |
| 2756 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2757 | |
| 2758 | try { |
| 2759 | mServiceLock.writeLock().lock(); |
| 2760 | mService = null; |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2761 | if (mLeScanClients != null) { |
| 2762 | mLeScanClients.clear(); |
| 2763 | } |
| 2764 | if (sBluetoothLeAdvertiser != null) { |
| 2765 | sBluetoothLeAdvertiser.cleanup(); |
| 2766 | } |
| 2767 | if (sBluetoothLeScanner != null) { |
| 2768 | sBluetoothLeScanner.cleanup(); |
| 2769 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2770 | } finally { |
| 2771 | mServiceLock.writeLock().unlock(); |
| 2772 | } |
| 2773 | |
| 2774 | synchronized (mProxyServiceStateCallbacks) { |
| 2775 | for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks) { |
| 2776 | try { |
| 2777 | if (cb != null) { |
| 2778 | cb.onBluetoothServiceDown(); |
| 2779 | } else { |
| 2780 | Log.d(TAG, "onBluetoothServiceDown: cb is null!"); |
| 2781 | } |
| 2782 | } catch (Exception e) { |
| 2783 | Log.e(TAG, "", e); |
| 2784 | } |
| 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | public void onBrEdrDown() { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2790 | if (VDBG) { |
| 2791 | Log.i(TAG, "onBrEdrDown: " + mService); |
| 2792 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2793 | } |
| 2794 | }; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2795 | |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 2796 | /** |
Martijn Coenen | 6c614b7 | 2012-04-18 13:01:15 -0700 | [diff] [blame] | 2797 | * Enable the Bluetooth Adapter, but don't auto-connect devices |
| 2798 | * and don't persist state. Only for use by system applications. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2799 | * |
Martijn Coenen | 6c614b7 | 2012-04-18 13:01:15 -0700 | [diff] [blame] | 2800 | * @hide |
| 2801 | */ |
Selim Gurun | 64bd35e | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 2802 | @SystemApi |
| 2803 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) |
Martijn Coenen | 6c614b7 | 2012-04-18 13:01:15 -0700 | [diff] [blame] | 2804 | public boolean enableNoAutoConnect() { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 2805 | if (isEnabled()) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2806 | if (DBG) { |
| 2807 | Log.d(TAG, "enableNoAutoConnect(): BT already enabled!"); |
| 2808 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 2809 | return true; |
| 2810 | } |
| 2811 | try { |
Marie Janssen | 5980456 | 2016-12-28 14:13:21 -0800 | [diff] [blame] | 2812 | return mManagerService.enableNoAutoConnect(ActivityThread.currentPackageName()); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2813 | } catch (RemoteException e) { |
| 2814 | Log.e(TAG, "", e); |
| 2815 | } |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 2816 | return false; |
Martijn Coenen | 6c614b7 | 2012-04-18 13:01:15 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | /** |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 2820 | * Enable control of the Bluetooth Adapter for a single application. |
| 2821 | * |
| 2822 | * <p>Some applications need to use Bluetooth for short periods of time to |
| 2823 | * transfer data but don't want all the associated implications like |
| 2824 | * automatic connection to headsets etc. |
| 2825 | * |
| 2826 | * <p> Multiple applications can call this. This is reference counted and |
| 2827 | * Bluetooth disabled only when no one else is using it. There will be no UI |
| 2828 | * shown to the user while bluetooth is being enabled. Any user action will |
| 2829 | * override this call. For example, if user wants Bluetooth on and the last |
| 2830 | * user of this API wanted to disable Bluetooth, Bluetooth will not be |
| 2831 | * turned off. |
| 2832 | * |
| 2833 | * <p> This API is only meant to be used by internal applications. Third |
| 2834 | * party applications but use {@link #enable} and {@link #disable} APIs. |
| 2835 | * |
| 2836 | * <p> If this API returns true, it means the callback will be called. |
| 2837 | * The callback will be called with the current state of Bluetooth. |
| 2838 | * If the state is not what was requested, an internal error would be the |
Jaikumar Ganesh | f5fb6c8 | 2011-08-03 14:17:22 -0700 | [diff] [blame] | 2839 | * reason. If Bluetooth is already on and if this function is called to turn |
| 2840 | * it on, the api will return true and a callback will be called. |
| 2841 | * |
| 2842 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 2843 | * |
| 2844 | * @param on True for on, false for off. |
| 2845 | * @param callback The callback to notify changes to the state. |
| 2846 | * @hide |
| 2847 | */ |
| 2848 | public boolean changeApplicationBluetoothState(boolean on, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2849 | BluetoothStateChangeCallback callback) { |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 2850 | return false; |
| 2851 | } |
| 2852 | |
| 2853 | /** |
| 2854 | * @hide |
| 2855 | */ |
| 2856 | public interface BluetoothStateChangeCallback { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2857 | /** |
| 2858 | * @hide |
| 2859 | */ |
| 2860 | void onBluetoothStateChange(boolean on); |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | /** |
| 2864 | * @hide |
| 2865 | */ |
| 2866 | public class StateChangeCallbackWrapper extends IBluetoothStateChangeCallback.Stub { |
| 2867 | private BluetoothStateChangeCallback mCallback; |
| 2868 | |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2869 | StateChangeCallbackWrapper(BluetoothStateChangeCallback callback) { |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 2870 | mCallback = callback; |
| 2871 | } |
| 2872 | |
| 2873 | @Override |
| 2874 | public void onBluetoothStateChange(boolean on) { |
| 2875 | mCallback.onBluetoothStateChange(on); |
| 2876 | } |
| 2877 | } |
| 2878 | |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 2879 | private Set<BluetoothDevice> toDeviceSet(BluetoothDevice[] devices) { |
| 2880 | Set<BluetoothDevice> deviceSet = new HashSet<BluetoothDevice>(Arrays.asList(devices)); |
| 2881 | return Collections.unmodifiableSet(deviceSet); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 2882 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 2883 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2884 | protected void finalize() throws Throwable { |
| 2885 | try { |
| 2886 | mManagerService.unregisterAdapter(mManagerCallback); |
| 2887 | } catch (RemoteException e) { |
| 2888 | Log.e(TAG, "", e); |
| 2889 | } finally { |
| 2890 | super.finalize(); |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 2895 | /** |
Nick Pelly | 75596b4 | 2011-12-07 15:03:55 -0800 | [diff] [blame] | 2896 | * Validate a String Bluetooth address, such as "00:43:A8:23:10:F0" |
Nick Pelly | 55e66f1 | 2009-09-18 11:37:06 -0700 | [diff] [blame] | 2897 | * <p>Alphabetic characters must be uppercase to be valid. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 2898 | * |
| 2899 | * @param address Bluetooth address as string |
| 2900 | * @return true if the address is valid, false otherwise |
| 2901 | */ |
| 2902 | public static boolean checkBluetoothAddress(String address) { |
| 2903 | if (address == null || address.length() != ADDRESS_LENGTH) { |
| 2904 | return false; |
| 2905 | } |
| 2906 | for (int i = 0; i < ADDRESS_LENGTH; i++) { |
| 2907 | char c = address.charAt(i); |
| 2908 | switch (i % 3) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2909 | case 0: |
| 2910 | case 1: |
| 2911 | if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) { |
| 2912 | // hex character, OK |
| 2913 | break; |
| 2914 | } |
| 2915 | return false; |
| 2916 | case 2: |
| 2917 | if (c == ':') { |
| 2918 | break; // OK |
| 2919 | } |
| 2920 | return false; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 2921 | } |
| 2922 | } |
| 2923 | return true; |
| 2924 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2925 | |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2926 | @UnsupportedAppUsage |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2927 | /*package*/ IBluetoothManager getBluetoothManager() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2928 | return mManagerService; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 2931 | private final ArrayList<IBluetoothManagerCallback> mProxyServiceStateCallbacks = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2932 | new ArrayList<IBluetoothManagerCallback>(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 2933 | |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 2934 | @UnsupportedAppUsage |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 2935 | /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) { |
Matthew Xie | 60675b2 | 2015-02-06 14:09:54 -0800 | [diff] [blame] | 2936 | synchronized (mProxyServiceStateCallbacks) { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 2937 | if (cb == null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 2938 | Log.w(TAG, "getBluetoothService() called with no BluetoothManagerCallback"); |
| 2939 | } else if (!mProxyServiceStateCallbacks.contains(cb)) { |
| 2940 | mProxyServiceStateCallbacks.add(cb); |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | return mService; |
| 2944 | } |
| 2945 | |
| 2946 | /*package*/ void removeServiceStateCallback(IBluetoothManagerCallback cb) { |
Matthew Xie | 60675b2 | 2015-02-06 14:09:54 -0800 | [diff] [blame] | 2947 | synchronized (mProxyServiceStateCallbacks) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 2948 | mProxyServiceStateCallbacks.remove(cb); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 2949 | } |
| 2950 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2951 | |
| 2952 | /** |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2953 | * Callback interface used to deliver LE scan results. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2954 | * |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2955 | * @see #startLeScan(LeScanCallback) |
| 2956 | * @see #startLeScan(UUID[], LeScanCallback) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2957 | */ |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2958 | public interface LeScanCallback { |
| 2959 | /** |
| 2960 | * Callback reporting an LE device found during a device scan initiated |
| 2961 | * by the {@link BluetoothAdapter#startLeScan} function. |
| 2962 | * |
| 2963 | * @param device Identifies the remote device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2964 | * @param rssi The RSSI value for the remote device as reported by the Bluetooth hardware. 0 |
| 2965 | * if no RSSI value is available. |
| 2966 | * @param scanRecord The content of the advertisement record offered by the remote device. |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2967 | */ |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 2968 | void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | /** |
| 2972 | * Starts a scan for Bluetooth LE devices. |
| 2973 | * |
| 2974 | * <p>Results of the scan are reported using the |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2975 | * {@link LeScanCallback#onLeScan} callback. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2976 | * |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2977 | * @param callback the callback LE scan results are delivered |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2978 | * @return true, if the scan was started successfully |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 2979 | * @deprecated use {@link BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback)} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2980 | * instead. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2981 | */ |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 2982 | @Deprecated |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 2983 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2984 | public boolean startLeScan(LeScanCallback callback) { |
| 2985 | return startLeScan(null, callback); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /** |
| 2989 | * Starts a scan for Bluetooth LE devices, looking for devices that |
| 2990 | * advertise given services. |
| 2991 | * |
| 2992 | * <p>Devices which advertise all specified services are reported using the |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2993 | * {@link LeScanCallback#onLeScan} callback. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2994 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2995 | * @param serviceUuids Array of services to look for |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 2996 | * @param callback the callback LE scan results are delivered |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 2997 | * @return true, if the scan was started successfully |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 2998 | * @deprecated use {@link BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback)} |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 2999 | * instead. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3000 | */ |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 3001 | @Deprecated |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 3002 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3003 | public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3004 | if (DBG) { |
| 3005 | Log.d(TAG, "startLeScan(): " + Arrays.toString(serviceUuids)); |
| 3006 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3007 | if (callback == null) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3008 | if (DBG) { |
| 3009 | Log.e(TAG, "startLeScan: null callback"); |
| 3010 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3011 | return false; |
| 3012 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3013 | BluetoothLeScanner scanner = getBluetoothLeScanner(); |
| 3014 | if (scanner == null) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3015 | if (DBG) { |
| 3016 | Log.e(TAG, "startLeScan: cannot get BluetoothLeScanner"); |
| 3017 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3018 | return false; |
| 3019 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3020 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 3021 | synchronized (mLeScanClients) { |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3022 | if (mLeScanClients.containsKey(callback)) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3023 | if (DBG) { |
| 3024 | Log.e(TAG, "LE Scan has already started"); |
| 3025 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3026 | return false; |
| 3027 | } |
| 3028 | |
| 3029 | try { |
| 3030 | IBluetoothGatt iGatt = mManagerService.getBluetoothGatt(); |
Matthew Xie | 32ab77b | 2013-05-08 19:26:57 -0700 | [diff] [blame] | 3031 | if (iGatt == null) { |
| 3032 | // BLE is not supported |
| 3033 | return false; |
| 3034 | } |
| 3035 | |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3036 | ScanCallback scanCallback = new ScanCallback() { |
| 3037 | @Override |
| 3038 | public void onScanResult(int callbackType, ScanResult result) { |
| 3039 | if (callbackType != ScanSettings.CALLBACK_TYPE_ALL_MATCHES) { |
| 3040 | // Should not happen. |
| 3041 | Log.e(TAG, "LE Scan has already started"); |
| 3042 | return; |
| 3043 | } |
| 3044 | ScanRecord scanRecord = result.getScanRecord(); |
| 3045 | if (scanRecord == null) { |
| 3046 | return; |
| 3047 | } |
| 3048 | if (serviceUuids != null) { |
| 3049 | List<ParcelUuid> uuids = new ArrayList<ParcelUuid>(); |
| 3050 | for (UUID uuid : serviceUuids) { |
| 3051 | uuids.add(new ParcelUuid(uuid)); |
| 3052 | } |
| 3053 | List<ParcelUuid> scanServiceUuids = scanRecord.getServiceUuids(); |
| 3054 | if (scanServiceUuids == null || !scanServiceUuids.containsAll(uuids)) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3055 | if (DBG) { |
| 3056 | Log.d(TAG, "uuids does not match"); |
| 3057 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3058 | return; |
| 3059 | } |
| 3060 | } |
| 3061 | callback.onLeScan(result.getDevice(), result.getRssi(), |
| 3062 | scanRecord.getBytes()); |
| 3063 | } |
| 3064 | }; |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3065 | ScanSettings settings = new ScanSettings.Builder().setCallbackType( |
| 3066 | ScanSettings.CALLBACK_TYPE_ALL_MATCHES) |
| 3067 | .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) |
| 3068 | .build(); |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3069 | |
| 3070 | List<ScanFilter> filters = new ArrayList<ScanFilter>(); |
| 3071 | if (serviceUuids != null && serviceUuids.length > 0) { |
| 3072 | // Note scan filter does not support matching an UUID array so we put one |
| 3073 | // UUID to hardware and match the whole array in callback. |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3074 | ScanFilter filter = |
| 3075 | new ScanFilter.Builder().setServiceUuid(new ParcelUuid(serviceUuids[0])) |
| 3076 | .build(); |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3077 | filters.add(filter); |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3078 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3079 | scanner.startScan(filters, settings, scanCallback); |
| 3080 | |
| 3081 | mLeScanClients.put(callback, scanCallback); |
| 3082 | return true; |
| 3083 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3084 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 3085 | Log.e(TAG, "", e); |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | return false; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | /** |
| 3092 | * Stops an ongoing Bluetooth LE device scan. |
| 3093 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 3094 | * @param callback used to identify which scan to stop must be the same handle used to start the |
| 3095 | * scan |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 3096 | * @deprecated Use {@link BluetoothLeScanner#stopScan(ScanCallback)} instead. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3097 | */ |
Wei Wang | af74e66 | 2014-07-09 14:03:42 -0700 | [diff] [blame] | 3098 | @Deprecated |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 3099 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3100 | public void stopLeScan(LeScanCallback callback) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3101 | if (DBG) { |
| 3102 | Log.d(TAG, "stopLeScan()"); |
| 3103 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3104 | BluetoothLeScanner scanner = getBluetoothLeScanner(); |
| 3105 | if (scanner == null) { |
| 3106 | return; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3107 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3108 | synchronized (mLeScanClients) { |
| 3109 | ScanCallback scanCallback = mLeScanClients.remove(callback); |
| 3110 | if (scanCallback == null) { |
Myles Watson | 125b545 | 2017-11-27 16:41:47 -0800 | [diff] [blame] | 3111 | if (DBG) { |
| 3112 | Log.d(TAG, "scan not started yet"); |
| 3113 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3114 | return; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 3115 | } |
Wei Wang | c3059cc | 2014-07-23 23:34:00 -0700 | [diff] [blame] | 3116 | scanner.stopScan(scanCallback); |
Prerepa Viswanadham | 8f2e74c | 2014-07-09 12:51:59 -0700 | [diff] [blame] | 3117 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 3118 | } |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3119 | |
| 3120 | /** |
| 3121 | * Create a secure L2CAP Connection-oriented Channel (CoC) {@link BluetoothServerSocket} and |
| 3122 | * assign a dynamic protocol/service multiplexer (PSM) value. This socket can be used to listen |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3123 | * for incoming connections. The supported Bluetooth transport is LE only. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3124 | * <p>A remote device connecting to this socket will be authenticated and communication on this |
| 3125 | * socket will be encrypted. |
| 3126 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming connections from a listening |
| 3127 | * {@link BluetoothServerSocket}. |
Andrew Solovay | a44f2c07 | 2018-10-02 14:14:42 -0700 | [diff] [blame] | 3128 | * <p>The system will assign a dynamic PSM value. This PSM value can be read from the {@link |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3129 | * BluetoothServerSocket#getPsm()} and this value will be released when this server socket is |
| 3130 | * closed, Bluetooth is turned off, or the application exits unexpectedly. |
| 3131 | * <p>The mechanism of disclosing the assigned dynamic PSM value to the initiating peer is |
| 3132 | * defined and performed by the application. |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3133 | * <p>Use {@link BluetoothDevice#createL2capChannel(int)} to connect to this server |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3134 | * socket from another Android device that is given the PSM value. |
| 3135 | * |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3136 | * @return an L2CAP CoC BluetoothServerSocket |
| 3137 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 3138 | * permissions, or unable to start this CoC |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3139 | */ |
| 3140 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Stanley Tng | 97341da | 2019-03-12 10:19:49 -0700 | [diff] [blame] | 3141 | public @NonNull BluetoothServerSocket listenUsingL2capChannel() |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3142 | throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3143 | BluetoothServerSocket socket = |
| 3144 | new BluetoothServerSocket(BluetoothSocket.TYPE_L2CAP_LE, true, true, |
| 3145 | SOCKET_CHANNEL_AUTO_STATIC_NO_SDP, false, false); |
| 3146 | int errno = socket.mSocket.bindListen(); |
| 3147 | if (errno != 0) { |
| 3148 | throw new IOException("Error: " + errno); |
| 3149 | } |
| 3150 | |
| 3151 | int assignedPsm = socket.mSocket.getPort(); |
| 3152 | if (assignedPsm == 0) { |
| 3153 | throw new IOException("Error: Unable to assign PSM value"); |
| 3154 | } |
| 3155 | if (DBG) { |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3156 | Log.d(TAG, "listenUsingL2capChannel: set assigned PSM to " |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3157 | + assignedPsm); |
| 3158 | } |
| 3159 | socket.setChannel(assignedPsm); |
| 3160 | |
| 3161 | return socket; |
| 3162 | } |
| 3163 | |
| 3164 | /** |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3165 | * TODO: Remove this hidden method once all the SL4A and other tests are updated to use the new |
| 3166 | * API name, listenUsingL2capChannel. |
| 3167 | * @hide |
| 3168 | */ |
| 3169 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 3170 | public BluetoothServerSocket listenUsingL2capCoc(int transport) |
| 3171 | throws IOException { |
| 3172 | Log.e(TAG, "listenUsingL2capCoc: PLEASE USE THE OFFICIAL API, listenUsingL2capChannel"); |
| 3173 | return listenUsingL2capChannel(); |
| 3174 | } |
| 3175 | |
| 3176 | /** |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3177 | * Create an insecure L2CAP Connection-oriented Channel (CoC) {@link BluetoothServerSocket} and |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3178 | * assign a dynamic PSM value. This socket can be used to listen for incoming connections. The |
| 3179 | * supported Bluetooth transport is LE only. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3180 | * <p>The link key is not required to be authenticated, i.e the communication may be vulnerable |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3181 | * to man-in-the-middle attacks. Use {@link #listenUsingL2capChannel}, if an encrypted and |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3182 | * authenticated communication channel is desired. |
| 3183 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming connections from a listening |
| 3184 | * {@link BluetoothServerSocket}. |
| 3185 | * <p>The system will assign a dynamic protocol/service multiplexer (PSM) value. This PSM value |
Andrew Solovay | a44f2c07 | 2018-10-02 14:14:42 -0700 | [diff] [blame] | 3186 | * can be read from the {@link BluetoothServerSocket#getPsm()} and this value will be released |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3187 | * when this server socket is closed, Bluetooth is turned off, or the application exits |
| 3188 | * unexpectedly. |
| 3189 | * <p>The mechanism of disclosing the assigned dynamic PSM value to the initiating peer is |
| 3190 | * defined and performed by the application. |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3191 | * <p>Use {@link BluetoothDevice#createInsecureL2capChannel(int)} to connect to this server |
| 3192 | * socket from another Android device that is given the PSM value. |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3193 | * |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3194 | * @return an L2CAP CoC BluetoothServerSocket |
| 3195 | * @throws IOException on error, for example Bluetooth not available, or insufficient |
| 3196 | * permissions, or unable to start this CoC |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3197 | */ |
| 3198 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Stanley Tng | 97341da | 2019-03-12 10:19:49 -0700 | [diff] [blame] | 3199 | public @NonNull BluetoothServerSocket listenUsingInsecureL2capChannel() |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3200 | throws IOException { |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3201 | BluetoothServerSocket socket = |
| 3202 | new BluetoothServerSocket(BluetoothSocket.TYPE_L2CAP_LE, false, false, |
| 3203 | SOCKET_CHANNEL_AUTO_STATIC_NO_SDP, false, false); |
| 3204 | int errno = socket.mSocket.bindListen(); |
| 3205 | if (errno != 0) { |
| 3206 | throw new IOException("Error: " + errno); |
| 3207 | } |
| 3208 | |
| 3209 | int assignedPsm = socket.mSocket.getPort(); |
| 3210 | if (assignedPsm == 0) { |
| 3211 | throw new IOException("Error: Unable to assign PSM value"); |
| 3212 | } |
| 3213 | if (DBG) { |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3214 | Log.d(TAG, "listenUsingInsecureL2capChannel: set assigned PSM to " |
Stanley Tng | e48468d | 2017-11-22 16:04:40 -0800 | [diff] [blame] | 3215 | + assignedPsm); |
| 3216 | } |
| 3217 | socket.setChannel(assignedPsm); |
| 3218 | |
| 3219 | return socket; |
| 3220 | } |
Stanley Tng | 1bff4ba | 2018-06-29 14:05:04 -0700 | [diff] [blame] | 3221 | |
| 3222 | /** |
| 3223 | * TODO: Remove this hidden method once all the SL4A and other tests are updated to use the new |
| 3224 | * API name, listenUsingInsecureL2capChannel. |
| 3225 | * @hide |
| 3226 | */ |
| 3227 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 3228 | public BluetoothServerSocket listenUsingInsecureL2capCoc(int transport) |
| 3229 | throws IOException { |
| 3230 | Log.e(TAG, "listenUsingInsecureL2capCoc: PLEASE USE THE OFFICIAL API, " |
| 3231 | + "listenUsingInsecureL2capChannel"); |
| 3232 | return listenUsingInsecureL2capChannel(); |
| 3233 | } |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3234 | |
| 3235 | /** |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3236 | * Register a {@link #OnMetadataChangedListener} to receive update about metadata |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3237 | * changes for this {@link BluetoothDevice}. |
| 3238 | * Registration must be done when Bluetooth is ON and will last until |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3239 | * {@link #removeOnMetadataChangedListener(BluetoothDevice)} is called, even when Bluetooth |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3240 | * restarted in the middle. |
| 3241 | * All input parameters should not be null or {@link NullPointerException} will be triggered. |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3242 | * The same {@link BluetoothDevice} and {@link #OnMetadataChangedListener} pair can only be |
| 3243 | * registered once, double registration would cause {@link IllegalArgumentException}. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3244 | * |
| 3245 | * @param device {@link BluetoothDevice} that will be registered |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3246 | * @param executor the executor for listener callback |
| 3247 | * @param listener {@link #OnMetadataChangedListener} that will receive asynchronous callbacks |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3248 | * @return true on success, false on error |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3249 | * @throws NullPointerException If one of {@code listener}, {@code device} or {@code executor} |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3250 | * is null. |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3251 | * @throws IllegalArgumentException The same {@link #OnMetadataChangedListener} and |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3252 | * {@link BluetoothDevice} are registered twice. |
| 3253 | * @hide |
| 3254 | */ |
| 3255 | @SystemApi |
| 3256 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3257 | public boolean addOnMetadataChangedListener(@NonNull BluetoothDevice device, |
| 3258 | @NonNull Executor executor, @NonNull OnMetadataChangedListener listener) { |
| 3259 | if (DBG) Log.d(TAG, "addOnMetadataChangedListener()"); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3260 | |
| 3261 | final IBluetooth service = mService; |
| 3262 | if (service == null) { |
| 3263 | Log.e(TAG, "Bluetooth is not enabled. Cannot register metadata listener"); |
| 3264 | return false; |
| 3265 | } |
| 3266 | if (listener == null) { |
| 3267 | throw new NullPointerException("listener is null"); |
| 3268 | } |
| 3269 | if (device == null) { |
| 3270 | throw new NullPointerException("device is null"); |
| 3271 | } |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3272 | if (executor == null) { |
| 3273 | throw new NullPointerException("executor is null"); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | synchronized (sMetadataListeners) { |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3277 | List<Pair<OnMetadataChangedListener, Executor>> listenerList = |
| 3278 | sMetadataListeners.get(device); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3279 | if (listenerList == null) { |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3280 | // Create new listener/executor list for registeration |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3281 | listenerList = new ArrayList<>(); |
| 3282 | sMetadataListeners.put(device, listenerList); |
| 3283 | } else { |
| 3284 | // Check whether this device was already registed by the lisenter |
| 3285 | if (listenerList.stream().anyMatch((pair) -> (pair.first.equals(listener)))) { |
| 3286 | throw new IllegalArgumentException("listener was already regestered" |
| 3287 | + " for the device"); |
| 3288 | } |
| 3289 | } |
| 3290 | |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3291 | Pair<OnMetadataChangedListener, Executor> listenerPair = new Pair(listener, executor); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3292 | listenerList.add(listenerPair); |
| 3293 | |
| 3294 | boolean ret = false; |
| 3295 | try { |
| 3296 | ret = service.registerMetadataListener(sBluetoothMetadataListener, device); |
| 3297 | } catch (RemoteException e) { |
| 3298 | Log.e(TAG, "registerMetadataListener fail", e); |
| 3299 | } finally { |
| 3300 | if (!ret) { |
| 3301 | // Remove listener registered earlier when fail. |
| 3302 | listenerList.remove(listenerPair); |
| 3303 | if (listenerList.isEmpty()) { |
| 3304 | // Remove the device if its listener list is empty |
| 3305 | sMetadataListeners.remove(device); |
| 3306 | } |
| 3307 | } |
| 3308 | } |
| 3309 | return ret; |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | /** |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3314 | * Unregister a {@link #OnMetadataChangedListener} from a registered {@link BluetoothDevice}. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3315 | * Unregistration can be done when Bluetooth is either ON or OFF. |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3316 | * {@link #addOnMetadataChangedListener(OnMetadataChangedListener, BluetoothDevice, Executor)} |
| 3317 | * must be called before unregisteration. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3318 | * |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3319 | * @param device {@link BluetoothDevice} that will be unregistered. It |
| 3320 | * should not be null or {@link NullPointerException} will be triggered. |
| 3321 | * @param listener {@link OnMetadataChangedListener} that will be unregistered. It |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3322 | * should not be null or {@link NullPointerException} will be triggered. |
| 3323 | * @return true on success, false on error |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3324 | * @throws NullPointerException If {@code listener} or {@code device} is null. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3325 | * @throws IllegalArgumentException If {@code device} has not been registered before. |
| 3326 | * @hide |
| 3327 | */ |
| 3328 | @SystemApi |
| 3329 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3330 | public boolean removeOnMetadataChangedListener(@NonNull BluetoothDevice device, |
| 3331 | @NonNull OnMetadataChangedListener listener) { |
| 3332 | if (DBG) Log.d(TAG, "removeOnMetadataChangedListener()"); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3333 | if (device == null) { |
| 3334 | throw new NullPointerException("device is null"); |
| 3335 | } |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3336 | if (listener == null) { |
| 3337 | throw new NullPointerException("listener is null"); |
| 3338 | } |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3339 | |
| 3340 | synchronized (sMetadataListeners) { |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3341 | if (!sMetadataListeners.containsKey(device)) { |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3342 | throw new IllegalArgumentException("device was not registered"); |
| 3343 | } |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3344 | // Remove issued listener from the registered device |
| 3345 | sMetadataListeners.get(device).removeIf((pair) -> (pair.first.equals(listener))); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3346 | |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3347 | if (sMetadataListeners.get(device).isEmpty()) { |
| 3348 | // Unregister to Bluetooth service if all listeners are removed from |
| 3349 | // the registered device |
| 3350 | sMetadataListeners.remove(device); |
| 3351 | final IBluetooth service = mService; |
| 3352 | if (service == null) { |
| 3353 | // Bluetooth is OFF, do nothing to Bluetooth service. |
| 3354 | return true; |
| 3355 | } |
| 3356 | try { |
| 3357 | return service.unregisterMetadataListener(device); |
| 3358 | } catch (RemoteException e) { |
| 3359 | Log.e(TAG, "unregisterMetadataListener fail", e); |
| 3360 | return false; |
| 3361 | } |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3362 | } |
| 3363 | } |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3364 | return true; |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | /** |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3368 | * This interface is used to implement {@link BluetoothAdapter} metadata listener. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3369 | * @hide |
| 3370 | */ |
| 3371 | @SystemApi |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3372 | public interface OnMetadataChangedListener { |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3373 | /** |
| 3374 | * Callback triggered if the metadata of {@link BluetoothDevice} registered in |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3375 | * {@link #addOnMetadataChangedListener}. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3376 | * |
| 3377 | * @param device changed {@link BluetoothDevice}. |
| 3378 | * @param key changed metadata key, one of BluetoothDevice.METADATA_*. |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3379 | * @param value the new value of metadata as byte array. |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3380 | */ |
Ugo Yu | 50e3c08 | 2019-03-05 16:20:27 +0800 | [diff] [blame] | 3381 | void onMetadataChanged(@NonNull BluetoothDevice device, int key, |
| 3382 | @Nullable byte[] value); |
Ugo Yu | 4a33b88 | 2019-01-08 09:00:09 +0800 | [diff] [blame] | 3383 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 3384 | } |