blob: 72ecda1211e5c33f3f0778547b107af7c42e9428 [file] [log] [blame]
Nick Pellybd022f42009-08-14 18:33:38 -07001/*
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
17package android.bluetooth;
18
Nick Pellyde893f52009-09-08 13:15:33 -070019import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -070021import android.content.Context;
Nick Pelly24bb9b82009-10-02 20:34:18 -070022import android.os.Binder;
Nick Pellyf242b7b2009-10-08 00:12:45 +020023import android.os.IBinder;
Nick Pelly24bb9b82009-10-02 20:34:18 -070024import android.os.Message;
Nick Pellyaef439e2009-09-28 12:33:17 -070025import android.os.ParcelUuid;
Nick Pellybd022f42009-08-14 18:33:38 -070026import android.os.RemoteException;
Nick Pellyf242b7b2009-10-08 00:12:45 +020027import android.os.ServiceManager;
Nick Pellybd022f42009-08-14 18:33:38 -070028import android.util.Log;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -070029import android.util.Pair;
Nick Pellybd022f42009-08-14 18:33:38 -070030
31import java.io.IOException;
Matthew Xiecdd94e32013-04-11 16:36:26 -070032import java.lang.ref.WeakReference;
fredc903ac6f2012-04-24 03:59:57 -070033import java.util.ArrayList;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -070034import java.util.Arrays;
Nick Pellybd022f42009-08-14 18:33:38 -070035import java.util.Collections;
Nick Pellybd022f42009-08-14 18:33:38 -070036import java.util.HashSet;
Matthew Xiecdd94e32013-04-11 16:36:26 -070037import java.util.HashMap;
Nick Pelly24bb9b82009-10-02 20:34:18 -070038import java.util.LinkedList;
Jeff Sharkeyfea17de2013-06-11 14:13:09 -070039import java.util.Locale;
Matthew Xiecdd94e32013-04-11 16:36:26 -070040import java.util.Map;
Nick Pelly24bb9b82009-10-02 20:34:18 -070041import java.util.Random;
42import java.util.Set;
Nick Pelly16fb88a2009-10-07 07:44:03 +020043import java.util.UUID;
Nick Pellybd022f42009-08-14 18:33:38 -070044
45/**
Scott Main9fab0ae2009-11-03 18:17:59 -080046 * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter}
47 * lets you perform fundamental Bluetooth tasks, such as initiate
48 * device discovery, query a list of bonded (paired) devices,
49 * instantiate a {@link BluetoothDevice} using a known MAC address, and create
50 * a {@link BluetoothServerSocket} to listen for connection requests from other
Matthew Xieb30f91e2013-05-29 10:19:06 -070051 * devices, and start a scan for Bluetooth LE devices.
Scott Main9fab0ae2009-11-03 18:17:59 -080052 *
53 * <p>To get a {@link BluetoothAdapter} representing the local Bluetooth
Matthew Xieb30f91e2013-05-29 10:19:06 -070054 * adapter, when running on JELLY_BEAN_MR1 and below, call the
55 * static {@link #getDefaultAdapter} method; when running on JELLY_BEAN_MR2 and
56 * higher, retrieve it through
57 * {@link android.content.Context#getSystemService} with
58 * {@link android.content.Context#BLUETOOTH_SERVICE}.
Scott Main9fab0ae2009-11-03 18:17:59 -080059 * Fundamentally, this is your starting point for all
60 * Bluetooth actions. Once you have the local adapter, you can get a set of
61 * {@link BluetoothDevice} objects representing all paired devices with
62 * {@link #getBondedDevices()}; start device discovery with
63 * {@link #startDiscovery()}; or create a {@link BluetoothServerSocket} to
64 * listen for incoming connection requests with
Matthew Xieb30f91e2013-05-29 10:19:06 -070065 * {@link #listenUsingRfcommWithServiceRecord(String,UUID)}; or start a scan for
66 * Bluetooth LE devices with {@link #startLeScan(LeScanCallback callback)}.
Scott Main9fab0ae2009-11-03 18:17:59 -080067 *
68 * <p class="note"><strong>Note:</strong>
69 * Most methods require the {@link android.Manifest.permission#BLUETOOTH}
70 * permission and some also require the
71 * {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
72 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080073 * <div class="special reference">
74 * <h3>Developer Guides</h3>
75 * <p>For more information about using Bluetooth, read the
76 * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p>
77 * </div>
78 *
Scott Main9fab0ae2009-11-03 18:17:59 -080079 * {@see BluetoothDevice}
80 * {@see BluetoothServerSocket}
Nick Pellybd022f42009-08-14 18:33:38 -070081 */
82public final class BluetoothAdapter {
83 private static final String TAG = "BluetoothAdapter";
fredc0f420372012-04-12 00:02:00 -070084 private static final boolean DBG = true;
Matthew Xie3b6214f2012-08-29 00:12:29 -070085 private static final boolean VDBG = false;
Nick Pellybd022f42009-08-14 18:33:38 -070086
Nick Pellyde893f52009-09-08 13:15:33 -070087 /**
Nick Pellyb24e11b2009-09-08 17:40:43 -070088 * Sentinel error value for this class. Guaranteed to not equal any other
89 * integer constant in this class. Provided as a convenience for functions
90 * that require a sentinel error value, for example:
91 * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
92 * BluetoothAdapter.ERROR)</code>
93 */
Nick Pelly005b2282009-09-10 10:21:56 -070094 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070095
96 /**
Nick Pellyde893f52009-09-08 13:15:33 -070097 * Broadcast Action: The state of the local Bluetooth adapter has been
98 * changed.
99 * <p>For example, Bluetooth has been turned on or off.
Nick Pelly005b2282009-09-10 10:21:56 -0700100 * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link
Nick Pellyde893f52009-09-08 13:15:33 -0700101 * #EXTRA_PREVIOUS_STATE} containing the new and old states
102 * respectively.
103 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
104 */
105 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
106 public static final String ACTION_STATE_CHANGED =
Nick Pelly005b2282009-09-10 10:21:56 -0700107 "android.bluetooth.adapter.action.STATE_CHANGED";
Nick Pellybd022f42009-08-14 18:33:38 -0700108
Nick Pellyde893f52009-09-08 13:15:33 -0700109 /**
110 * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
111 * intents to request the current power state. Possible values are:
112 * {@link #STATE_OFF},
113 * {@link #STATE_TURNING_ON},
114 * {@link #STATE_ON},
115 * {@link #STATE_TURNING_OFF},
116 */
117 public static final String EXTRA_STATE =
Nick Pelly005b2282009-09-10 10:21:56 -0700118 "android.bluetooth.adapter.extra.STATE";
Nick Pellyde893f52009-09-08 13:15:33 -0700119 /**
120 * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
121 * intents to request the previous power state. Possible values are:
122 * {@link #STATE_OFF},
123 * {@link #STATE_TURNING_ON},
124 * {@link #STATE_ON},
125 * {@link #STATE_TURNING_OFF},
126 */
127 public static final String EXTRA_PREVIOUS_STATE =
Nick Pelly005b2282009-09-10 10:21:56 -0700128 "android.bluetooth.adapter.extra.PREVIOUS_STATE";
Nick Pellybd022f42009-08-14 18:33:38 -0700129
Nick Pellyde893f52009-09-08 13:15:33 -0700130 /**
131 * Indicates the local Bluetooth adapter is off.
132 */
Nick Pelly005b2282009-09-10 10:21:56 -0700133 public static final int STATE_OFF = 10;
Nick Pellyde893f52009-09-08 13:15:33 -0700134 /**
135 * Indicates the local Bluetooth adapter is turning on. However local
136 * clients should wait for {@link #STATE_ON} before attempting to
137 * use the adapter.
138 */
Nick Pelly005b2282009-09-10 10:21:56 -0700139 public static final int STATE_TURNING_ON = 11;
Nick Pellyde893f52009-09-08 13:15:33 -0700140 /**
141 * Indicates the local Bluetooth adapter is on, and ready for use.
142 */
Nick Pelly005b2282009-09-10 10:21:56 -0700143 public static final int STATE_ON = 12;
Nick Pellyde893f52009-09-08 13:15:33 -0700144 /**
145 * Indicates the local Bluetooth adapter is turning off. Local clients
146 * should immediately attempt graceful disconnection of any remote links.
147 */
Nick Pelly005b2282009-09-10 10:21:56 -0700148 public static final int STATE_TURNING_OFF = 13;
Nick Pellyde893f52009-09-08 13:15:33 -0700149
150 /**
Nick Pelly18b1e792009-09-24 11:14:15 -0700151 * Activity Action: Show a system activity that requests discoverable mode.
Scott Main6d95fc02009-11-19 17:00:19 -0800152 * This activity will also request the user to turn on Bluetooth if it
Nick Pelly1acdcc12009-09-28 10:33:55 -0700153 * is not currently enabled.
Nick Pelly18b1e792009-09-24 11:14:15 -0700154 * <p>Discoverable mode is equivalent to {@link
155 * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see
156 * this Bluetooth adapter when they perform a discovery.
Scott Main6d95fc02009-11-19 17:00:19 -0800157 * <p>For privacy, Android is not discoverable by default.
158 * <p>The sender of this Intent can optionally use extra field {@link
Nick Pelly18b1e792009-09-24 11:14:15 -0700159 * #EXTRA_DISCOVERABLE_DURATION} to request the duration of
160 * discoverability. Currently the default duration is 120 seconds, and
161 * maximum duration is capped at 300 seconds for each request.
162 * <p>Notification of the result of this activity is posted using the
163 * {@link android.app.Activity#onActivityResult} callback. The
164 * <code>resultCode</code>
Michael Chancdd28642009-11-05 18:29:01 -0800165 * will be the duration (in seconds) of discoverability or
166 * {@link android.app.Activity#RESULT_CANCELED} if the user rejected
167 * discoverability or an error has occurred.
Nick Pelly18b1e792009-09-24 11:14:15 -0700168 * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED}
Scott Main6d95fc02009-11-19 17:00:19 -0800169 * for global notification whenever the scan mode changes. For example, an
170 * application can be notified when the device has ended discoverability.
Nick Pelly1acdcc12009-09-28 10:33:55 -0700171 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pelly18b1e792009-09-24 11:14:15 -0700172 */
173 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
174 public static final String ACTION_REQUEST_DISCOVERABLE =
175 "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE";
176
177 /**
178 * Used as an optional int extra field in {@link
179 * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration
180 * for discoverability in seconds. The current default is 120 seconds, and
181 * requests over 300 seconds will be capped. These values could change.
182 */
183 public static final String EXTRA_DISCOVERABLE_DURATION =
184 "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION";
185
186 /**
Nick Pelly1acdcc12009-09-28 10:33:55 -0700187 * Activity Action: Show a system activity that allows the user to turn on
188 * Bluetooth.
189 * <p>This system activity will return once Bluetooth has completed turning
190 * on, or the user has decided not to turn Bluetooth on.
191 * <p>Notification of the result of this activity is posted using the
192 * {@link android.app.Activity#onActivityResult} callback. The
193 * <code>resultCode</code>
Michael Chancdd28642009-11-05 18:29:01 -0800194 * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
195 * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
196 * has rejected the request or an error has occurred.
Nick Pelly1acdcc12009-09-28 10:33:55 -0700197 * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED}
198 * for global notification whenever Bluetooth is turned on or off.
199 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
200 */
201 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
202 public static final String ACTION_REQUEST_ENABLE =
203 "android.bluetooth.adapter.action.REQUEST_ENABLE";
204
205 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700206 * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter
207 * has changed.
Nick Pelly005b2282009-09-10 10:21:56 -0700208 * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link
Nick Pellyde893f52009-09-08 13:15:33 -0700209 * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes
210 * respectively.
211 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
212 */
213 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
214 public static final String ACTION_SCAN_MODE_CHANGED =
Nick Pelly005b2282009-09-10 10:21:56 -0700215 "android.bluetooth.adapter.action.SCAN_MODE_CHANGED";
Nick Pellyde893f52009-09-08 13:15:33 -0700216
217 /**
218 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
219 * intents to request the current scan mode. Possible values are:
220 * {@link #SCAN_MODE_NONE},
221 * {@link #SCAN_MODE_CONNECTABLE},
222 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
223 */
Nick Pelly005b2282009-09-10 10:21:56 -0700224 public static final String EXTRA_SCAN_MODE = "android.bluetooth.adapter.extra.SCAN_MODE";
Nick Pellyde893f52009-09-08 13:15:33 -0700225 /**
226 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
227 * intents to request the previous scan mode. Possible values are:
228 * {@link #SCAN_MODE_NONE},
229 * {@link #SCAN_MODE_CONNECTABLE},
230 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
231 */
232 public static final String EXTRA_PREVIOUS_SCAN_MODE =
Nick Pelly005b2282009-09-10 10:21:56 -0700233 "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE";
Nick Pellyde893f52009-09-08 13:15:33 -0700234
235 /**
236 * Indicates that both inquiry scan and page scan are disabled on the local
237 * Bluetooth adapter. Therefore this device is neither discoverable
238 * nor connectable from remote Bluetooth devices.
239 */
Nick Pelly005b2282009-09-10 10:21:56 -0700240 public static final int SCAN_MODE_NONE = 20;
Nick Pellyde893f52009-09-08 13:15:33 -0700241 /**
242 * Indicates that inquiry scan is disabled, but page scan is enabled on the
243 * local Bluetooth adapter. Therefore this device is not discoverable from
244 * remote Bluetooth devices, but is connectable from remote devices that
245 * have previously discovered this device.
246 */
Nick Pelly005b2282009-09-10 10:21:56 -0700247 public static final int SCAN_MODE_CONNECTABLE = 21;
Nick Pellyde893f52009-09-08 13:15:33 -0700248 /**
249 * Indicates that both inquiry scan and page scan are enabled on the local
250 * Bluetooth adapter. Therefore this device is both discoverable and
251 * connectable from remote Bluetooth devices.
252 */
Nick Pelly005b2282009-09-10 10:21:56 -0700253 public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23;
Nick Pellybd022f42009-08-14 18:33:38 -0700254
Nick Pelly005b2282009-09-10 10:21:56 -0700255
256 /**
257 * Broadcast Action: The local Bluetooth adapter has started the remote
258 * device discovery process.
259 * <p>This usually involves an inquiry scan of about 12 seconds, followed
260 * by a page scan of each new device to retrieve its Bluetooth name.
261 * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as
262 * remote Bluetooth devices are found.
263 * <p>Device discovery is a heavyweight procedure. New connections to
264 * remote Bluetooth devices should not be attempted while discovery is in
265 * progress, and existing connections will experience limited bandwidth
266 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
267 * discovery.
268 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
269 */
270 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
271 public static final String ACTION_DISCOVERY_STARTED =
272 "android.bluetooth.adapter.action.DISCOVERY_STARTED";
273 /**
274 * Broadcast Action: The local Bluetooth adapter has finished the device
275 * discovery process.
276 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
277 */
278 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
279 public static final String ACTION_DISCOVERY_FINISHED =
280 "android.bluetooth.adapter.action.DISCOVERY_FINISHED";
281
282 /**
283 * Broadcast Action: The local Bluetooth adapter has changed its friendly
284 * Bluetooth name.
285 * <p>This name is visible to remote Bluetooth devices.
286 * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing
287 * the name.
288 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
289 */
290 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
291 public static final String ACTION_LOCAL_NAME_CHANGED =
292 "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED";
293 /**
294 * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED}
295 * intents to request the local Bluetooth name.
296 */
297 public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME";
298
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700299 /**
300 * Intent used to broadcast the change in connection state of the local
301 * Bluetooth adapter to a profile of the remote device. When the adapter is
302 * not connected to any profiles of any remote devices and it attempts a
303 * connection to a profile this intent will sent. Once connected, this intent
304 * will not be sent for any more connection attempts to any profiles of any
305 * remote device. When the adapter disconnects from the last profile its
306 * connected to of any remote device, this intent will be sent.
307 *
308 * <p> This intent is useful for applications that are only concerned about
309 * whether the local adapter is connected to any profile of any device and
310 * are not really concerned about which profile. For example, an application
311 * which displays an icon to display whether Bluetooth is connected or not
312 * can use this intent.
313 *
314 * <p>This intent will have 3 extras:
Jaikumar Ganesh0b5b35f2011-02-01 16:47:11 -0800315 * {@link #EXTRA_CONNECTION_STATE} - The current connection state.
316 * {@link #EXTRA_PREVIOUS_CONNECTION_STATE}- The previous connection state.
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700317 * {@link BluetoothDevice#EXTRA_DEVICE} - The remote device.
318 *
Jaikumar Ganesh0b5b35f2011-02-01 16:47:11 -0800319 * {@link #EXTRA_CONNECTION_STATE} or {@link #EXTRA_PREVIOUS_CONNECTION_STATE}
320 * can be any of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700321 * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
322 *
323 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
324 */
325 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
326 public static final String ACTION_CONNECTION_STATE_CHANGED =
327 "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED";
328
329 /**
330 * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED}
331 *
332 * This extra represents the current connection state.
333 */
334 public static final String EXTRA_CONNECTION_STATE =
335 "android.bluetooth.adapter.extra.CONNECTION_STATE";
336
337 /**
338 * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED}
339 *
340 * This extra represents the previous connection state.
341 */
342 public static final String EXTRA_PREVIOUS_CONNECTION_STATE =
343 "android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE";
344
345 /** The profile is in disconnected state */
346 public static final int STATE_DISCONNECTED = 0;
347 /** The profile is in connecting state */
348 public static final int STATE_CONNECTING = 1;
349 /** The profile is in connected state */
350 public static final int STATE_CONNECTED = 2;
351 /** The profile is in disconnecting state */
352 public static final int STATE_DISCONNECTING = 3;
353
Nick Pellyf242b7b2009-10-08 00:12:45 +0200354 /** @hide */
fredc0f420372012-04-12 00:02:00 -0700355 public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager";
Nick Pellyf242b7b2009-10-08 00:12:45 +0200356
Nick Pelly005b2282009-09-10 10:21:56 -0700357 private static final int ADDRESS_LENGTH = 17;
Nick Pellybd022f42009-08-14 18:33:38 -0700358
Nick Pellyf242b7b2009-10-08 00:12:45 +0200359 /**
Jake Hambyf51eada2010-09-21 13:39:53 -0700360 * Lazily initialized singleton. Guaranteed final after first object
Nick Pellyf242b7b2009-10-08 00:12:45 +0200361 * constructed.
362 */
363 private static BluetoothAdapter sAdapter;
364
fredc0f420372012-04-12 00:02:00 -0700365 private final IBluetoothManager mManagerService;
366 private IBluetooth mService;
Nick Pellybd022f42009-08-14 18:33:38 -0700367
Matthew Xiecdd94e32013-04-11 16:36:26 -0700368 private final Map<LeScanCallback, GattCallbackWrapper> mLeScanClients;
Matthew Xie484867a2011-08-25 16:45:58 -0700369
Nick Pellybd022f42009-08-14 18:33:38 -0700370 /**
Nick Pellyf242b7b2009-10-08 00:12:45 +0200371 * Get a handle to the default local Bluetooth adapter.
372 * <p>Currently Android only supports one Bluetooth adapter, but the API
373 * could be extended to support more. This will always return the default
374 * adapter.
375 * @return the default local adapter, or null if Bluetooth is not supported
376 * on this hardware platform
377 */
378 public static synchronized BluetoothAdapter getDefaultAdapter() {
379 if (sAdapter == null) {
fredc0f420372012-04-12 00:02:00 -0700380 IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
Nick Pellyf242b7b2009-10-08 00:12:45 +0200381 if (b != null) {
fredc0f420372012-04-12 00:02:00 -0700382 IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
383 sAdapter = new BluetoothAdapter(managerService);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800384 } else {
385 Log.e(TAG, "Bluetooth binder is null");
Nick Pellyf242b7b2009-10-08 00:12:45 +0200386 }
387 }
388 return sAdapter;
389 }
390
391 /**
392 * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.
Nick Pellybd022f42009-08-14 18:33:38 -0700393 */
fredc0f420372012-04-12 00:02:00 -0700394 BluetoothAdapter(IBluetoothManager managerService) {
395
396 if (managerService == null) {
397 throw new IllegalArgumentException("bluetooth manager service is null");
Nick Pellybd022f42009-08-14 18:33:38 -0700398 }
fredc0f420372012-04-12 00:02:00 -0700399 try {
400 mService = managerService.registerAdapter(mManagerCallback);
401 } catch (RemoteException e) {Log.e(TAG, "", e);}
402 mManagerService = managerService;
Matthew Xiecdd94e32013-04-11 16:36:26 -0700403 mLeScanClients = new HashMap<LeScanCallback, GattCallbackWrapper>();
Nick Pellybd022f42009-08-14 18:33:38 -0700404 }
405
406 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700407 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
408 * address.
409 * <p>Valid Bluetooth hardware addresses must be upper case, in a format
Nick Pelly005b2282009-09-10 10:21:56 -0700410 * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is
411 * available to validate a Bluetooth address.
Nick Pelly45e27042009-08-19 11:00:00 -0700412 * <p>A {@link BluetoothDevice} will always be returned for a valid
413 * hardware address, even if this adapter has never seen that device.
Nick Pellyde893f52009-09-08 13:15:33 -0700414 *
Nick Pellybd022f42009-08-14 18:33:38 -0700415 * @param address valid Bluetooth MAC address
Nick Pelly45e27042009-08-19 11:00:00 -0700416 * @throws IllegalArgumentException if address is invalid
Nick Pellybd022f42009-08-14 18:33:38 -0700417 */
418 public BluetoothDevice getRemoteDevice(String address) {
419 return new BluetoothDevice(address);
420 }
421
422 /**
Nick Pelly75596b42011-12-07 15:03:55 -0800423 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
424 * address.
425 * <p>Valid Bluetooth hardware addresses must be 6 bytes. This method
426 * expects the address in network byte order (MSB first).
427 * <p>A {@link BluetoothDevice} will always be returned for a valid
428 * hardware address, even if this adapter has never seen that device.
429 *
430 * @param address Bluetooth MAC address (6 bytes)
431 * @throws IllegalArgumentException if address is invalid
432 */
433 public BluetoothDevice getRemoteDevice(byte[] address) {
434 if (address == null || address.length != 6) {
435 throw new IllegalArgumentException("Bluetooth address must have 6 bytes");
436 }
Jeff Sharkeyfea17de2013-06-11 14:13:09 -0700437 return new BluetoothDevice(String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X",
Nick Pelly75596b42011-12-07 15:03:55 -0800438 address[0], address[1], address[2], address[3], address[4], address[5]));
439 }
440
441 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700442 * Return true if Bluetooth is currently enabled and ready for use.
443 * <p>Equivalent to:
444 * <code>getBluetoothState() == STATE_ON</code>
445 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700446 *
Nick Pellyde893f52009-09-08 13:15:33 -0700447 * @return true if the local adapter is turned on
Nick Pellybd022f42009-08-14 18:33:38 -0700448 */
449 public boolean isEnabled() {
fredc0f420372012-04-12 00:02:00 -0700450
Nick Pellybd022f42009-08-14 18:33:38 -0700451 try {
fredc0f420372012-04-12 00:02:00 -0700452 synchronized(mManagerCallback) {
453 if (mService != null) return mService.isEnabled();
454 }
Nick Pellybd022f42009-08-14 18:33:38 -0700455 } catch (RemoteException e) {Log.e(TAG, "", e);}
456 return false;
457 }
458
459 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700460 * Get the current state of the local Bluetooth adapter.
461 * <p>Possible return values are
462 * {@link #STATE_OFF},
463 * {@link #STATE_TURNING_ON},
464 * {@link #STATE_ON},
465 * {@link #STATE_TURNING_OFF}.
466 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700467 *
Nick Pellyde893f52009-09-08 13:15:33 -0700468 * @return current state of Bluetooth adapter
Nick Pellybd022f42009-08-14 18:33:38 -0700469 */
Nick Pellyde893f52009-09-08 13:15:33 -0700470 public int getState() {
Nick Pellybd022f42009-08-14 18:33:38 -0700471 try {
fredc0f420372012-04-12 00:02:00 -0700472 synchronized(mManagerCallback) {
473 if (mService != null)
474 {
fredcbf072a72012-05-09 16:52:50 -0700475 int state= mService.getState();
Matthew Xie3b6214f2012-08-29 00:12:29 -0700476 if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state);
fredcbf072a72012-05-09 16:52:50 -0700477 return state;
fredc0f420372012-04-12 00:02:00 -0700478 }
479 // TODO(BT) there might be a small gap during STATE_TURNING_ON that
480 // mService is null, handle that case
481 }
Nick Pellybd022f42009-08-14 18:33:38 -0700482 } catch (RemoteException e) {Log.e(TAG, "", e);}
fredcbf072a72012-05-09 16:52:50 -0700483 if (DBG) Log.d(TAG, "" + hashCode() + ": getState() : mService = null. Returning STATE_OFF");
Nick Pellyde893f52009-09-08 13:15:33 -0700484 return STATE_OFF;
Nick Pellybd022f42009-08-14 18:33:38 -0700485 }
486
487 /**
Scott Mained2a70d2009-12-09 16:07:39 -0800488 * Turn on the local Bluetooth adapter&mdash;do not use without explicit
489 * user action to turn on Bluetooth.
Nick Pellyde893f52009-09-08 13:15:33 -0700490 * <p>This powers on the underlying Bluetooth hardware, and starts all
491 * Bluetooth system services.
Scott Mained2a70d2009-12-09 16:07:39 -0800492 * <p class="caution"><strong>Bluetooth should never be enabled without
493 * direct user consent</strong>. If you want to turn on Bluetooth in order
494 * to create a wireless connection, you should use the {@link
495 * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests
496 * user permission to turn on Bluetooth. The {@link #enable()} method is
497 * provided only for applications that include a user interface for changing
498 * system settings, such as a "power manager" app.</p>
Brad Fitzpatrick3219ab42009-09-25 16:31:39 +0400499 * <p>This is an asynchronous call: it will return immediately, and
Nick Pellyde893f52009-09-08 13:15:33 -0700500 * clients should listen for {@link #ACTION_STATE_CHANGED}
501 * to be notified of subsequent adapter state changes. If this call returns
502 * true, then the adapter state will immediately transition from {@link
503 * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time
504 * later transition to either {@link #STATE_OFF} or {@link
505 * #STATE_ON}. If this call returns false then there was an
506 * immediate problem that will prevent the adapter from being turned on -
507 * such as Airplane mode, or the adapter is already turned on.
Scott Mained2a70d2009-12-09 16:07:39 -0800508 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
509 * permission
Nick Pellyde893f52009-09-08 13:15:33 -0700510 *
511 * @return true to indicate adapter startup has begun, or false on
512 * immediate error
Nick Pellybd022f42009-08-14 18:33:38 -0700513 */
514 public boolean enable() {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700515 if (isEnabled() == true){
516 if (DBG) Log.d(TAG, "enable(): BT is already enabled..!");
517 return true;
518 }
Nick Pellybd022f42009-08-14 18:33:38 -0700519 try {
Kausik Sinnaswamya097f512012-04-16 16:38:27 +0530520 return mManagerService.enable();
Nick Pellybd022f42009-08-14 18:33:38 -0700521 } catch (RemoteException e) {Log.e(TAG, "", e);}
522 return false;
523 }
524
525 /**
Scott Mained2a70d2009-12-09 16:07:39 -0800526 * Turn off the local Bluetooth adapter&mdash;do not use without explicit
527 * user action to turn off Bluetooth.
Nick Pellyde893f52009-09-08 13:15:33 -0700528 * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth
529 * system services, and powers down the underlying Bluetooth hardware.
Jake Hambyf51eada2010-09-21 13:39:53 -0700530 * <p class="caution"><strong>Bluetooth should never be disabled without
Scott Mained2a70d2009-12-09 16:07:39 -0800531 * direct user consent</strong>. The {@link #disable()} method is
532 * provided only for applications that include a user interface for changing
533 * system settings, such as a "power manager" app.</p>
Brad Fitzpatrick3219ab42009-09-25 16:31:39 +0400534 * <p>This is an asynchronous call: it will return immediately, and
Nick Pellyde893f52009-09-08 13:15:33 -0700535 * clients should listen for {@link #ACTION_STATE_CHANGED}
536 * to be notified of subsequent adapter state changes. If this call returns
537 * true, then the adapter state will immediately transition from {@link
538 * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time
539 * later transition to either {@link #STATE_OFF} or {@link
540 * #STATE_ON}. If this call returns false then there was an
541 * immediate problem that will prevent the adapter from being turned off -
542 * such as the adapter already being turned off.
Scott Mained2a70d2009-12-09 16:07:39 -0800543 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
544 * permission
Nick Pellybd022f42009-08-14 18:33:38 -0700545 *
Nick Pellyde893f52009-09-08 13:15:33 -0700546 * @return true to indicate adapter shutdown has begun, or false on
547 * immediate error
Nick Pellybd022f42009-08-14 18:33:38 -0700548 */
549 public boolean disable() {
550 try {
fredc0f420372012-04-12 00:02:00 -0700551 return mManagerService.disable(true);
Jaikumar Ganeshe21a4ac2012-03-06 17:15:16 -0800552 } catch (RemoteException e) {Log.e(TAG, "", e);}
553 return false;
554 }
555
556 /**
557 * Turn off the local Bluetooth adapter and don't persist the setting.
558 *
559 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
560 * permission
561 *
562 * @return true to indicate adapter shutdown has begun, or false on
563 * immediate error
564 * @hide
565 */
566 public boolean disable(boolean persist) {
fredc0f420372012-04-12 00:02:00 -0700567
Jaikumar Ganeshe21a4ac2012-03-06 17:15:16 -0800568 try {
fredc0f420372012-04-12 00:02:00 -0700569 return mManagerService.disable(persist);
Nick Pellybd022f42009-08-14 18:33:38 -0700570 } catch (RemoteException e) {Log.e(TAG, "", e);}
571 return false;
572 }
573
Nick Pellyde893f52009-09-08 13:15:33 -0700574 /**
575 * Returns the hardware address of the local Bluetooth adapter.
576 * <p>For example, "00:11:22:AA:BB:CC".
577 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
578 *
579 * @return Bluetooth hardware address as string
580 */
Nick Pellybd022f42009-08-14 18:33:38 -0700581 public String getAddress() {
582 try {
fredc0f420372012-04-12 00:02:00 -0700583 return mManagerService.getAddress();
Nick Pellybd022f42009-08-14 18:33:38 -0700584 } catch (RemoteException e) {Log.e(TAG, "", e);}
585 return null;
586 }
587
588 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700589 * Get the friendly Bluetooth name of the local Bluetooth adapter.
590 * <p>This name is visible to remote Bluetooth devices.
591 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700592 *
Nick Pellyde893f52009-09-08 13:15:33 -0700593 * @return the Bluetooth name, or null on error
Nick Pellybd022f42009-08-14 18:33:38 -0700594 */
595 public String getName() {
596 try {
fredc116d1d462012-04-20 14:47:08 -0700597 return mManagerService.getName();
Nick Pellybd022f42009-08-14 18:33:38 -0700598 } catch (RemoteException e) {Log.e(TAG, "", e);}
599 return null;
600 }
601
602 /**
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800603 * Get the UUIDs supported by the local Bluetooth adapter.
604 *
605 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
606 *
607 * @return the UUIDs supported by the local Bluetooth Adapter.
608 * @hide
609 */
610 public ParcelUuid[] getUuids() {
Matthew Xie44b58ab2011-11-16 12:27:57 -0800611 if (getState() != STATE_ON) return null;
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800612 try {
fredc0f420372012-04-12 00:02:00 -0700613 synchronized(mManagerCallback) {
614 if (mService != null) return mService.getUuids();
615 }
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800616 } catch (RemoteException e) {Log.e(TAG, "", e);}
617 return null;
618 }
619
620 /**
Jake Hamby0f584302010-09-16 18:12:51 -0700621 * Set the friendly Bluetooth name of the local Bluetooth adapter.
Nick Pellyde893f52009-09-08 13:15:33 -0700622 * <p>This name is visible to remote Bluetooth devices.
Jake Hamby0f584302010-09-16 18:12:51 -0700623 * <p>Valid Bluetooth names are a maximum of 248 bytes using UTF-8
624 * encoding, although many remote devices can only display the first
625 * 40 characters, and some may be limited to just 20.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700626 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
627 * will return false. After turning on Bluetooth,
628 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
629 * to get the updated value.
Nick Pellyde893f52009-09-08 13:15:33 -0700630 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
Nick Pellybd022f42009-08-14 18:33:38 -0700631 *
Nick Pellyde893f52009-09-08 13:15:33 -0700632 * @param name a valid Bluetooth name
633 * @return true if the name was set, false otherwise
Nick Pellybd022f42009-08-14 18:33:38 -0700634 */
635 public boolean setName(String name) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700636 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700637 try {
fredc0f420372012-04-12 00:02:00 -0700638 synchronized(mManagerCallback) {
639 if (mService != null) return mService.setName(name);
640 }
Nick Pellybd022f42009-08-14 18:33:38 -0700641 } catch (RemoteException e) {Log.e(TAG, "", e);}
642 return false;
643 }
644
645 /**
Jake Hamby0f584302010-09-16 18:12:51 -0700646 * Get the current Bluetooth scan mode of the local Bluetooth adapter.
Nick Pellyde893f52009-09-08 13:15:33 -0700647 * <p>The Bluetooth scan mode determines if the local adapter is
648 * connectable and/or discoverable from remote Bluetooth devices.
649 * <p>Possible values are:
650 * {@link #SCAN_MODE_NONE},
651 * {@link #SCAN_MODE_CONNECTABLE},
652 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700653 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
654 * will return {@link #SCAN_MODE_NONE}. After turning on Bluetooth,
655 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
656 * to get the updated value.
Nick Pellyde893f52009-09-08 13:15:33 -0700657 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
658 *
659 * @return scan mode
Nick Pellybd022f42009-08-14 18:33:38 -0700660 */
661 public int getScanMode() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700662 if (getState() != STATE_ON) return SCAN_MODE_NONE;
Nick Pellybd022f42009-08-14 18:33:38 -0700663 try {
fredc0f420372012-04-12 00:02:00 -0700664 synchronized(mManagerCallback) {
665 if (mService != null) return mService.getScanMode();
666 }
Nick Pellybd022f42009-08-14 18:33:38 -0700667 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyde893f52009-09-08 13:15:33 -0700668 return SCAN_MODE_NONE;
Nick Pellybd022f42009-08-14 18:33:38 -0700669 }
670
671 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700672 * Set the Bluetooth scan mode of the local Bluetooth adapter.
673 * <p>The Bluetooth scan mode determines if the local adapter is
674 * connectable and/or discoverable from remote Bluetooth devices.
Nick Pelly12835472009-09-25 15:00:29 -0700675 * <p>For privacy reasons, discoverable mode is automatically turned off
676 * after <code>duration</code> seconds. For example, 120 seconds should be
677 * enough for a remote device to initiate and complete its discovery
678 * process.
Nick Pellyde893f52009-09-08 13:15:33 -0700679 * <p>Valid scan mode values are:
680 * {@link #SCAN_MODE_NONE},
681 * {@link #SCAN_MODE_CONNECTABLE},
682 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700683 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
684 * will return false. After turning on Bluetooth,
685 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
686 * to get the updated value.
Nick Pelly18b1e792009-09-24 11:14:15 -0700687 * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS}
688 * <p>Applications cannot set the scan mode. They should use
689 * <code>startActivityForResult(
690 * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE})
691 * </code>instead.
Nick Pellyde893f52009-09-08 13:15:33 -0700692 *
693 * @param mode valid scan mode
Nick Pelly12835472009-09-25 15:00:29 -0700694 * @param duration time in seconds to apply scan mode, only used for
695 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}
Nick Pellyde893f52009-09-08 13:15:33 -0700696 * @return true if the scan mode was set, false otherwise
Nick Pelly18b1e792009-09-24 11:14:15 -0700697 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700698 */
Nick Pelly12835472009-09-25 15:00:29 -0700699 public boolean setScanMode(int mode, int duration) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700700 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700701 try {
fredc0f420372012-04-12 00:02:00 -0700702 synchronized(mManagerCallback) {
703 if (mService != null) return mService.setScanMode(mode, duration);
704 }
Nick Pellybd022f42009-08-14 18:33:38 -0700705 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyde893f52009-09-08 13:15:33 -0700706 return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700707 }
708
Nick Pelly45e27042009-08-19 11:00:00 -0700709 /** @hide */
Nick Pelly12835472009-09-25 15:00:29 -0700710 public boolean setScanMode(int mode) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700711 if (getState() != STATE_ON) return false;
Srikanth Uppala827de2d2012-04-04 03:33:26 -0700712 /* getDiscoverableTimeout() to use the latest from NV than use 0 */
713 return setScanMode(mode, getDiscoverableTimeout());
Nick Pelly12835472009-09-25 15:00:29 -0700714 }
715
716 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700717 public int getDiscoverableTimeout() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700718 if (getState() != STATE_ON) return -1;
Nick Pellybd022f42009-08-14 18:33:38 -0700719 try {
fredc0f420372012-04-12 00:02:00 -0700720 synchronized(mManagerCallback) {
721 if (mService != null) return mService.getDiscoverableTimeout();
722 }
Nick Pellybd022f42009-08-14 18:33:38 -0700723 } catch (RemoteException e) {Log.e(TAG, "", e);}
724 return -1;
725 }
726
Nick Pelly45e27042009-08-19 11:00:00 -0700727 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700728 public void setDiscoverableTimeout(int timeout) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700729 if (getState() != STATE_ON) return;
Nick Pellybd022f42009-08-14 18:33:38 -0700730 try {
fredc0f420372012-04-12 00:02:00 -0700731 synchronized(mManagerCallback) {
732 if (mService != null) mService.setDiscoverableTimeout(timeout);
733 }
Nick Pellybd022f42009-08-14 18:33:38 -0700734 } catch (RemoteException e) {Log.e(TAG, "", e);}
735 }
736
Nick Pelly005b2282009-09-10 10:21:56 -0700737 /**
738 * Start the remote device discovery process.
739 * <p>The discovery process usually involves an inquiry scan of about 12
740 * seconds, followed by a page scan of each new device to retrieve its
741 * Bluetooth name.
742 * <p>This is an asynchronous call, it will return immediately. Register
743 * for {@link #ACTION_DISCOVERY_STARTED} and {@link
744 * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the
745 * discovery starts and completes. Register for {@link
746 * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices
747 * are found.
748 * <p>Device discovery is a heavyweight procedure. New connections to
749 * remote Bluetooth devices should not be attempted while discovery is in
750 * progress, and existing connections will experience limited bandwidth
751 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
Scott Main6d95fc02009-11-19 17:00:19 -0800752 * discovery. Discovery is not managed by the Activity,
753 * but is run as a system service, so an application should always call
754 * {@link BluetoothAdapter#cancelDiscovery()} even if it
755 * did not directly request a discovery, just to be sure.
Nick Pelly005b2282009-09-10 10:21:56 -0700756 * <p>Device discovery will only find remote devices that are currently
757 * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are
758 * not discoverable by default, and need to be entered into a special mode.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700759 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
760 * will return false. After turning on Bluetooth,
761 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
762 * to get the updated value.
Nick Pelly005b2282009-09-10 10:21:56 -0700763 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
764 *
765 * @return true on success, false on error
766 */
Nick Pellybd022f42009-08-14 18:33:38 -0700767 public boolean startDiscovery() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700768 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700769 try {
fredc0f420372012-04-12 00:02:00 -0700770 synchronized(mManagerCallback) {
771 if (mService != null) return mService.startDiscovery();
772 }
Nick Pellybd022f42009-08-14 18:33:38 -0700773 } catch (RemoteException e) {Log.e(TAG, "", e);}
774 return false;
775 }
776
Nick Pelly005b2282009-09-10 10:21:56 -0700777 /**
778 * Cancel the current device discovery process.
779 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Jake Hamby0f584302010-09-16 18:12:51 -0700780 * <p>Because discovery is a heavyweight procedure for the Bluetooth
Scott Main6d95fc02009-11-19 17:00:19 -0800781 * adapter, this method should always be called before attempting to connect
782 * to a remote device with {@link
783 * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by
784 * the Activity, but is run as a system service, so an application should
785 * always call cancel discovery even if it did not directly request a
786 * discovery, just to be sure.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700787 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
788 * will return false. After turning on Bluetooth,
789 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
790 * to get the updated value.
Nick Pelly005b2282009-09-10 10:21:56 -0700791 *
792 * @return true on success, false on error
793 */
794 public boolean cancelDiscovery() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700795 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700796 try {
fredc0f420372012-04-12 00:02:00 -0700797 synchronized(mManagerCallback) {
798 if (mService != null) return mService.cancelDiscovery();
799 }
Nick Pellybd022f42009-08-14 18:33:38 -0700800 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700801 return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700802 }
803
Nick Pelly005b2282009-09-10 10:21:56 -0700804 /**
805 * Return true if the local Bluetooth adapter is currently in the device
806 * discovery process.
807 * <p>Device discovery is a heavyweight procedure. New connections to
808 * remote Bluetooth devices should not be attempted while discovery is in
809 * progress, and existing connections will experience limited bandwidth
810 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
811 * discovery.
812 * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED}
813 * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery
814 * starts or completes.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700815 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
816 * will return false. After turning on Bluetooth,
817 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
818 * to get the updated value.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200819 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Nick Pelly005b2282009-09-10 10:21:56 -0700820 *
821 * @return true if discovering
822 */
Nick Pellybd022f42009-08-14 18:33:38 -0700823 public boolean isDiscovering() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700824 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700825 try {
fredc0f420372012-04-12 00:02:00 -0700826 synchronized(mManagerCallback) {
827 if (mService != null ) return mService.isDiscovering();
828 }
Nick Pellybd022f42009-08-14 18:33:38 -0700829 } catch (RemoteException e) {Log.e(TAG, "", e);}
830 return false;
831 }
832
833 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700834 * Return the set of {@link BluetoothDevice} objects that are bonded
835 * (paired) to the local adapter.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700836 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
837 * will return an empty set. After turning on Bluetooth,
838 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
839 * to get the updated value.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200840 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Nick Pellybd022f42009-08-14 18:33:38 -0700841 *
Nick Pelly005b2282009-09-10 10:21:56 -0700842 * @return unmodifiable set of {@link BluetoothDevice}, or null on error
Nick Pellybd022f42009-08-14 18:33:38 -0700843 */
844 public Set<BluetoothDevice> getBondedDevices() {
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700845 if (getState() != STATE_ON) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800846 return toDeviceSet(new BluetoothDevice[0]);
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700847 }
Nick Pellybd022f42009-08-14 18:33:38 -0700848 try {
fredc0f420372012-04-12 00:02:00 -0700849 synchronized(mManagerCallback) {
850 if (mService != null) return toDeviceSet(mService.getBondedDevices());
851 }
852 return toDeviceSet(new BluetoothDevice[0]);
Nick Pellybd022f42009-08-14 18:33:38 -0700853 } catch (RemoteException e) {Log.e(TAG, "", e);}
854 return null;
855 }
856
857 /**
Jaikumar Ganeshc53cab22010-10-26 16:02:26 -0700858 * Get the current connection state of the local Bluetooth adapter.
859 * This can be used to check whether the local Bluetooth adapter is connected
860 * to any profile of any other remote Bluetooth Device.
861 *
862 * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED}
863 * intent to get the connection state of the adapter.
864 *
865 * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED},
866 * {@link #STATE_CONNECTING} or {@link #STATE_DISCONNECTED}
867 *
868 * @hide
869 */
870 public int getConnectionState() {
871 if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED;
872 try {
fredc0f420372012-04-12 00:02:00 -0700873 synchronized(mManagerCallback) {
874 if (mService != null) return mService.getAdapterConnectionState();
875 }
Jaikumar Ganeshc53cab22010-10-26 16:02:26 -0700876 } catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);}
877 return BluetoothAdapter.STATE_DISCONNECTED;
878 }
879
880 /**
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700881 * Get the current connection state of a profile.
882 * This function can be used to check whether the local Bluetooth adapter
883 * is connected to any remote device for a specific profile.
Scott Main2d68a6b2011-09-26 22:59:38 -0700884 * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET},
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700885 * {@link BluetoothProfile#A2DP}.
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700886 *
887 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
888 *
889 * <p> Return value can be one of
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700890 * {@link BluetoothProfile#STATE_DISCONNECTED},
891 * {@link BluetoothProfile#STATE_CONNECTING},
892 * {@link BluetoothProfile#STATE_CONNECTED},
893 * {@link BluetoothProfile#STATE_DISCONNECTING}
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700894 */
895 public int getProfileConnectionState(int profile) {
896 if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED;
897 try {
fredc0f420372012-04-12 00:02:00 -0700898 synchronized(mManagerCallback) {
899 if (mService != null) return mService.getProfileConnectionState(profile);
900 }
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700901 } catch (RemoteException e) {
902 Log.e(TAG, "getProfileConnectionState:", e);
903 }
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700904 return BluetoothProfile.STATE_DISCONNECTED;
905 }
906
907 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700908 * Create a listening, secure RFCOMM Bluetooth socket.
909 * <p>A remote device connecting to this socket will be authenticated and
Nick Pellybd022f42009-08-14 18:33:38 -0700910 * communication on this socket will be encrypted.
Nick Pelly45e27042009-08-19 11:00:00 -0700911 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
Nick Pelly24bb9b82009-10-02 20:34:18 -0700912 * connections from a listening {@link BluetoothServerSocket}.
Nick Pelly45e27042009-08-19 11:00:00 -0700913 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700914 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
Nick Pelly45e27042009-08-19 11:00:00 -0700915 * @param channel RFCOMM channel to listen on
916 * @return a listening RFCOMM BluetoothServerSocket
917 * @throws IOException on error, for example Bluetooth not available, or
918 * insufficient permissions, or channel in use.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700919 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700920 */
Nick Pelly45e27042009-08-19 11:00:00 -0700921 public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException {
Nick Pellybd022f42009-08-14 18:33:38 -0700922 BluetoothServerSocket socket = new BluetoothServerSocket(
Nick Pelly45e27042009-08-19 11:00:00 -0700923 BluetoothSocket.TYPE_RFCOMM, true, true, channel);
Nick Pelly24bb9b82009-10-02 20:34:18 -0700924 int errno = socket.mSocket.bindListen();
925 if (errno != 0) {
zzy3b147b72012-04-03 19:48:32 -0700926 //TODO(BT): Throw the same exception error code
927 // that the previous code was using.
928 //socket.mSocket.throwErrnoNative(errno);
929 throw new IOException("Error: " + errno);
Nick Pellybd022f42009-08-14 18:33:38 -0700930 }
931 return socket;
932 }
933
934 /**
Nick Pelly24bb9b82009-10-02 20:34:18 -0700935 * Create a listening, secure RFCOMM Bluetooth socket with Service Record.
936 * <p>A remote device connecting to this socket will be authenticated and
937 * communication on this socket will be encrypted.
938 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
939 * connections from a listening {@link BluetoothServerSocket}.
940 * <p>The system will assign an unused RFCOMM channel to listen on.
941 * <p>The system will also register a Service Discovery
942 * Protocol (SDP) record with the local SDP server containing the specified
943 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
944 * can use the same UUID to query our SDP server and discover which channel
945 * to connect to. This SDP record will be removed when this socket is
946 * closed, or if this application closes unexpectedly.
Nick Pelly16fb88a2009-10-07 07:44:03 +0200947 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
948 * connect to this socket from another device using the same {@link UUID}.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700949 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
950 * @param name service name for SDP record
951 * @param uuid uuid for SDP record
952 * @return a listening RFCOMM BluetoothServerSocket
953 * @throws IOException on error, for example Bluetooth not available, or
954 * insufficient permissions, or channel in use.
955 */
Nick Pelly16fb88a2009-10-07 07:44:03 +0200956 public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid)
Nick Pelly24bb9b82009-10-02 20:34:18 -0700957 throws IOException {
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800958 return createNewRfcommSocketAndRecord(name, uuid, true, true);
959 }
960
961 /**
962 * Create a listening, insecure RFCOMM Bluetooth socket with Service Record.
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100963 * <p>The link key is not required to be authenticated, i.e the communication may be
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800964 * vulnerable to Man In the Middle attacks. For Bluetooth 2.1 devices,
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100965 * the link will be encrypted, as encryption is mandartory.
966 * For legacy devices (pre Bluetooth 2.1 devices) the link will not
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800967 * be encrypted. Use {@link #listenUsingRfcommWithServiceRecord}, if an
968 * encrypted and authenticated communication channel is desired.
969 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
970 * connections from a listening {@link BluetoothServerSocket}.
971 * <p>The system will assign an unused RFCOMM channel to listen on.
972 * <p>The system will also register a Service Discovery
973 * Protocol (SDP) record with the local SDP server containing the specified
974 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
975 * can use the same UUID to query our SDP server and discover which channel
976 * to connect to. This SDP record will be removed when this socket is
977 * closed, or if this application closes unexpectedly.
978 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
979 * connect to this socket from another device using the same {@link UUID}.
980 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
981 * @param name service name for SDP record
982 * @param uuid uuid for SDP record
983 * @return a listening RFCOMM BluetoothServerSocket
984 * @throws IOException on error, for example Bluetooth not available, or
985 * insufficient permissions, or channel in use.
986 */
987 public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
988 throws IOException {
989 return createNewRfcommSocketAndRecord(name, uuid, false, false);
990 }
991
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100992 /**
993 * Create a listening, encrypted,
994 * RFCOMM Bluetooth socket with Service Record.
995 * <p>The link will be encrypted, but the link key is not required to be authenticated
996 * i.e the communication is vulnerable to Man In the Middle attacks. Use
997 * {@link #listenUsingRfcommWithServiceRecord}, to ensure an authenticated link key.
998 * <p> Use this socket if authentication of link key is not possible.
999 * For example, for Bluetooth 2.1 devices, if any of the devices does not have
1000 * an input and output capability or just has the ability to display a numeric key,
1001 * a secure socket connection is not possible and this socket can be used.
1002 * Use {@link #listenUsingInsecureRfcommWithServiceRecord}, if encryption is not required.
1003 * For Bluetooth 2.1 devices, the link will be encrypted, as encryption is mandartory.
1004 * For more details, refer to the Security Model section 5.2 (vol 3) of
1005 * Bluetooth Core Specification version 2.1 + EDR.
1006 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
1007 * connections from a listening {@link BluetoothServerSocket}.
1008 * <p>The system will assign an unused RFCOMM channel to listen on.
1009 * <p>The system will also register a Service Discovery
1010 * Protocol (SDP) record with the local SDP server containing the specified
1011 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
1012 * can use the same UUID to query our SDP server and discover which channel
1013 * to connect to. This SDP record will be removed when this socket is
1014 * closed, or if this application closes unexpectedly.
1015 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
1016 * connect to this socket from another device using the same {@link UUID}.
1017 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1018 * @param name service name for SDP record
1019 * @param uuid uuid for SDP record
1020 * @return a listening RFCOMM BluetoothServerSocket
1021 * @throws IOException on error, for example Bluetooth not available, or
1022 * insufficient permissions, or channel in use.
1023 * @hide
1024 */
1025 public BluetoothServerSocket listenUsingEncryptedRfcommWithServiceRecord(
1026 String name, UUID uuid) throws IOException {
1027 return createNewRfcommSocketAndRecord(name, uuid, false, true);
1028 }
1029
zzy3b147b72012-04-03 19:48:32 -07001030
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001031 private BluetoothServerSocket createNewRfcommSocketAndRecord(String name, UUID uuid,
1032 boolean auth, boolean encrypt) throws IOException {
Nick Pelly24bb9b82009-10-02 20:34:18 -07001033 BluetoothServerSocket socket;
zzy3b147b72012-04-03 19:48:32 -07001034 socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, auth,
1035 encrypt, new ParcelUuid(uuid));
1036 socket.setServiceName(name);
1037 int errno = socket.mSocket.bindListen();
1038 if (errno != 0) {
1039 //TODO(BT): Throw the same exception error code
1040 // that the previous code was using.
1041 //socket.mSocket.throwErrnoNative(errno);
1042 throw new IOException("Error: " + errno);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001043 }
Nick Pelly24bb9b82009-10-02 20:34:18 -07001044 return socket;
1045 }
1046
1047 /**
Nick Pellybd022f42009-08-14 18:33:38 -07001048 * Construct an unencrypted, unauthenticated, RFCOMM server socket.
1049 * Call #accept to retrieve connections to this socket.
1050 * @return An RFCOMM BluetoothServerSocket
1051 * @throws IOException On error, for example Bluetooth not available, or
1052 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001053 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001054 */
1055 public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
1056 BluetoothServerSocket socket = new BluetoothServerSocket(
1057 BluetoothSocket.TYPE_RFCOMM, false, false, port);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001058 int errno = socket.mSocket.bindListen();
1059 if (errno != 0) {
zzy3b147b72012-04-03 19:48:32 -07001060 //TODO(BT): Throw the same exception error code
1061 // that the previous code was using.
1062 //socket.mSocket.throwErrnoNative(errno);
1063 throw new IOException("Error: " + errno);
Nick Pellybd022f42009-08-14 18:33:38 -07001064 }
1065 return socket;
1066 }
1067
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +01001068 /**
1069 * Construct an encrypted, RFCOMM server socket.
1070 * Call #accept to retrieve connections to this socket.
1071 * @return An RFCOMM BluetoothServerSocket
1072 * @throws IOException On error, for example Bluetooth not available, or
1073 * insufficient permissions.
1074 * @hide
1075 */
1076 public BluetoothServerSocket listenUsingEncryptedRfcommOn(int port)
1077 throws IOException {
1078 BluetoothServerSocket socket = new BluetoothServerSocket(
1079 BluetoothSocket.TYPE_RFCOMM, false, true, port);
1080 int errno = socket.mSocket.bindListen();
zzy3b147b72012-04-03 19:48:32 -07001081 if (errno < 0) {
1082 //TODO(BT): Throw the same exception error code
1083 // that the previous code was using.
1084 //socket.mSocket.throwErrnoNative(errno);
1085 throw new IOException("Error: " + errno);
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +01001086 }
1087 return socket;
1088 }
1089
Nick Pellybd022f42009-08-14 18:33:38 -07001090 /**
1091 * Construct a SCO server socket.
1092 * Call #accept to retrieve connections to this socket.
1093 * @return A SCO BluetoothServerSocket
1094 * @throws IOException On error, for example Bluetooth not available, or
1095 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001096 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001097 */
1098 public static BluetoothServerSocket listenUsingScoOn() throws IOException {
1099 BluetoothServerSocket socket = new BluetoothServerSocket(
1100 BluetoothSocket.TYPE_SCO, false, false, -1);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001101 int errno = socket.mSocket.bindListen();
zzy3b147b72012-04-03 19:48:32 -07001102 if (errno < 0) {
1103 //TODO(BT): Throw the same exception error code
1104 // that the previous code was using.
1105 //socket.mSocket.throwErrnoNative(errno);
Nick Pellybd022f42009-08-14 18:33:38 -07001106 }
1107 return socket;
1108 }
1109
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001110 /**
1111 * Read the local Out of Band Pairing Data
1112 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1113 *
1114 * @return Pair<byte[], byte[]> of Hash and Randomizer
1115 *
1116 * @hide
1117 */
1118 public Pair<byte[], byte[]> readOutOfBandData() {
1119 if (getState() != STATE_ON) return null;
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001120 //TODO(BT
1121 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001122 try {
Jake Hambyf51eada2010-09-21 13:39:53 -07001123 byte[] hash;
1124 byte[] randomizer;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001125
1126 byte[] ret = mService.readOutOfBandData();
1127
1128 if (ret == null || ret.length != 32) return null;
1129
1130 hash = Arrays.copyOfRange(ret, 0, 16);
1131 randomizer = Arrays.copyOfRange(ret, 16, 32);
1132
1133 if (DBG) {
1134 Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) +
1135 ":" + Arrays.toString(randomizer));
1136 }
1137 return new Pair<byte[], byte[]>(hash, randomizer);
1138
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001139 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001140 return null;
1141 }
1142
Scott Main299ae672011-01-19 21:13:18 -08001143 /**
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001144 * Get the profile proxy object associated with the profile.
1145 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001146 * <p>Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET},
Matthew Xieddf7e472013-03-01 18:41:02 -08001147 * {@link BluetoothProfile#A2DP}, {@link BluetoothProfile#GATT}, or
1148 * {@link BluetoothProfile#GATT_SERVER}. Clients must implement
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001149 * {@link BluetoothProfile.ServiceListener} to get notified of
1150 * the connection status and to get the proxy object.
1151 *
1152 * @param context Context of the application
1153 * @param listener The service Listener for connection callbacks.
Scott Main2d68a6b2011-09-26 22:59:38 -07001154 * @param profile The Bluetooth profile; either {@link BluetoothProfile#HEALTH},
1155 * {@link BluetoothProfile#HEADSET} or {@link BluetoothProfile#A2DP}.
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001156 * @return true on success, false on error
1157 */
1158 public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener,
1159 int profile) {
1160 if (context == null || listener == null) return false;
1161
1162 if (profile == BluetoothProfile.HEADSET) {
1163 BluetoothHeadset headset = new BluetoothHeadset(context, listener);
1164 return true;
1165 } else if (profile == BluetoothProfile.A2DP) {
1166 BluetoothA2dp a2dp = new BluetoothA2dp(context, listener);
1167 return true;
Jaikumar Ganesh4ab0e772011-02-18 14:52:32 -08001168 } else if (profile == BluetoothProfile.INPUT_DEVICE) {
1169 BluetoothInputDevice iDev = new BluetoothInputDevice(context, listener);
1170 return true;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -08001171 } else if (profile == BluetoothProfile.PAN) {
1172 BluetoothPan pan = new BluetoothPan(context, listener);
1173 return true;
Jaikumar Ganesh2ea1e852011-04-01 16:33:09 -07001174 } else if (profile == BluetoothProfile.HEALTH) {
1175 BluetoothHealth health = new BluetoothHealth(context, listener);
1176 return true;
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001177 } else {
1178 return false;
1179 }
1180 }
1181
1182 /**
1183 * Close the connection of the profile proxy to the Service.
1184 *
1185 * <p> Clients should call this when they are no longer using
1186 * the proxy obtained from {@link #getProfileProxy}.
Scott Main2d68a6b2011-09-26 22:59:38 -07001187 * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET} or
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001188 * {@link BluetoothProfile#A2DP}
1189 *
1190 * @param profile
1191 * @param proxy Profile proxy object
1192 */
1193 public void closeProfileProxy(int profile, BluetoothProfile proxy) {
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -08001194 if (proxy == null) return;
1195
1196 switch (profile) {
1197 case BluetoothProfile.HEADSET:
1198 BluetoothHeadset headset = (BluetoothHeadset)proxy;
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001199 headset.close();
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -08001200 break;
1201 case BluetoothProfile.A2DP:
1202 BluetoothA2dp a2dp = (BluetoothA2dp)proxy;
1203 a2dp.close();
1204 break;
1205 case BluetoothProfile.INPUT_DEVICE:
1206 BluetoothInputDevice iDev = (BluetoothInputDevice)proxy;
1207 iDev.close();
1208 break;
1209 case BluetoothProfile.PAN:
1210 BluetoothPan pan = (BluetoothPan)proxy;
1211 pan.close();
1212 break;
1213 case BluetoothProfile.HEALTH:
1214 BluetoothHealth health = (BluetoothHealth)proxy;
1215 health.close();
1216 break;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001217 case BluetoothProfile.GATT:
1218 BluetoothGatt gatt = (BluetoothGatt)proxy;
1219 gatt.close();
1220 break;
1221 case BluetoothProfile.GATT_SERVER:
1222 BluetoothGattServer gattServer = (BluetoothGattServer)proxy;
1223 gattServer.close();
1224 break;
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001225 }
1226 }
1227
fredc0f420372012-04-12 00:02:00 -07001228 final private IBluetoothManagerCallback mManagerCallback =
1229 new IBluetoothManagerCallback.Stub() {
1230 public void onBluetoothServiceUp(IBluetooth bluetoothService) {
Matthew Xied77982e2012-11-29 20:26:19 -08001231 if (VDBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService);
fredc0f420372012-04-12 00:02:00 -07001232 synchronized (mManagerCallback) {
1233 mService = bluetoothService;
fredcbf072a72012-05-09 16:52:50 -07001234 for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
fredc903ac6f2012-04-24 03:59:57 -07001235 try {
fredcd6883532012-04-25 17:46:13 -07001236 if (cb != null) {
1237 cb.onBluetoothServiceUp(bluetoothService);
1238 } else {
1239 Log.d(TAG, "onBluetoothServiceUp: cb is null!!!");
1240 }
fredc903ac6f2012-04-24 03:59:57 -07001241 } catch (Exception e) { Log.e(TAG,"",e);}
1242 }
fredc0f420372012-04-12 00:02:00 -07001243 }
1244 }
1245
1246 public void onBluetoothServiceDown() {
Matthew Xied77982e2012-11-29 20:26:19 -08001247 if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService);
fredc0f420372012-04-12 00:02:00 -07001248 synchronized (mManagerCallback) {
1249 mService = null;
fredcbf072a72012-05-09 16:52:50 -07001250 for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
fredc903ac6f2012-04-24 03:59:57 -07001251 try {
fredcd6883532012-04-25 17:46:13 -07001252 if (cb != null) {
1253 cb.onBluetoothServiceDown();
1254 } else {
1255 Log.d(TAG, "onBluetoothServiceDown: cb is null!!!");
1256 }
fredc903ac6f2012-04-24 03:59:57 -07001257 } catch (Exception e) { Log.e(TAG,"",e);}
1258 }
fredc0f420372012-04-12 00:02:00 -07001259 }
1260 }
1261 };
1262
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001263 /**
Martijn Coenen6c614b72012-04-18 13:01:15 -07001264 * Enable the Bluetooth Adapter, but don't auto-connect devices
1265 * and don't persist state. Only for use by system applications.
1266 * @hide
1267 */
1268 public boolean enableNoAutoConnect() {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001269 if (isEnabled() == true){
1270 if (DBG) Log.d(TAG, "enableNoAutoConnect(): BT is already enabled..!");
1271 return true;
1272 }
1273 try {
1274 return mManagerService.enableNoAutoConnect();
1275 } catch (RemoteException e) {Log.e(TAG, "", e);}
1276 return false;
Martijn Coenen6c614b72012-04-18 13:01:15 -07001277 }
1278
1279 /**
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001280 * Enable control of the Bluetooth Adapter for a single application.
1281 *
1282 * <p>Some applications need to use Bluetooth for short periods of time to
1283 * transfer data but don't want all the associated implications like
1284 * automatic connection to headsets etc.
1285 *
1286 * <p> Multiple applications can call this. This is reference counted and
1287 * Bluetooth disabled only when no one else is using it. There will be no UI
1288 * shown to the user while bluetooth is being enabled. Any user action will
1289 * override this call. For example, if user wants Bluetooth on and the last
1290 * user of this API wanted to disable Bluetooth, Bluetooth will not be
1291 * turned off.
1292 *
1293 * <p> This API is only meant to be used by internal applications. Third
1294 * party applications but use {@link #enable} and {@link #disable} APIs.
1295 *
1296 * <p> If this API returns true, it means the callback will be called.
1297 * The callback will be called with the current state of Bluetooth.
1298 * If the state is not what was requested, an internal error would be the
Jaikumar Ganeshf5fb6c82011-08-03 14:17:22 -07001299 * reason. If Bluetooth is already on and if this function is called to turn
1300 * it on, the api will return true and a callback will be called.
1301 *
1302 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001303 *
1304 * @param on True for on, false for off.
1305 * @param callback The callback to notify changes to the state.
1306 * @hide
1307 */
1308 public boolean changeApplicationBluetoothState(boolean on,
1309 BluetoothStateChangeCallback callback) {
1310 if (callback == null) return false;
1311
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001312 //TODO(BT)
1313 /*
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001314 try {
1315 return mService.changeApplicationBluetoothState(on, new
1316 StateChangeCallbackWrapper(callback), new Binder());
1317 } catch (RemoteException e) {
1318 Log.e(TAG, "changeBluetoothState", e);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001319 }*/
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001320 return false;
1321 }
1322
1323 /**
1324 * @hide
1325 */
1326 public interface BluetoothStateChangeCallback {
1327 public void onBluetoothStateChange(boolean on);
1328 }
1329
1330 /**
1331 * @hide
1332 */
1333 public class StateChangeCallbackWrapper extends IBluetoothStateChangeCallback.Stub {
1334 private BluetoothStateChangeCallback mCallback;
1335
1336 StateChangeCallbackWrapper(BluetoothStateChangeCallback
1337 callback) {
1338 mCallback = callback;
1339 }
1340
1341 @Override
1342 public void onBluetoothStateChange(boolean on) {
1343 mCallback.onBluetoothStateChange(on);
1344 }
1345 }
1346
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001347 private Set<BluetoothDevice> toDeviceSet(BluetoothDevice[] devices) {
1348 Set<BluetoothDevice> deviceSet = new HashSet<BluetoothDevice>(Arrays.asList(devices));
1349 return Collections.unmodifiableSet(deviceSet);
Nick Pellybd022f42009-08-14 18:33:38 -07001350 }
Nick Pelly005b2282009-09-10 10:21:56 -07001351
fredc0f420372012-04-12 00:02:00 -07001352 protected void finalize() throws Throwable {
1353 try {
1354 mManagerService.unregisterAdapter(mManagerCallback);
1355 } catch (RemoteException e) {
1356 Log.e(TAG, "", e);
1357 } finally {
1358 super.finalize();
1359 }
1360 }
1361
1362
Nick Pelly005b2282009-09-10 10:21:56 -07001363 /**
Nick Pelly75596b42011-12-07 15:03:55 -08001364 * Validate a String Bluetooth address, such as "00:43:A8:23:10:F0"
Nick Pelly55e66f12009-09-18 11:37:06 -07001365 * <p>Alphabetic characters must be uppercase to be valid.
Nick Pelly005b2282009-09-10 10:21:56 -07001366 *
1367 * @param address Bluetooth address as string
1368 * @return true if the address is valid, false otherwise
1369 */
1370 public static boolean checkBluetoothAddress(String address) {
1371 if (address == null || address.length() != ADDRESS_LENGTH) {
1372 return false;
1373 }
1374 for (int i = 0; i < ADDRESS_LENGTH; i++) {
1375 char c = address.charAt(i);
1376 switch (i % 3) {
1377 case 0:
1378 case 1:
Nick Pelly55e66f12009-09-18 11:37:06 -07001379 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
1380 // hex character, OK
1381 break;
Nick Pelly005b2282009-09-10 10:21:56 -07001382 }
1383 return false;
1384 case 2:
1385 if (c == ':') {
1386 break; // OK
1387 }
1388 return false;
1389 }
1390 }
1391 return true;
1392 }
fredc0f420372012-04-12 00:02:00 -07001393
1394 /*package*/ IBluetoothManager getBluetoothManager() {
1395 return mManagerService;
1396 }
1397
fredcbf072a72012-05-09 16:52:50 -07001398 private ArrayList<IBluetoothManagerCallback> mProxyServiceStateCallbacks = new ArrayList<IBluetoothManagerCallback>();
fredcd6883532012-04-25 17:46:13 -07001399
fredc903ac6f2012-04-24 03:59:57 -07001400 /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) {
fredc0f420372012-04-12 00:02:00 -07001401 synchronized (mManagerCallback) {
fredcd6883532012-04-25 17:46:13 -07001402 if (cb == null) {
fredcbf072a72012-05-09 16:52:50 -07001403 Log.w(TAG, "getBluetoothService() called with no BluetoothManagerCallback");
1404 } else if (!mProxyServiceStateCallbacks.contains(cb)) {
1405 mProxyServiceStateCallbacks.add(cb);
fredc903ac6f2012-04-24 03:59:57 -07001406 }
1407 }
1408 return mService;
1409 }
1410
1411 /*package*/ void removeServiceStateCallback(IBluetoothManagerCallback cb) {
1412 synchronized (mManagerCallback) {
fredcbf072a72012-05-09 16:52:50 -07001413 mProxyServiceStateCallbacks.remove(cb);
fredc0f420372012-04-12 00:02:00 -07001414 }
1415 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001416
1417 /**
Matthew Xiecdd94e32013-04-11 16:36:26 -07001418 * Callback interface used to deliver LE scan results.
Matthew Xieddf7e472013-03-01 18:41:02 -08001419 *
Matthew Xiecdd94e32013-04-11 16:36:26 -07001420 * @see #startLeScan(LeScanCallback)
1421 * @see #startLeScan(UUID[], LeScanCallback)
Matthew Xieddf7e472013-03-01 18:41:02 -08001422 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001423 public interface LeScanCallback {
1424 /**
1425 * Callback reporting an LE device found during a device scan initiated
1426 * by the {@link BluetoothAdapter#startLeScan} function.
1427 *
1428 * @param device Identifies the remote device
1429 * @param rssi The RSSI value for the remote device as reported by the
1430 * Bluetooth hardware. 0 if no RSSI value is available.
1431 * @param scanRecord The content of the advertisement record offered by
1432 * the remote device.
1433 */
1434 public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord);
Matthew Xieddf7e472013-03-01 18:41:02 -08001435 }
1436
1437 /**
1438 * Starts a scan for Bluetooth LE devices.
1439 *
1440 * <p>Results of the scan are reported using the
Matthew Xiecdd94e32013-04-11 16:36:26 -07001441 * {@link LeScanCallback#onLeScan} callback.
Matthew Xieddf7e472013-03-01 18:41:02 -08001442 *
Matthew Xied5752332013-04-24 17:51:37 -07001443 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
Matthew Xieddf7e472013-03-01 18:41:02 -08001444 *
Matthew Xiecdd94e32013-04-11 16:36:26 -07001445 * @param callback the callback LE scan results are delivered
Matthew Xieddf7e472013-03-01 18:41:02 -08001446 * @return true, if the scan was started successfully
1447 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001448 public boolean startLeScan(LeScanCallback callback) {
1449 return startLeScan(null, callback);
Matthew Xieddf7e472013-03-01 18:41:02 -08001450 }
1451
1452 /**
1453 * Starts a scan for Bluetooth LE devices, looking for devices that
1454 * advertise given services.
1455 *
1456 * <p>Devices which advertise all specified services are reported using the
Matthew Xiecdd94e32013-04-11 16:36:26 -07001457 * {@link LeScanCallback#onLeScan} callback.
Matthew Xieddf7e472013-03-01 18:41:02 -08001458 *
Matthew Xied5752332013-04-24 17:51:37 -07001459 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
Matthew Xieddf7e472013-03-01 18:41:02 -08001460 *
1461 * @param serviceUuids Array of services to look for
Matthew Xiecdd94e32013-04-11 16:36:26 -07001462 * @param callback the callback LE scan results are delivered
Matthew Xieddf7e472013-03-01 18:41:02 -08001463 * @return true, if the scan was started successfully
1464 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001465 public boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback) {
1466 if (DBG) Log.d(TAG, "startLeScan(): " + serviceUuids);
Matthew Xieddf7e472013-03-01 18:41:02 -08001467
Matthew Xiecdd94e32013-04-11 16:36:26 -07001468 if (callback == null) {
1469 if (DBG) Log.e(TAG, "startLeScan: null callback");
Matthew Xieddf7e472013-03-01 18:41:02 -08001470 return false;
1471 }
1472
Matthew Xiecdd94e32013-04-11 16:36:26 -07001473 synchronized(mLeScanClients) {
1474 if (mLeScanClients.containsKey(callback)) {
1475 if (DBG) Log.e(TAG, "LE Scan has already started");
1476 return false;
1477 }
1478
1479 try {
1480 IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
Matthew Xie32ab77b2013-05-08 19:26:57 -07001481 if (iGatt == null) {
1482 // BLE is not supported
1483 return false;
1484 }
1485
Matthew Xiecdd94e32013-04-11 16:36:26 -07001486 UUID uuid = UUID.randomUUID();
1487 GattCallbackWrapper wrapper = new GattCallbackWrapper(this, callback, serviceUuids);
Matthew Xiecdd94e32013-04-11 16:36:26 -07001488 iGatt.registerClient(new ParcelUuid(uuid), wrapper);
1489 if (wrapper.scanStarted()) {
1490 mLeScanClients.put(callback, wrapper);
1491 return true;
1492 }
1493 } catch (RemoteException e) {
1494 Log.e(TAG,"",e);
1495 }
1496 }
1497 return false;
Matthew Xieddf7e472013-03-01 18:41:02 -08001498 }
1499
1500 /**
1501 * Stops an ongoing Bluetooth LE device scan.
1502 *
Matthew Xied5752332013-04-24 17:51:37 -07001503 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
Matthew Xiecdd94e32013-04-11 16:36:26 -07001504 *
1505 * @param callback used to identify which scan to stop
1506 * must be the same handle used to start the scan
Matthew Xieddf7e472013-03-01 18:41:02 -08001507 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001508 public void stopLeScan(LeScanCallback callback) {
1509 if (DBG) Log.d(TAG, "stopLeScan()");
1510 GattCallbackWrapper wrapper;
1511 synchronized(mLeScanClients) {
1512 wrapper = mLeScanClients.remove(callback);
1513 if (wrapper == null) return;
Matthew Xieddf7e472013-03-01 18:41:02 -08001514 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001515 wrapper.stopLeScan();
Matthew Xieddf7e472013-03-01 18:41:02 -08001516 }
1517
1518 /**
1519 * Bluetooth GATT interface callbacks
1520 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001521 private static class GattCallbackWrapper extends IBluetoothGattCallback.Stub {
1522 private static final int LE_CALLBACK_REG_TIMEOUT = 2000;
1523 private static final int LE_CALLBACK_REG_WAIT_COUNT = 5;
Matthew Xieddf7e472013-03-01 18:41:02 -08001524
Matthew Xiecdd94e32013-04-11 16:36:26 -07001525 private final LeScanCallback mLeScanCb;
1526 // mLeHandle 0: not registered
1527 // -1: scan stopped
1528 // >0: registered and scan started
1529 private int mLeHandle;
1530 private final UUID[] mScanFilter;
1531 private WeakReference<BluetoothAdapter> mBluetoothAdapter;
Matthew Xieddf7e472013-03-01 18:41:02 -08001532
Matthew Xiecdd94e32013-04-11 16:36:26 -07001533 public GattCallbackWrapper(BluetoothAdapter bluetoothAdapter,
1534 LeScanCallback leScanCb, UUID[] uuid) {
1535 mBluetoothAdapter = new WeakReference<BluetoothAdapter>(bluetoothAdapter);
1536 mLeScanCb = leScanCb;
1537 mScanFilter = uuid;
1538 mLeHandle = 0;
1539 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001540
Matthew Xiecdd94e32013-04-11 16:36:26 -07001541 public boolean scanStarted() {
1542 boolean started = false;
1543 synchronized(this) {
1544 if (mLeHandle == -1) return false;
1545
1546 int count = 0;
1547 // wait for callback registration and LE scan to start
1548 while (mLeHandle == 0 && count < LE_CALLBACK_REG_WAIT_COUNT) {
1549 try {
1550 wait(LE_CALLBACK_REG_TIMEOUT);
1551 } catch (InterruptedException e) {
1552 Log.e(TAG, "Callback reg wait interrupted: " + e);
1553 }
1554 count++;
Matthew Xieddf7e472013-03-01 18:41:02 -08001555 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001556 started = (mLeHandle > 0);
Matthew Xieddf7e472013-03-01 18:41:02 -08001557 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001558 return started;
1559 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001560
Matthew Xiecdd94e32013-04-11 16:36:26 -07001561 public void stopLeScan() {
1562 synchronized(this) {
1563 if (mLeHandle <= 0) {
1564 Log.e(TAG, "Error state, mLeHandle: " + mLeHandle);
1565 return;
1566 }
1567 BluetoothAdapter adapter = mBluetoothAdapter.get();
1568 if (adapter != null) {
1569 try {
1570 IBluetoothGatt iGatt = adapter.getBluetoothManager().getBluetoothGatt();
1571 iGatt.stopScan(mLeHandle, false);
1572 iGatt.unregisterClient(mLeHandle);
1573 } catch (RemoteException e) {
1574 Log.e(TAG, "Failed to stop scan and unregister" + e);
1575 }
1576 } else {
1577 Log.e(TAG, "stopLeScan, BluetoothAdapter is null");
1578 }
1579 mLeHandle = -1;
1580 notifyAll();
Matthew Xieddf7e472013-03-01 18:41:02 -08001581 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001582 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001583
Matthew Xiecdd94e32013-04-11 16:36:26 -07001584 /**
1585 * Application interface registered - app is ready to go
1586 */
1587 public void onClientRegistered(int status, int clientIf) {
1588 if (DBG) Log.d(TAG, "onClientRegistered() - status=" + status +
1589 " clientIf=" + clientIf);
1590 synchronized(this) {
1591 if (mLeHandle == -1) {
1592 if (DBG) Log.d(TAG, "onClientRegistered LE scan canceled");
1593 }
1594
1595 if (status == BluetoothGatt.GATT_SUCCESS) {
1596 mLeHandle = clientIf;
1597 IBluetoothGatt iGatt = null;
1598 try {
1599 BluetoothAdapter adapter = mBluetoothAdapter.get();
1600 if (adapter != null) {
1601 iGatt = adapter.getBluetoothManager().getBluetoothGatt();
1602 if (mScanFilter == null) {
1603 iGatt.startScan(mLeHandle, false);
1604 } else {
1605 ParcelUuid[] uuids = new ParcelUuid[mScanFilter.length];
1606 for(int i = 0; i != uuids.length; ++i) {
1607 uuids[i] = new ParcelUuid(mScanFilter[i]);
1608 }
1609 iGatt.startScanWithUuids(mLeHandle, false, uuids);
1610 }
1611 } else {
1612 Log.e(TAG, "onClientRegistered, BluetoothAdapter null");
1613 mLeHandle = -1;
1614 }
1615 } catch (RemoteException e) {
1616 Log.e(TAG, "fail to start le scan: " + e);
1617 mLeHandle = -1;
1618 }
1619 if (mLeHandle == -1) {
1620 // registration succeeded but start scan failed
1621 if (iGatt != null) {
1622 try {
1623 iGatt.unregisterClient(mLeHandle);
1624 } catch (RemoteException e) {
1625 Log.e(TAG, "fail to unregister callback: " + mLeHandle +
1626 " error: " + e);
1627 }
1628 }
1629 }
1630 } else {
1631 // registration failed
1632 mLeHandle = -1;
1633 }
1634 notifyAll();
Matthew Xieddf7e472013-03-01 18:41:02 -08001635 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001636 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001637
Matthew Xiecdd94e32013-04-11 16:36:26 -07001638 public void onClientConnectionState(int status, int clientIf,
1639 boolean connected, String address) {
1640 // no op
1641 }
1642
1643 /**
1644 * Callback reporting an LE scan result.
1645 * @hide
1646 */
1647 public void onScanResult(String address, int rssi, byte[] advData) {
1648 if (DBG) Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" +rssi);
1649
1650 // Check null in case the scan has been stopped
1651 synchronized(this) {
1652 if (mLeHandle <= 0) return;
Matthew Xieddf7e472013-03-01 18:41:02 -08001653 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001654 try {
1655 BluetoothAdapter adapter = mBluetoothAdapter.get();
1656 if (adapter == null) {
1657 Log.d(TAG, "onScanResult, BluetoothAdapter null");
1658 return;
1659 }
1660 mLeScanCb.onLeScan(adapter.getRemoteDevice(address), rssi, advData);
1661 } catch (Exception ex) {
1662 Log.w(TAG, "Unhandled exception: " + ex);
Matthew Xieddf7e472013-03-01 18:41:02 -08001663 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001664 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001665
Matthew Xiecdd94e32013-04-11 16:36:26 -07001666 public void onGetService(String address, int srvcType,
1667 int srvcInstId, ParcelUuid srvcUuid) {
1668 // no op
1669 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001670
Matthew Xiecdd94e32013-04-11 16:36:26 -07001671 public void onGetIncludedService(String address, int srvcType,
1672 int srvcInstId, ParcelUuid srvcUuid,
1673 int inclSrvcType, int inclSrvcInstId,
1674 ParcelUuid inclSrvcUuid) {
1675 // no op
1676 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001677
Matthew Xiecdd94e32013-04-11 16:36:26 -07001678 public void onGetCharacteristic(String address, int srvcType,
1679 int srvcInstId, ParcelUuid srvcUuid,
1680 int charInstId, ParcelUuid charUuid,
1681 int charProps) {
1682 // no op
1683 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001684
Matthew Xiecdd94e32013-04-11 16:36:26 -07001685 public void onGetDescriptor(String address, int srvcType,
1686 int srvcInstId, ParcelUuid srvcUuid,
1687 int charInstId, ParcelUuid charUuid,
1688 ParcelUuid descUuid) {
1689 // no op
1690 }
1691
1692 public void onSearchComplete(String address, int status) {
1693 // no op
1694 }
1695
1696 public void onCharacteristicRead(String address, int status, int srvcType,
1697 int srvcInstId, ParcelUuid srvcUuid,
1698 int charInstId, ParcelUuid charUuid, byte[] value) {
1699 // no op
1700 }
1701
1702 public void onCharacteristicWrite(String address, int status, int srvcType,
1703 int srvcInstId, ParcelUuid srvcUuid,
1704 int charInstId, ParcelUuid charUuid) {
1705 // no op
1706 }
1707
1708 public void onNotify(String address, int srvcType,
Matthew Xieddf7e472013-03-01 18:41:02 -08001709 int srvcInstId, ParcelUuid srvcUuid,
1710 int charInstId, ParcelUuid charUuid,
1711 byte[] value) {
Matthew Xiecdd94e32013-04-11 16:36:26 -07001712 // no op
1713 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001714
Matthew Xiecdd94e32013-04-11 16:36:26 -07001715 public void onDescriptorRead(String address, int status, int srvcType,
1716 int srvcInstId, ParcelUuid srvcUuid,
1717 int charInstId, ParcelUuid charUuid,
1718 ParcelUuid descrUuid, byte[] value) {
1719 // no op
1720 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001721
Matthew Xiecdd94e32013-04-11 16:36:26 -07001722 public void onDescriptorWrite(String address, int status, int srvcType,
1723 int srvcInstId, ParcelUuid srvcUuid,
1724 int charInstId, ParcelUuid charUuid,
1725 ParcelUuid descrUuid) {
1726 // no op
1727 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001728
Matthew Xiecdd94e32013-04-11 16:36:26 -07001729 public void onExecuteWrite(String address, int status) {
1730 // no op
1731 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001732
Matthew Xiecdd94e32013-04-11 16:36:26 -07001733 public void onReadRemoteRssi(String address, int rssi, int status) {
1734 // no op
1735 }
1736 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001737
Nick Pellybd022f42009-08-14 18:33:38 -07001738}