Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 22 | import android.os.Binder; |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 23 | import android.os.IBinder; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 24 | import android.os.Message; |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 25 | import android.os.ParcelUuid; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 27 | import android.os.ServiceManager; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 29 | import android.util.Pair; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 30 | |
| 31 | import java.io.IOException; |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 32 | import java.lang.ref.WeakReference; |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 34 | import java.util.Arrays; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 35 | import java.util.Collections; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 36 | import java.util.HashSet; |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 37 | import java.util.HashMap; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 38 | import java.util.LinkedList; |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 39 | import java.util.Map; |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 40 | import java.util.Random; |
| 41 | import java.util.Set; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 42 | import java.util.UUID; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 45 | * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter} |
| 46 | * lets you perform fundamental Bluetooth tasks, such as initiate |
| 47 | * device discovery, query a list of bonded (paired) devices, |
| 48 | * instantiate a {@link BluetoothDevice} using a known MAC address, and create |
| 49 | * a {@link BluetoothServerSocket} to listen for connection requests from other |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 50 | * devices. |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 51 | * |
| 52 | * <p>To get a {@link BluetoothAdapter} representing the local Bluetooth |
| 53 | * adapter, call the static {@link #getDefaultAdapter} method. |
| 54 | * Fundamentally, this is your starting point for all |
| 55 | * Bluetooth actions. Once you have the local adapter, you can get a set of |
| 56 | * {@link BluetoothDevice} objects representing all paired devices with |
| 57 | * {@link #getBondedDevices()}; start device discovery with |
| 58 | * {@link #startDiscovery()}; or create a {@link BluetoothServerSocket} to |
| 59 | * listen for incoming connection requests with |
| 60 | * {@link #listenUsingRfcommWithServiceRecord(String,UUID)}. |
| 61 | * |
| 62 | * <p class="note"><strong>Note:</strong> |
| 63 | * Most methods require the {@link android.Manifest.permission#BLUETOOTH} |
| 64 | * permission and some also require the |
| 65 | * {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 66 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 67 | * <div class="special reference"> |
| 68 | * <h3>Developer Guides</h3> |
| 69 | * <p>For more information about using Bluetooth, read the |
| 70 | * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p> |
| 71 | * </div> |
| 72 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 73 | * {@see BluetoothDevice} |
| 74 | * {@see BluetoothServerSocket} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 75 | */ |
| 76 | public final class BluetoothAdapter { |
| 77 | private static final String TAG = "BluetoothAdapter"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 78 | private static final boolean DBG = true; |
Matthew Xie | 3b6214f | 2012-08-29 00:12:29 -0700 | [diff] [blame] | 79 | private static final boolean VDBG = false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 80 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 81 | /** |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 82 | * Sentinel error value for this class. Guaranteed to not equal any other |
| 83 | * integer constant in this class. Provided as a convenience for functions |
| 84 | * that require a sentinel error value, for example: |
| 85 | * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, |
| 86 | * BluetoothAdapter.ERROR)</code> |
| 87 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 88 | public static final int ERROR = Integer.MIN_VALUE; |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 91 | * Broadcast Action: The state of the local Bluetooth adapter has been |
| 92 | * changed. |
| 93 | * <p>For example, Bluetooth has been turned on or off. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 94 | * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 95 | * #EXTRA_PREVIOUS_STATE} containing the new and old states |
| 96 | * respectively. |
| 97 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 98 | */ |
| 99 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 100 | public static final String ACTION_STATE_CHANGED = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 101 | "android.bluetooth.adapter.action.STATE_CHANGED"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 102 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Used as an int extra field in {@link #ACTION_STATE_CHANGED} |
| 105 | * intents to request the current power state. Possible values are: |
| 106 | * {@link #STATE_OFF}, |
| 107 | * {@link #STATE_TURNING_ON}, |
| 108 | * {@link #STATE_ON}, |
| 109 | * {@link #STATE_TURNING_OFF}, |
| 110 | */ |
| 111 | public static final String EXTRA_STATE = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 112 | "android.bluetooth.adapter.extra.STATE"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Used as an int extra field in {@link #ACTION_STATE_CHANGED} |
| 115 | * intents to request the previous power state. Possible values are: |
| 116 | * {@link #STATE_OFF}, |
| 117 | * {@link #STATE_TURNING_ON}, |
| 118 | * {@link #STATE_ON}, |
| 119 | * {@link #STATE_TURNING_OFF}, |
| 120 | */ |
| 121 | public static final String EXTRA_PREVIOUS_STATE = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 122 | "android.bluetooth.adapter.extra.PREVIOUS_STATE"; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 123 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Indicates the local Bluetooth adapter is off. |
| 126 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 127 | public static final int STATE_OFF = 10; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Indicates the local Bluetooth adapter is turning on. However local |
| 130 | * clients should wait for {@link #STATE_ON} before attempting to |
| 131 | * use the adapter. |
| 132 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 133 | public static final int STATE_TURNING_ON = 11; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Indicates the local Bluetooth adapter is on, and ready for use. |
| 136 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 137 | public static final int STATE_ON = 12; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Indicates the local Bluetooth adapter is turning off. Local clients |
| 140 | * should immediately attempt graceful disconnection of any remote links. |
| 141 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 142 | public static final int STATE_TURNING_OFF = 13; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 145 | * Activity Action: Show a system activity that requests discoverable mode. |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 146 | * 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] | 147 | * is not currently enabled. |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 148 | * <p>Discoverable mode is equivalent to {@link |
| 149 | * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see |
| 150 | * this Bluetooth adapter when they perform a discovery. |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 151 | * <p>For privacy, Android is not discoverable by default. |
| 152 | * <p>The sender of this Intent can optionally use extra field {@link |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 153 | * #EXTRA_DISCOVERABLE_DURATION} to request the duration of |
| 154 | * discoverability. Currently the default duration is 120 seconds, and |
| 155 | * maximum duration is capped at 300 seconds for each request. |
| 156 | * <p>Notification of the result of this activity is posted using the |
| 157 | * {@link android.app.Activity#onActivityResult} callback. The |
| 158 | * <code>resultCode</code> |
Michael Chan | cdd2864 | 2009-11-05 18:29:01 -0800 | [diff] [blame] | 159 | * will be the duration (in seconds) of discoverability or |
| 160 | * {@link android.app.Activity#RESULT_CANCELED} if the user rejected |
| 161 | * discoverability or an error has occurred. |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 162 | * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED} |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 163 | * for global notification whenever the scan mode changes. For example, an |
| 164 | * application can be notified when the device has ended discoverability. |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 165 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 166 | */ |
| 167 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 168 | public static final String ACTION_REQUEST_DISCOVERABLE = |
| 169 | "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE"; |
| 170 | |
| 171 | /** |
| 172 | * Used as an optional int extra field in {@link |
| 173 | * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration |
| 174 | * for discoverability in seconds. The current default is 120 seconds, and |
| 175 | * requests over 300 seconds will be capped. These values could change. |
| 176 | */ |
| 177 | public static final String EXTRA_DISCOVERABLE_DURATION = |
| 178 | "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION"; |
| 179 | |
| 180 | /** |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 181 | * Activity Action: Show a system activity that allows the user to turn on |
| 182 | * Bluetooth. |
| 183 | * <p>This system activity will return once Bluetooth has completed turning |
| 184 | * on, or the user has decided not to turn Bluetooth on. |
| 185 | * <p>Notification of the result of this activity is posted using the |
| 186 | * {@link android.app.Activity#onActivityResult} callback. The |
| 187 | * <code>resultCode</code> |
Michael Chan | cdd2864 | 2009-11-05 18:29:01 -0800 | [diff] [blame] | 188 | * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been |
| 189 | * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user |
| 190 | * has rejected the request or an error has occurred. |
Nick Pelly | 1acdcc1 | 2009-09-28 10:33:55 -0700 | [diff] [blame] | 191 | * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED} |
| 192 | * for global notification whenever Bluetooth is turned on or off. |
| 193 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 194 | */ |
| 195 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 196 | public static final String ACTION_REQUEST_ENABLE = |
| 197 | "android.bluetooth.adapter.action.REQUEST_ENABLE"; |
| 198 | |
| 199 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 200 | * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter |
| 201 | * has changed. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 202 | * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 203 | * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes |
| 204 | * respectively. |
| 205 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 206 | */ |
| 207 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 208 | public static final String ACTION_SCAN_MODE_CHANGED = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 209 | "android.bluetooth.adapter.action.SCAN_MODE_CHANGED"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED} |
| 213 | * intents to request the current scan mode. Possible values are: |
| 214 | * {@link #SCAN_MODE_NONE}, |
| 215 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 216 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}, |
| 217 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 218 | 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] | 219 | /** |
| 220 | * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED} |
| 221 | * intents to request the previous scan mode. Possible values are: |
| 222 | * {@link #SCAN_MODE_NONE}, |
| 223 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 224 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}, |
| 225 | */ |
| 226 | public static final String EXTRA_PREVIOUS_SCAN_MODE = |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 227 | "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE"; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Indicates that both inquiry scan and page scan are disabled on the local |
| 231 | * Bluetooth adapter. Therefore this device is neither discoverable |
| 232 | * nor connectable from remote Bluetooth devices. |
| 233 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 234 | public static final int SCAN_MODE_NONE = 20; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Indicates that inquiry scan is disabled, but page scan is enabled on the |
| 237 | * local Bluetooth adapter. Therefore this device is not discoverable from |
| 238 | * remote Bluetooth devices, but is connectable from remote devices that |
| 239 | * have previously discovered this device. |
| 240 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 241 | public static final int SCAN_MODE_CONNECTABLE = 21; |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Indicates that both inquiry scan and page scan are enabled on the local |
| 244 | * Bluetooth adapter. Therefore this device is both discoverable and |
| 245 | * connectable from remote Bluetooth devices. |
| 246 | */ |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 247 | public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 248 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * Broadcast Action: The local Bluetooth adapter has started the remote |
| 252 | * device discovery process. |
| 253 | * <p>This usually involves an inquiry scan of about 12 seconds, followed |
| 254 | * by a page scan of each new device to retrieve its Bluetooth name. |
| 255 | * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as |
| 256 | * remote Bluetooth devices are found. |
| 257 | * <p>Device discovery is a heavyweight procedure. New connections to |
| 258 | * remote Bluetooth devices should not be attempted while discovery is in |
| 259 | * progress, and existing connections will experience limited bandwidth |
| 260 | * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing |
| 261 | * discovery. |
| 262 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 263 | */ |
| 264 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 265 | public static final String ACTION_DISCOVERY_STARTED = |
| 266 | "android.bluetooth.adapter.action.DISCOVERY_STARTED"; |
| 267 | /** |
| 268 | * Broadcast Action: The local Bluetooth adapter has finished the device |
| 269 | * discovery process. |
| 270 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 271 | */ |
| 272 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 273 | public static final String ACTION_DISCOVERY_FINISHED = |
| 274 | "android.bluetooth.adapter.action.DISCOVERY_FINISHED"; |
| 275 | |
| 276 | /** |
| 277 | * Broadcast Action: The local Bluetooth adapter has changed its friendly |
| 278 | * Bluetooth name. |
| 279 | * <p>This name is visible to remote Bluetooth devices. |
| 280 | * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing |
| 281 | * the name. |
| 282 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 283 | */ |
| 284 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 285 | public static final String ACTION_LOCAL_NAME_CHANGED = |
| 286 | "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED"; |
| 287 | /** |
| 288 | * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED} |
| 289 | * intents to request the local Bluetooth name. |
| 290 | */ |
| 291 | public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME"; |
| 292 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 293 | /** |
| 294 | * Intent used to broadcast the change in connection state of the local |
| 295 | * Bluetooth adapter to a profile of the remote device. When the adapter is |
| 296 | * not connected to any profiles of any remote devices and it attempts a |
| 297 | * connection to a profile this intent will sent. Once connected, this intent |
| 298 | * will not be sent for any more connection attempts to any profiles of any |
| 299 | * remote device. When the adapter disconnects from the last profile its |
| 300 | * connected to of any remote device, this intent will be sent. |
| 301 | * |
| 302 | * <p> This intent is useful for applications that are only concerned about |
| 303 | * whether the local adapter is connected to any profile of any device and |
| 304 | * are not really concerned about which profile. For example, an application |
| 305 | * which displays an icon to display whether Bluetooth is connected or not |
| 306 | * can use this intent. |
| 307 | * |
| 308 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | 0b5b35f | 2011-02-01 16:47:11 -0800 | [diff] [blame] | 309 | * {@link #EXTRA_CONNECTION_STATE} - The current connection state. |
| 310 | * {@link #EXTRA_PREVIOUS_CONNECTION_STATE}- The previous connection state. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 311 | * {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. |
| 312 | * |
Jaikumar Ganesh | 0b5b35f | 2011-02-01 16:47:11 -0800 | [diff] [blame] | 313 | * {@link #EXTRA_CONNECTION_STATE} or {@link #EXTRA_PREVIOUS_CONNECTION_STATE} |
| 314 | * can be any of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 315 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 316 | * |
| 317 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. |
| 318 | */ |
| 319 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 320 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 321 | "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED"; |
| 322 | |
| 323 | /** |
| 324 | * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 325 | * |
| 326 | * This extra represents the current connection state. |
| 327 | */ |
| 328 | public static final String EXTRA_CONNECTION_STATE = |
| 329 | "android.bluetooth.adapter.extra.CONNECTION_STATE"; |
| 330 | |
| 331 | /** |
| 332 | * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 333 | * |
| 334 | * This extra represents the previous connection state. |
| 335 | */ |
| 336 | public static final String EXTRA_PREVIOUS_CONNECTION_STATE = |
| 337 | "android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE"; |
| 338 | |
| 339 | /** The profile is in disconnected state */ |
| 340 | public static final int STATE_DISCONNECTED = 0; |
| 341 | /** The profile is in connecting state */ |
| 342 | public static final int STATE_CONNECTING = 1; |
| 343 | /** The profile is in connected state */ |
| 344 | public static final int STATE_CONNECTED = 2; |
| 345 | /** The profile is in disconnecting state */ |
| 346 | public static final int STATE_DISCONNECTING = 3; |
| 347 | |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 348 | /** @hide */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 349 | public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager"; |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 350 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 351 | private static final int ADDRESS_LENGTH = 17; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 352 | |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 353 | /** |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 354 | * Lazily initialized singleton. Guaranteed final after first object |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 355 | * constructed. |
| 356 | */ |
| 357 | private static BluetoothAdapter sAdapter; |
| 358 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 359 | private final IBluetoothManager mManagerService; |
| 360 | private IBluetooth mService; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 361 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 362 | private final Map<LeScanCallback, GattCallbackWrapper> mLeScanClients; |
Matthew Xie | 484867a | 2011-08-25 16:45:58 -0700 | [diff] [blame] | 363 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 364 | /** |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 365 | * Get a handle to the default local Bluetooth adapter. |
| 366 | * <p>Currently Android only supports one Bluetooth adapter, but the API |
| 367 | * could be extended to support more. This will always return the default |
| 368 | * adapter. |
| 369 | * @return the default local adapter, or null if Bluetooth is not supported |
| 370 | * on this hardware platform |
| 371 | */ |
| 372 | public static synchronized BluetoothAdapter getDefaultAdapter() { |
| 373 | if (sAdapter == null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 374 | IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE); |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 375 | if (b != null) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 376 | IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b); |
| 377 | sAdapter = new BluetoothAdapter(managerService); |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 378 | } else { |
| 379 | Log.e(TAG, "Bluetooth binder is null"); |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | return sAdapter; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 387 | */ |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 388 | BluetoothAdapter(IBluetoothManager managerService) { |
| 389 | |
| 390 | if (managerService == null) { |
| 391 | throw new IllegalArgumentException("bluetooth manager service is null"); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 392 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 393 | try { |
| 394 | mService = managerService.registerAdapter(mManagerCallback); |
| 395 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 396 | mManagerService = managerService; |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 397 | mLeScanClients = new HashMap<LeScanCallback, GattCallbackWrapper>(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 401 | * Get a {@link BluetoothDevice} object for the given Bluetooth hardware |
| 402 | * address. |
| 403 | * <p>Valid Bluetooth hardware addresses must be upper case, in a format |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 404 | * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is |
| 405 | * available to validate a Bluetooth address. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 406 | * <p>A {@link BluetoothDevice} will always be returned for a valid |
| 407 | * hardware address, even if this adapter has never seen that device. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 408 | * |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 409 | * @param address valid Bluetooth MAC address |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 410 | * @throws IllegalArgumentException if address is invalid |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 411 | */ |
| 412 | public BluetoothDevice getRemoteDevice(String address) { |
| 413 | return new BluetoothDevice(address); |
| 414 | } |
| 415 | |
| 416 | /** |
Nick Pelly | 75596b4 | 2011-12-07 15:03:55 -0800 | [diff] [blame] | 417 | * Get a {@link BluetoothDevice} object for the given Bluetooth hardware |
| 418 | * address. |
| 419 | * <p>Valid Bluetooth hardware addresses must be 6 bytes. This method |
| 420 | * expects the address in network byte order (MSB first). |
| 421 | * <p>A {@link BluetoothDevice} will always be returned for a valid |
| 422 | * hardware address, even if this adapter has never seen that device. |
| 423 | * |
| 424 | * @param address Bluetooth MAC address (6 bytes) |
| 425 | * @throws IllegalArgumentException if address is invalid |
| 426 | */ |
| 427 | public BluetoothDevice getRemoteDevice(byte[] address) { |
| 428 | if (address == null || address.length != 6) { |
| 429 | throw new IllegalArgumentException("Bluetooth address must have 6 bytes"); |
| 430 | } |
| 431 | return new BluetoothDevice(String.format("%02X:%02X:%02X:%02X:%02X:%02X", |
| 432 | address[0], address[1], address[2], address[3], address[4], address[5])); |
| 433 | } |
| 434 | |
| 435 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 436 | * Return true if Bluetooth is currently enabled and ready for use. |
| 437 | * <p>Equivalent to: |
| 438 | * <code>getBluetoothState() == STATE_ON</code> |
| 439 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 440 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 441 | * @return true if the local adapter is turned on |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 442 | */ |
| 443 | public boolean isEnabled() { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 444 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 445 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 446 | synchronized(mManagerCallback) { |
| 447 | if (mService != null) return mService.isEnabled(); |
| 448 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 449 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 454 | * Get the current state of the local Bluetooth adapter. |
| 455 | * <p>Possible return values are |
| 456 | * {@link #STATE_OFF}, |
| 457 | * {@link #STATE_TURNING_ON}, |
| 458 | * {@link #STATE_ON}, |
| 459 | * {@link #STATE_TURNING_OFF}. |
| 460 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 461 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 462 | * @return current state of Bluetooth adapter |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 463 | */ |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 464 | public int getState() { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 465 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 466 | synchronized(mManagerCallback) { |
| 467 | if (mService != null) |
| 468 | { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 469 | int state= mService.getState(); |
Matthew Xie | 3b6214f | 2012-08-29 00:12:29 -0700 | [diff] [blame] | 470 | if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state); |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 471 | return state; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 472 | } |
| 473 | // TODO(BT) there might be a small gap during STATE_TURNING_ON that |
| 474 | // mService is null, handle that case |
| 475 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 476 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 477 | if (DBG) Log.d(TAG, "" + hashCode() + ": getState() : mService = null. Returning STATE_OFF"); |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 478 | return STATE_OFF; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 482 | * Turn on the local Bluetooth adapter—do not use without explicit |
| 483 | * user action to turn on Bluetooth. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 484 | * <p>This powers on the underlying Bluetooth hardware, and starts all |
| 485 | * Bluetooth system services. |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 486 | * <p class="caution"><strong>Bluetooth should never be enabled without |
| 487 | * direct user consent</strong>. If you want to turn on Bluetooth in order |
| 488 | * to create a wireless connection, you should use the {@link |
| 489 | * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests |
| 490 | * user permission to turn on Bluetooth. The {@link #enable()} method is |
| 491 | * provided only for applications that include a user interface for changing |
| 492 | * system settings, such as a "power manager" app.</p> |
Brad Fitzpatrick | 3219ab4 | 2009-09-25 16:31:39 +0400 | [diff] [blame] | 493 | * <p>This is an asynchronous call: it will return immediately, and |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 494 | * clients should listen for {@link #ACTION_STATE_CHANGED} |
| 495 | * to be notified of subsequent adapter state changes. If this call returns |
| 496 | * true, then the adapter state will immediately transition from {@link |
| 497 | * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time |
| 498 | * later transition to either {@link #STATE_OFF} or {@link |
| 499 | * #STATE_ON}. If this call returns false then there was an |
| 500 | * immediate problem that will prevent the adapter from being turned on - |
| 501 | * such as Airplane mode, or the adapter is already turned on. |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 502 | * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 503 | * permission |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 504 | * |
| 505 | * @return true to indicate adapter startup has begun, or false on |
| 506 | * immediate error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 507 | */ |
| 508 | public boolean enable() { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 509 | if (isEnabled() == true){ |
| 510 | if (DBG) Log.d(TAG, "enable(): BT is already enabled..!"); |
| 511 | return true; |
| 512 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 513 | try { |
Kausik Sinnaswamy | a097f51 | 2012-04-16 16:38:27 +0530 | [diff] [blame] | 514 | return mManagerService.enable(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 515 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | /** |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 520 | * Turn off the local Bluetooth adapter—do not use without explicit |
| 521 | * user action to turn off Bluetooth. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 522 | * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth |
| 523 | * system services, and powers down the underlying Bluetooth hardware. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 524 | * <p class="caution"><strong>Bluetooth should never be disabled without |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 525 | * direct user consent</strong>. The {@link #disable()} method is |
| 526 | * provided only for applications that include a user interface for changing |
| 527 | * system settings, such as a "power manager" app.</p> |
Brad Fitzpatrick | 3219ab4 | 2009-09-25 16:31:39 +0400 | [diff] [blame] | 528 | * <p>This is an asynchronous call: it will return immediately, and |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 529 | * clients should listen for {@link #ACTION_STATE_CHANGED} |
| 530 | * to be notified of subsequent adapter state changes. If this call returns |
| 531 | * true, then the adapter state will immediately transition from {@link |
| 532 | * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time |
| 533 | * later transition to either {@link #STATE_OFF} or {@link |
| 534 | * #STATE_ON}. If this call returns false then there was an |
| 535 | * immediate problem that will prevent the adapter from being turned off - |
| 536 | * such as the adapter already being turned off. |
Scott Main | ed2a70d | 2009-12-09 16:07:39 -0800 | [diff] [blame] | 537 | * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 538 | * permission |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 539 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 540 | * @return true to indicate adapter shutdown has begun, or false on |
| 541 | * immediate error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 542 | */ |
| 543 | public boolean disable() { |
| 544 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 545 | return mManagerService.disable(true); |
Jaikumar Ganesh | e21a4ac | 2012-03-06 17:15:16 -0800 | [diff] [blame] | 546 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Turn off the local Bluetooth adapter and don't persist the setting. |
| 552 | * |
| 553 | * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 554 | * permission |
| 555 | * |
| 556 | * @return true to indicate adapter shutdown has begun, or false on |
| 557 | * immediate error |
| 558 | * @hide |
| 559 | */ |
| 560 | public boolean disable(boolean persist) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 561 | |
Jaikumar Ganesh | e21a4ac | 2012-03-06 17:15:16 -0800 | [diff] [blame] | 562 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 563 | return mManagerService.disable(persist); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 564 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 565 | return false; |
| 566 | } |
| 567 | |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 568 | /** |
| 569 | * Returns the hardware address of the local Bluetooth adapter. |
| 570 | * <p>For example, "00:11:22:AA:BB:CC". |
| 571 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 572 | * |
| 573 | * @return Bluetooth hardware address as string |
| 574 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 575 | public String getAddress() { |
| 576 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 577 | return mManagerService.getAddress(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 578 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 579 | return null; |
| 580 | } |
| 581 | |
| 582 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 583 | * Get the friendly Bluetooth name of the local Bluetooth adapter. |
| 584 | * <p>This name is visible to remote Bluetooth devices. |
| 585 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 586 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 587 | * @return the Bluetooth name, or null on error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 588 | */ |
| 589 | public String getName() { |
| 590 | try { |
fredc | 116d1d46 | 2012-04-20 14:47:08 -0700 | [diff] [blame] | 591 | return mManagerService.getName(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 592 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 593 | return null; |
| 594 | } |
| 595 | |
| 596 | /** |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 597 | * Get the UUIDs supported by the local Bluetooth adapter. |
| 598 | * |
| 599 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 600 | * |
| 601 | * @return the UUIDs supported by the local Bluetooth Adapter. |
| 602 | * @hide |
| 603 | */ |
| 604 | public ParcelUuid[] getUuids() { |
Matthew Xie | 44b58ab | 2011-11-16 12:27:57 -0800 | [diff] [blame] | 605 | if (getState() != STATE_ON) return null; |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 606 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 607 | synchronized(mManagerCallback) { |
| 608 | if (mService != null) return mService.getUuids(); |
| 609 | } |
Jaikumar Ganesh | 58b93c3 | 2010-11-23 20:03:10 -0800 | [diff] [blame] | 610 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 611 | return null; |
| 612 | } |
| 613 | |
| 614 | /** |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 615 | * Set the friendly Bluetooth name of the local Bluetooth adapter. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 616 | * <p>This name is visible to remote Bluetooth devices. |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 617 | * <p>Valid Bluetooth names are a maximum of 248 bytes using UTF-8 |
| 618 | * encoding, although many remote devices can only display the first |
| 619 | * 40 characters, and some may be limited to just 20. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 620 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 621 | * will return false. After turning on Bluetooth, |
| 622 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 623 | * to get the updated value. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 624 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 625 | * |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 626 | * @param name a valid Bluetooth name |
| 627 | * @return true if the name was set, false otherwise |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 628 | */ |
| 629 | public boolean setName(String name) { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 630 | if (getState() != STATE_ON) return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 631 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 632 | synchronized(mManagerCallback) { |
| 633 | if (mService != null) return mService.setName(name); |
| 634 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 635 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | /** |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 640 | * Get the current Bluetooth scan mode of the local Bluetooth adapter. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 641 | * <p>The Bluetooth scan mode determines if the local adapter is |
| 642 | * connectable and/or discoverable from remote Bluetooth devices. |
| 643 | * <p>Possible values are: |
| 644 | * {@link #SCAN_MODE_NONE}, |
| 645 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 646 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 647 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 648 | * will return {@link #SCAN_MODE_NONE}. After turning on Bluetooth, |
| 649 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 650 | * to get the updated value. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 651 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 652 | * |
| 653 | * @return scan mode |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 654 | */ |
| 655 | public int getScanMode() { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 656 | if (getState() != STATE_ON) return SCAN_MODE_NONE; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 657 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 658 | synchronized(mManagerCallback) { |
| 659 | if (mService != null) return mService.getScanMode(); |
| 660 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 661 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 662 | return SCAN_MODE_NONE; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 666 | * Set the Bluetooth scan mode of the local Bluetooth adapter. |
| 667 | * <p>The Bluetooth scan mode determines if the local adapter is |
| 668 | * connectable and/or discoverable from remote Bluetooth devices. |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 669 | * <p>For privacy reasons, discoverable mode is automatically turned off |
| 670 | * after <code>duration</code> seconds. For example, 120 seconds should be |
| 671 | * enough for a remote device to initiate and complete its discovery |
| 672 | * process. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 673 | * <p>Valid scan mode values are: |
| 674 | * {@link #SCAN_MODE_NONE}, |
| 675 | * {@link #SCAN_MODE_CONNECTABLE}, |
| 676 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 677 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 678 | * will return false. After turning on Bluetooth, |
| 679 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 680 | * to get the updated value. |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 681 | * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} |
| 682 | * <p>Applications cannot set the scan mode. They should use |
| 683 | * <code>startActivityForResult( |
| 684 | * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE}) |
| 685 | * </code>instead. |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 686 | * |
| 687 | * @param mode valid scan mode |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 688 | * @param duration time in seconds to apply scan mode, only used for |
| 689 | * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 690 | * @return true if the scan mode was set, false otherwise |
Nick Pelly | 18b1e79 | 2009-09-24 11:14:15 -0700 | [diff] [blame] | 691 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 692 | */ |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 693 | public boolean setScanMode(int mode, int duration) { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 694 | if (getState() != STATE_ON) return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 695 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 696 | synchronized(mManagerCallback) { |
| 697 | if (mService != null) return mService.setScanMode(mode, duration); |
| 698 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 699 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 700 | return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 703 | /** @hide */ |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 704 | public boolean setScanMode(int mode) { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 705 | if (getState() != STATE_ON) return false; |
Srikanth Uppala | 827de2d | 2012-04-04 03:33:26 -0700 | [diff] [blame] | 706 | /* getDiscoverableTimeout() to use the latest from NV than use 0 */ |
| 707 | return setScanMode(mode, getDiscoverableTimeout()); |
Nick Pelly | 1283547 | 2009-09-25 15:00:29 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** @hide */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 711 | public int getDiscoverableTimeout() { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 712 | if (getState() != STATE_ON) return -1; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 713 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 714 | synchronized(mManagerCallback) { |
| 715 | if (mService != null) return mService.getDiscoverableTimeout(); |
| 716 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 717 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 718 | return -1; |
| 719 | } |
| 720 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 721 | /** @hide */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 722 | public void setDiscoverableTimeout(int timeout) { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 723 | if (getState() != STATE_ON) return; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 724 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 725 | synchronized(mManagerCallback) { |
| 726 | if (mService != null) mService.setDiscoverableTimeout(timeout); |
| 727 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 728 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 729 | } |
| 730 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 731 | /** |
| 732 | * Start the remote device discovery process. |
| 733 | * <p>The discovery process usually involves an inquiry scan of about 12 |
| 734 | * seconds, followed by a page scan of each new device to retrieve its |
| 735 | * Bluetooth name. |
| 736 | * <p>This is an asynchronous call, it will return immediately. Register |
| 737 | * for {@link #ACTION_DISCOVERY_STARTED} and {@link |
| 738 | * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the |
| 739 | * discovery starts and completes. Register for {@link |
| 740 | * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices |
| 741 | * are found. |
| 742 | * <p>Device discovery is a heavyweight procedure. New connections to |
| 743 | * remote Bluetooth devices should not be attempted while discovery is in |
| 744 | * progress, and existing connections will experience limited bandwidth |
| 745 | * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 746 | * discovery. Discovery is not managed by the Activity, |
| 747 | * but is run as a system service, so an application should always call |
| 748 | * {@link BluetoothAdapter#cancelDiscovery()} even if it |
| 749 | * did not directly request a discovery, just to be sure. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 750 | * <p>Device discovery will only find remote devices that are currently |
| 751 | * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are |
| 752 | * 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] | 753 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 754 | * will return false. After turning on Bluetooth, |
| 755 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 756 | * to get the updated value. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 757 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
| 758 | * |
| 759 | * @return true on success, false on error |
| 760 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 761 | public boolean startDiscovery() { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 762 | if (getState() != STATE_ON) return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 763 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 764 | synchronized(mManagerCallback) { |
| 765 | if (mService != null) return mService.startDiscovery(); |
| 766 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 767 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 768 | return false; |
| 769 | } |
| 770 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 771 | /** |
| 772 | * Cancel the current device discovery process. |
| 773 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. |
Jake Hamby | 0f58430 | 2010-09-16 18:12:51 -0700 | [diff] [blame] | 774 | * <p>Because discovery is a heavyweight procedure for the Bluetooth |
Scott Main | 6d95fc0 | 2009-11-19 17:00:19 -0800 | [diff] [blame] | 775 | * adapter, this method should always be called before attempting to connect |
| 776 | * to a remote device with {@link |
| 777 | * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by |
| 778 | * the Activity, but is run as a system service, so an application should |
| 779 | * always call cancel discovery even if it did not directly request a |
| 780 | * discovery, just to be sure. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 781 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 782 | * will return false. After turning on Bluetooth, |
| 783 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 784 | * to get the updated value. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 785 | * |
| 786 | * @return true on success, false on error |
| 787 | */ |
| 788 | public boolean cancelDiscovery() { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 789 | if (getState() != STATE_ON) return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 790 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 791 | synchronized(mManagerCallback) { |
| 792 | if (mService != null) return mService.cancelDiscovery(); |
| 793 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 794 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 795 | return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Return true if the local Bluetooth adapter is currently in the device |
| 800 | * discovery process. |
| 801 | * <p>Device discovery is a heavyweight procedure. New connections to |
| 802 | * remote Bluetooth devices should not be attempted while discovery is in |
| 803 | * progress, and existing connections will experience limited bandwidth |
| 804 | * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing |
| 805 | * discovery. |
| 806 | * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED} |
| 807 | * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery |
| 808 | * starts or completes. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 809 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 810 | * will return false. After turning on Bluetooth, |
| 811 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 812 | * to get the updated value. |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 813 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 814 | * |
| 815 | * @return true if discovering |
| 816 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 817 | public boolean isDiscovering() { |
Jaikumar Ganesh | f5ff170 | 2010-08-06 19:03:13 -0700 | [diff] [blame] | 818 | if (getState() != STATE_ON) return false; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 819 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 820 | synchronized(mManagerCallback) { |
| 821 | if (mService != null ) return mService.isDiscovering(); |
| 822 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 823 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | /** |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 828 | * Return the set of {@link BluetoothDevice} objects that are bonded |
| 829 | * (paired) to the local adapter. |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 830 | * <p>If Bluetooth state is not {@link #STATE_ON}, this API |
| 831 | * will return an empty set. After turning on Bluetooth, |
| 832 | * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON} |
| 833 | * to get the updated value. |
Nick Pelly | e6ee3be | 2009-10-08 23:27:28 +0200 | [diff] [blame] | 834 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 835 | * |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 836 | * @return unmodifiable set of {@link BluetoothDevice}, or null on error |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 837 | */ |
| 838 | public Set<BluetoothDevice> getBondedDevices() { |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 839 | if (getState() != STATE_ON) { |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 840 | return toDeviceSet(new BluetoothDevice[0]); |
Jaikumar Ganesh | fec86f4 | 2010-08-09 16:54:03 -0700 | [diff] [blame] | 841 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 842 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 843 | synchronized(mManagerCallback) { |
| 844 | if (mService != null) return toDeviceSet(mService.getBondedDevices()); |
| 845 | } |
| 846 | return toDeviceSet(new BluetoothDevice[0]); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 847 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 848 | return null; |
| 849 | } |
| 850 | |
| 851 | /** |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 852 | * Get the current connection state of the local Bluetooth adapter. |
| 853 | * This can be used to check whether the local Bluetooth adapter is connected |
| 854 | * to any profile of any other remote Bluetooth Device. |
| 855 | * |
| 856 | * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 857 | * intent to get the connection state of the adapter. |
| 858 | * |
| 859 | * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED}, |
| 860 | * {@link #STATE_CONNECTING} or {@link #STATE_DISCONNECTED} |
| 861 | * |
| 862 | * @hide |
| 863 | */ |
| 864 | public int getConnectionState() { |
| 865 | if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED; |
| 866 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 867 | synchronized(mManagerCallback) { |
| 868 | if (mService != null) return mService.getAdapterConnectionState(); |
| 869 | } |
Jaikumar Ganesh | c53cab2 | 2010-10-26 16:02:26 -0700 | [diff] [blame] | 870 | } catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);} |
| 871 | return BluetoothAdapter.STATE_DISCONNECTED; |
| 872 | } |
| 873 | |
| 874 | /** |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 875 | * Get the current connection state of a profile. |
| 876 | * This function can be used to check whether the local Bluetooth adapter |
| 877 | * is connected to any remote device for a specific profile. |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 878 | * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET}, |
Jaikumar Ganesh | 9354790 | 2011-08-23 12:21:55 -0700 | [diff] [blame] | 879 | * {@link BluetoothProfile#A2DP}. |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 880 | * |
| 881 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH}. |
| 882 | * |
| 883 | * <p> Return value can be one of |
Jaikumar Ganesh | 9354790 | 2011-08-23 12:21:55 -0700 | [diff] [blame] | 884 | * {@link BluetoothProfile#STATE_DISCONNECTED}, |
| 885 | * {@link BluetoothProfile#STATE_CONNECTING}, |
| 886 | * {@link BluetoothProfile#STATE_CONNECTED}, |
| 887 | * {@link BluetoothProfile#STATE_DISCONNECTING} |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 888 | */ |
| 889 | public int getProfileConnectionState(int profile) { |
| 890 | if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED; |
| 891 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 892 | synchronized(mManagerCallback) { |
| 893 | if (mService != null) return mService.getProfileConnectionState(profile); |
| 894 | } |
Jaikumar Ganesh | 9354790 | 2011-08-23 12:21:55 -0700 | [diff] [blame] | 895 | } catch (RemoteException e) { |
| 896 | Log.e(TAG, "getProfileConnectionState:", e); |
| 897 | } |
Jaikumar Ganesh | cb1d354 | 2011-08-19 10:26:32 -0700 | [diff] [blame] | 898 | return BluetoothProfile.STATE_DISCONNECTED; |
| 899 | } |
| 900 | |
| 901 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 902 | * Create a listening, secure RFCOMM Bluetooth socket. |
| 903 | * <p>A remote device connecting to this socket will be authenticated and |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 904 | * communication on this socket will be encrypted. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 905 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 906 | * connections from a listening {@link BluetoothServerSocket}. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 907 | * <p>Valid RFCOMM channels are in range 1 to 30. |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 908 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 909 | * @param channel RFCOMM channel to listen on |
| 910 | * @return a listening RFCOMM BluetoothServerSocket |
| 911 | * @throws IOException on error, for example Bluetooth not available, or |
| 912 | * insufficient permissions, or channel in use. |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 913 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 914 | */ |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 915 | public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 916 | BluetoothServerSocket socket = new BluetoothServerSocket( |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 917 | BluetoothSocket.TYPE_RFCOMM, true, true, channel); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 918 | int errno = socket.mSocket.bindListen(); |
| 919 | if (errno != 0) { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 920 | //TODO(BT): Throw the same exception error code |
| 921 | // that the previous code was using. |
| 922 | //socket.mSocket.throwErrnoNative(errno); |
| 923 | throw new IOException("Error: " + errno); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 924 | } |
| 925 | return socket; |
| 926 | } |
| 927 | |
| 928 | /** |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 929 | * Create a listening, secure RFCOMM Bluetooth socket with Service Record. |
| 930 | * <p>A remote device connecting to this socket will be authenticated and |
| 931 | * communication on this socket will be encrypted. |
| 932 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 933 | * connections from a listening {@link BluetoothServerSocket}. |
| 934 | * <p>The system will assign an unused RFCOMM channel to listen on. |
| 935 | * <p>The system will also register a Service Discovery |
| 936 | * Protocol (SDP) record with the local SDP server containing the specified |
| 937 | * UUID, service name, and auto-assigned channel. Remote Bluetooth devices |
| 938 | * can use the same UUID to query our SDP server and discover which channel |
| 939 | * to connect to. This SDP record will be removed when this socket is |
| 940 | * closed, or if this application closes unexpectedly. |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 941 | * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to |
| 942 | * connect to this socket from another device using the same {@link UUID}. |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 943 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 944 | * @param name service name for SDP record |
| 945 | * @param uuid uuid for SDP record |
| 946 | * @return a listening RFCOMM BluetoothServerSocket |
| 947 | * @throws IOException on error, for example Bluetooth not available, or |
| 948 | * insufficient permissions, or channel in use. |
| 949 | */ |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 950 | public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid) |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 951 | throws IOException { |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 952 | return createNewRfcommSocketAndRecord(name, uuid, true, true); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Create a listening, insecure RFCOMM Bluetooth socket with Service Record. |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 957 | * <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] | 958 | * vulnerable to Man In the Middle attacks. For Bluetooth 2.1 devices, |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 959 | * the link will be encrypted, as encryption is mandartory. |
| 960 | * For legacy devices (pre Bluetooth 2.1 devices) the link will not |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 961 | * be encrypted. Use {@link #listenUsingRfcommWithServiceRecord}, if an |
| 962 | * encrypted and authenticated communication channel is desired. |
| 963 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 964 | * connections from a listening {@link BluetoothServerSocket}. |
| 965 | * <p>The system will assign an unused RFCOMM channel to listen on. |
| 966 | * <p>The system will also register a Service Discovery |
| 967 | * Protocol (SDP) record with the local SDP server containing the specified |
| 968 | * UUID, service name, and auto-assigned channel. Remote Bluetooth devices |
| 969 | * can use the same UUID to query our SDP server and discover which channel |
| 970 | * to connect to. This SDP record will be removed when this socket is |
| 971 | * closed, or if this application closes unexpectedly. |
| 972 | * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to |
| 973 | * connect to this socket from another device using the same {@link UUID}. |
| 974 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 975 | * @param name service name for SDP record |
| 976 | * @param uuid uuid for SDP record |
| 977 | * @return a listening RFCOMM BluetoothServerSocket |
| 978 | * @throws IOException on error, for example Bluetooth not available, or |
| 979 | * insufficient permissions, or channel in use. |
| 980 | */ |
| 981 | public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid) |
| 982 | throws IOException { |
| 983 | return createNewRfcommSocketAndRecord(name, uuid, false, false); |
| 984 | } |
| 985 | |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 986 | /** |
| 987 | * Create a listening, encrypted, |
| 988 | * RFCOMM Bluetooth socket with Service Record. |
| 989 | * <p>The link will be encrypted, but the link key is not required to be authenticated |
| 990 | * i.e the communication is vulnerable to Man In the Middle attacks. Use |
| 991 | * {@link #listenUsingRfcommWithServiceRecord}, to ensure an authenticated link key. |
| 992 | * <p> Use this socket if authentication of link key is not possible. |
| 993 | * For example, for Bluetooth 2.1 devices, if any of the devices does not have |
| 994 | * an input and output capability or just has the ability to display a numeric key, |
| 995 | * a secure socket connection is not possible and this socket can be used. |
| 996 | * Use {@link #listenUsingInsecureRfcommWithServiceRecord}, if encryption is not required. |
| 997 | * For Bluetooth 2.1 devices, the link will be encrypted, as encryption is mandartory. |
| 998 | * For more details, refer to the Security Model section 5.2 (vol 3) of |
| 999 | * Bluetooth Core Specification version 2.1 + EDR. |
| 1000 | * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming |
| 1001 | * connections from a listening {@link BluetoothServerSocket}. |
| 1002 | * <p>The system will assign an unused RFCOMM channel to listen on. |
| 1003 | * <p>The system will also register a Service Discovery |
| 1004 | * Protocol (SDP) record with the local SDP server containing the specified |
| 1005 | * UUID, service name, and auto-assigned channel. Remote Bluetooth devices |
| 1006 | * can use the same UUID to query our SDP server and discover which channel |
| 1007 | * to connect to. This SDP record will be removed when this socket is |
| 1008 | * closed, or if this application closes unexpectedly. |
| 1009 | * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to |
| 1010 | * connect to this socket from another device using the same {@link UUID}. |
| 1011 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1012 | * @param name service name for SDP record |
| 1013 | * @param uuid uuid for SDP record |
| 1014 | * @return a listening RFCOMM BluetoothServerSocket |
| 1015 | * @throws IOException on error, for example Bluetooth not available, or |
| 1016 | * insufficient permissions, or channel in use. |
| 1017 | * @hide |
| 1018 | */ |
| 1019 | public BluetoothServerSocket listenUsingEncryptedRfcommWithServiceRecord( |
| 1020 | String name, UUID uuid) throws IOException { |
| 1021 | return createNewRfcommSocketAndRecord(name, uuid, false, true); |
| 1022 | } |
| 1023 | |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 1024 | |
Jaikumar Ganesh | 6eef14a | 2010-12-23 12:57:02 -0800 | [diff] [blame] | 1025 | private BluetoothServerSocket createNewRfcommSocketAndRecord(String name, UUID uuid, |
| 1026 | boolean auth, boolean encrypt) throws IOException { |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1027 | BluetoothServerSocket socket; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 1028 | socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, auth, |
| 1029 | encrypt, new ParcelUuid(uuid)); |
| 1030 | socket.setServiceName(name); |
| 1031 | int errno = socket.mSocket.bindListen(); |
| 1032 | if (errno != 0) { |
| 1033 | //TODO(BT): Throw the same exception error code |
| 1034 | // that the previous code was using. |
| 1035 | //socket.mSocket.throwErrnoNative(errno); |
| 1036 | throw new IOException("Error: " + errno); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1037 | } |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1038 | return socket; |
| 1039 | } |
| 1040 | |
| 1041 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1042 | * Construct an unencrypted, unauthenticated, RFCOMM server socket. |
| 1043 | * Call #accept to retrieve connections to this socket. |
| 1044 | * @return An RFCOMM BluetoothServerSocket |
| 1045 | * @throws IOException On error, for example Bluetooth not available, or |
| 1046 | * insufficient permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1047 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1048 | */ |
| 1049 | public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException { |
| 1050 | BluetoothServerSocket socket = new BluetoothServerSocket( |
| 1051 | BluetoothSocket.TYPE_RFCOMM, false, false, port); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1052 | int errno = socket.mSocket.bindListen(); |
| 1053 | if (errno != 0) { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 1054 | //TODO(BT): Throw the same exception error code |
| 1055 | // that the previous code was using. |
| 1056 | //socket.mSocket.throwErrnoNative(errno); |
| 1057 | throw new IOException("Error: " + errno); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1058 | } |
| 1059 | return socket; |
| 1060 | } |
| 1061 | |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 1062 | /** |
| 1063 | * Construct an encrypted, RFCOMM server socket. |
| 1064 | * Call #accept to retrieve connections to this socket. |
| 1065 | * @return An RFCOMM BluetoothServerSocket |
| 1066 | * @throws IOException On error, for example Bluetooth not available, or |
| 1067 | * insufficient permissions. |
| 1068 | * @hide |
| 1069 | */ |
| 1070 | public BluetoothServerSocket listenUsingEncryptedRfcommOn(int port) |
| 1071 | throws IOException { |
| 1072 | BluetoothServerSocket socket = new BluetoothServerSocket( |
| 1073 | BluetoothSocket.TYPE_RFCOMM, false, true, port); |
| 1074 | int errno = socket.mSocket.bindListen(); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 1075 | if (errno < 0) { |
| 1076 | //TODO(BT): Throw the same exception error code |
| 1077 | // that the previous code was using. |
| 1078 | //socket.mSocket.throwErrnoNative(errno); |
| 1079 | throw new IOException("Error: " + errno); |
Mathias Jeppsson | e3b9dc10 | 2011-03-21 15:06:52 +0100 | [diff] [blame] | 1080 | } |
| 1081 | return socket; |
| 1082 | } |
| 1083 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1084 | /** |
| 1085 | * Construct a SCO server socket. |
| 1086 | * Call #accept to retrieve connections to this socket. |
| 1087 | * @return A SCO BluetoothServerSocket |
| 1088 | * @throws IOException On error, for example Bluetooth not available, or |
| 1089 | * insufficient permissions. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 1090 | * @hide |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1091 | */ |
| 1092 | public static BluetoothServerSocket listenUsingScoOn() throws IOException { |
| 1093 | BluetoothServerSocket socket = new BluetoothServerSocket( |
| 1094 | BluetoothSocket.TYPE_SCO, false, false, -1); |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 1095 | int errno = socket.mSocket.bindListen(); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 1096 | if (errno < 0) { |
| 1097 | //TODO(BT): Throw the same exception error code |
| 1098 | // that the previous code was using. |
| 1099 | //socket.mSocket.throwErrnoNative(errno); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1100 | } |
| 1101 | return socket; |
| 1102 | } |
| 1103 | |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1104 | /** |
| 1105 | * Read the local Out of Band Pairing Data |
| 1106 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 1107 | * |
| 1108 | * @return Pair<byte[], byte[]> of Hash and Randomizer |
| 1109 | * |
| 1110 | * @hide |
| 1111 | */ |
| 1112 | public Pair<byte[], byte[]> readOutOfBandData() { |
| 1113 | if (getState() != STATE_ON) return null; |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1114 | //TODO(BT |
| 1115 | /* |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1116 | try { |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 1117 | byte[] hash; |
| 1118 | byte[] randomizer; |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1119 | |
| 1120 | byte[] ret = mService.readOutOfBandData(); |
| 1121 | |
| 1122 | if (ret == null || ret.length != 32) return null; |
| 1123 | |
| 1124 | hash = Arrays.copyOfRange(ret, 0, 16); |
| 1125 | randomizer = Arrays.copyOfRange(ret, 16, 32); |
| 1126 | |
| 1127 | if (DBG) { |
| 1128 | Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) + |
| 1129 | ":" + Arrays.toString(randomizer)); |
| 1130 | } |
| 1131 | return new Pair<byte[], byte[]>(hash, randomizer); |
| 1132 | |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1133 | } catch (RemoteException e) {Log.e(TAG, "", e);}*/ |
Jaikumar Ganesh | cc5494c | 2010-09-09 15:37:57 -0700 | [diff] [blame] | 1134 | return null; |
| 1135 | } |
| 1136 | |
Scott Main | 299ae67 | 2011-01-19 21:13:18 -0800 | [diff] [blame] | 1137 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1138 | * Get the profile proxy object associated with the profile. |
| 1139 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1140 | * <p>Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET}, |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1141 | * {@link BluetoothProfile#A2DP}, {@link BluetoothProfile#GATT}, or |
| 1142 | * {@link BluetoothProfile#GATT_SERVER}. Clients must implement |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1143 | * {@link BluetoothProfile.ServiceListener} to get notified of |
| 1144 | * the connection status and to get the proxy object. |
| 1145 | * |
| 1146 | * @param context Context of the application |
| 1147 | * @param listener The service Listener for connection callbacks. |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 1148 | * @param profile The Bluetooth profile; either {@link BluetoothProfile#HEALTH}, |
| 1149 | * {@link BluetoothProfile#HEADSET} or {@link BluetoothProfile#A2DP}. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1150 | * @return true on success, false on error |
| 1151 | */ |
| 1152 | public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, |
| 1153 | int profile) { |
| 1154 | if (context == null || listener == null) return false; |
| 1155 | |
| 1156 | if (profile == BluetoothProfile.HEADSET) { |
| 1157 | BluetoothHeadset headset = new BluetoothHeadset(context, listener); |
| 1158 | return true; |
| 1159 | } else if (profile == BluetoothProfile.A2DP) { |
| 1160 | BluetoothA2dp a2dp = new BluetoothA2dp(context, listener); |
| 1161 | return true; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 1162 | } else if (profile == BluetoothProfile.INPUT_DEVICE) { |
| 1163 | BluetoothInputDevice iDev = new BluetoothInputDevice(context, listener); |
| 1164 | return true; |
Jaikumar Ganesh | 74ef119 | 2011-02-23 10:22:15 -0800 | [diff] [blame] | 1165 | } else if (profile == BluetoothProfile.PAN) { |
| 1166 | BluetoothPan pan = new BluetoothPan(context, listener); |
| 1167 | return true; |
Jaikumar Ganesh | 2ea1e85 | 2011-04-01 16:33:09 -0700 | [diff] [blame] | 1168 | } else if (profile == BluetoothProfile.HEALTH) { |
| 1169 | BluetoothHealth health = new BluetoothHealth(context, listener); |
| 1170 | return true; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1171 | } else { |
| 1172 | return false; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * Close the connection of the profile proxy to the Service. |
| 1178 | * |
| 1179 | * <p> Clients should call this when they are no longer using |
| 1180 | * the proxy obtained from {@link #getProfileProxy}. |
Scott Main | 2d68a6b | 2011-09-26 22:59:38 -0700 | [diff] [blame] | 1181 | * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET} or |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1182 | * {@link BluetoothProfile#A2DP} |
| 1183 | * |
| 1184 | * @param profile |
| 1185 | * @param proxy Profile proxy object |
| 1186 | */ |
| 1187 | public void closeProfileProxy(int profile, BluetoothProfile proxy) { |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 1188 | if (proxy == null) return; |
| 1189 | |
| 1190 | switch (profile) { |
| 1191 | case BluetoothProfile.HEADSET: |
| 1192 | BluetoothHeadset headset = (BluetoothHeadset)proxy; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1193 | headset.close(); |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 1194 | break; |
| 1195 | case BluetoothProfile.A2DP: |
| 1196 | BluetoothA2dp a2dp = (BluetoothA2dp)proxy; |
| 1197 | a2dp.close(); |
| 1198 | break; |
| 1199 | case BluetoothProfile.INPUT_DEVICE: |
| 1200 | BluetoothInputDevice iDev = (BluetoothInputDevice)proxy; |
| 1201 | iDev.close(); |
| 1202 | break; |
| 1203 | case BluetoothProfile.PAN: |
| 1204 | BluetoothPan pan = (BluetoothPan)proxy; |
| 1205 | pan.close(); |
| 1206 | break; |
| 1207 | case BluetoothProfile.HEALTH: |
| 1208 | BluetoothHealth health = (BluetoothHealth)proxy; |
| 1209 | health.close(); |
| 1210 | break; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1211 | case BluetoothProfile.GATT: |
| 1212 | BluetoothGatt gatt = (BluetoothGatt)proxy; |
| 1213 | gatt.close(); |
| 1214 | break; |
| 1215 | case BluetoothProfile.GATT_SERVER: |
| 1216 | BluetoothGattServer gattServer = (BluetoothGattServer)proxy; |
| 1217 | gattServer.close(); |
| 1218 | break; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1222 | final private IBluetoothManagerCallback mManagerCallback = |
| 1223 | new IBluetoothManagerCallback.Stub() { |
| 1224 | public void onBluetoothServiceUp(IBluetooth bluetoothService) { |
Matthew Xie | d77982e | 2012-11-29 20:26:19 -0800 | [diff] [blame] | 1225 | if (VDBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1226 | synchronized (mManagerCallback) { |
| 1227 | mService = bluetoothService; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1228 | for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){ |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 1229 | try { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1230 | if (cb != null) { |
| 1231 | cb.onBluetoothServiceUp(bluetoothService); |
| 1232 | } else { |
| 1233 | Log.d(TAG, "onBluetoothServiceUp: cb is null!!!"); |
| 1234 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 1235 | } catch (Exception e) { Log.e(TAG,"",e);} |
| 1236 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | public void onBluetoothServiceDown() { |
Matthew Xie | d77982e | 2012-11-29 20:26:19 -0800 | [diff] [blame] | 1241 | if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1242 | synchronized (mManagerCallback) { |
| 1243 | mService = null; |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1244 | for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){ |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 1245 | try { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1246 | if (cb != null) { |
| 1247 | cb.onBluetoothServiceDown(); |
| 1248 | } else { |
| 1249 | Log.d(TAG, "onBluetoothServiceDown: cb is null!!!"); |
| 1250 | } |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 1251 | } catch (Exception e) { Log.e(TAG,"",e);} |
| 1252 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | }; |
| 1256 | |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 1257 | /** |
Martijn Coenen | 6c614b7 | 2012-04-18 13:01:15 -0700 | [diff] [blame] | 1258 | * Enable the Bluetooth Adapter, but don't auto-connect devices |
| 1259 | * and don't persist state. Only for use by system applications. |
| 1260 | * @hide |
| 1261 | */ |
| 1262 | public boolean enableNoAutoConnect() { |
Ganesh Ganapathi Batta | fffa86b | 2012-08-08 15:35:49 -0700 | [diff] [blame] | 1263 | if (isEnabled() == true){ |
| 1264 | if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT is already enabled..!"); |
| 1265 | return true; |
| 1266 | } |
| 1267 | try { |
| 1268 | return mManagerService.enableNoAutoConnect(); |
| 1269 | } catch (RemoteException e) {Log.e(TAG, "", e);} |
| 1270 | return false; |
Martijn Coenen | 6c614b7 | 2012-04-18 13:01:15 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | /** |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 1274 | * Enable control of the Bluetooth Adapter for a single application. |
| 1275 | * |
| 1276 | * <p>Some applications need to use Bluetooth for short periods of time to |
| 1277 | * transfer data but don't want all the associated implications like |
| 1278 | * automatic connection to headsets etc. |
| 1279 | * |
| 1280 | * <p> Multiple applications can call this. This is reference counted and |
| 1281 | * Bluetooth disabled only when no one else is using it. There will be no UI |
| 1282 | * shown to the user while bluetooth is being enabled. Any user action will |
| 1283 | * override this call. For example, if user wants Bluetooth on and the last |
| 1284 | * user of this API wanted to disable Bluetooth, Bluetooth will not be |
| 1285 | * turned off. |
| 1286 | * |
| 1287 | * <p> This API is only meant to be used by internal applications. Third |
| 1288 | * party applications but use {@link #enable} and {@link #disable} APIs. |
| 1289 | * |
| 1290 | * <p> If this API returns true, it means the callback will be called. |
| 1291 | * The callback will be called with the current state of Bluetooth. |
| 1292 | * 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] | 1293 | * reason. If Bluetooth is already on and if this function is called to turn |
| 1294 | * it on, the api will return true and a callback will be called. |
| 1295 | * |
| 1296 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 1297 | * |
| 1298 | * @param on True for on, false for off. |
| 1299 | * @param callback The callback to notify changes to the state. |
| 1300 | * @hide |
| 1301 | */ |
| 1302 | public boolean changeApplicationBluetoothState(boolean on, |
| 1303 | BluetoothStateChangeCallback callback) { |
| 1304 | if (callback == null) return false; |
| 1305 | |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1306 | //TODO(BT) |
| 1307 | /* |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 1308 | try { |
| 1309 | return mService.changeApplicationBluetoothState(on, new |
| 1310 | StateChangeCallbackWrapper(callback), new Binder()); |
| 1311 | } catch (RemoteException e) { |
| 1312 | Log.e(TAG, "changeBluetoothState", e); |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1313 | }*/ |
Jaikumar Ganesh | ef2cb7c | 2011-07-21 18:13:38 -0700 | [diff] [blame] | 1314 | return false; |
| 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * @hide |
| 1319 | */ |
| 1320 | public interface BluetoothStateChangeCallback { |
| 1321 | public void onBluetoothStateChange(boolean on); |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * @hide |
| 1326 | */ |
| 1327 | public class StateChangeCallbackWrapper extends IBluetoothStateChangeCallback.Stub { |
| 1328 | private BluetoothStateChangeCallback mCallback; |
| 1329 | |
| 1330 | StateChangeCallbackWrapper(BluetoothStateChangeCallback |
| 1331 | callback) { |
| 1332 | mCallback = callback; |
| 1333 | } |
| 1334 | |
| 1335 | @Override |
| 1336 | public void onBluetoothStateChange(boolean on) { |
| 1337 | mCallback.onBluetoothStateChange(on); |
| 1338 | } |
| 1339 | } |
| 1340 | |
Jaikumar Ganesh | e4caddb | 2012-01-25 16:16:48 -0800 | [diff] [blame] | 1341 | private Set<BluetoothDevice> toDeviceSet(BluetoothDevice[] devices) { |
| 1342 | Set<BluetoothDevice> deviceSet = new HashSet<BluetoothDevice>(Arrays.asList(devices)); |
| 1343 | return Collections.unmodifiableSet(deviceSet); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1344 | } |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1345 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1346 | protected void finalize() throws Throwable { |
| 1347 | try { |
| 1348 | mManagerService.unregisterAdapter(mManagerCallback); |
| 1349 | } catch (RemoteException e) { |
| 1350 | Log.e(TAG, "", e); |
| 1351 | } finally { |
| 1352 | super.finalize(); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1357 | /** |
Nick Pelly | 75596b4 | 2011-12-07 15:03:55 -0800 | [diff] [blame] | 1358 | * 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] | 1359 | * <p>Alphabetic characters must be uppercase to be valid. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1360 | * |
| 1361 | * @param address Bluetooth address as string |
| 1362 | * @return true if the address is valid, false otherwise |
| 1363 | */ |
| 1364 | public static boolean checkBluetoothAddress(String address) { |
| 1365 | if (address == null || address.length() != ADDRESS_LENGTH) { |
| 1366 | return false; |
| 1367 | } |
| 1368 | for (int i = 0; i < ADDRESS_LENGTH; i++) { |
| 1369 | char c = address.charAt(i); |
| 1370 | switch (i % 3) { |
| 1371 | case 0: |
| 1372 | case 1: |
Nick Pelly | 55e66f1 | 2009-09-18 11:37:06 -0700 | [diff] [blame] | 1373 | if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) { |
| 1374 | // hex character, OK |
| 1375 | break; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 1376 | } |
| 1377 | return false; |
| 1378 | case 2: |
| 1379 | if (c == ':') { |
| 1380 | break; // OK |
| 1381 | } |
| 1382 | return false; |
| 1383 | } |
| 1384 | } |
| 1385 | return true; |
| 1386 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1387 | |
| 1388 | /*package*/ IBluetoothManager getBluetoothManager() { |
| 1389 | return mManagerService; |
| 1390 | } |
| 1391 | |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1392 | private ArrayList<IBluetoothManagerCallback> mProxyServiceStateCallbacks = new ArrayList<IBluetoothManagerCallback>(); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1393 | |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 1394 | /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1395 | synchronized (mManagerCallback) { |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 1396 | if (cb == null) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1397 | Log.w(TAG, "getBluetoothService() called with no BluetoothManagerCallback"); |
| 1398 | } else if (!mProxyServiceStateCallbacks.contains(cb)) { |
| 1399 | mProxyServiceStateCallbacks.add(cb); |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | return mService; |
| 1403 | } |
| 1404 | |
| 1405 | /*package*/ void removeServiceStateCallback(IBluetoothManagerCallback cb) { |
| 1406 | synchronized (mManagerCallback) { |
fredc | bf072a7 | 2012-05-09 16:52:50 -0700 | [diff] [blame] | 1407 | mProxyServiceStateCallbacks.remove(cb); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1410 | |
| 1411 | /** |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1412 | * Callback interface used to deliver LE scan results. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1413 | * |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1414 | * @see #startLeScan(LeScanCallback) |
| 1415 | * @see #startLeScan(UUID[], LeScanCallback) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1416 | */ |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1417 | public interface LeScanCallback { |
| 1418 | /** |
| 1419 | * Callback reporting an LE device found during a device scan initiated |
| 1420 | * by the {@link BluetoothAdapter#startLeScan} function. |
| 1421 | * |
| 1422 | * @param device Identifies the remote device |
| 1423 | * @param rssi The RSSI value for the remote device as reported by the |
| 1424 | * Bluetooth hardware. 0 if no RSSI value is available. |
| 1425 | * @param scanRecord The content of the advertisement record offered by |
| 1426 | * the remote device. |
| 1427 | */ |
| 1428 | public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * Starts a scan for Bluetooth LE devices. |
| 1433 | * |
| 1434 | * <p>Results of the scan are reported using the |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1435 | * {@link LeScanCallback#onLeScan} callback. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1436 | * |
Matthew Xie | d575233 | 2013-04-24 17:51:37 -0700 | [diff] [blame] | 1437 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1438 | * |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1439 | * @param callback the callback LE scan results are delivered |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1440 | * @return true, if the scan was started successfully |
| 1441 | */ |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1442 | public boolean startLeScan(LeScanCallback callback) { |
| 1443 | return startLeScan(null, callback); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /** |
| 1447 | * Starts a scan for Bluetooth LE devices, looking for devices that |
| 1448 | * advertise given services. |
| 1449 | * |
| 1450 | * <p>Devices which advertise all specified services are reported using the |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1451 | * {@link LeScanCallback#onLeScan} callback. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1452 | * |
Matthew Xie | d575233 | 2013-04-24 17:51:37 -0700 | [diff] [blame] | 1453 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1454 | * |
| 1455 | * @param serviceUuids Array of services to look for |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1456 | * @param callback the callback LE scan results are delivered |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1457 | * @return true, if the scan was started successfully |
| 1458 | */ |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1459 | public boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback) { |
| 1460 | if (DBG) Log.d(TAG, "startLeScan(): " + serviceUuids); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1461 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1462 | if (callback == null) { |
| 1463 | if (DBG) Log.e(TAG, "startLeScan: null callback"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1464 | return false; |
| 1465 | } |
| 1466 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1467 | synchronized(mLeScanClients) { |
| 1468 | if (mLeScanClients.containsKey(callback)) { |
| 1469 | if (DBG) Log.e(TAG, "LE Scan has already started"); |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | try { |
| 1474 | IBluetoothGatt iGatt = mManagerService.getBluetoothGatt(); |
| 1475 | UUID uuid = UUID.randomUUID(); |
| 1476 | GattCallbackWrapper wrapper = new GattCallbackWrapper(this, callback, serviceUuids); |
| 1477 | |
| 1478 | iGatt.registerClient(new ParcelUuid(uuid), wrapper); |
| 1479 | if (wrapper.scanStarted()) { |
| 1480 | mLeScanClients.put(callback, wrapper); |
| 1481 | return true; |
| 1482 | } |
| 1483 | } catch (RemoteException e) { |
| 1484 | Log.e(TAG,"",e); |
| 1485 | } |
| 1486 | } |
| 1487 | return false; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * Stops an ongoing Bluetooth LE device scan. |
| 1492 | * |
Matthew Xie | d575233 | 2013-04-24 17:51:37 -0700 | [diff] [blame] | 1493 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1494 | * |
| 1495 | * @param callback used to identify which scan to stop |
| 1496 | * must be the same handle used to start the scan |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1497 | */ |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1498 | public void stopLeScan(LeScanCallback callback) { |
| 1499 | if (DBG) Log.d(TAG, "stopLeScan()"); |
| 1500 | GattCallbackWrapper wrapper; |
| 1501 | synchronized(mLeScanClients) { |
| 1502 | wrapper = mLeScanClients.remove(callback); |
| 1503 | if (wrapper == null) return; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1504 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1505 | wrapper.stopLeScan(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * Bluetooth GATT interface callbacks |
| 1510 | */ |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1511 | private static class GattCallbackWrapper extends IBluetoothGattCallback.Stub { |
| 1512 | private static final int LE_CALLBACK_REG_TIMEOUT = 2000; |
| 1513 | private static final int LE_CALLBACK_REG_WAIT_COUNT = 5; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1514 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1515 | private final LeScanCallback mLeScanCb; |
| 1516 | // mLeHandle 0: not registered |
| 1517 | // -1: scan stopped |
| 1518 | // >0: registered and scan started |
| 1519 | private int mLeHandle; |
| 1520 | private final UUID[] mScanFilter; |
| 1521 | private WeakReference<BluetoothAdapter> mBluetoothAdapter; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1522 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1523 | public GattCallbackWrapper(BluetoothAdapter bluetoothAdapter, |
| 1524 | LeScanCallback leScanCb, UUID[] uuid) { |
| 1525 | mBluetoothAdapter = new WeakReference<BluetoothAdapter>(bluetoothAdapter); |
| 1526 | mLeScanCb = leScanCb; |
| 1527 | mScanFilter = uuid; |
| 1528 | mLeHandle = 0; |
| 1529 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1530 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1531 | public boolean scanStarted() { |
| 1532 | boolean started = false; |
| 1533 | synchronized(this) { |
| 1534 | if (mLeHandle == -1) return false; |
| 1535 | |
| 1536 | int count = 0; |
| 1537 | // wait for callback registration and LE scan to start |
| 1538 | while (mLeHandle == 0 && count < LE_CALLBACK_REG_WAIT_COUNT) { |
| 1539 | try { |
| 1540 | wait(LE_CALLBACK_REG_TIMEOUT); |
| 1541 | } catch (InterruptedException e) { |
| 1542 | Log.e(TAG, "Callback reg wait interrupted: " + e); |
| 1543 | } |
| 1544 | count++; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1545 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1546 | started = (mLeHandle > 0); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1547 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1548 | return started; |
| 1549 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1550 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1551 | public void stopLeScan() { |
| 1552 | synchronized(this) { |
| 1553 | if (mLeHandle <= 0) { |
| 1554 | Log.e(TAG, "Error state, mLeHandle: " + mLeHandle); |
| 1555 | return; |
| 1556 | } |
| 1557 | BluetoothAdapter adapter = mBluetoothAdapter.get(); |
| 1558 | if (adapter != null) { |
| 1559 | try { |
| 1560 | IBluetoothGatt iGatt = adapter.getBluetoothManager().getBluetoothGatt(); |
| 1561 | iGatt.stopScan(mLeHandle, false); |
| 1562 | iGatt.unregisterClient(mLeHandle); |
| 1563 | } catch (RemoteException e) { |
| 1564 | Log.e(TAG, "Failed to stop scan and unregister" + e); |
| 1565 | } |
| 1566 | } else { |
| 1567 | Log.e(TAG, "stopLeScan, BluetoothAdapter is null"); |
| 1568 | } |
| 1569 | mLeHandle = -1; |
| 1570 | notifyAll(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1571 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1572 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1573 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1574 | /** |
| 1575 | * Application interface registered - app is ready to go |
| 1576 | */ |
| 1577 | public void onClientRegistered(int status, int clientIf) { |
| 1578 | if (DBG) Log.d(TAG, "onClientRegistered() - status=" + status + |
| 1579 | " clientIf=" + clientIf); |
| 1580 | synchronized(this) { |
| 1581 | if (mLeHandle == -1) { |
| 1582 | if (DBG) Log.d(TAG, "onClientRegistered LE scan canceled"); |
| 1583 | } |
| 1584 | |
| 1585 | if (status == BluetoothGatt.GATT_SUCCESS) { |
| 1586 | mLeHandle = clientIf; |
| 1587 | IBluetoothGatt iGatt = null; |
| 1588 | try { |
| 1589 | BluetoothAdapter adapter = mBluetoothAdapter.get(); |
| 1590 | if (adapter != null) { |
| 1591 | iGatt = adapter.getBluetoothManager().getBluetoothGatt(); |
| 1592 | if (mScanFilter == null) { |
| 1593 | iGatt.startScan(mLeHandle, false); |
| 1594 | } else { |
| 1595 | ParcelUuid[] uuids = new ParcelUuid[mScanFilter.length]; |
| 1596 | for(int i = 0; i != uuids.length; ++i) { |
| 1597 | uuids[i] = new ParcelUuid(mScanFilter[i]); |
| 1598 | } |
| 1599 | iGatt.startScanWithUuids(mLeHandle, false, uuids); |
| 1600 | } |
| 1601 | } else { |
| 1602 | Log.e(TAG, "onClientRegistered, BluetoothAdapter null"); |
| 1603 | mLeHandle = -1; |
| 1604 | } |
| 1605 | } catch (RemoteException e) { |
| 1606 | Log.e(TAG, "fail to start le scan: " + e); |
| 1607 | mLeHandle = -1; |
| 1608 | } |
| 1609 | if (mLeHandle == -1) { |
| 1610 | // registration succeeded but start scan failed |
| 1611 | if (iGatt != null) { |
| 1612 | try { |
| 1613 | iGatt.unregisterClient(mLeHandle); |
| 1614 | } catch (RemoteException e) { |
| 1615 | Log.e(TAG, "fail to unregister callback: " + mLeHandle + |
| 1616 | " error: " + e); |
| 1617 | } |
| 1618 | } |
| 1619 | } |
| 1620 | } else { |
| 1621 | // registration failed |
| 1622 | mLeHandle = -1; |
| 1623 | } |
| 1624 | notifyAll(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1625 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1626 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1627 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1628 | public void onClientConnectionState(int status, int clientIf, |
| 1629 | boolean connected, String address) { |
| 1630 | // no op |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * Callback reporting an LE scan result. |
| 1635 | * @hide |
| 1636 | */ |
| 1637 | public void onScanResult(String address, int rssi, byte[] advData) { |
| 1638 | if (DBG) Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" +rssi); |
| 1639 | |
| 1640 | // Check null in case the scan has been stopped |
| 1641 | synchronized(this) { |
| 1642 | if (mLeHandle <= 0) return; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1643 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1644 | try { |
| 1645 | BluetoothAdapter adapter = mBluetoothAdapter.get(); |
| 1646 | if (adapter == null) { |
| 1647 | Log.d(TAG, "onScanResult, BluetoothAdapter null"); |
| 1648 | return; |
| 1649 | } |
| 1650 | mLeScanCb.onLeScan(adapter.getRemoteDevice(address), rssi, advData); |
| 1651 | } catch (Exception ex) { |
| 1652 | Log.w(TAG, "Unhandled exception: " + ex); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1653 | } |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1654 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1655 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1656 | public void onGetService(String address, int srvcType, |
| 1657 | int srvcInstId, ParcelUuid srvcUuid) { |
| 1658 | // no op |
| 1659 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1660 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1661 | public void onGetIncludedService(String address, int srvcType, |
| 1662 | int srvcInstId, ParcelUuid srvcUuid, |
| 1663 | int inclSrvcType, int inclSrvcInstId, |
| 1664 | ParcelUuid inclSrvcUuid) { |
| 1665 | // no op |
| 1666 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1667 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1668 | public void onGetCharacteristic(String address, int srvcType, |
| 1669 | int srvcInstId, ParcelUuid srvcUuid, |
| 1670 | int charInstId, ParcelUuid charUuid, |
| 1671 | int charProps) { |
| 1672 | // no op |
| 1673 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1674 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1675 | public void onGetDescriptor(String address, int srvcType, |
| 1676 | int srvcInstId, ParcelUuid srvcUuid, |
| 1677 | int charInstId, ParcelUuid charUuid, |
| 1678 | ParcelUuid descUuid) { |
| 1679 | // no op |
| 1680 | } |
| 1681 | |
| 1682 | public void onSearchComplete(String address, int status) { |
| 1683 | // no op |
| 1684 | } |
| 1685 | |
| 1686 | public void onCharacteristicRead(String address, int status, int srvcType, |
| 1687 | int srvcInstId, ParcelUuid srvcUuid, |
| 1688 | int charInstId, ParcelUuid charUuid, byte[] value) { |
| 1689 | // no op |
| 1690 | } |
| 1691 | |
| 1692 | public void onCharacteristicWrite(String address, int status, int srvcType, |
| 1693 | int srvcInstId, ParcelUuid srvcUuid, |
| 1694 | int charInstId, ParcelUuid charUuid) { |
| 1695 | // no op |
| 1696 | } |
| 1697 | |
| 1698 | public void onNotify(String address, int srvcType, |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1699 | int srvcInstId, ParcelUuid srvcUuid, |
| 1700 | int charInstId, ParcelUuid charUuid, |
| 1701 | byte[] value) { |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1702 | // no op |
| 1703 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1704 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1705 | public void onDescriptorRead(String address, int status, int srvcType, |
| 1706 | int srvcInstId, ParcelUuid srvcUuid, |
| 1707 | int charInstId, ParcelUuid charUuid, |
| 1708 | ParcelUuid descrUuid, byte[] value) { |
| 1709 | // no op |
| 1710 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1711 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1712 | public void onDescriptorWrite(String address, int status, int srvcType, |
| 1713 | int srvcInstId, ParcelUuid srvcUuid, |
| 1714 | int charInstId, ParcelUuid charUuid, |
| 1715 | ParcelUuid descrUuid) { |
| 1716 | // no op |
| 1717 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1718 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1719 | public void onExecuteWrite(String address, int status) { |
| 1720 | // no op |
| 1721 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1722 | |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 1723 | public void onReadRemoteRssi(String address, int rssi, int status) { |
| 1724 | // no op |
| 1725 | } |
| 1726 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1727 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 1728 | } |