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