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