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