blob: 79bb476cb2120d3a67e5b6d7bde5b5b86ce5d2f2 [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;
Matthew Xiecdd94e32013-04-11 16:36:26 -070039import java.util.Map;
Nick Pelly24bb9b82009-10-02 20:34:18 -070040import java.util.Random;
41import java.util.Set;
Nick Pelly16fb88a2009-10-07 07:44:03 +020042import java.util.UUID;
Nick Pellybd022f42009-08-14 18:33:38 -070043
44/**
Scott Main9fab0ae2009-11-03 18:17:59 -080045 * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter}
46 * lets you perform fundamental Bluetooth tasks, such as initiate
47 * device discovery, query a list of bonded (paired) devices,
48 * instantiate a {@link BluetoothDevice} using a known MAC address, and create
49 * a {@link BluetoothServerSocket} to listen for connection requests from other
Matthew Xieb30f91e2013-05-29 10:19:06 -070050 * devices, and start a scan for Bluetooth LE devices.
Scott Main9fab0ae2009-11-03 18:17:59 -080051 *
52 * <p>To get a {@link BluetoothAdapter} representing the local Bluetooth
Matthew Xieb30f91e2013-05-29 10:19:06 -070053 * adapter, when running on JELLY_BEAN_MR1 and below, call the
54 * static {@link #getDefaultAdapter} method; when running on JELLY_BEAN_MR2 and
55 * higher, retrieve it through
56 * {@link android.content.Context#getSystemService} with
57 * {@link android.content.Context#BLUETOOTH_SERVICE}.
Scott Main9fab0ae2009-11-03 18:17:59 -080058 * Fundamentally, this is your starting point for all
59 * Bluetooth actions. Once you have the local adapter, you can get a set of
60 * {@link BluetoothDevice} objects representing all paired devices with
61 * {@link #getBondedDevices()}; start device discovery with
62 * {@link #startDiscovery()}; or create a {@link BluetoothServerSocket} to
63 * listen for incoming connection requests with
Matthew Xieb30f91e2013-05-29 10:19:06 -070064 * {@link #listenUsingRfcommWithServiceRecord(String,UUID)}; or start a scan for
65 * Bluetooth LE devices with {@link #startLeScan(LeScanCallback callback)}.
Scott Main9fab0ae2009-11-03 18:17:59 -080066 *
67 * <p class="note"><strong>Note:</strong>
68 * Most methods require the {@link android.Manifest.permission#BLUETOOTH}
69 * permission and some also require the
70 * {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
71 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080072 * <div class="special reference">
73 * <h3>Developer Guides</h3>
74 * <p>For more information about using Bluetooth, read the
75 * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p>
76 * </div>
77 *
Scott Main9fab0ae2009-11-03 18:17:59 -080078 * {@see BluetoothDevice}
79 * {@see BluetoothServerSocket}
Nick Pellybd022f42009-08-14 18:33:38 -070080 */
81public final class BluetoothAdapter {
82 private static final String TAG = "BluetoothAdapter";
fredc0f420372012-04-12 00:02:00 -070083 private static final boolean DBG = true;
Matthew Xie3b6214f2012-08-29 00:12:29 -070084 private static final boolean VDBG = false;
Nick Pellybd022f42009-08-14 18:33:38 -070085
Nick Pellyde893f52009-09-08 13:15:33 -070086 /**
Nick Pellyb24e11b2009-09-08 17:40:43 -070087 * Sentinel error value for this class. Guaranteed to not equal any other
88 * integer constant in this class. Provided as a convenience for functions
89 * that require a sentinel error value, for example:
90 * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
91 * BluetoothAdapter.ERROR)</code>
92 */
Nick Pelly005b2282009-09-10 10:21:56 -070093 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070094
95 /**
Nick Pellyde893f52009-09-08 13:15:33 -070096 * Broadcast Action: The state of the local Bluetooth adapter has been
97 * changed.
98 * <p>For example, Bluetooth has been turned on or off.
Nick Pelly005b2282009-09-10 10:21:56 -070099 * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link
Nick Pellyde893f52009-09-08 13:15:33 -0700100 * #EXTRA_PREVIOUS_STATE} containing the new and old states
101 * respectively.
102 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
103 */
104 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
105 public static final String ACTION_STATE_CHANGED =
Nick Pelly005b2282009-09-10 10:21:56 -0700106 "android.bluetooth.adapter.action.STATE_CHANGED";
Nick Pellybd022f42009-08-14 18:33:38 -0700107
Nick Pellyde893f52009-09-08 13:15:33 -0700108 /**
109 * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
110 * intents to request the current power state. Possible values are:
111 * {@link #STATE_OFF},
112 * {@link #STATE_TURNING_ON},
113 * {@link #STATE_ON},
114 * {@link #STATE_TURNING_OFF},
115 */
116 public static final String EXTRA_STATE =
Nick Pelly005b2282009-09-10 10:21:56 -0700117 "android.bluetooth.adapter.extra.STATE";
Nick Pellyde893f52009-09-08 13:15:33 -0700118 /**
119 * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
120 * intents to request the previous power state. Possible values are:
121 * {@link #STATE_OFF},
122 * {@link #STATE_TURNING_ON},
123 * {@link #STATE_ON},
124 * {@link #STATE_TURNING_OFF},
125 */
126 public static final String EXTRA_PREVIOUS_STATE =
Nick Pelly005b2282009-09-10 10:21:56 -0700127 "android.bluetooth.adapter.extra.PREVIOUS_STATE";
Nick Pellybd022f42009-08-14 18:33:38 -0700128
Nick Pellyde893f52009-09-08 13:15:33 -0700129 /**
130 * Indicates the local Bluetooth adapter is off.
131 */
Nick Pelly005b2282009-09-10 10:21:56 -0700132 public static final int STATE_OFF = 10;
Nick Pellyde893f52009-09-08 13:15:33 -0700133 /**
134 * Indicates the local Bluetooth adapter is turning on. However local
135 * clients should wait for {@link #STATE_ON} before attempting to
136 * use the adapter.
137 */
Nick Pelly005b2282009-09-10 10:21:56 -0700138 public static final int STATE_TURNING_ON = 11;
Nick Pellyde893f52009-09-08 13:15:33 -0700139 /**
140 * Indicates the local Bluetooth adapter is on, and ready for use.
141 */
Nick Pelly005b2282009-09-10 10:21:56 -0700142 public static final int STATE_ON = 12;
Nick Pellyde893f52009-09-08 13:15:33 -0700143 /**
144 * Indicates the local Bluetooth adapter is turning off. Local clients
145 * should immediately attempt graceful disconnection of any remote links.
146 */
Nick Pelly005b2282009-09-10 10:21:56 -0700147 public static final int STATE_TURNING_OFF = 13;
Nick Pellyde893f52009-09-08 13:15:33 -0700148
149 /**
Nick Pelly18b1e792009-09-24 11:14:15 -0700150 * Activity Action: Show a system activity that requests discoverable mode.
Scott Main6d95fc02009-11-19 17:00:19 -0800151 * This activity will also request the user to turn on Bluetooth if it
Nick Pelly1acdcc12009-09-28 10:33:55 -0700152 * is not currently enabled.
Nick Pelly18b1e792009-09-24 11:14:15 -0700153 * <p>Discoverable mode is equivalent to {@link
154 * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see
155 * this Bluetooth adapter when they perform a discovery.
Scott Main6d95fc02009-11-19 17:00:19 -0800156 * <p>For privacy, Android is not discoverable by default.
157 * <p>The sender of this Intent can optionally use extra field {@link
Nick Pelly18b1e792009-09-24 11:14:15 -0700158 * #EXTRA_DISCOVERABLE_DURATION} to request the duration of
159 * discoverability. Currently the default duration is 120 seconds, and
160 * maximum duration is capped at 300 seconds for each request.
161 * <p>Notification of the result of this activity is posted using the
162 * {@link android.app.Activity#onActivityResult} callback. The
163 * <code>resultCode</code>
Michael Chancdd28642009-11-05 18:29:01 -0800164 * will be the duration (in seconds) of discoverability or
165 * {@link android.app.Activity#RESULT_CANCELED} if the user rejected
166 * discoverability or an error has occurred.
Nick Pelly18b1e792009-09-24 11:14:15 -0700167 * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED}
Scott Main6d95fc02009-11-19 17:00:19 -0800168 * for global notification whenever the scan mode changes. For example, an
169 * application can be notified when the device has ended discoverability.
Nick Pelly1acdcc12009-09-28 10:33:55 -0700170 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pelly18b1e792009-09-24 11:14:15 -0700171 */
172 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
173 public static final String ACTION_REQUEST_DISCOVERABLE =
174 "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE";
175
176 /**
177 * Used as an optional int extra field in {@link
178 * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration
179 * for discoverability in seconds. The current default is 120 seconds, and
180 * requests over 300 seconds will be capped. These values could change.
181 */
182 public static final String EXTRA_DISCOVERABLE_DURATION =
183 "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION";
184
185 /**
Nick Pelly1acdcc12009-09-28 10:33:55 -0700186 * Activity Action: Show a system activity that allows the user to turn on
187 * Bluetooth.
188 * <p>This system activity will return once Bluetooth has completed turning
189 * on, or the user has decided not to turn Bluetooth on.
190 * <p>Notification of the result of this activity is posted using the
191 * {@link android.app.Activity#onActivityResult} callback. The
192 * <code>resultCode</code>
Michael Chancdd28642009-11-05 18:29:01 -0800193 * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
194 * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
195 * has rejected the request or an error has occurred.
Nick Pelly1acdcc12009-09-28 10:33:55 -0700196 * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED}
197 * for global notification whenever Bluetooth is turned on or off.
198 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
199 */
200 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
201 public static final String ACTION_REQUEST_ENABLE =
202 "android.bluetooth.adapter.action.REQUEST_ENABLE";
203
204 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700205 * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter
206 * has changed.
Nick Pelly005b2282009-09-10 10:21:56 -0700207 * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link
Nick Pellyde893f52009-09-08 13:15:33 -0700208 * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes
209 * respectively.
210 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
211 */
212 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
213 public static final String ACTION_SCAN_MODE_CHANGED =
Nick Pelly005b2282009-09-10 10:21:56 -0700214 "android.bluetooth.adapter.action.SCAN_MODE_CHANGED";
Nick Pellyde893f52009-09-08 13:15:33 -0700215
216 /**
217 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
218 * intents to request the current scan mode. Possible values are:
219 * {@link #SCAN_MODE_NONE},
220 * {@link #SCAN_MODE_CONNECTABLE},
221 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
222 */
Nick Pelly005b2282009-09-10 10:21:56 -0700223 public static final String EXTRA_SCAN_MODE = "android.bluetooth.adapter.extra.SCAN_MODE";
Nick Pellyde893f52009-09-08 13:15:33 -0700224 /**
225 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
226 * intents to request the previous scan mode. Possible values are:
227 * {@link #SCAN_MODE_NONE},
228 * {@link #SCAN_MODE_CONNECTABLE},
229 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
230 */
231 public static final String EXTRA_PREVIOUS_SCAN_MODE =
Nick Pelly005b2282009-09-10 10:21:56 -0700232 "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE";
Nick Pellyde893f52009-09-08 13:15:33 -0700233
234 /**
235 * Indicates that both inquiry scan and page scan are disabled on the local
236 * Bluetooth adapter. Therefore this device is neither discoverable
237 * nor connectable from remote Bluetooth devices.
238 */
Nick Pelly005b2282009-09-10 10:21:56 -0700239 public static final int SCAN_MODE_NONE = 20;
Nick Pellyde893f52009-09-08 13:15:33 -0700240 /**
241 * Indicates that inquiry scan is disabled, but page scan is enabled on the
242 * local Bluetooth adapter. Therefore this device is not discoverable from
243 * remote Bluetooth devices, but is connectable from remote devices that
244 * have previously discovered this device.
245 */
Nick Pelly005b2282009-09-10 10:21:56 -0700246 public static final int SCAN_MODE_CONNECTABLE = 21;
Nick Pellyde893f52009-09-08 13:15:33 -0700247 /**
248 * Indicates that both inquiry scan and page scan are enabled on the local
249 * Bluetooth adapter. Therefore this device is both discoverable and
250 * connectable from remote Bluetooth devices.
251 */
Nick Pelly005b2282009-09-10 10:21:56 -0700252 public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23;
Nick Pellybd022f42009-08-14 18:33:38 -0700253
Nick Pelly005b2282009-09-10 10:21:56 -0700254
255 /**
256 * Broadcast Action: The local Bluetooth adapter has started the remote
257 * device discovery process.
258 * <p>This usually involves an inquiry scan of about 12 seconds, followed
259 * by a page scan of each new device to retrieve its Bluetooth name.
260 * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as
261 * remote Bluetooth devices are found.
262 * <p>Device discovery is a heavyweight procedure. New connections to
263 * remote Bluetooth devices should not be attempted while discovery is in
264 * progress, and existing connections will experience limited bandwidth
265 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
266 * discovery.
267 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
268 */
269 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
270 public static final String ACTION_DISCOVERY_STARTED =
271 "android.bluetooth.adapter.action.DISCOVERY_STARTED";
272 /**
273 * Broadcast Action: The local Bluetooth adapter has finished the device
274 * discovery process.
275 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
276 */
277 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
278 public static final String ACTION_DISCOVERY_FINISHED =
279 "android.bluetooth.adapter.action.DISCOVERY_FINISHED";
280
281 /**
282 * Broadcast Action: The local Bluetooth adapter has changed its friendly
283 * Bluetooth name.
284 * <p>This name is visible to remote Bluetooth devices.
285 * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing
286 * the name.
287 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
288 */
289 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
290 public static final String ACTION_LOCAL_NAME_CHANGED =
291 "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED";
292 /**
293 * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED}
294 * intents to request the local Bluetooth name.
295 */
296 public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME";
297
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700298 /**
299 * Intent used to broadcast the change in connection state of the local
300 * Bluetooth adapter to a profile of the remote device. When the adapter is
301 * not connected to any profiles of any remote devices and it attempts a
302 * connection to a profile this intent will sent. Once connected, this intent
303 * will not be sent for any more connection attempts to any profiles of any
304 * remote device. When the adapter disconnects from the last profile its
305 * connected to of any remote device, this intent will be sent.
306 *
307 * <p> This intent is useful for applications that are only concerned about
308 * whether the local adapter is connected to any profile of any device and
309 * are not really concerned about which profile. For example, an application
310 * which displays an icon to display whether Bluetooth is connected or not
311 * can use this intent.
312 *
313 * <p>This intent will have 3 extras:
Jaikumar Ganesh0b5b35f2011-02-01 16:47:11 -0800314 * {@link #EXTRA_CONNECTION_STATE} - The current connection state.
315 * {@link #EXTRA_PREVIOUS_CONNECTION_STATE}- The previous connection state.
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700316 * {@link BluetoothDevice#EXTRA_DEVICE} - The remote device.
317 *
Jaikumar Ganesh0b5b35f2011-02-01 16:47:11 -0800318 * {@link #EXTRA_CONNECTION_STATE} or {@link #EXTRA_PREVIOUS_CONNECTION_STATE}
319 * can be any of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700320 * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
321 *
322 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
323 */
324 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
325 public static final String ACTION_CONNECTION_STATE_CHANGED =
326 "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED";
327
328 /**
329 * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED}
330 *
331 * This extra represents the current connection state.
332 */
333 public static final String EXTRA_CONNECTION_STATE =
334 "android.bluetooth.adapter.extra.CONNECTION_STATE";
335
336 /**
337 * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED}
338 *
339 * This extra represents the previous connection state.
340 */
341 public static final String EXTRA_PREVIOUS_CONNECTION_STATE =
342 "android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE";
343
344 /** The profile is in disconnected state */
345 public static final int STATE_DISCONNECTED = 0;
346 /** The profile is in connecting state */
347 public static final int STATE_CONNECTING = 1;
348 /** The profile is in connected state */
349 public static final int STATE_CONNECTED = 2;
350 /** The profile is in disconnecting state */
351 public static final int STATE_DISCONNECTING = 3;
352
Nick Pellyf242b7b2009-10-08 00:12:45 +0200353 /** @hide */
fredc0f420372012-04-12 00:02:00 -0700354 public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager";
Nick Pellyf242b7b2009-10-08 00:12:45 +0200355
Nick Pelly005b2282009-09-10 10:21:56 -0700356 private static final int ADDRESS_LENGTH = 17;
Nick Pellybd022f42009-08-14 18:33:38 -0700357
Nick Pellyf242b7b2009-10-08 00:12:45 +0200358 /**
Jake Hambyf51eada2010-09-21 13:39:53 -0700359 * Lazily initialized singleton. Guaranteed final after first object
Nick Pellyf242b7b2009-10-08 00:12:45 +0200360 * constructed.
361 */
362 private static BluetoothAdapter sAdapter;
363
fredc0f420372012-04-12 00:02:00 -0700364 private final IBluetoothManager mManagerService;
365 private IBluetooth mService;
Nick Pellybd022f42009-08-14 18:33:38 -0700366
Matthew Xiecdd94e32013-04-11 16:36:26 -0700367 private final Map<LeScanCallback, GattCallbackWrapper> mLeScanClients;
Matthew Xie484867a2011-08-25 16:45:58 -0700368
Nick Pellybd022f42009-08-14 18:33:38 -0700369 /**
Nick Pellyf242b7b2009-10-08 00:12:45 +0200370 * Get a handle to the default local Bluetooth adapter.
371 * <p>Currently Android only supports one Bluetooth adapter, but the API
372 * could be extended to support more. This will always return the default
373 * adapter.
374 * @return the default local adapter, or null if Bluetooth is not supported
375 * on this hardware platform
376 */
377 public static synchronized BluetoothAdapter getDefaultAdapter() {
378 if (sAdapter == null) {
fredc0f420372012-04-12 00:02:00 -0700379 IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
Nick Pellyf242b7b2009-10-08 00:12:45 +0200380 if (b != null) {
fredc0f420372012-04-12 00:02:00 -0700381 IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
382 sAdapter = new BluetoothAdapter(managerService);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800383 } else {
384 Log.e(TAG, "Bluetooth binder is null");
Nick Pellyf242b7b2009-10-08 00:12:45 +0200385 }
386 }
387 return sAdapter;
388 }
389
390 /**
391 * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.
Nick Pellybd022f42009-08-14 18:33:38 -0700392 */
fredc0f420372012-04-12 00:02:00 -0700393 BluetoothAdapter(IBluetoothManager managerService) {
394
395 if (managerService == null) {
396 throw new IllegalArgumentException("bluetooth manager service is null");
Nick Pellybd022f42009-08-14 18:33:38 -0700397 }
fredc0f420372012-04-12 00:02:00 -0700398 try {
399 mService = managerService.registerAdapter(mManagerCallback);
400 } catch (RemoteException e) {Log.e(TAG, "", e);}
401 mManagerService = managerService;
Matthew Xiecdd94e32013-04-11 16:36:26 -0700402 mLeScanClients = new HashMap<LeScanCallback, GattCallbackWrapper>();
Nick Pellybd022f42009-08-14 18:33:38 -0700403 }
404
405 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700406 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
407 * address.
408 * <p>Valid Bluetooth hardware addresses must be upper case, in a format
Nick Pelly005b2282009-09-10 10:21:56 -0700409 * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is
410 * available to validate a Bluetooth address.
Nick Pelly45e27042009-08-19 11:00:00 -0700411 * <p>A {@link BluetoothDevice} will always be returned for a valid
412 * hardware address, even if this adapter has never seen that device.
Nick Pellyde893f52009-09-08 13:15:33 -0700413 *
Nick Pellybd022f42009-08-14 18:33:38 -0700414 * @param address valid Bluetooth MAC address
Nick Pelly45e27042009-08-19 11:00:00 -0700415 * @throws IllegalArgumentException if address is invalid
Nick Pellybd022f42009-08-14 18:33:38 -0700416 */
417 public BluetoothDevice getRemoteDevice(String address) {
418 return new BluetoothDevice(address);
419 }
420
421 /**
Nick Pelly75596b42011-12-07 15:03:55 -0800422 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
423 * address.
424 * <p>Valid Bluetooth hardware addresses must be 6 bytes. This method
425 * expects the address in network byte order (MSB first).
426 * <p>A {@link BluetoothDevice} will always be returned for a valid
427 * hardware address, even if this adapter has never seen that device.
428 *
429 * @param address Bluetooth MAC address (6 bytes)
430 * @throws IllegalArgumentException if address is invalid
431 */
432 public BluetoothDevice getRemoteDevice(byte[] address) {
433 if (address == null || address.length != 6) {
434 throw new IllegalArgumentException("Bluetooth address must have 6 bytes");
435 }
436 return new BluetoothDevice(String.format("%02X:%02X:%02X:%02X:%02X:%02X",
437 address[0], address[1], address[2], address[3], address[4], address[5]));
438 }
439
440 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700441 * Return true if Bluetooth is currently enabled and ready for use.
442 * <p>Equivalent to:
443 * <code>getBluetoothState() == STATE_ON</code>
444 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700445 *
Nick Pellyde893f52009-09-08 13:15:33 -0700446 * @return true if the local adapter is turned on
Nick Pellybd022f42009-08-14 18:33:38 -0700447 */
448 public boolean isEnabled() {
fredc0f420372012-04-12 00:02:00 -0700449
Nick Pellybd022f42009-08-14 18:33:38 -0700450 try {
fredc0f420372012-04-12 00:02:00 -0700451 synchronized(mManagerCallback) {
452 if (mService != null) return mService.isEnabled();
453 }
Nick Pellybd022f42009-08-14 18:33:38 -0700454 } catch (RemoteException e) {Log.e(TAG, "", e);}
455 return false;
456 }
457
458 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700459 * Get the current state of the local Bluetooth adapter.
460 * <p>Possible return values are
461 * {@link #STATE_OFF},
462 * {@link #STATE_TURNING_ON},
463 * {@link #STATE_ON},
464 * {@link #STATE_TURNING_OFF}.
465 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700466 *
Nick Pellyde893f52009-09-08 13:15:33 -0700467 * @return current state of Bluetooth adapter
Nick Pellybd022f42009-08-14 18:33:38 -0700468 */
Nick Pellyde893f52009-09-08 13:15:33 -0700469 public int getState() {
Nick Pellybd022f42009-08-14 18:33:38 -0700470 try {
fredc0f420372012-04-12 00:02:00 -0700471 synchronized(mManagerCallback) {
472 if (mService != null)
473 {
fredcbf072a72012-05-09 16:52:50 -0700474 int state= mService.getState();
Matthew Xie3b6214f2012-08-29 00:12:29 -0700475 if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state);
fredcbf072a72012-05-09 16:52:50 -0700476 return state;
fredc0f420372012-04-12 00:02:00 -0700477 }
478 // TODO(BT) there might be a small gap during STATE_TURNING_ON that
479 // mService is null, handle that case
480 }
Nick Pellybd022f42009-08-14 18:33:38 -0700481 } catch (RemoteException e) {Log.e(TAG, "", e);}
fredcbf072a72012-05-09 16:52:50 -0700482 if (DBG) Log.d(TAG, "" + hashCode() + ": getState() : mService = null. Returning STATE_OFF");
Nick Pellyde893f52009-09-08 13:15:33 -0700483 return STATE_OFF;
Nick Pellybd022f42009-08-14 18:33:38 -0700484 }
485
486 /**
Scott Mained2a70d2009-12-09 16:07:39 -0800487 * Turn on the local Bluetooth adapter&mdash;do not use without explicit
488 * user action to turn on Bluetooth.
Nick Pellyde893f52009-09-08 13:15:33 -0700489 * <p>This powers on the underlying Bluetooth hardware, and starts all
490 * Bluetooth system services.
Scott Mained2a70d2009-12-09 16:07:39 -0800491 * <p class="caution"><strong>Bluetooth should never be enabled without
492 * direct user consent</strong>. If you want to turn on Bluetooth in order
493 * to create a wireless connection, you should use the {@link
494 * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests
495 * user permission to turn on Bluetooth. The {@link #enable()} method is
496 * provided only for applications that include a user interface for changing
497 * system settings, such as a "power manager" app.</p>
Brad Fitzpatrick3219ab42009-09-25 16:31:39 +0400498 * <p>This is an asynchronous call: it will return immediately, and
Nick Pellyde893f52009-09-08 13:15:33 -0700499 * clients should listen for {@link #ACTION_STATE_CHANGED}
500 * to be notified of subsequent adapter state changes. If this call returns
501 * true, then the adapter state will immediately transition from {@link
502 * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time
503 * later transition to either {@link #STATE_OFF} or {@link
504 * #STATE_ON}. If this call returns false then there was an
505 * immediate problem that will prevent the adapter from being turned on -
506 * such as Airplane mode, or the adapter is already turned on.
Scott Mained2a70d2009-12-09 16:07:39 -0800507 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
508 * permission
Nick Pellyde893f52009-09-08 13:15:33 -0700509 *
510 * @return true to indicate adapter startup has begun, or false on
511 * immediate error
Nick Pellybd022f42009-08-14 18:33:38 -0700512 */
513 public boolean enable() {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700514 if (isEnabled() == true){
515 if (DBG) Log.d(TAG, "enable(): BT is already enabled..!");
516 return true;
517 }
Nick Pellybd022f42009-08-14 18:33:38 -0700518 try {
Kausik Sinnaswamya097f512012-04-16 16:38:27 +0530519 return mManagerService.enable();
Nick Pellybd022f42009-08-14 18:33:38 -0700520 } catch (RemoteException e) {Log.e(TAG, "", e);}
521 return false;
522 }
523
524 /**
Scott Mained2a70d2009-12-09 16:07:39 -0800525 * Turn off the local Bluetooth adapter&mdash;do not use without explicit
526 * user action to turn off Bluetooth.
Nick Pellyde893f52009-09-08 13:15:33 -0700527 * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth
528 * system services, and powers down the underlying Bluetooth hardware.
Jake Hambyf51eada2010-09-21 13:39:53 -0700529 * <p class="caution"><strong>Bluetooth should never be disabled without
Scott Mained2a70d2009-12-09 16:07:39 -0800530 * direct user consent</strong>. The {@link #disable()} method is
531 * provided only for applications that include a user interface for changing
532 * system settings, such as a "power manager" app.</p>
Brad Fitzpatrick3219ab42009-09-25 16:31:39 +0400533 * <p>This is an asynchronous call: it will return immediately, and
Nick Pellyde893f52009-09-08 13:15:33 -0700534 * clients should listen for {@link #ACTION_STATE_CHANGED}
535 * to be notified of subsequent adapter state changes. If this call returns
536 * true, then the adapter state will immediately transition from {@link
537 * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time
538 * later transition to either {@link #STATE_OFF} or {@link
539 * #STATE_ON}. If this call returns false then there was an
540 * immediate problem that will prevent the adapter from being turned off -
541 * such as the adapter already being turned off.
Scott Mained2a70d2009-12-09 16:07:39 -0800542 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
543 * permission
Nick Pellybd022f42009-08-14 18:33:38 -0700544 *
Nick Pellyde893f52009-09-08 13:15:33 -0700545 * @return true to indicate adapter shutdown has begun, or false on
546 * immediate error
Nick Pellybd022f42009-08-14 18:33:38 -0700547 */
548 public boolean disable() {
549 try {
fredc0f420372012-04-12 00:02:00 -0700550 return mManagerService.disable(true);
Jaikumar Ganeshe21a4ac2012-03-06 17:15:16 -0800551 } catch (RemoteException e) {Log.e(TAG, "", e);}
552 return false;
553 }
554
555 /**
556 * Turn off the local Bluetooth adapter and don't persist the setting.
557 *
558 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
559 * permission
560 *
561 * @return true to indicate adapter shutdown has begun, or false on
562 * immediate error
563 * @hide
564 */
565 public boolean disable(boolean persist) {
fredc0f420372012-04-12 00:02:00 -0700566
Jaikumar Ganeshe21a4ac2012-03-06 17:15:16 -0800567 try {
fredc0f420372012-04-12 00:02:00 -0700568 return mManagerService.disable(persist);
Nick Pellybd022f42009-08-14 18:33:38 -0700569 } catch (RemoteException e) {Log.e(TAG, "", e);}
570 return false;
571 }
572
Nick Pellyde893f52009-09-08 13:15:33 -0700573 /**
574 * Returns the hardware address of the local Bluetooth adapter.
575 * <p>For example, "00:11:22:AA:BB:CC".
576 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
577 *
578 * @return Bluetooth hardware address as string
579 */
Nick Pellybd022f42009-08-14 18:33:38 -0700580 public String getAddress() {
581 try {
fredc0f420372012-04-12 00:02:00 -0700582 return mManagerService.getAddress();
Nick Pellybd022f42009-08-14 18:33:38 -0700583 } catch (RemoteException e) {Log.e(TAG, "", e);}
584 return null;
585 }
586
587 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700588 * Get the friendly Bluetooth name of the local Bluetooth adapter.
589 * <p>This name is visible to remote Bluetooth devices.
590 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700591 *
Nick Pellyde893f52009-09-08 13:15:33 -0700592 * @return the Bluetooth name, or null on error
Nick Pellybd022f42009-08-14 18:33:38 -0700593 */
594 public String getName() {
595 try {
fredc116d1d462012-04-20 14:47:08 -0700596 return mManagerService.getName();
Nick Pellybd022f42009-08-14 18:33:38 -0700597 } catch (RemoteException e) {Log.e(TAG, "", e);}
598 return null;
599 }
600
601 /**
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800602 * Get the UUIDs supported by the local Bluetooth adapter.
603 *
604 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
605 *
606 * @return the UUIDs supported by the local Bluetooth Adapter.
607 * @hide
608 */
609 public ParcelUuid[] getUuids() {
Matthew Xie44b58ab2011-11-16 12:27:57 -0800610 if (getState() != STATE_ON) return null;
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800611 try {
fredc0f420372012-04-12 00:02:00 -0700612 synchronized(mManagerCallback) {
613 if (mService != null) return mService.getUuids();
614 }
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800615 } catch (RemoteException e) {Log.e(TAG, "", e);}
616 return null;
617 }
618
619 /**
Jake Hamby0f584302010-09-16 18:12:51 -0700620 * Set the friendly Bluetooth name of the local Bluetooth adapter.
Nick Pellyde893f52009-09-08 13:15:33 -0700621 * <p>This name is visible to remote Bluetooth devices.
Jake Hamby0f584302010-09-16 18:12:51 -0700622 * <p>Valid Bluetooth names are a maximum of 248 bytes using UTF-8
623 * encoding, although many remote devices can only display the first
624 * 40 characters, and some may be limited to just 20.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700625 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
626 * will return false. After turning on Bluetooth,
627 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
628 * to get the updated value.
Nick Pellyde893f52009-09-08 13:15:33 -0700629 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
Nick Pellybd022f42009-08-14 18:33:38 -0700630 *
Nick Pellyde893f52009-09-08 13:15:33 -0700631 * @param name a valid Bluetooth name
632 * @return true if the name was set, false otherwise
Nick Pellybd022f42009-08-14 18:33:38 -0700633 */
634 public boolean setName(String name) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700635 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700636 try {
fredc0f420372012-04-12 00:02:00 -0700637 synchronized(mManagerCallback) {
638 if (mService != null) return mService.setName(name);
639 }
Nick Pellybd022f42009-08-14 18:33:38 -0700640 } catch (RemoteException e) {Log.e(TAG, "", e);}
641 return false;
642 }
643
644 /**
Jake Hamby0f584302010-09-16 18:12:51 -0700645 * Get the current Bluetooth scan mode of the local Bluetooth adapter.
Nick Pellyde893f52009-09-08 13:15:33 -0700646 * <p>The Bluetooth scan mode determines if the local adapter is
647 * connectable and/or discoverable from remote Bluetooth devices.
648 * <p>Possible values are:
649 * {@link #SCAN_MODE_NONE},
650 * {@link #SCAN_MODE_CONNECTABLE},
651 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700652 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
653 * will return {@link #SCAN_MODE_NONE}. After turning on Bluetooth,
654 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
655 * to get the updated value.
Nick Pellyde893f52009-09-08 13:15:33 -0700656 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
657 *
658 * @return scan mode
Nick Pellybd022f42009-08-14 18:33:38 -0700659 */
660 public int getScanMode() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700661 if (getState() != STATE_ON) return SCAN_MODE_NONE;
Nick Pellybd022f42009-08-14 18:33:38 -0700662 try {
fredc0f420372012-04-12 00:02:00 -0700663 synchronized(mManagerCallback) {
664 if (mService != null) return mService.getScanMode();
665 }
Nick Pellybd022f42009-08-14 18:33:38 -0700666 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyde893f52009-09-08 13:15:33 -0700667 return SCAN_MODE_NONE;
Nick Pellybd022f42009-08-14 18:33:38 -0700668 }
669
670 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700671 * Set the Bluetooth scan mode of the local Bluetooth adapter.
672 * <p>The Bluetooth scan mode determines if the local adapter is
673 * connectable and/or discoverable from remote Bluetooth devices.
Nick Pelly12835472009-09-25 15:00:29 -0700674 * <p>For privacy reasons, discoverable mode is automatically turned off
675 * after <code>duration</code> seconds. For example, 120 seconds should be
676 * enough for a remote device to initiate and complete its discovery
677 * process.
Nick Pellyde893f52009-09-08 13:15:33 -0700678 * <p>Valid scan mode values are:
679 * {@link #SCAN_MODE_NONE},
680 * {@link #SCAN_MODE_CONNECTABLE},
681 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700682 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
683 * will return false. After turning on Bluetooth,
684 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
685 * to get the updated value.
Nick Pelly18b1e792009-09-24 11:14:15 -0700686 * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS}
687 * <p>Applications cannot set the scan mode. They should use
688 * <code>startActivityForResult(
689 * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE})
690 * </code>instead.
Nick Pellyde893f52009-09-08 13:15:33 -0700691 *
692 * @param mode valid scan mode
Nick Pelly12835472009-09-25 15:00:29 -0700693 * @param duration time in seconds to apply scan mode, only used for
694 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}
Nick Pellyde893f52009-09-08 13:15:33 -0700695 * @return true if the scan mode was set, false otherwise
Nick Pelly18b1e792009-09-24 11:14:15 -0700696 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700697 */
Nick Pelly12835472009-09-25 15:00:29 -0700698 public boolean setScanMode(int mode, int duration) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700699 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700700 try {
fredc0f420372012-04-12 00:02:00 -0700701 synchronized(mManagerCallback) {
702 if (mService != null) return mService.setScanMode(mode, duration);
703 }
Nick Pellybd022f42009-08-14 18:33:38 -0700704 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyde893f52009-09-08 13:15:33 -0700705 return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700706 }
707
Nick Pelly45e27042009-08-19 11:00:00 -0700708 /** @hide */
Nick Pelly12835472009-09-25 15:00:29 -0700709 public boolean setScanMode(int mode) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700710 if (getState() != STATE_ON) return false;
Srikanth Uppala827de2d2012-04-04 03:33:26 -0700711 /* getDiscoverableTimeout() to use the latest from NV than use 0 */
712 return setScanMode(mode, getDiscoverableTimeout());
Nick Pelly12835472009-09-25 15:00:29 -0700713 }
714
715 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700716 public int getDiscoverableTimeout() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700717 if (getState() != STATE_ON) return -1;
Nick Pellybd022f42009-08-14 18:33:38 -0700718 try {
fredc0f420372012-04-12 00:02:00 -0700719 synchronized(mManagerCallback) {
720 if (mService != null) return mService.getDiscoverableTimeout();
721 }
Nick Pellybd022f42009-08-14 18:33:38 -0700722 } catch (RemoteException e) {Log.e(TAG, "", e);}
723 return -1;
724 }
725
Nick Pelly45e27042009-08-19 11:00:00 -0700726 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700727 public void setDiscoverableTimeout(int timeout) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700728 if (getState() != STATE_ON) return;
Nick Pellybd022f42009-08-14 18:33:38 -0700729 try {
fredc0f420372012-04-12 00:02:00 -0700730 synchronized(mManagerCallback) {
731 if (mService != null) mService.setDiscoverableTimeout(timeout);
732 }
Nick Pellybd022f42009-08-14 18:33:38 -0700733 } catch (RemoteException e) {Log.e(TAG, "", e);}
734 }
735
Nick Pelly005b2282009-09-10 10:21:56 -0700736 /**
737 * Start the remote device discovery process.
738 * <p>The discovery process usually involves an inquiry scan of about 12
739 * seconds, followed by a page scan of each new device to retrieve its
740 * Bluetooth name.
741 * <p>This is an asynchronous call, it will return immediately. Register
742 * for {@link #ACTION_DISCOVERY_STARTED} and {@link
743 * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the
744 * discovery starts and completes. Register for {@link
745 * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices
746 * are found.
747 * <p>Device discovery is a heavyweight procedure. New connections to
748 * remote Bluetooth devices should not be attempted while discovery is in
749 * progress, and existing connections will experience limited bandwidth
750 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
Scott Main6d95fc02009-11-19 17:00:19 -0800751 * discovery. Discovery is not managed by the Activity,
752 * but is run as a system service, so an application should always call
753 * {@link BluetoothAdapter#cancelDiscovery()} even if it
754 * did not directly request a discovery, just to be sure.
Nick Pelly005b2282009-09-10 10:21:56 -0700755 * <p>Device discovery will only find remote devices that are currently
756 * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are
757 * not discoverable by default, and need to be entered into a special mode.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700758 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
759 * will return false. After turning on Bluetooth,
760 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
761 * to get the updated value.
Nick Pelly005b2282009-09-10 10:21:56 -0700762 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
763 *
764 * @return true on success, false on error
765 */
Nick Pellybd022f42009-08-14 18:33:38 -0700766 public boolean startDiscovery() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700767 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700768 try {
fredc0f420372012-04-12 00:02:00 -0700769 synchronized(mManagerCallback) {
770 if (mService != null) return mService.startDiscovery();
771 }
Nick Pellybd022f42009-08-14 18:33:38 -0700772 } catch (RemoteException e) {Log.e(TAG, "", e);}
773 return false;
774 }
775
Nick Pelly005b2282009-09-10 10:21:56 -0700776 /**
777 * Cancel the current device discovery process.
778 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Jake Hamby0f584302010-09-16 18:12:51 -0700779 * <p>Because discovery is a heavyweight procedure for the Bluetooth
Scott Main6d95fc02009-11-19 17:00:19 -0800780 * adapter, this method should always be called before attempting to connect
781 * to a remote device with {@link
782 * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by
783 * the Activity, but is run as a system service, so an application should
784 * always call cancel discovery even if it did not directly request a
785 * discovery, just to be sure.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700786 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
787 * will return false. After turning on Bluetooth,
788 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
789 * to get the updated value.
Nick Pelly005b2282009-09-10 10:21:56 -0700790 *
791 * @return true on success, false on error
792 */
793 public boolean cancelDiscovery() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700794 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700795 try {
fredc0f420372012-04-12 00:02:00 -0700796 synchronized(mManagerCallback) {
797 if (mService != null) return mService.cancelDiscovery();
798 }
Nick Pellybd022f42009-08-14 18:33:38 -0700799 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700800 return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700801 }
802
Nick Pelly005b2282009-09-10 10:21:56 -0700803 /**
804 * Return true if the local Bluetooth adapter is currently in the device
805 * discovery process.
806 * <p>Device discovery is a heavyweight procedure. New connections to
807 * remote Bluetooth devices should not be attempted while discovery is in
808 * progress, and existing connections will experience limited bandwidth
809 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
810 * discovery.
811 * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED}
812 * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery
813 * starts or completes.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700814 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
815 * will return false. After turning on Bluetooth,
816 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
817 * to get the updated value.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200818 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Nick Pelly005b2282009-09-10 10:21:56 -0700819 *
820 * @return true if discovering
821 */
Nick Pellybd022f42009-08-14 18:33:38 -0700822 public boolean isDiscovering() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700823 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700824 try {
fredc0f420372012-04-12 00:02:00 -0700825 synchronized(mManagerCallback) {
826 if (mService != null ) return mService.isDiscovering();
827 }
Nick Pellybd022f42009-08-14 18:33:38 -0700828 } catch (RemoteException e) {Log.e(TAG, "", e);}
829 return false;
830 }
831
832 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700833 * Return the set of {@link BluetoothDevice} objects that are bonded
834 * (paired) to the local adapter.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700835 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
836 * will return an empty set. After turning on Bluetooth,
837 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
838 * to get the updated value.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200839 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Nick Pellybd022f42009-08-14 18:33:38 -0700840 *
Nick Pelly005b2282009-09-10 10:21:56 -0700841 * @return unmodifiable set of {@link BluetoothDevice}, or null on error
Nick Pellybd022f42009-08-14 18:33:38 -0700842 */
843 public Set<BluetoothDevice> getBondedDevices() {
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700844 if (getState() != STATE_ON) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800845 return toDeviceSet(new BluetoothDevice[0]);
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700846 }
Nick Pellybd022f42009-08-14 18:33:38 -0700847 try {
fredc0f420372012-04-12 00:02:00 -0700848 synchronized(mManagerCallback) {
849 if (mService != null) return toDeviceSet(mService.getBondedDevices());
850 }
851 return toDeviceSet(new BluetoothDevice[0]);
Nick Pellybd022f42009-08-14 18:33:38 -0700852 } catch (RemoteException e) {Log.e(TAG, "", e);}
853 return null;
854 }
855
856 /**
Jaikumar Ganeshc53cab22010-10-26 16:02:26 -0700857 * Get the current connection state of the local Bluetooth adapter.
858 * This can be used to check whether the local Bluetooth adapter is connected
859 * to any profile of any other remote Bluetooth Device.
860 *
861 * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED}
862 * intent to get the connection state of the adapter.
863 *
864 * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED},
865 * {@link #STATE_CONNECTING} or {@link #STATE_DISCONNECTED}
866 *
867 * @hide
868 */
869 public int getConnectionState() {
870 if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED;
871 try {
fredc0f420372012-04-12 00:02:00 -0700872 synchronized(mManagerCallback) {
873 if (mService != null) return mService.getAdapterConnectionState();
874 }
Jaikumar Ganeshc53cab22010-10-26 16:02:26 -0700875 } catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);}
876 return BluetoothAdapter.STATE_DISCONNECTED;
877 }
878
879 /**
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700880 * Get the current connection state of a profile.
881 * This function can be used to check whether the local Bluetooth adapter
882 * is connected to any remote device for a specific profile.
Scott Main2d68a6b2011-09-26 22:59:38 -0700883 * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET},
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700884 * {@link BluetoothProfile#A2DP}.
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700885 *
886 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
887 *
888 * <p> Return value can be one of
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700889 * {@link BluetoothProfile#STATE_DISCONNECTED},
890 * {@link BluetoothProfile#STATE_CONNECTING},
891 * {@link BluetoothProfile#STATE_CONNECTED},
892 * {@link BluetoothProfile#STATE_DISCONNECTING}
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700893 */
894 public int getProfileConnectionState(int profile) {
895 if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED;
896 try {
fredc0f420372012-04-12 00:02:00 -0700897 synchronized(mManagerCallback) {
898 if (mService != null) return mService.getProfileConnectionState(profile);
899 }
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700900 } catch (RemoteException e) {
901 Log.e(TAG, "getProfileConnectionState:", e);
902 }
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700903 return BluetoothProfile.STATE_DISCONNECTED;
904 }
905
906 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700907 * Create a listening, secure RFCOMM Bluetooth socket.
908 * <p>A remote device connecting to this socket will be authenticated and
Nick Pellybd022f42009-08-14 18:33:38 -0700909 * communication on this socket will be encrypted.
Nick Pelly45e27042009-08-19 11:00:00 -0700910 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
Nick Pelly24bb9b82009-10-02 20:34:18 -0700911 * connections from a listening {@link BluetoothServerSocket}.
Nick Pelly45e27042009-08-19 11:00:00 -0700912 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700913 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
Nick Pelly45e27042009-08-19 11:00:00 -0700914 * @param channel RFCOMM channel to listen on
915 * @return a listening RFCOMM BluetoothServerSocket
916 * @throws IOException on error, for example Bluetooth not available, or
917 * insufficient permissions, or channel in use.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700918 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700919 */
Nick Pelly45e27042009-08-19 11:00:00 -0700920 public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException {
Nick Pellybd022f42009-08-14 18:33:38 -0700921 BluetoothServerSocket socket = new BluetoothServerSocket(
Nick Pelly45e27042009-08-19 11:00:00 -0700922 BluetoothSocket.TYPE_RFCOMM, true, true, channel);
Nick Pelly24bb9b82009-10-02 20:34:18 -0700923 int errno = socket.mSocket.bindListen();
924 if (errno != 0) {
zzy3b147b72012-04-03 19:48:32 -0700925 //TODO(BT): Throw the same exception error code
926 // that the previous code was using.
927 //socket.mSocket.throwErrnoNative(errno);
928 throw new IOException("Error: " + errno);
Nick Pellybd022f42009-08-14 18:33:38 -0700929 }
930 return socket;
931 }
932
933 /**
Nick Pelly24bb9b82009-10-02 20:34:18 -0700934 * Create a listening, secure RFCOMM Bluetooth socket with Service Record.
935 * <p>A remote device connecting to this socket will be authenticated and
936 * communication on this socket will be encrypted.
937 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
938 * connections from a listening {@link BluetoothServerSocket}.
939 * <p>The system will assign an unused RFCOMM channel to listen on.
940 * <p>The system will also register a Service Discovery
941 * Protocol (SDP) record with the local SDP server containing the specified
942 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
943 * can use the same UUID to query our SDP server and discover which channel
944 * to connect to. This SDP record will be removed when this socket is
945 * closed, or if this application closes unexpectedly.
Nick Pelly16fb88a2009-10-07 07:44:03 +0200946 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
947 * connect to this socket from another device using the same {@link UUID}.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700948 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
949 * @param name service name for SDP record
950 * @param uuid uuid for SDP record
951 * @return a listening RFCOMM BluetoothServerSocket
952 * @throws IOException on error, for example Bluetooth not available, or
953 * insufficient permissions, or channel in use.
954 */
Nick Pelly16fb88a2009-10-07 07:44:03 +0200955 public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid)
Nick Pelly24bb9b82009-10-02 20:34:18 -0700956 throws IOException {
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800957 return createNewRfcommSocketAndRecord(name, uuid, true, true);
958 }
959
960 /**
961 * Create a listening, insecure RFCOMM Bluetooth socket with Service Record.
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100962 * <p>The link key is not required to be authenticated, i.e the communication may be
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800963 * vulnerable to Man In the Middle attacks. For Bluetooth 2.1 devices,
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100964 * the link will be encrypted, as encryption is mandartory.
965 * For legacy devices (pre Bluetooth 2.1 devices) the link will not
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800966 * be encrypted. Use {@link #listenUsingRfcommWithServiceRecord}, if an
967 * encrypted and authenticated communication channel is desired.
968 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
969 * connections from a listening {@link BluetoothServerSocket}.
970 * <p>The system will assign an unused RFCOMM channel to listen on.
971 * <p>The system will also register a Service Discovery
972 * Protocol (SDP) record with the local SDP server containing the specified
973 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
974 * can use the same UUID to query our SDP server and discover which channel
975 * to connect to. This SDP record will be removed when this socket is
976 * closed, or if this application closes unexpectedly.
977 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
978 * connect to this socket from another device using the same {@link UUID}.
979 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
980 * @param name service name for SDP record
981 * @param uuid uuid for SDP record
982 * @return a listening RFCOMM BluetoothServerSocket
983 * @throws IOException on error, for example Bluetooth not available, or
984 * insufficient permissions, or channel in use.
985 */
986 public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
987 throws IOException {
988 return createNewRfcommSocketAndRecord(name, uuid, false, false);
989 }
990
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100991 /**
992 * Create a listening, encrypted,
993 * RFCOMM Bluetooth socket with Service Record.
994 * <p>The link will be encrypted, but the link key is not required to be authenticated
995 * i.e the communication is vulnerable to Man In the Middle attacks. Use
996 * {@link #listenUsingRfcommWithServiceRecord}, to ensure an authenticated link key.
997 * <p> Use this socket if authentication of link key is not possible.
998 * For example, for Bluetooth 2.1 devices, if any of the devices does not have
999 * an input and output capability or just has the ability to display a numeric key,
1000 * a secure socket connection is not possible and this socket can be used.
1001 * Use {@link #listenUsingInsecureRfcommWithServiceRecord}, if encryption is not required.
1002 * For Bluetooth 2.1 devices, the link will be encrypted, as encryption is mandartory.
1003 * For more details, refer to the Security Model section 5.2 (vol 3) of
1004 * Bluetooth Core Specification version 2.1 + EDR.
1005 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
1006 * connections from a listening {@link BluetoothServerSocket}.
1007 * <p>The system will assign an unused RFCOMM channel to listen on.
1008 * <p>The system will also register a Service Discovery
1009 * Protocol (SDP) record with the local SDP server containing the specified
1010 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
1011 * can use the same UUID to query our SDP server and discover which channel
1012 * to connect to. This SDP record will be removed when this socket is
1013 * closed, or if this application closes unexpectedly.
1014 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
1015 * connect to this socket from another device using the same {@link UUID}.
1016 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1017 * @param name service name for SDP record
1018 * @param uuid uuid for SDP record
1019 * @return a listening RFCOMM BluetoothServerSocket
1020 * @throws IOException on error, for example Bluetooth not available, or
1021 * insufficient permissions, or channel in use.
1022 * @hide
1023 */
1024 public BluetoothServerSocket listenUsingEncryptedRfcommWithServiceRecord(
1025 String name, UUID uuid) throws IOException {
1026 return createNewRfcommSocketAndRecord(name, uuid, false, true);
1027 }
1028
zzy3b147b72012-04-03 19:48:32 -07001029
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001030 private BluetoothServerSocket createNewRfcommSocketAndRecord(String name, UUID uuid,
1031 boolean auth, boolean encrypt) throws IOException {
Nick Pelly24bb9b82009-10-02 20:34:18 -07001032 BluetoothServerSocket socket;
zzy3b147b72012-04-03 19:48:32 -07001033 socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, auth,
1034 encrypt, new ParcelUuid(uuid));
1035 socket.setServiceName(name);
1036 int errno = socket.mSocket.bindListen();
1037 if (errno != 0) {
1038 //TODO(BT): Throw the same exception error code
1039 // that the previous code was using.
1040 //socket.mSocket.throwErrnoNative(errno);
1041 throw new IOException("Error: " + errno);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001042 }
Nick Pelly24bb9b82009-10-02 20:34:18 -07001043 return socket;
1044 }
1045
1046 /**
Nick Pellybd022f42009-08-14 18:33:38 -07001047 * Construct an unencrypted, unauthenticated, RFCOMM server socket.
1048 * Call #accept to retrieve connections to this socket.
1049 * @return An RFCOMM BluetoothServerSocket
1050 * @throws IOException On error, for example Bluetooth not available, or
1051 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001052 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001053 */
1054 public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
1055 BluetoothServerSocket socket = new BluetoothServerSocket(
1056 BluetoothSocket.TYPE_RFCOMM, false, false, port);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001057 int errno = socket.mSocket.bindListen();
1058 if (errno != 0) {
zzy3b147b72012-04-03 19:48:32 -07001059 //TODO(BT): Throw the same exception error code
1060 // that the previous code was using.
1061 //socket.mSocket.throwErrnoNative(errno);
1062 throw new IOException("Error: " + errno);
Nick Pellybd022f42009-08-14 18:33:38 -07001063 }
1064 return socket;
1065 }
1066
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +01001067 /**
1068 * Construct an encrypted, RFCOMM server socket.
1069 * Call #accept to retrieve connections to this socket.
1070 * @return An RFCOMM BluetoothServerSocket
1071 * @throws IOException On error, for example Bluetooth not available, or
1072 * insufficient permissions.
1073 * @hide
1074 */
1075 public BluetoothServerSocket listenUsingEncryptedRfcommOn(int port)
1076 throws IOException {
1077 BluetoothServerSocket socket = new BluetoothServerSocket(
1078 BluetoothSocket.TYPE_RFCOMM, false, true, port);
1079 int errno = socket.mSocket.bindListen();
zzy3b147b72012-04-03 19:48:32 -07001080 if (errno < 0) {
1081 //TODO(BT): Throw the same exception error code
1082 // that the previous code was using.
1083 //socket.mSocket.throwErrnoNative(errno);
1084 throw new IOException("Error: " + errno);
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +01001085 }
1086 return socket;
1087 }
1088
Nick Pellybd022f42009-08-14 18:33:38 -07001089 /**
1090 * Construct a SCO server socket.
1091 * Call #accept to retrieve connections to this socket.
1092 * @return A SCO BluetoothServerSocket
1093 * @throws IOException On error, for example Bluetooth not available, or
1094 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001095 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001096 */
1097 public static BluetoothServerSocket listenUsingScoOn() throws IOException {
1098 BluetoothServerSocket socket = new BluetoothServerSocket(
1099 BluetoothSocket.TYPE_SCO, false, false, -1);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001100 int errno = socket.mSocket.bindListen();
zzy3b147b72012-04-03 19:48:32 -07001101 if (errno < 0) {
1102 //TODO(BT): Throw the same exception error code
1103 // that the previous code was using.
1104 //socket.mSocket.throwErrnoNative(errno);
Nick Pellybd022f42009-08-14 18:33:38 -07001105 }
1106 return socket;
1107 }
1108
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001109 /**
1110 * Read the local Out of Band Pairing Data
1111 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1112 *
1113 * @return Pair<byte[], byte[]> of Hash and Randomizer
1114 *
1115 * @hide
1116 */
1117 public Pair<byte[], byte[]> readOutOfBandData() {
1118 if (getState() != STATE_ON) return null;
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001119 //TODO(BT
1120 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001121 try {
Jake Hambyf51eada2010-09-21 13:39:53 -07001122 byte[] hash;
1123 byte[] randomizer;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001124
1125 byte[] ret = mService.readOutOfBandData();
1126
1127 if (ret == null || ret.length != 32) return null;
1128
1129 hash = Arrays.copyOfRange(ret, 0, 16);
1130 randomizer = Arrays.copyOfRange(ret, 16, 32);
1131
1132 if (DBG) {
1133 Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) +
1134 ":" + Arrays.toString(randomizer));
1135 }
1136 return new Pair<byte[], byte[]>(hash, randomizer);
1137
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001138 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001139 return null;
1140 }
1141
Scott Main299ae672011-01-19 21:13:18 -08001142 /**
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001143 * Get the profile proxy object associated with the profile.
1144 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001145 * <p>Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET},
Matthew Xieddf7e472013-03-01 18:41:02 -08001146 * {@link BluetoothProfile#A2DP}, {@link BluetoothProfile#GATT}, or
1147 * {@link BluetoothProfile#GATT_SERVER}. Clients must implement
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001148 * {@link BluetoothProfile.ServiceListener} to get notified of
1149 * the connection status and to get the proxy object.
1150 *
1151 * @param context Context of the application
1152 * @param listener The service Listener for connection callbacks.
Scott Main2d68a6b2011-09-26 22:59:38 -07001153 * @param profile The Bluetooth profile; either {@link BluetoothProfile#HEALTH},
1154 * {@link BluetoothProfile#HEADSET} or {@link BluetoothProfile#A2DP}.
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001155 * @return true on success, false on error
1156 */
1157 public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener,
1158 int profile) {
1159 if (context == null || listener == null) return false;
1160
1161 if (profile == BluetoothProfile.HEADSET) {
1162 BluetoothHeadset headset = new BluetoothHeadset(context, listener);
1163 return true;
1164 } else if (profile == BluetoothProfile.A2DP) {
1165 BluetoothA2dp a2dp = new BluetoothA2dp(context, listener);
1166 return true;
Jaikumar Ganesh4ab0e772011-02-18 14:52:32 -08001167 } else if (profile == BluetoothProfile.INPUT_DEVICE) {
1168 BluetoothInputDevice iDev = new BluetoothInputDevice(context, listener);
1169 return true;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -08001170 } else if (profile == BluetoothProfile.PAN) {
1171 BluetoothPan pan = new BluetoothPan(context, listener);
1172 return true;
Jaikumar Ganesh2ea1e852011-04-01 16:33:09 -07001173 } else if (profile == BluetoothProfile.HEALTH) {
1174 BluetoothHealth health = new BluetoothHealth(context, listener);
1175 return true;
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001176 } 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 Main2d68a6b2011-09-26 22:59:38 -07001186 * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET} or
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001187 * {@link BluetoothProfile#A2DP}
1188 *
1189 * @param profile
1190 * @param proxy Profile proxy object
1191 */
1192 public void closeProfileProxy(int profile, BluetoothProfile proxy) {
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -08001193 if (proxy == null) return;
1194
1195 switch (profile) {
1196 case BluetoothProfile.HEADSET:
1197 BluetoothHeadset headset = (BluetoothHeadset)proxy;
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001198 headset.close();
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -08001199 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 Batta99081122013-02-05 15:28:33 -08001216 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 Ganesh62c37ef2010-08-24 17:36:13 -07001224 }
1225 }
1226
fredc0f420372012-04-12 00:02:00 -07001227 final private IBluetoothManagerCallback mManagerCallback =
1228 new IBluetoothManagerCallback.Stub() {
1229 public void onBluetoothServiceUp(IBluetooth bluetoothService) {
Matthew Xied77982e2012-11-29 20:26:19 -08001230 if (VDBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService);
fredc0f420372012-04-12 00:02:00 -07001231 synchronized (mManagerCallback) {
1232 mService = bluetoothService;
fredcbf072a72012-05-09 16:52:50 -07001233 for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
fredc903ac6f2012-04-24 03:59:57 -07001234 try {
fredcd6883532012-04-25 17:46:13 -07001235 if (cb != null) {
1236 cb.onBluetoothServiceUp(bluetoothService);
1237 } else {
1238 Log.d(TAG, "onBluetoothServiceUp: cb is null!!!");
1239 }
fredc903ac6f2012-04-24 03:59:57 -07001240 } catch (Exception e) { Log.e(TAG,"",e);}
1241 }
fredc0f420372012-04-12 00:02:00 -07001242 }
1243 }
1244
1245 public void onBluetoothServiceDown() {
Matthew Xied77982e2012-11-29 20:26:19 -08001246 if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService);
fredc0f420372012-04-12 00:02:00 -07001247 synchronized (mManagerCallback) {
1248 mService = null;
fredcbf072a72012-05-09 16:52:50 -07001249 for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
fredc903ac6f2012-04-24 03:59:57 -07001250 try {
fredcd6883532012-04-25 17:46:13 -07001251 if (cb != null) {
1252 cb.onBluetoothServiceDown();
1253 } else {
1254 Log.d(TAG, "onBluetoothServiceDown: cb is null!!!");
1255 }
fredc903ac6f2012-04-24 03:59:57 -07001256 } catch (Exception e) { Log.e(TAG,"",e);}
1257 }
fredc0f420372012-04-12 00:02:00 -07001258 }
1259 }
1260 };
1261
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001262 /**
Martijn Coenen6c614b72012-04-18 13:01:15 -07001263 * 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 Battafffa86b2012-08-08 15:35:49 -07001268 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 Coenen6c614b72012-04-18 13:01:15 -07001276 }
1277
1278 /**
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001279 * 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 Ganeshf5fb6c82011-08-03 14:17:22 -07001298 * 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 Ganeshef2cb7c2011-07-21 18:13:38 -07001302 *
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 Ganeshe4caddb2012-01-25 16:16:48 -08001311 //TODO(BT)
1312 /*
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001313 try {
1314 return mService.changeApplicationBluetoothState(on, new
1315 StateChangeCallbackWrapper(callback), new Binder());
1316 } catch (RemoteException e) {
1317 Log.e(TAG, "changeBluetoothState", e);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001318 }*/
Jaikumar Ganeshef2cb7c2011-07-21 18:13:38 -07001319 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 Ganeshe4caddb2012-01-25 16:16:48 -08001346 private Set<BluetoothDevice> toDeviceSet(BluetoothDevice[] devices) {
1347 Set<BluetoothDevice> deviceSet = new HashSet<BluetoothDevice>(Arrays.asList(devices));
1348 return Collections.unmodifiableSet(deviceSet);
Nick Pellybd022f42009-08-14 18:33:38 -07001349 }
Nick Pelly005b2282009-09-10 10:21:56 -07001350
fredc0f420372012-04-12 00:02:00 -07001351 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 Pelly005b2282009-09-10 10:21:56 -07001362 /**
Nick Pelly75596b42011-12-07 15:03:55 -08001363 * Validate a String Bluetooth address, such as "00:43:A8:23:10:F0"
Nick Pelly55e66f12009-09-18 11:37:06 -07001364 * <p>Alphabetic characters must be uppercase to be valid.
Nick Pelly005b2282009-09-10 10:21:56 -07001365 *
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 Pelly55e66f12009-09-18 11:37:06 -07001378 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
1379 // hex character, OK
1380 break;
Nick Pelly005b2282009-09-10 10:21:56 -07001381 }
1382 return false;
1383 case 2:
1384 if (c == ':') {
1385 break; // OK
1386 }
1387 return false;
1388 }
1389 }
1390 return true;
1391 }
fredc0f420372012-04-12 00:02:00 -07001392
1393 /*package*/ IBluetoothManager getBluetoothManager() {
1394 return mManagerService;
1395 }
1396
fredcbf072a72012-05-09 16:52:50 -07001397 private ArrayList<IBluetoothManagerCallback> mProxyServiceStateCallbacks = new ArrayList<IBluetoothManagerCallback>();
fredcd6883532012-04-25 17:46:13 -07001398
fredc903ac6f2012-04-24 03:59:57 -07001399 /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) {
fredc0f420372012-04-12 00:02:00 -07001400 synchronized (mManagerCallback) {
fredcd6883532012-04-25 17:46:13 -07001401 if (cb == null) {
fredcbf072a72012-05-09 16:52:50 -07001402 Log.w(TAG, "getBluetoothService() called with no BluetoothManagerCallback");
1403 } else if (!mProxyServiceStateCallbacks.contains(cb)) {
1404 mProxyServiceStateCallbacks.add(cb);
fredc903ac6f2012-04-24 03:59:57 -07001405 }
1406 }
1407 return mService;
1408 }
1409
1410 /*package*/ void removeServiceStateCallback(IBluetoothManagerCallback cb) {
1411 synchronized (mManagerCallback) {
fredcbf072a72012-05-09 16:52:50 -07001412 mProxyServiceStateCallbacks.remove(cb);
fredc0f420372012-04-12 00:02:00 -07001413 }
1414 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001415
1416 /**
Matthew Xiecdd94e32013-04-11 16:36:26 -07001417 * Callback interface used to deliver LE scan results.
Matthew Xieddf7e472013-03-01 18:41:02 -08001418 *
Matthew Xiecdd94e32013-04-11 16:36:26 -07001419 * @see #startLeScan(LeScanCallback)
1420 * @see #startLeScan(UUID[], LeScanCallback)
Matthew Xieddf7e472013-03-01 18:41:02 -08001421 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001422 public interface LeScanCallback {
1423 /**
1424 * Callback reporting an LE device found during a device scan initiated
1425 * by the {@link BluetoothAdapter#startLeScan} function.
1426 *
1427 * @param device Identifies the remote device
1428 * @param rssi The RSSI value for the remote device as reported by the
1429 * Bluetooth hardware. 0 if no RSSI value is available.
1430 * @param scanRecord The content of the advertisement record offered by
1431 * the remote device.
1432 */
1433 public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord);
Matthew Xieddf7e472013-03-01 18:41:02 -08001434 }
1435
1436 /**
1437 * Starts a scan for Bluetooth LE devices.
1438 *
1439 * <p>Results of the scan are reported using the
Matthew Xiecdd94e32013-04-11 16:36:26 -07001440 * {@link LeScanCallback#onLeScan} callback.
Matthew Xieddf7e472013-03-01 18:41:02 -08001441 *
Matthew Xied5752332013-04-24 17:51:37 -07001442 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
Matthew Xieddf7e472013-03-01 18:41:02 -08001443 *
Matthew Xiecdd94e32013-04-11 16:36:26 -07001444 * @param callback the callback LE scan results are delivered
Matthew Xieddf7e472013-03-01 18:41:02 -08001445 * @return true, if the scan was started successfully
1446 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001447 public boolean startLeScan(LeScanCallback callback) {
1448 return startLeScan(null, callback);
Matthew Xieddf7e472013-03-01 18:41:02 -08001449 }
1450
1451 /**
1452 * Starts a scan for Bluetooth LE devices, looking for devices that
1453 * advertise given services.
1454 *
1455 * <p>Devices which advertise all specified services are reported using the
Matthew Xiecdd94e32013-04-11 16:36:26 -07001456 * {@link LeScanCallback#onLeScan} callback.
Matthew Xieddf7e472013-03-01 18:41:02 -08001457 *
Matthew Xied5752332013-04-24 17:51:37 -07001458 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
Matthew Xieddf7e472013-03-01 18:41:02 -08001459 *
1460 * @param serviceUuids Array of services to look for
Matthew Xiecdd94e32013-04-11 16:36:26 -07001461 * @param callback the callback LE scan results are delivered
Matthew Xieddf7e472013-03-01 18:41:02 -08001462 * @return true, if the scan was started successfully
1463 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001464 public boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback) {
1465 if (DBG) Log.d(TAG, "startLeScan(): " + serviceUuids);
Matthew Xieddf7e472013-03-01 18:41:02 -08001466
Matthew Xiecdd94e32013-04-11 16:36:26 -07001467 if (callback == null) {
1468 if (DBG) Log.e(TAG, "startLeScan: null callback");
Matthew Xieddf7e472013-03-01 18:41:02 -08001469 return false;
1470 }
1471
Matthew Xiecdd94e32013-04-11 16:36:26 -07001472 synchronized(mLeScanClients) {
1473 if (mLeScanClients.containsKey(callback)) {
1474 if (DBG) Log.e(TAG, "LE Scan has already started");
1475 return false;
1476 }
1477
1478 try {
1479 IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
Matthew Xie32ab77b2013-05-08 19:26:57 -07001480 if (iGatt == null) {
1481 // BLE is not supported
1482 return false;
1483 }
1484
Matthew Xiecdd94e32013-04-11 16:36:26 -07001485 UUID uuid = UUID.randomUUID();
1486 GattCallbackWrapper wrapper = new GattCallbackWrapper(this, callback, serviceUuids);
Matthew Xiecdd94e32013-04-11 16:36:26 -07001487 iGatt.registerClient(new ParcelUuid(uuid), wrapper);
1488 if (wrapper.scanStarted()) {
1489 mLeScanClients.put(callback, wrapper);
1490 return true;
1491 }
1492 } catch (RemoteException e) {
1493 Log.e(TAG,"",e);
1494 }
1495 }
1496 return false;
Matthew Xieddf7e472013-03-01 18:41:02 -08001497 }
1498
1499 /**
1500 * Stops an ongoing Bluetooth LE device scan.
1501 *
Matthew Xied5752332013-04-24 17:51:37 -07001502 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
Matthew Xiecdd94e32013-04-11 16:36:26 -07001503 *
1504 * @param callback used to identify which scan to stop
1505 * must be the same handle used to start the scan
Matthew Xieddf7e472013-03-01 18:41:02 -08001506 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001507 public void stopLeScan(LeScanCallback callback) {
1508 if (DBG) Log.d(TAG, "stopLeScan()");
1509 GattCallbackWrapper wrapper;
1510 synchronized(mLeScanClients) {
1511 wrapper = mLeScanClients.remove(callback);
1512 if (wrapper == null) return;
Matthew Xieddf7e472013-03-01 18:41:02 -08001513 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001514 wrapper.stopLeScan();
Matthew Xieddf7e472013-03-01 18:41:02 -08001515 }
1516
1517 /**
1518 * Bluetooth GATT interface callbacks
1519 */
Matthew Xiecdd94e32013-04-11 16:36:26 -07001520 private static class GattCallbackWrapper extends IBluetoothGattCallback.Stub {
1521 private static final int LE_CALLBACK_REG_TIMEOUT = 2000;
1522 private static final int LE_CALLBACK_REG_WAIT_COUNT = 5;
Matthew Xieddf7e472013-03-01 18:41:02 -08001523
Matthew Xiecdd94e32013-04-11 16:36:26 -07001524 private final LeScanCallback mLeScanCb;
1525 // mLeHandle 0: not registered
1526 // -1: scan stopped
1527 // >0: registered and scan started
1528 private int mLeHandle;
1529 private final UUID[] mScanFilter;
1530 private WeakReference<BluetoothAdapter> mBluetoothAdapter;
Matthew Xieddf7e472013-03-01 18:41:02 -08001531
Matthew Xiecdd94e32013-04-11 16:36:26 -07001532 public GattCallbackWrapper(BluetoothAdapter bluetoothAdapter,
1533 LeScanCallback leScanCb, UUID[] uuid) {
1534 mBluetoothAdapter = new WeakReference<BluetoothAdapter>(bluetoothAdapter);
1535 mLeScanCb = leScanCb;
1536 mScanFilter = uuid;
1537 mLeHandle = 0;
1538 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001539
Matthew Xiecdd94e32013-04-11 16:36:26 -07001540 public boolean scanStarted() {
1541 boolean started = false;
1542 synchronized(this) {
1543 if (mLeHandle == -1) return false;
1544
1545 int count = 0;
1546 // wait for callback registration and LE scan to start
1547 while (mLeHandle == 0 && count < LE_CALLBACK_REG_WAIT_COUNT) {
1548 try {
1549 wait(LE_CALLBACK_REG_TIMEOUT);
1550 } catch (InterruptedException e) {
1551 Log.e(TAG, "Callback reg wait interrupted: " + e);
1552 }
1553 count++;
Matthew Xieddf7e472013-03-01 18:41:02 -08001554 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001555 started = (mLeHandle > 0);
Matthew Xieddf7e472013-03-01 18:41:02 -08001556 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001557 return started;
1558 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001559
Matthew Xiecdd94e32013-04-11 16:36:26 -07001560 public void stopLeScan() {
1561 synchronized(this) {
1562 if (mLeHandle <= 0) {
1563 Log.e(TAG, "Error state, mLeHandle: " + mLeHandle);
1564 return;
1565 }
1566 BluetoothAdapter adapter = mBluetoothAdapter.get();
1567 if (adapter != null) {
1568 try {
1569 IBluetoothGatt iGatt = adapter.getBluetoothManager().getBluetoothGatt();
1570 iGatt.stopScan(mLeHandle, false);
1571 iGatt.unregisterClient(mLeHandle);
1572 } catch (RemoteException e) {
1573 Log.e(TAG, "Failed to stop scan and unregister" + e);
1574 }
1575 } else {
1576 Log.e(TAG, "stopLeScan, BluetoothAdapter is null");
1577 }
1578 mLeHandle = -1;
1579 notifyAll();
Matthew Xieddf7e472013-03-01 18:41:02 -08001580 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001581 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001582
Matthew Xiecdd94e32013-04-11 16:36:26 -07001583 /**
1584 * Application interface registered - app is ready to go
1585 */
1586 public void onClientRegistered(int status, int clientIf) {
1587 if (DBG) Log.d(TAG, "onClientRegistered() - status=" + status +
1588 " clientIf=" + clientIf);
1589 synchronized(this) {
1590 if (mLeHandle == -1) {
1591 if (DBG) Log.d(TAG, "onClientRegistered LE scan canceled");
1592 }
1593
1594 if (status == BluetoothGatt.GATT_SUCCESS) {
1595 mLeHandle = clientIf;
1596 IBluetoothGatt iGatt = null;
1597 try {
1598 BluetoothAdapter adapter = mBluetoothAdapter.get();
1599 if (adapter != null) {
1600 iGatt = adapter.getBluetoothManager().getBluetoothGatt();
1601 if (mScanFilter == null) {
1602 iGatt.startScan(mLeHandle, false);
1603 } else {
1604 ParcelUuid[] uuids = new ParcelUuid[mScanFilter.length];
1605 for(int i = 0; i != uuids.length; ++i) {
1606 uuids[i] = new ParcelUuid(mScanFilter[i]);
1607 }
1608 iGatt.startScanWithUuids(mLeHandle, false, uuids);
1609 }
1610 } else {
1611 Log.e(TAG, "onClientRegistered, BluetoothAdapter null");
1612 mLeHandle = -1;
1613 }
1614 } catch (RemoteException e) {
1615 Log.e(TAG, "fail to start le scan: " + e);
1616 mLeHandle = -1;
1617 }
1618 if (mLeHandle == -1) {
1619 // registration succeeded but start scan failed
1620 if (iGatt != null) {
1621 try {
1622 iGatt.unregisterClient(mLeHandle);
1623 } catch (RemoteException e) {
1624 Log.e(TAG, "fail to unregister callback: " + mLeHandle +
1625 " error: " + e);
1626 }
1627 }
1628 }
1629 } else {
1630 // registration failed
1631 mLeHandle = -1;
1632 }
1633 notifyAll();
Matthew Xieddf7e472013-03-01 18:41:02 -08001634 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001635 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001636
Matthew Xiecdd94e32013-04-11 16:36:26 -07001637 public void onClientConnectionState(int status, int clientIf,
1638 boolean connected, String address) {
1639 // no op
1640 }
1641
1642 /**
1643 * Callback reporting an LE scan result.
1644 * @hide
1645 */
1646 public void onScanResult(String address, int rssi, byte[] advData) {
1647 if (DBG) Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" +rssi);
1648
1649 // Check null in case the scan has been stopped
1650 synchronized(this) {
1651 if (mLeHandle <= 0) return;
Matthew Xieddf7e472013-03-01 18:41:02 -08001652 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001653 try {
1654 BluetoothAdapter adapter = mBluetoothAdapter.get();
1655 if (adapter == null) {
1656 Log.d(TAG, "onScanResult, BluetoothAdapter null");
1657 return;
1658 }
1659 mLeScanCb.onLeScan(adapter.getRemoteDevice(address), rssi, advData);
1660 } catch (Exception ex) {
1661 Log.w(TAG, "Unhandled exception: " + ex);
Matthew Xieddf7e472013-03-01 18:41:02 -08001662 }
Matthew Xiecdd94e32013-04-11 16:36:26 -07001663 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001664
Matthew Xiecdd94e32013-04-11 16:36:26 -07001665 public void onGetService(String address, int srvcType,
1666 int srvcInstId, ParcelUuid srvcUuid) {
1667 // no op
1668 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001669
Matthew Xiecdd94e32013-04-11 16:36:26 -07001670 public void onGetIncludedService(String address, int srvcType,
1671 int srvcInstId, ParcelUuid srvcUuid,
1672 int inclSrvcType, int inclSrvcInstId,
1673 ParcelUuid inclSrvcUuid) {
1674 // no op
1675 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001676
Matthew Xiecdd94e32013-04-11 16:36:26 -07001677 public void onGetCharacteristic(String address, int srvcType,
1678 int srvcInstId, ParcelUuid srvcUuid,
1679 int charInstId, ParcelUuid charUuid,
1680 int charProps) {
1681 // no op
1682 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001683
Matthew Xiecdd94e32013-04-11 16:36:26 -07001684 public void onGetDescriptor(String address, int srvcType,
1685 int srvcInstId, ParcelUuid srvcUuid,
1686 int charInstId, ParcelUuid charUuid,
1687 ParcelUuid descUuid) {
1688 // no op
1689 }
1690
1691 public void onSearchComplete(String address, int status) {
1692 // no op
1693 }
1694
1695 public void onCharacteristicRead(String address, int status, int srvcType,
1696 int srvcInstId, ParcelUuid srvcUuid,
1697 int charInstId, ParcelUuid charUuid, byte[] value) {
1698 // no op
1699 }
1700
1701 public void onCharacteristicWrite(String address, int status, int srvcType,
1702 int srvcInstId, ParcelUuid srvcUuid,
1703 int charInstId, ParcelUuid charUuid) {
1704 // no op
1705 }
1706
1707 public void onNotify(String address, int srvcType,
Matthew Xieddf7e472013-03-01 18:41:02 -08001708 int srvcInstId, ParcelUuid srvcUuid,
1709 int charInstId, ParcelUuid charUuid,
1710 byte[] value) {
Matthew Xiecdd94e32013-04-11 16:36:26 -07001711 // no op
1712 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001713
Matthew Xiecdd94e32013-04-11 16:36:26 -07001714 public void onDescriptorRead(String address, int status, int srvcType,
1715 int srvcInstId, ParcelUuid srvcUuid,
1716 int charInstId, ParcelUuid charUuid,
1717 ParcelUuid descrUuid, byte[] value) {
1718 // no op
1719 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001720
Matthew Xiecdd94e32013-04-11 16:36:26 -07001721 public void onDescriptorWrite(String address, int status, int srvcType,
1722 int srvcInstId, ParcelUuid srvcUuid,
1723 int charInstId, ParcelUuid charUuid,
1724 ParcelUuid descrUuid) {
1725 // no op
1726 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001727
Matthew Xiecdd94e32013-04-11 16:36:26 -07001728 public void onExecuteWrite(String address, int status) {
1729 // no op
1730 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001731
Matthew Xiecdd94e32013-04-11 16:36:26 -07001732 public void onReadRemoteRssi(String address, int rssi, int status) {
1733 // no op
1734 }
1735 }
Matthew Xieddf7e472013-03-01 18:41:02 -08001736
Nick Pellybd022f42009-08-14 18:33:38 -07001737}