blob: 1bbfb5dfb31203924c4730073c049ebd922caf98 [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;
23import android.os.Handler;
Nick Pellyf242b7b2009-10-08 00:12:45 +020024import android.os.IBinder;
Matthew Xie484867a2011-08-25 16:45:58 -070025import android.os.Looper;
Nick Pelly24bb9b82009-10-02 20:34:18 -070026import android.os.Message;
Nick Pellyaef439e2009-09-28 12:33:17 -070027import android.os.ParcelUuid;
Nick Pellybd022f42009-08-14 18:33:38 -070028import android.os.RemoteException;
Nick Pellyf242b7b2009-10-08 00:12:45 +020029import android.os.ServiceManager;
Nick Pellybd022f42009-08-14 18:33:38 -070030import android.util.Log;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -070031import android.util.Pair;
Nick Pellybd022f42009-08-14 18:33:38 -070032
33import java.io.IOException;
fredc903ac6f2012-04-24 03:59:57 -070034import java.util.ArrayList;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -070035import java.util.Arrays;
Nick Pellybd022f42009-08-14 18:33:38 -070036import java.util.Collections;
Nick Pellybd022f42009-08-14 18:33:38 -070037import java.util.HashSet;
Nick Pelly24bb9b82009-10-02 20:34:18 -070038import java.util.LinkedList;
39import java.util.Random;
40import java.util.Set;
Nick Pelly16fb88a2009-10-07 07:44:03 +020041import java.util.UUID;
Nick Pellybd022f42009-08-14 18:33:38 -070042
43/**
Scott Main9fab0ae2009-11-03 18:17:59 -080044 * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter}
45 * lets you perform fundamental Bluetooth tasks, such as initiate
46 * device discovery, query a list of bonded (paired) devices,
47 * instantiate a {@link BluetoothDevice} using a known MAC address, and create
48 * a {@link BluetoothServerSocket} to listen for connection requests from other
Nick Pelly45e27042009-08-19 11:00:00 -070049 * devices.
Scott Main9fab0ae2009-11-03 18:17:59 -080050 *
51 * <p>To get a {@link BluetoothAdapter} representing the local Bluetooth
52 * adapter, call the static {@link #getDefaultAdapter} method.
53 * Fundamentally, this is your starting point for all
54 * Bluetooth actions. Once you have the local adapter, you can get a set of
55 * {@link BluetoothDevice} objects representing all paired devices with
56 * {@link #getBondedDevices()}; start device discovery with
57 * {@link #startDiscovery()}; or create a {@link BluetoothServerSocket} to
58 * listen for incoming connection requests with
59 * {@link #listenUsingRfcommWithServiceRecord(String,UUID)}.
60 *
61 * <p class="note"><strong>Note:</strong>
62 * Most methods require the {@link android.Manifest.permission#BLUETOOTH}
63 * permission and some also require the
64 * {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
65 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080066 * <div class="special reference">
67 * <h3>Developer Guides</h3>
68 * <p>For more information about using Bluetooth, read the
69 * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p>
70 * </div>
71 *
Scott Main9fab0ae2009-11-03 18:17:59 -080072 * {@see BluetoothDevice}
73 * {@see BluetoothServerSocket}
Nick Pellybd022f42009-08-14 18:33:38 -070074 */
75public final class BluetoothAdapter {
76 private static final String TAG = "BluetoothAdapter";
fredc0f420372012-04-12 00:02:00 -070077 private static final boolean DBG = true;
Matthew Xie3b6214f2012-08-29 00:12:29 -070078 private static final boolean VDBG = false;
Nick Pellybd022f42009-08-14 18:33:38 -070079
Nick Pellyde893f52009-09-08 13:15:33 -070080 /**
Nick Pellyb24e11b2009-09-08 17:40:43 -070081 * Sentinel error value for this class. Guaranteed to not equal any other
82 * integer constant in this class. Provided as a convenience for functions
83 * that require a sentinel error value, for example:
84 * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
85 * BluetoothAdapter.ERROR)</code>
86 */
Nick Pelly005b2282009-09-10 10:21:56 -070087 public static final int ERROR = Integer.MIN_VALUE;
Nick Pellyb24e11b2009-09-08 17:40:43 -070088
89 /**
Nick Pellyde893f52009-09-08 13:15:33 -070090 * Broadcast Action: The state of the local Bluetooth adapter has been
91 * changed.
92 * <p>For example, Bluetooth has been turned on or off.
Nick Pelly005b2282009-09-10 10:21:56 -070093 * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link
Nick Pellyde893f52009-09-08 13:15:33 -070094 * #EXTRA_PREVIOUS_STATE} containing the new and old states
95 * respectively.
96 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
97 */
98 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
99 public static final String ACTION_STATE_CHANGED =
Nick Pelly005b2282009-09-10 10:21:56 -0700100 "android.bluetooth.adapter.action.STATE_CHANGED";
Nick Pellybd022f42009-08-14 18:33:38 -0700101
Nick Pellyde893f52009-09-08 13:15:33 -0700102 /**
103 * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
104 * intents to request the current power state. Possible values are:
105 * {@link #STATE_OFF},
106 * {@link #STATE_TURNING_ON},
107 * {@link #STATE_ON},
108 * {@link #STATE_TURNING_OFF},
109 */
110 public static final String EXTRA_STATE =
Nick Pelly005b2282009-09-10 10:21:56 -0700111 "android.bluetooth.adapter.extra.STATE";
Nick Pellyde893f52009-09-08 13:15:33 -0700112 /**
113 * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
114 * intents to request the previous power state. Possible values are:
115 * {@link #STATE_OFF},
116 * {@link #STATE_TURNING_ON},
117 * {@link #STATE_ON},
118 * {@link #STATE_TURNING_OFF},
119 */
120 public static final String EXTRA_PREVIOUS_STATE =
Nick Pelly005b2282009-09-10 10:21:56 -0700121 "android.bluetooth.adapter.extra.PREVIOUS_STATE";
Nick Pellybd022f42009-08-14 18:33:38 -0700122
Nick Pellyde893f52009-09-08 13:15:33 -0700123 /**
124 * Indicates the local Bluetooth adapter is off.
125 */
Nick Pelly005b2282009-09-10 10:21:56 -0700126 public static final int STATE_OFF = 10;
Nick Pellyde893f52009-09-08 13:15:33 -0700127 /**
128 * Indicates the local Bluetooth adapter is turning on. However local
129 * clients should wait for {@link #STATE_ON} before attempting to
130 * use the adapter.
131 */
Nick Pelly005b2282009-09-10 10:21:56 -0700132 public static final int STATE_TURNING_ON = 11;
Nick Pellyde893f52009-09-08 13:15:33 -0700133 /**
134 * Indicates the local Bluetooth adapter is on, and ready for use.
135 */
Nick Pelly005b2282009-09-10 10:21:56 -0700136 public static final int STATE_ON = 12;
Nick Pellyde893f52009-09-08 13:15:33 -0700137 /**
138 * Indicates the local Bluetooth adapter is turning off. Local clients
139 * should immediately attempt graceful disconnection of any remote links.
140 */
Nick Pelly005b2282009-09-10 10:21:56 -0700141 public static final int STATE_TURNING_OFF = 13;
Nick Pellyde893f52009-09-08 13:15:33 -0700142
143 /**
Nick Pelly18b1e792009-09-24 11:14:15 -0700144 * Activity Action: Show a system activity that requests discoverable mode.
Scott Main6d95fc02009-11-19 17:00:19 -0800145 * This activity will also request the user to turn on Bluetooth if it
Nick Pelly1acdcc12009-09-28 10:33:55 -0700146 * is not currently enabled.
Nick Pelly18b1e792009-09-24 11:14:15 -0700147 * <p>Discoverable mode is equivalent to {@link
148 * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see
149 * this Bluetooth adapter when they perform a discovery.
Scott Main6d95fc02009-11-19 17:00:19 -0800150 * <p>For privacy, Android is not discoverable by default.
151 * <p>The sender of this Intent can optionally use extra field {@link
Nick Pelly18b1e792009-09-24 11:14:15 -0700152 * #EXTRA_DISCOVERABLE_DURATION} to request the duration of
153 * discoverability. Currently the default duration is 120 seconds, and
154 * maximum duration is capped at 300 seconds for each request.
155 * <p>Notification of the result of this activity is posted using the
156 * {@link android.app.Activity#onActivityResult} callback. The
157 * <code>resultCode</code>
Michael Chancdd28642009-11-05 18:29:01 -0800158 * will be the duration (in seconds) of discoverability or
159 * {@link android.app.Activity#RESULT_CANCELED} if the user rejected
160 * discoverability or an error has occurred.
Nick Pelly18b1e792009-09-24 11:14:15 -0700161 * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED}
Scott Main6d95fc02009-11-19 17:00:19 -0800162 * for global notification whenever the scan mode changes. For example, an
163 * application can be notified when the device has ended discoverability.
Nick Pelly1acdcc12009-09-28 10:33:55 -0700164 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pelly18b1e792009-09-24 11:14:15 -0700165 */
166 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
167 public static final String ACTION_REQUEST_DISCOVERABLE =
168 "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE";
169
170 /**
171 * Used as an optional int extra field in {@link
172 * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration
173 * for discoverability in seconds. The current default is 120 seconds, and
174 * requests over 300 seconds will be capped. These values could change.
175 */
176 public static final String EXTRA_DISCOVERABLE_DURATION =
177 "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION";
178
179 /**
Nick Pelly1acdcc12009-09-28 10:33:55 -0700180 * Activity Action: Show a system activity that allows the user to turn on
181 * Bluetooth.
182 * <p>This system activity will return once Bluetooth has completed turning
183 * on, or the user has decided not to turn Bluetooth on.
184 * <p>Notification of the result of this activity is posted using the
185 * {@link android.app.Activity#onActivityResult} callback. The
186 * <code>resultCode</code>
Michael Chancdd28642009-11-05 18:29:01 -0800187 * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
188 * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
189 * has rejected the request or an error has occurred.
Nick Pelly1acdcc12009-09-28 10:33:55 -0700190 * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED}
191 * for global notification whenever Bluetooth is turned on or off.
192 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
193 */
194 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
195 public static final String ACTION_REQUEST_ENABLE =
196 "android.bluetooth.adapter.action.REQUEST_ENABLE";
197
198 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700199 * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter
200 * has changed.
Nick Pelly005b2282009-09-10 10:21:56 -0700201 * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link
Nick Pellyde893f52009-09-08 13:15:33 -0700202 * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes
203 * respectively.
204 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
205 */
206 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
207 public static final String ACTION_SCAN_MODE_CHANGED =
Nick Pelly005b2282009-09-10 10:21:56 -0700208 "android.bluetooth.adapter.action.SCAN_MODE_CHANGED";
Nick Pellyde893f52009-09-08 13:15:33 -0700209
210 /**
211 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
212 * intents to request the current scan mode. Possible values are:
213 * {@link #SCAN_MODE_NONE},
214 * {@link #SCAN_MODE_CONNECTABLE},
215 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
216 */
Nick Pelly005b2282009-09-10 10:21:56 -0700217 public static final String EXTRA_SCAN_MODE = "android.bluetooth.adapter.extra.SCAN_MODE";
Nick Pellyde893f52009-09-08 13:15:33 -0700218 /**
219 * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
220 * intents to request the previous scan mode. Possible values are:
221 * {@link #SCAN_MODE_NONE},
222 * {@link #SCAN_MODE_CONNECTABLE},
223 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
224 */
225 public static final String EXTRA_PREVIOUS_SCAN_MODE =
Nick Pelly005b2282009-09-10 10:21:56 -0700226 "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE";
Nick Pellyde893f52009-09-08 13:15:33 -0700227
228 /**
229 * Indicates that both inquiry scan and page scan are disabled on the local
230 * Bluetooth adapter. Therefore this device is neither discoverable
231 * nor connectable from remote Bluetooth devices.
232 */
Nick Pelly005b2282009-09-10 10:21:56 -0700233 public static final int SCAN_MODE_NONE = 20;
Nick Pellyde893f52009-09-08 13:15:33 -0700234 /**
235 * Indicates that inquiry scan is disabled, but page scan is enabled on the
236 * local Bluetooth adapter. Therefore this device is not discoverable from
237 * remote Bluetooth devices, but is connectable from remote devices that
238 * have previously discovered this device.
239 */
Nick Pelly005b2282009-09-10 10:21:56 -0700240 public static final int SCAN_MODE_CONNECTABLE = 21;
Nick Pellyde893f52009-09-08 13:15:33 -0700241 /**
242 * Indicates that both inquiry scan and page scan are enabled on the local
243 * Bluetooth adapter. Therefore this device is both discoverable and
244 * connectable from remote Bluetooth devices.
245 */
Nick Pelly005b2282009-09-10 10:21:56 -0700246 public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23;
Nick Pellybd022f42009-08-14 18:33:38 -0700247
Nick Pelly005b2282009-09-10 10:21:56 -0700248
249 /**
250 * Broadcast Action: The local Bluetooth adapter has started the remote
251 * device discovery process.
252 * <p>This usually involves an inquiry scan of about 12 seconds, followed
253 * by a page scan of each new device to retrieve its Bluetooth name.
254 * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as
255 * remote Bluetooth devices are found.
256 * <p>Device discovery is a heavyweight procedure. New connections to
257 * remote Bluetooth devices should not be attempted while discovery is in
258 * progress, and existing connections will experience limited bandwidth
259 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
260 * discovery.
261 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
262 */
263 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
264 public static final String ACTION_DISCOVERY_STARTED =
265 "android.bluetooth.adapter.action.DISCOVERY_STARTED";
266 /**
267 * Broadcast Action: The local Bluetooth adapter has finished the device
268 * discovery process.
269 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
270 */
271 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
272 public static final String ACTION_DISCOVERY_FINISHED =
273 "android.bluetooth.adapter.action.DISCOVERY_FINISHED";
274
275 /**
276 * Broadcast Action: The local Bluetooth adapter has changed its friendly
277 * Bluetooth name.
278 * <p>This name is visible to remote Bluetooth devices.
279 * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing
280 * the name.
281 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
282 */
283 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
284 public static final String ACTION_LOCAL_NAME_CHANGED =
285 "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED";
286 /**
287 * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED}
288 * intents to request the local Bluetooth name.
289 */
290 public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME";
291
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700292 /**
293 * Intent used to broadcast the change in connection state of the local
294 * Bluetooth adapter to a profile of the remote device. When the adapter is
295 * not connected to any profiles of any remote devices and it attempts a
296 * connection to a profile this intent will sent. Once connected, this intent
297 * will not be sent for any more connection attempts to any profiles of any
298 * remote device. When the adapter disconnects from the last profile its
299 * connected to of any remote device, this intent will be sent.
300 *
301 * <p> This intent is useful for applications that are only concerned about
302 * whether the local adapter is connected to any profile of any device and
303 * are not really concerned about which profile. For example, an application
304 * which displays an icon to display whether Bluetooth is connected or not
305 * can use this intent.
306 *
307 * <p>This intent will have 3 extras:
Jaikumar Ganesh0b5b35f2011-02-01 16:47:11 -0800308 * {@link #EXTRA_CONNECTION_STATE} - The current connection state.
309 * {@link #EXTRA_PREVIOUS_CONNECTION_STATE}- The previous connection state.
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700310 * {@link BluetoothDevice#EXTRA_DEVICE} - The remote device.
311 *
Jaikumar Ganesh0b5b35f2011-02-01 16:47:11 -0800312 * {@link #EXTRA_CONNECTION_STATE} or {@link #EXTRA_PREVIOUS_CONNECTION_STATE}
313 * can be any of {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -0700314 * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
315 *
316 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
317 */
318 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
319 public static final String ACTION_CONNECTION_STATE_CHANGED =
320 "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED";
321
322 /**
323 * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED}
324 *
325 * This extra represents the current connection state.
326 */
327 public static final String EXTRA_CONNECTION_STATE =
328 "android.bluetooth.adapter.extra.CONNECTION_STATE";
329
330 /**
331 * Extra used by {@link #ACTION_CONNECTION_STATE_CHANGED}
332 *
333 * This extra represents the previous connection state.
334 */
335 public static final String EXTRA_PREVIOUS_CONNECTION_STATE =
336 "android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE";
337
338 /** The profile is in disconnected state */
339 public static final int STATE_DISCONNECTED = 0;
340 /** The profile is in connecting state */
341 public static final int STATE_CONNECTING = 1;
342 /** The profile is in connected state */
343 public static final int STATE_CONNECTED = 2;
344 /** The profile is in disconnecting state */
345 public static final int STATE_DISCONNECTING = 3;
346
Nick Pellyf242b7b2009-10-08 00:12:45 +0200347 /** @hide */
fredc0f420372012-04-12 00:02:00 -0700348 public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager";
Nick Pellyf242b7b2009-10-08 00:12:45 +0200349
Nick Pelly005b2282009-09-10 10:21:56 -0700350 private static final int ADDRESS_LENGTH = 17;
Nick Pellybd022f42009-08-14 18:33:38 -0700351
Nick Pellyf242b7b2009-10-08 00:12:45 +0200352 /**
Jake Hambyf51eada2010-09-21 13:39:53 -0700353 * Lazily initialized singleton. Guaranteed final after first object
Nick Pellyf242b7b2009-10-08 00:12:45 +0200354 * constructed.
355 */
356 private static BluetoothAdapter sAdapter;
357
fredc0f420372012-04-12 00:02:00 -0700358 private final IBluetoothManager mManagerService;
359 private IBluetooth mService;
Nick Pellybd022f42009-08-14 18:33:38 -0700360
Matthew Xie484867a2011-08-25 16:45:58 -0700361 private Handler mServiceRecordHandler;
362
Nick Pellybd022f42009-08-14 18:33:38 -0700363 /**
Nick Pellyf242b7b2009-10-08 00:12:45 +0200364 * Get a handle to the default local Bluetooth adapter.
365 * <p>Currently Android only supports one Bluetooth adapter, but the API
366 * could be extended to support more. This will always return the default
367 * adapter.
368 * @return the default local adapter, or null if Bluetooth is not supported
369 * on this hardware platform
370 */
371 public static synchronized BluetoothAdapter getDefaultAdapter() {
372 if (sAdapter == null) {
fredc0f420372012-04-12 00:02:00 -0700373 IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
Nick Pellyf242b7b2009-10-08 00:12:45 +0200374 if (b != null) {
fredc0f420372012-04-12 00:02:00 -0700375 IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
376 sAdapter = new BluetoothAdapter(managerService);
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800377 } else {
378 Log.e(TAG, "Bluetooth binder is null");
Nick Pellyf242b7b2009-10-08 00:12:45 +0200379 }
380 }
381 return sAdapter;
382 }
383
384 /**
385 * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.
Nick Pellybd022f42009-08-14 18:33:38 -0700386 */
fredc0f420372012-04-12 00:02:00 -0700387 BluetoothAdapter(IBluetoothManager managerService) {
388
389 if (managerService == null) {
390 throw new IllegalArgumentException("bluetooth manager service is null");
Nick Pellybd022f42009-08-14 18:33:38 -0700391 }
fredc0f420372012-04-12 00:02:00 -0700392 try {
393 mService = managerService.registerAdapter(mManagerCallback);
394 } catch (RemoteException e) {Log.e(TAG, "", e);}
395 mManagerService = managerService;
Matthew Xie484867a2011-08-25 16:45:58 -0700396 mServiceRecordHandler = null;
Nick Pellybd022f42009-08-14 18:33:38 -0700397 }
398
399 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700400 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
401 * address.
402 * <p>Valid Bluetooth hardware addresses must be upper case, in a format
Nick Pelly005b2282009-09-10 10:21:56 -0700403 * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is
404 * available to validate a Bluetooth address.
Nick Pelly45e27042009-08-19 11:00:00 -0700405 * <p>A {@link BluetoothDevice} will always be returned for a valid
406 * hardware address, even if this adapter has never seen that device.
Nick Pellyde893f52009-09-08 13:15:33 -0700407 *
Nick Pellybd022f42009-08-14 18:33:38 -0700408 * @param address valid Bluetooth MAC address
Nick Pelly45e27042009-08-19 11:00:00 -0700409 * @throws IllegalArgumentException if address is invalid
Nick Pellybd022f42009-08-14 18:33:38 -0700410 */
411 public BluetoothDevice getRemoteDevice(String address) {
412 return new BluetoothDevice(address);
413 }
414
415 /**
Nick Pelly75596b42011-12-07 15:03:55 -0800416 * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
417 * address.
418 * <p>Valid Bluetooth hardware addresses must be 6 bytes. This method
419 * expects the address in network byte order (MSB first).
420 * <p>A {@link BluetoothDevice} will always be returned for a valid
421 * hardware address, even if this adapter has never seen that device.
422 *
423 * @param address Bluetooth MAC address (6 bytes)
424 * @throws IllegalArgumentException if address is invalid
425 */
426 public BluetoothDevice getRemoteDevice(byte[] address) {
427 if (address == null || address.length != 6) {
428 throw new IllegalArgumentException("Bluetooth address must have 6 bytes");
429 }
430 return new BluetoothDevice(String.format("%02X:%02X:%02X:%02X:%02X:%02X",
431 address[0], address[1], address[2], address[3], address[4], address[5]));
432 }
433
434 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700435 * Return true if Bluetooth is currently enabled and ready for use.
436 * <p>Equivalent to:
437 * <code>getBluetoothState() == STATE_ON</code>
438 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700439 *
Nick Pellyde893f52009-09-08 13:15:33 -0700440 * @return true if the local adapter is turned on
Nick Pellybd022f42009-08-14 18:33:38 -0700441 */
442 public boolean isEnabled() {
fredc0f420372012-04-12 00:02:00 -0700443
Nick Pellybd022f42009-08-14 18:33:38 -0700444 try {
fredc0f420372012-04-12 00:02:00 -0700445 synchronized(mManagerCallback) {
446 if (mService != null) return mService.isEnabled();
447 }
Nick Pellybd022f42009-08-14 18:33:38 -0700448 } catch (RemoteException e) {Log.e(TAG, "", e);}
449 return false;
450 }
451
452 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700453 * Get the current state of the local Bluetooth adapter.
454 * <p>Possible return values are
455 * {@link #STATE_OFF},
456 * {@link #STATE_TURNING_ON},
457 * {@link #STATE_ON},
458 * {@link #STATE_TURNING_OFF}.
459 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700460 *
Nick Pellyde893f52009-09-08 13:15:33 -0700461 * @return current state of Bluetooth adapter
Nick Pellybd022f42009-08-14 18:33:38 -0700462 */
Nick Pellyde893f52009-09-08 13:15:33 -0700463 public int getState() {
Nick Pellybd022f42009-08-14 18:33:38 -0700464 try {
fredc0f420372012-04-12 00:02:00 -0700465 synchronized(mManagerCallback) {
466 if (mService != null)
467 {
fredcbf072a72012-05-09 16:52:50 -0700468 int state= mService.getState();
Matthew Xie3b6214f2012-08-29 00:12:29 -0700469 if (VDBG) Log.d(TAG, "" + hashCode() + ": getState(). Returning " + state);
fredcbf072a72012-05-09 16:52:50 -0700470 return state;
fredc0f420372012-04-12 00:02:00 -0700471 }
472 // TODO(BT) there might be a small gap during STATE_TURNING_ON that
473 // mService is null, handle that case
474 }
Nick Pellybd022f42009-08-14 18:33:38 -0700475 } catch (RemoteException e) {Log.e(TAG, "", e);}
fredcbf072a72012-05-09 16:52:50 -0700476 if (DBG) Log.d(TAG, "" + hashCode() + ": getState() : mService = null. Returning STATE_OFF");
Nick Pellyde893f52009-09-08 13:15:33 -0700477 return STATE_OFF;
Nick Pellybd022f42009-08-14 18:33:38 -0700478 }
479
480 /**
Scott Mained2a70d2009-12-09 16:07:39 -0800481 * Turn on the local Bluetooth adapter&mdash;do not use without explicit
482 * user action to turn on Bluetooth.
Nick Pellyde893f52009-09-08 13:15:33 -0700483 * <p>This powers on the underlying Bluetooth hardware, and starts all
484 * Bluetooth system services.
Scott Mained2a70d2009-12-09 16:07:39 -0800485 * <p class="caution"><strong>Bluetooth should never be enabled without
486 * direct user consent</strong>. If you want to turn on Bluetooth in order
487 * to create a wireless connection, you should use the {@link
488 * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests
489 * user permission to turn on Bluetooth. The {@link #enable()} method is
490 * provided only for applications that include a user interface for changing
491 * system settings, such as a "power manager" app.</p>
Brad Fitzpatrick3219ab42009-09-25 16:31:39 +0400492 * <p>This is an asynchronous call: it will return immediately, and
Nick Pellyde893f52009-09-08 13:15:33 -0700493 * clients should listen for {@link #ACTION_STATE_CHANGED}
494 * to be notified of subsequent adapter state changes. If this call returns
495 * true, then the adapter state will immediately transition from {@link
496 * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time
497 * later transition to either {@link #STATE_OFF} or {@link
498 * #STATE_ON}. If this call returns false then there was an
499 * immediate problem that will prevent the adapter from being turned on -
500 * such as Airplane mode, or the adapter is already turned on.
Scott Mained2a70d2009-12-09 16:07:39 -0800501 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
502 * permission
Nick Pellyde893f52009-09-08 13:15:33 -0700503 *
504 * @return true to indicate adapter startup has begun, or false on
505 * immediate error
Nick Pellybd022f42009-08-14 18:33:38 -0700506 */
507 public boolean enable() {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700508 if (isEnabled() == true){
509 if (DBG) Log.d(TAG, "enable(): BT is already enabled..!");
510 return true;
511 }
Nick Pellybd022f42009-08-14 18:33:38 -0700512 try {
Kausik Sinnaswamya097f512012-04-16 16:38:27 +0530513 return mManagerService.enable();
Nick Pellybd022f42009-08-14 18:33:38 -0700514 } catch (RemoteException e) {Log.e(TAG, "", e);}
515 return false;
516 }
517
518 /**
Scott Mained2a70d2009-12-09 16:07:39 -0800519 * Turn off the local Bluetooth adapter&mdash;do not use without explicit
520 * user action to turn off Bluetooth.
Nick Pellyde893f52009-09-08 13:15:33 -0700521 * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth
522 * system services, and powers down the underlying Bluetooth hardware.
Jake Hambyf51eada2010-09-21 13:39:53 -0700523 * <p class="caution"><strong>Bluetooth should never be disabled without
Scott Mained2a70d2009-12-09 16:07:39 -0800524 * direct user consent</strong>. The {@link #disable()} method is
525 * provided only for applications that include a user interface for changing
526 * system settings, such as a "power manager" app.</p>
Brad Fitzpatrick3219ab42009-09-25 16:31:39 +0400527 * <p>This is an asynchronous call: it will return immediately, and
Nick Pellyde893f52009-09-08 13:15:33 -0700528 * clients should listen for {@link #ACTION_STATE_CHANGED}
529 * to be notified of subsequent adapter state changes. If this call returns
530 * true, then the adapter state will immediately transition from {@link
531 * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time
532 * later transition to either {@link #STATE_OFF} or {@link
533 * #STATE_ON}. If this call returns false then there was an
534 * immediate problem that will prevent the adapter from being turned off -
535 * such as the adapter already being turned off.
Scott Mained2a70d2009-12-09 16:07:39 -0800536 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
537 * permission
Nick Pellybd022f42009-08-14 18:33:38 -0700538 *
Nick Pellyde893f52009-09-08 13:15:33 -0700539 * @return true to indicate adapter shutdown has begun, or false on
540 * immediate error
Nick Pellybd022f42009-08-14 18:33:38 -0700541 */
542 public boolean disable() {
543 try {
fredc0f420372012-04-12 00:02:00 -0700544 return mManagerService.disable(true);
Jaikumar Ganeshe21a4ac2012-03-06 17:15:16 -0800545 } catch (RemoteException e) {Log.e(TAG, "", e);}
546 return false;
547 }
548
549 /**
550 * Turn off the local Bluetooth adapter and don't persist the setting.
551 *
552 * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
553 * permission
554 *
555 * @return true to indicate adapter shutdown has begun, or false on
556 * immediate error
557 * @hide
558 */
559 public boolean disable(boolean persist) {
fredc0f420372012-04-12 00:02:00 -0700560
Jaikumar Ganeshe21a4ac2012-03-06 17:15:16 -0800561 try {
fredc0f420372012-04-12 00:02:00 -0700562 return mManagerService.disable(persist);
Nick Pellybd022f42009-08-14 18:33:38 -0700563 } catch (RemoteException e) {Log.e(TAG, "", e);}
564 return false;
565 }
566
Nick Pellyde893f52009-09-08 13:15:33 -0700567 /**
568 * Returns the hardware address of the local Bluetooth adapter.
569 * <p>For example, "00:11:22:AA:BB:CC".
570 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
571 *
572 * @return Bluetooth hardware address as string
573 */
Nick Pellybd022f42009-08-14 18:33:38 -0700574 public String getAddress() {
575 try {
fredc0f420372012-04-12 00:02:00 -0700576 return mManagerService.getAddress();
Nick Pellybd022f42009-08-14 18:33:38 -0700577 } catch (RemoteException e) {Log.e(TAG, "", e);}
578 return null;
579 }
580
581 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700582 * Get the friendly Bluetooth name of the local Bluetooth adapter.
583 * <p>This name is visible to remote Bluetooth devices.
584 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
Nick Pellybd022f42009-08-14 18:33:38 -0700585 *
Nick Pellyde893f52009-09-08 13:15:33 -0700586 * @return the Bluetooth name, or null on error
Nick Pellybd022f42009-08-14 18:33:38 -0700587 */
588 public String getName() {
589 try {
fredc116d1d462012-04-20 14:47:08 -0700590 return mManagerService.getName();
Nick Pellybd022f42009-08-14 18:33:38 -0700591 } catch (RemoteException e) {Log.e(TAG, "", e);}
592 return null;
593 }
594
595 /**
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800596 * Get the UUIDs supported by the local Bluetooth adapter.
597 *
598 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
599 *
600 * @return the UUIDs supported by the local Bluetooth Adapter.
601 * @hide
602 */
603 public ParcelUuid[] getUuids() {
Matthew Xie44b58ab2011-11-16 12:27:57 -0800604 if (getState() != STATE_ON) return null;
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800605 try {
fredc0f420372012-04-12 00:02:00 -0700606 synchronized(mManagerCallback) {
607 if (mService != null) return mService.getUuids();
608 }
Jaikumar Ganesh58b93c32010-11-23 20:03:10 -0800609 } catch (RemoteException e) {Log.e(TAG, "", e);}
610 return null;
611 }
612
613 /**
Jake Hamby0f584302010-09-16 18:12:51 -0700614 * Set the friendly Bluetooth name of the local Bluetooth adapter.
Nick Pellyde893f52009-09-08 13:15:33 -0700615 * <p>This name is visible to remote Bluetooth devices.
Jake Hamby0f584302010-09-16 18:12:51 -0700616 * <p>Valid Bluetooth names are a maximum of 248 bytes using UTF-8
617 * encoding, although many remote devices can only display the first
618 * 40 characters, and some may be limited to just 20.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700619 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
620 * will return false. After turning on Bluetooth,
621 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
622 * to get the updated value.
Nick Pellyde893f52009-09-08 13:15:33 -0700623 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
Nick Pellybd022f42009-08-14 18:33:38 -0700624 *
Nick Pellyde893f52009-09-08 13:15:33 -0700625 * @param name a valid Bluetooth name
626 * @return true if the name was set, false otherwise
Nick Pellybd022f42009-08-14 18:33:38 -0700627 */
628 public boolean setName(String name) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700629 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700630 try {
fredc0f420372012-04-12 00:02:00 -0700631 synchronized(mManagerCallback) {
632 if (mService != null) return mService.setName(name);
633 }
Nick Pellybd022f42009-08-14 18:33:38 -0700634 } catch (RemoteException e) {Log.e(TAG, "", e);}
635 return false;
636 }
637
638 /**
Jake Hamby0f584302010-09-16 18:12:51 -0700639 * Get the current Bluetooth scan mode of the local Bluetooth adapter.
Nick Pellyde893f52009-09-08 13:15:33 -0700640 * <p>The Bluetooth scan mode determines if the local adapter is
641 * connectable and/or discoverable from remote Bluetooth devices.
642 * <p>Possible values are:
643 * {@link #SCAN_MODE_NONE},
644 * {@link #SCAN_MODE_CONNECTABLE},
645 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700646 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
647 * will return {@link #SCAN_MODE_NONE}. After turning on Bluetooth,
648 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
649 * to get the updated value.
Nick Pellyde893f52009-09-08 13:15:33 -0700650 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
651 *
652 * @return scan mode
Nick Pellybd022f42009-08-14 18:33:38 -0700653 */
654 public int getScanMode() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700655 if (getState() != STATE_ON) return SCAN_MODE_NONE;
Nick Pellybd022f42009-08-14 18:33:38 -0700656 try {
fredc0f420372012-04-12 00:02:00 -0700657 synchronized(mManagerCallback) {
658 if (mService != null) return mService.getScanMode();
659 }
Nick Pellybd022f42009-08-14 18:33:38 -0700660 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyde893f52009-09-08 13:15:33 -0700661 return SCAN_MODE_NONE;
Nick Pellybd022f42009-08-14 18:33:38 -0700662 }
663
664 /**
Nick Pellyde893f52009-09-08 13:15:33 -0700665 * Set the Bluetooth scan mode of the local Bluetooth adapter.
666 * <p>The Bluetooth scan mode determines if the local adapter is
667 * connectable and/or discoverable from remote Bluetooth devices.
Nick Pelly12835472009-09-25 15:00:29 -0700668 * <p>For privacy reasons, discoverable mode is automatically turned off
669 * after <code>duration</code> seconds. For example, 120 seconds should be
670 * enough for a remote device to initiate and complete its discovery
671 * process.
Nick Pellyde893f52009-09-08 13:15:33 -0700672 * <p>Valid scan mode values are:
673 * {@link #SCAN_MODE_NONE},
674 * {@link #SCAN_MODE_CONNECTABLE},
675 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700676 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
677 * will return false. After turning on Bluetooth,
678 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
679 * to get the updated value.
Nick Pelly18b1e792009-09-24 11:14:15 -0700680 * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS}
681 * <p>Applications cannot set the scan mode. They should use
682 * <code>startActivityForResult(
683 * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE})
684 * </code>instead.
Nick Pellyde893f52009-09-08 13:15:33 -0700685 *
686 * @param mode valid scan mode
Nick Pelly12835472009-09-25 15:00:29 -0700687 * @param duration time in seconds to apply scan mode, only used for
688 * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}
Nick Pellyde893f52009-09-08 13:15:33 -0700689 * @return true if the scan mode was set, false otherwise
Nick Pelly18b1e792009-09-24 11:14:15 -0700690 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700691 */
Nick Pelly12835472009-09-25 15:00:29 -0700692 public boolean setScanMode(int mode, int duration) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700693 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700694 try {
fredc0f420372012-04-12 00:02:00 -0700695 synchronized(mManagerCallback) {
696 if (mService != null) return mService.setScanMode(mode, duration);
697 }
Nick Pellybd022f42009-08-14 18:33:38 -0700698 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pellyde893f52009-09-08 13:15:33 -0700699 return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700700 }
701
Nick Pelly45e27042009-08-19 11:00:00 -0700702 /** @hide */
Nick Pelly12835472009-09-25 15:00:29 -0700703 public boolean setScanMode(int mode) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700704 if (getState() != STATE_ON) return false;
Srikanth Uppala827de2d2012-04-04 03:33:26 -0700705 /* getDiscoverableTimeout() to use the latest from NV than use 0 */
706 return setScanMode(mode, getDiscoverableTimeout());
Nick Pelly12835472009-09-25 15:00:29 -0700707 }
708
709 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700710 public int getDiscoverableTimeout() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700711 if (getState() != STATE_ON) return -1;
Nick Pellybd022f42009-08-14 18:33:38 -0700712 try {
fredc0f420372012-04-12 00:02:00 -0700713 synchronized(mManagerCallback) {
714 if (mService != null) return mService.getDiscoverableTimeout();
715 }
Nick Pellybd022f42009-08-14 18:33:38 -0700716 } catch (RemoteException e) {Log.e(TAG, "", e);}
717 return -1;
718 }
719
Nick Pelly45e27042009-08-19 11:00:00 -0700720 /** @hide */
Nick Pellybd022f42009-08-14 18:33:38 -0700721 public void setDiscoverableTimeout(int timeout) {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700722 if (getState() != STATE_ON) return;
Nick Pellybd022f42009-08-14 18:33:38 -0700723 try {
fredc0f420372012-04-12 00:02:00 -0700724 synchronized(mManagerCallback) {
725 if (mService != null) mService.setDiscoverableTimeout(timeout);
726 }
Nick Pellybd022f42009-08-14 18:33:38 -0700727 } catch (RemoteException e) {Log.e(TAG, "", e);}
728 }
729
Nick Pelly005b2282009-09-10 10:21:56 -0700730 /**
731 * Start the remote device discovery process.
732 * <p>The discovery process usually involves an inquiry scan of about 12
733 * seconds, followed by a page scan of each new device to retrieve its
734 * Bluetooth name.
735 * <p>This is an asynchronous call, it will return immediately. Register
736 * for {@link #ACTION_DISCOVERY_STARTED} and {@link
737 * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the
738 * discovery starts and completes. Register for {@link
739 * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices
740 * are found.
741 * <p>Device discovery is a heavyweight procedure. New connections to
742 * remote Bluetooth devices should not be attempted while discovery is in
743 * progress, and existing connections will experience limited bandwidth
744 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
Scott Main6d95fc02009-11-19 17:00:19 -0800745 * discovery. Discovery is not managed by the Activity,
746 * but is run as a system service, so an application should always call
747 * {@link BluetoothAdapter#cancelDiscovery()} even if it
748 * did not directly request a discovery, just to be sure.
Nick Pelly005b2282009-09-10 10:21:56 -0700749 * <p>Device discovery will only find remote devices that are currently
750 * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are
751 * not discoverable by default, and need to be entered into a special mode.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700752 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
753 * will return false. After turning on Bluetooth,
754 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
755 * to get the updated value.
Nick Pelly005b2282009-09-10 10:21:56 -0700756 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
757 *
758 * @return true on success, false on error
759 */
Nick Pellybd022f42009-08-14 18:33:38 -0700760 public boolean startDiscovery() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700761 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700762 try {
fredc0f420372012-04-12 00:02:00 -0700763 synchronized(mManagerCallback) {
764 if (mService != null) return mService.startDiscovery();
765 }
Nick Pellybd022f42009-08-14 18:33:38 -0700766 } catch (RemoteException e) {Log.e(TAG, "", e);}
767 return false;
768 }
769
Nick Pelly005b2282009-09-10 10:21:56 -0700770 /**
771 * Cancel the current device discovery process.
772 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
Jake Hamby0f584302010-09-16 18:12:51 -0700773 * <p>Because discovery is a heavyweight procedure for the Bluetooth
Scott Main6d95fc02009-11-19 17:00:19 -0800774 * adapter, this method should always be called before attempting to connect
775 * to a remote device with {@link
776 * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by
777 * the Activity, but is run as a system service, so an application should
778 * always call cancel discovery even if it did not directly request a
779 * discovery, just to be sure.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700780 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
781 * will return false. After turning on Bluetooth,
782 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
783 * to get the updated value.
Nick Pelly005b2282009-09-10 10:21:56 -0700784 *
785 * @return true on success, false on error
786 */
787 public boolean cancelDiscovery() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700788 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700789 try {
fredc0f420372012-04-12 00:02:00 -0700790 synchronized(mManagerCallback) {
791 if (mService != null) return mService.cancelDiscovery();
792 }
Nick Pellybd022f42009-08-14 18:33:38 -0700793 } catch (RemoteException e) {Log.e(TAG, "", e);}
Nick Pelly005b2282009-09-10 10:21:56 -0700794 return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700795 }
796
Nick Pelly005b2282009-09-10 10:21:56 -0700797 /**
798 * Return true if the local Bluetooth adapter is currently in the device
799 * discovery process.
800 * <p>Device discovery is a heavyweight procedure. New connections to
801 * remote Bluetooth devices should not be attempted while discovery is in
802 * progress, and existing connections will experience limited bandwidth
803 * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
804 * discovery.
805 * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED}
806 * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery
807 * starts or completes.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700808 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
809 * will return false. After turning on Bluetooth,
810 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
811 * to get the updated value.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200812 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Nick Pelly005b2282009-09-10 10:21:56 -0700813 *
814 * @return true if discovering
815 */
Nick Pellybd022f42009-08-14 18:33:38 -0700816 public boolean isDiscovering() {
Jaikumar Ganeshf5ff1702010-08-06 19:03:13 -0700817 if (getState() != STATE_ON) return false;
Nick Pellybd022f42009-08-14 18:33:38 -0700818 try {
fredc0f420372012-04-12 00:02:00 -0700819 synchronized(mManagerCallback) {
820 if (mService != null ) return mService.isDiscovering();
821 }
Nick Pellybd022f42009-08-14 18:33:38 -0700822 } catch (RemoteException e) {Log.e(TAG, "", e);}
823 return false;
824 }
825
826 /**
Nick Pelly005b2282009-09-10 10:21:56 -0700827 * Return the set of {@link BluetoothDevice} objects that are bonded
828 * (paired) to the local adapter.
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700829 * <p>If Bluetooth state is not {@link #STATE_ON}, this API
830 * will return an empty set. After turning on Bluetooth,
831 * wait for {@link #ACTION_STATE_CHANGED} with {@link #STATE_ON}
832 * to get the updated value.
Nick Pellye6ee3be2009-10-08 23:27:28 +0200833 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
Nick Pellybd022f42009-08-14 18:33:38 -0700834 *
Nick Pelly005b2282009-09-10 10:21:56 -0700835 * @return unmodifiable set of {@link BluetoothDevice}, or null on error
Nick Pellybd022f42009-08-14 18:33:38 -0700836 */
837 public Set<BluetoothDevice> getBondedDevices() {
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700838 if (getState() != STATE_ON) {
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800839 return toDeviceSet(new BluetoothDevice[0]);
Jaikumar Ganeshfec86f42010-08-09 16:54:03 -0700840 }
Nick Pellybd022f42009-08-14 18:33:38 -0700841 try {
fredc0f420372012-04-12 00:02:00 -0700842 synchronized(mManagerCallback) {
843 if (mService != null) return toDeviceSet(mService.getBondedDevices());
844 }
845 return toDeviceSet(new BluetoothDevice[0]);
Nick Pellybd022f42009-08-14 18:33:38 -0700846 } catch (RemoteException e) {Log.e(TAG, "", e);}
847 return null;
848 }
849
850 /**
Jaikumar Ganeshc53cab22010-10-26 16:02:26 -0700851 * Get the current connection state of the local Bluetooth adapter.
852 * This can be used to check whether the local Bluetooth adapter is connected
853 * to any profile of any other remote Bluetooth Device.
854 *
855 * <p> Use this function along with {@link #ACTION_CONNECTION_STATE_CHANGED}
856 * intent to get the connection state of the adapter.
857 *
858 * @return One of {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTED},
859 * {@link #STATE_CONNECTING} or {@link #STATE_DISCONNECTED}
860 *
861 * @hide
862 */
863 public int getConnectionState() {
864 if (getState() != STATE_ON) return BluetoothAdapter.STATE_DISCONNECTED;
865 try {
fredc0f420372012-04-12 00:02:00 -0700866 synchronized(mManagerCallback) {
867 if (mService != null) return mService.getAdapterConnectionState();
868 }
Jaikumar Ganeshc53cab22010-10-26 16:02:26 -0700869 } catch (RemoteException e) {Log.e(TAG, "getConnectionState:", e);}
870 return BluetoothAdapter.STATE_DISCONNECTED;
871 }
872
873 /**
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700874 * Get the current connection state of a profile.
875 * This function can be used to check whether the local Bluetooth adapter
876 * is connected to any remote device for a specific profile.
Scott Main2d68a6b2011-09-26 22:59:38 -0700877 * Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET},
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700878 * {@link BluetoothProfile#A2DP}.
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700879 *
880 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
881 *
882 * <p> Return value can be one of
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700883 * {@link BluetoothProfile#STATE_DISCONNECTED},
884 * {@link BluetoothProfile#STATE_CONNECTING},
885 * {@link BluetoothProfile#STATE_CONNECTED},
886 * {@link BluetoothProfile#STATE_DISCONNECTING}
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700887 */
888 public int getProfileConnectionState(int profile) {
889 if (getState() != STATE_ON) return BluetoothProfile.STATE_DISCONNECTED;
890 try {
fredc0f420372012-04-12 00:02:00 -0700891 synchronized(mManagerCallback) {
892 if (mService != null) return mService.getProfileConnectionState(profile);
893 }
Jaikumar Ganesh93547902011-08-23 12:21:55 -0700894 } catch (RemoteException e) {
895 Log.e(TAG, "getProfileConnectionState:", e);
896 }
Jaikumar Ganeshcb1d3542011-08-19 10:26:32 -0700897 return BluetoothProfile.STATE_DISCONNECTED;
898 }
899
900 /**
Nick Pelly45e27042009-08-19 11:00:00 -0700901 * Create a listening, secure RFCOMM Bluetooth socket.
902 * <p>A remote device connecting to this socket will be authenticated and
Nick Pellybd022f42009-08-14 18:33:38 -0700903 * communication on this socket will be encrypted.
Nick Pelly45e27042009-08-19 11:00:00 -0700904 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
Nick Pelly24bb9b82009-10-02 20:34:18 -0700905 * connections from a listening {@link BluetoothServerSocket}.
Nick Pelly45e27042009-08-19 11:00:00 -0700906 * <p>Valid RFCOMM channels are in range 1 to 30.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700907 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
Nick Pelly45e27042009-08-19 11:00:00 -0700908 * @param channel RFCOMM channel to listen on
909 * @return a listening RFCOMM BluetoothServerSocket
910 * @throws IOException on error, for example Bluetooth not available, or
911 * insufficient permissions, or channel in use.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700912 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -0700913 */
Nick Pelly45e27042009-08-19 11:00:00 -0700914 public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException {
Nick Pellybd022f42009-08-14 18:33:38 -0700915 BluetoothServerSocket socket = new BluetoothServerSocket(
Nick Pelly45e27042009-08-19 11:00:00 -0700916 BluetoothSocket.TYPE_RFCOMM, true, true, channel);
Nick Pelly24bb9b82009-10-02 20:34:18 -0700917 int errno = socket.mSocket.bindListen();
918 if (errno != 0) {
zzy3b147b72012-04-03 19:48:32 -0700919 //TODO(BT): Throw the same exception error code
920 // that the previous code was using.
921 //socket.mSocket.throwErrnoNative(errno);
922 throw new IOException("Error: " + errno);
Nick Pellybd022f42009-08-14 18:33:38 -0700923 }
924 return socket;
925 }
926
927 /**
Nick Pelly24bb9b82009-10-02 20:34:18 -0700928 * Create a listening, secure RFCOMM Bluetooth socket with Service Record.
929 * <p>A remote device connecting to this socket will be authenticated and
930 * communication on this socket will be encrypted.
931 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
932 * connections from a listening {@link BluetoothServerSocket}.
933 * <p>The system will assign an unused RFCOMM channel to listen on.
934 * <p>The system will also register a Service Discovery
935 * Protocol (SDP) record with the local SDP server containing the specified
936 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
937 * can use the same UUID to query our SDP server and discover which channel
938 * to connect to. This SDP record will be removed when this socket is
939 * closed, or if this application closes unexpectedly.
Nick Pelly16fb88a2009-10-07 07:44:03 +0200940 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
941 * connect to this socket from another device using the same {@link UUID}.
Nick Pelly24bb9b82009-10-02 20:34:18 -0700942 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
943 * @param name service name for SDP record
944 * @param uuid uuid for SDP record
945 * @return a listening RFCOMM BluetoothServerSocket
946 * @throws IOException on error, for example Bluetooth not available, or
947 * insufficient permissions, or channel in use.
948 */
Nick Pelly16fb88a2009-10-07 07:44:03 +0200949 public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid)
Nick Pelly24bb9b82009-10-02 20:34:18 -0700950 throws IOException {
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800951 return createNewRfcommSocketAndRecord(name, uuid, true, true);
952 }
953
954 /**
955 * Create a listening, insecure RFCOMM Bluetooth socket with Service Record.
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100956 * <p>The link key is not required to be authenticated, i.e the communication may be
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800957 * vulnerable to Man In the Middle attacks. For Bluetooth 2.1 devices,
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100958 * the link will be encrypted, as encryption is mandartory.
959 * For legacy devices (pre Bluetooth 2.1 devices) the link will not
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -0800960 * be encrypted. Use {@link #listenUsingRfcommWithServiceRecord}, if an
961 * encrypted and authenticated communication channel is desired.
962 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
963 * connections from a listening {@link BluetoothServerSocket}.
964 * <p>The system will assign an unused RFCOMM channel to listen on.
965 * <p>The system will also register a Service Discovery
966 * Protocol (SDP) record with the local SDP server containing the specified
967 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
968 * can use the same UUID to query our SDP server and discover which channel
969 * to connect to. This SDP record will be removed when this socket is
970 * closed, or if this application closes unexpectedly.
971 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
972 * connect to this socket from another device using the same {@link UUID}.
973 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
974 * @param name service name for SDP record
975 * @param uuid uuid for SDP record
976 * @return a listening RFCOMM BluetoothServerSocket
977 * @throws IOException on error, for example Bluetooth not available, or
978 * insufficient permissions, or channel in use.
979 */
980 public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String name, UUID uuid)
981 throws IOException {
982 return createNewRfcommSocketAndRecord(name, uuid, false, false);
983 }
984
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +0100985 /**
986 * Create a listening, encrypted,
987 * RFCOMM Bluetooth socket with Service Record.
988 * <p>The link will be encrypted, but the link key is not required to be authenticated
989 * i.e the communication is vulnerable to Man In the Middle attacks. Use
990 * {@link #listenUsingRfcommWithServiceRecord}, to ensure an authenticated link key.
991 * <p> Use this socket if authentication of link key is not possible.
992 * For example, for Bluetooth 2.1 devices, if any of the devices does not have
993 * an input and output capability or just has the ability to display a numeric key,
994 * a secure socket connection is not possible and this socket can be used.
995 * Use {@link #listenUsingInsecureRfcommWithServiceRecord}, if encryption is not required.
996 * For Bluetooth 2.1 devices, the link will be encrypted, as encryption is mandartory.
997 * For more details, refer to the Security Model section 5.2 (vol 3) of
998 * Bluetooth Core Specification version 2.1 + EDR.
999 * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
1000 * connections from a listening {@link BluetoothServerSocket}.
1001 * <p>The system will assign an unused RFCOMM channel to listen on.
1002 * <p>The system will also register a Service Discovery
1003 * Protocol (SDP) record with the local SDP server containing the specified
1004 * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
1005 * can use the same UUID to query our SDP server and discover which channel
1006 * to connect to. This SDP record will be removed when this socket is
1007 * closed, or if this application closes unexpectedly.
1008 * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
1009 * connect to this socket from another device using the same {@link UUID}.
1010 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1011 * @param name service name for SDP record
1012 * @param uuid uuid for SDP record
1013 * @return a listening RFCOMM BluetoothServerSocket
1014 * @throws IOException on error, for example Bluetooth not available, or
1015 * insufficient permissions, or channel in use.
1016 * @hide
1017 */
1018 public BluetoothServerSocket listenUsingEncryptedRfcommWithServiceRecord(
1019 String name, UUID uuid) throws IOException {
1020 return createNewRfcommSocketAndRecord(name, uuid, false, true);
1021 }
1022
zzy3b147b72012-04-03 19:48:32 -07001023
Jaikumar Ganesh6eef14a2010-12-23 12:57:02 -08001024 private BluetoothServerSocket createNewRfcommSocketAndRecord(String name, UUID uuid,
1025 boolean auth, boolean encrypt) throws IOException {
Nick Pelly24bb9b82009-10-02 20:34:18 -07001026 BluetoothServerSocket socket;
zzy3b147b72012-04-03 19:48:32 -07001027 socket = new BluetoothServerSocket(BluetoothSocket.TYPE_RFCOMM, auth,
1028 encrypt, new ParcelUuid(uuid));
1029 socket.setServiceName(name);
1030 int errno = socket.mSocket.bindListen();
1031 if (errno != 0) {
1032 //TODO(BT): Throw the same exception error code
1033 // that the previous code was using.
1034 //socket.mSocket.throwErrnoNative(errno);
1035 throw new IOException("Error: " + errno);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001036 }
Nick Pelly24bb9b82009-10-02 20:34:18 -07001037 return socket;
1038 }
1039
1040 /**
Nick Pellybd022f42009-08-14 18:33:38 -07001041 * Construct an unencrypted, unauthenticated, RFCOMM server socket.
1042 * Call #accept to retrieve connections to this socket.
1043 * @return An RFCOMM BluetoothServerSocket
1044 * @throws IOException On error, for example Bluetooth not available, or
1045 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001046 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001047 */
1048 public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
1049 BluetoothServerSocket socket = new BluetoothServerSocket(
1050 BluetoothSocket.TYPE_RFCOMM, false, false, port);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001051 int errno = socket.mSocket.bindListen();
1052 if (errno != 0) {
zzy3b147b72012-04-03 19:48:32 -07001053 //TODO(BT): Throw the same exception error code
1054 // that the previous code was using.
1055 //socket.mSocket.throwErrnoNative(errno);
1056 throw new IOException("Error: " + errno);
Nick Pellybd022f42009-08-14 18:33:38 -07001057 }
1058 return socket;
1059 }
1060
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +01001061 /**
1062 * Construct an encrypted, RFCOMM server socket.
1063 * Call #accept to retrieve connections to this socket.
1064 * @return An RFCOMM BluetoothServerSocket
1065 * @throws IOException On error, for example Bluetooth not available, or
1066 * insufficient permissions.
1067 * @hide
1068 */
1069 public BluetoothServerSocket listenUsingEncryptedRfcommOn(int port)
1070 throws IOException {
1071 BluetoothServerSocket socket = new BluetoothServerSocket(
1072 BluetoothSocket.TYPE_RFCOMM, false, true, port);
1073 int errno = socket.mSocket.bindListen();
zzy3b147b72012-04-03 19:48:32 -07001074 if (errno < 0) {
1075 //TODO(BT): Throw the same exception error code
1076 // that the previous code was using.
1077 //socket.mSocket.throwErrnoNative(errno);
1078 throw new IOException("Error: " + errno);
Mathias Jeppssone3b9dc102011-03-21 15:06:52 +01001079 }
1080 return socket;
1081 }
1082
Nick Pellybd022f42009-08-14 18:33:38 -07001083 /**
1084 * Construct a SCO server socket.
1085 * Call #accept to retrieve connections to this socket.
1086 * @return A SCO BluetoothServerSocket
1087 * @throws IOException On error, for example Bluetooth not available, or
1088 * insufficient permissions.
Nick Pelly45e27042009-08-19 11:00:00 -07001089 * @hide
Nick Pellybd022f42009-08-14 18:33:38 -07001090 */
1091 public static BluetoothServerSocket listenUsingScoOn() throws IOException {
1092 BluetoothServerSocket socket = new BluetoothServerSocket(
1093 BluetoothSocket.TYPE_SCO, false, false, -1);
Nick Pelly24bb9b82009-10-02 20:34:18 -07001094 int errno = socket.mSocket.bindListen();
zzy3b147b72012-04-03 19:48:32 -07001095 if (errno < 0) {
1096 //TODO(BT): Throw the same exception error code
1097 // that the previous code was using.
1098 //socket.mSocket.throwErrnoNative(errno);
Nick Pellybd022f42009-08-14 18:33:38 -07001099 }
1100 return socket;
1101 }
1102
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001103 /**
1104 * Read the local Out of Band Pairing Data
1105 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1106 *
1107 * @return Pair<byte[], byte[]> of Hash and Randomizer
1108 *
1109 * @hide
1110 */
1111 public Pair<byte[], byte[]> readOutOfBandData() {
1112 if (getState() != STATE_ON) return null;
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001113 //TODO(BT
1114 /*
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001115 try {
Jake Hambyf51eada2010-09-21 13:39:53 -07001116 byte[] hash;
1117 byte[] randomizer;
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001118
1119 byte[] ret = mService.readOutOfBandData();
1120
1121 if (ret == null || ret.length != 32) return null;
1122
1123 hash = Arrays.copyOfRange(ret, 0, 16);
1124 randomizer = Arrays.copyOfRange(ret, 16, 32);
1125
1126 if (DBG) {
1127 Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) +
1128 ":" + Arrays.toString(randomizer));
1129 }
1130 return new Pair<byte[], byte[]>(hash, randomizer);
1131
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -08001132 } catch (RemoteException e) {Log.e(TAG, "", e);}*/
Jaikumar Ganeshcc5494c2010-09-09 15:37:57 -07001133 return null;
1134 }
1135
Scott Main299ae672011-01-19 21:13:18 -08001136 /**
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001137 * Get the profile proxy object associated with the profile.
1138 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001139 * <p>Profile can be one of {@link BluetoothProfile#HEALTH}, {@link BluetoothProfile#HEADSET},
1140 * {@link BluetoothProfile#A2DP}, {@link BluetoothProfile#GATT},
1141 * or {@link BluetoothProfile#GATT_SERVER}. Clients must implements
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001142 * {@link BluetoothProfile.ServiceListener} to get notified of
1143 * the connection status and to get the proxy object.
1144 *
1145 * @param context Context of the application
1146 * @param listener The service Listener for connection callbacks.
Scott Main2d68a6b2011-09-26 22:59:38 -07001147 * @param profile The Bluetooth profile; either {@link BluetoothProfile#HEALTH},
1148 * {@link BluetoothProfile#HEADSET} or {@link BluetoothProfile#A2DP}.
Jaikumar Ganesh62c37ef2010-08-24 17:36:13 -07001149 * @return true on success, false on error
1150 */
1151 public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener,
1152 int profile) {
1153 if (context == null || listener == null) return false;
1154
1155 if (profile == BluetoothProfile.HEADSET) {
1156 BluetoothHeadset headset = new BluetoothHeadset(context, listener);
1157 return true;
1158 } else if (profile == BluetoothProfile.A2DP) {
1159 BluetoothA2dp a2dp = new BluetoothA2dp(context, listener);
1160 return true;
Jaikumar Ganesh4ab0e772011-02-18 14:52:32 -08001161 } else if (profile == BluetoothProfile.INPUT_DEVICE) {
1162 BluetoothInputDevice iDev = new BluetoothInputDevice(context, listener);
1163 return true;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -08001164 } else if (profile == BluetoothProfile.PAN) {
1165 BluetoothPan pan = new BluetoothPan(context, listener);
1166 return true;
Jaikumar Ganesh2ea1e852011-04-01 16:33:09 -07001167 } else if (profile == BluetoothProfile.HEALTH) {
1168 BluetoothHealth health = new BluetoothHealth(context, listener);
1169 return true;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001170 } else if (profile == BluetoothProfile.GATT) {
1171 BluetoothGatt gatt = new BluetoothGatt(context, listener);
1172 return true;
1173 } else if (profile == BluetoothProfile.GATT_SERVER) {
1174 BluetoothGattServer gattServer = new BluetoothGattServer(context, listener);
1175 return true;
Jaikumar 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 }
Nick Pellybd022f42009-08-14 18:33:38 -07001415}