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