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