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