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