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