1bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly/*
2bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Copyright (C) 2009 The Android Open Source Project
3bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly *
4bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
5bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * you may not use this file except in compliance with the License.
6bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * You may obtain a copy of the License at
7bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly *
8bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
9bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly *
10bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * Unless required by applicable law or agreed to in writing, software
11bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
12bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * See the License for the specific language governing permissions and
14bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly * limitations under the License.
15bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */
16bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
17bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellypackage android.bluetooth;
18bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
19de893f550301a60274e87aa8168225e7a7a42184Nick Pellyimport android.annotation.SdkConstant;
20de893f550301a60274e87aa8168225e7a7a42184Nick Pellyimport android.annotation.SdkConstant.SdkConstantType;
2124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport android.os.Binder;
2224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport android.os.Handler;
23f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pellyimport android.os.IBinder;
2424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport android.os.Message;
25aef439e6f825c0cb99a2ac08c8207f48b7a9fe10Nick Pellyimport android.os.ParcelUuid;
26bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.os.RemoteException;
27f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pellyimport android.os.ServiceManager;
28bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport android.util.Log;
29bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
30bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport java.io.IOException;
31bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport java.util.Collections;
32bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellyimport java.util.HashSet;
3324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport java.util.LinkedList;
3424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport java.util.Random;
3524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pellyimport java.util.Set;
3616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pellyimport java.util.UUID;
37bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
38bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly/**
399fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * Represents the local device Bluetooth adapter. The {@link BluetoothAdapter}
409fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * lets you perform fundamental Bluetooth tasks, such as initiate
419fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * device discovery, query a list of bonded (paired) devices,
429fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * instantiate a {@link BluetoothDevice} using a known MAC address, and create
439fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * a {@link BluetoothServerSocket} to listen for connection requests from other
449fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * devices.
45bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly *
469fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * <p>To get a {@link BluetoothAdapter} representing the local Bluetooth
479fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * adapter, call the static {@link #getDefaultAdapter} method.
489fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * Fundamentally, this is your starting point for all
499fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * Bluetooth actions. Once you have the local adapter, you can get a set of
509fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@link BluetoothDevice} objects representing all paired devices with
519fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@link #getBondedDevices()}; start device discovery with
529fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@link #startDiscovery()}; or create a {@link BluetoothServerSocket} to
539fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * listen for incoming connection requests with
549fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@link #listenUsingRfcommWithServiceRecord(String,UUID)}.
5545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly *
569fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * <p class="note"><strong>Note:</strong>
579fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * Most methods require the {@link android.Manifest.permission#BLUETOOTH}
589fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * permission and some also require the
599fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
609fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main *
619fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@see BluetoothDevice}
629fab0aef19a4633d2e4670564e5d7ae9e52fe11fScott Main * {@see BluetoothServerSocket}
63bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly */
64bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pellypublic final class BluetoothAdapter {
65bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    private static final String TAG = "BluetoothAdapter";
66af21fa61c2158fa2a693d98bb281abd56e658b40Jaikumar Ganesh    private static final boolean DBG = false;
67bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
68de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
69b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly     * Sentinel error value for this class. Guaranteed to not equal any other
70b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly     * integer constant in this class. Provided as a convenience for functions
71b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly     * that require a sentinel error value, for example:
72b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly     * <p><code>Intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
73b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly     * BluetoothAdapter.ERROR)</code>
74b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly     */
75005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int ERROR = Integer.MIN_VALUE;
76b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly
77b24e11baac589fe16426f2d243b460ab84991c7bNick Pelly    /**
78de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Broadcast Action: The state of the local Bluetooth adapter has been
79de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * changed.
80de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>For example, Bluetooth has been turned on or off.
81005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Always contains the extra fields {@link #EXTRA_STATE} and {@link
82de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * #EXTRA_PREVIOUS_STATE} containing the new and old states
83de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * respectively.
84de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
85de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
86de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
87de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    public static final String ACTION_STATE_CHANGED =
88005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.action.STATE_CHANGED";
89bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
90de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
91de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
92de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * intents to request the current power state. Possible values are:
93de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_OFF},
94de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_TURNING_ON},
95de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_ON},
96de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_TURNING_OFF},
97de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
98de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    public static final String EXTRA_STATE =
99005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.extra.STATE";
100de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
101de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Used as an int extra field in {@link #ACTION_STATE_CHANGED}
102de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * intents to request the previous power state. Possible values are:
103de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_OFF},
104de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_TURNING_ON},
105de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_ON},
106de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_TURNING_OFF},
107de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
108de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    public static final String EXTRA_PREVIOUS_STATE =
109005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.extra.PREVIOUS_STATE";
110de893f550301a60274e87aa8168225e7a7a42184Nick Pelly
111de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
112de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates the local Bluetooth adapter is off.
113de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
114005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int STATE_OFF = 10;
115de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
116de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates the local Bluetooth adapter is turning on. However local
117de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * clients should wait for {@link #STATE_ON} before attempting to
118de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * use the adapter.
119de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
120005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int STATE_TURNING_ON = 11;
121de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
122de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates the local Bluetooth adapter is on, and ready for use.
123de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
124005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int STATE_ON = 12;
125de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
126de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates the local Bluetooth adapter is turning off. Local clients
127de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * should immediately attempt graceful disconnection of any remote links.
128de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
129005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int STATE_TURNING_OFF = 13;
130de893f550301a60274e87aa8168225e7a7a42184Nick Pelly
131de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
13218b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * Activity Action: Show a system activity that requests discoverable mode.
1336d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * This activity will also request the user to turn on Bluetooth if it
1341acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * is not currently enabled.
13518b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <p>Discoverable mode is equivalent to {@link
13618b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * #SCAN_MODE_CONNECTABLE_DISCOVERABLE}. It allows remote devices to see
13718b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * this Bluetooth adapter when they perform a discovery.
1386d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * <p>For privacy, Android is not discoverable by default.
1396d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * <p>The sender of this Intent can optionally use extra field {@link
14018b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * #EXTRA_DISCOVERABLE_DURATION} to request the duration of
14118b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * discoverability. Currently the default duration is 120 seconds, and
14218b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * maximum duration is capped at 300 seconds for each request.
14318b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <p>Notification of the result of this activity is posted using the
14418b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * {@link android.app.Activity#onActivityResult} callback. The
14518b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <code>resultCode</code>
146cdd2864eb340fe46a97f4991f3949db1196bdde8Michael Chan     * will be the duration (in seconds) of discoverability or
147cdd2864eb340fe46a97f4991f3949db1196bdde8Michael Chan     * {@link android.app.Activity#RESULT_CANCELED} if the user rejected
148cdd2864eb340fe46a97f4991f3949db1196bdde8Michael Chan     * discoverability or an error has occurred.
14918b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <p>Applications can also listen for {@link #ACTION_SCAN_MODE_CHANGED}
1506d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * for global notification whenever the scan mode changes. For example, an
1516d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * application can be notified when the device has ended discoverability.
1521acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
15318b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     */
15418b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
15518b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly    public static final String ACTION_REQUEST_DISCOVERABLE =
15618b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly            "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE";
15718b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly
15818b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly    /**
15918b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * Used as an optional int extra field in {@link
16018b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * #ACTION_REQUEST_DISCOVERABLE} intents to request a specific duration
16118b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * for discoverability in seconds. The current default is 120 seconds, and
16218b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * requests over 300 seconds will be capped. These values could change.
16318b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     */
16418b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly    public static final String EXTRA_DISCOVERABLE_DURATION =
16518b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly            "android.bluetooth.adapter.extra.DISCOVERABLE_DURATION";
16618b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly
16718b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly    /**
1681acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * Activity Action: Show a system activity that allows the user to turn on
1691acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * Bluetooth.
1701acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * <p>This system activity will return once Bluetooth has completed turning
1711acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * on, or the user has decided not to turn Bluetooth on.
1721acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * <p>Notification of the result of this activity is posted using the
1731acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * {@link android.app.Activity#onActivityResult} callback. The
1741acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * <code>resultCode</code>
175cdd2864eb340fe46a97f4991f3949db1196bdde8Michael Chan     * will be {@link android.app.Activity#RESULT_OK} if Bluetooth has been
176cdd2864eb340fe46a97f4991f3949db1196bdde8Michael Chan     * turned on or {@link android.app.Activity#RESULT_CANCELED} if the user
177cdd2864eb340fe46a97f4991f3949db1196bdde8Michael Chan     * has rejected the request or an error has occurred.
1781acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * <p>Applications can also listen for {@link #ACTION_STATE_CHANGED}
1791acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * for global notification whenever Bluetooth is turned on or off.
1801acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
1811acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly     */
1821acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
1831acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly    public static final String ACTION_REQUEST_ENABLE =
1841acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly            "android.bluetooth.adapter.action.REQUEST_ENABLE";
1851acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly
1861acdcc1a508c00dc31751a2a2eef539a59cb21ceNick Pelly    /**
187de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Broadcast Action: Indicates the Bluetooth scan mode of the local Adapter
188de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * has changed.
189005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Always contains the extra fields {@link #EXTRA_SCAN_MODE} and {@link
190de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * #EXTRA_PREVIOUS_SCAN_MODE} containing the new and old scan modes
191de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * respectively.
192de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
193de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
194de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
195de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    public static final String ACTION_SCAN_MODE_CHANGED =
196005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.action.SCAN_MODE_CHANGED";
197de893f550301a60274e87aa8168225e7a7a42184Nick Pelly
198de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
199de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
200de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * intents to request the current scan mode. Possible values are:
201de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_NONE},
202de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE},
203de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
204de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
205005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final String EXTRA_SCAN_MODE = "android.bluetooth.adapter.extra.SCAN_MODE";
206de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
207de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Used as an int extra field in {@link #ACTION_SCAN_MODE_CHANGED}
208de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * intents to request the previous scan mode. Possible values are:
209de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_NONE},
210de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE},
211de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE},
212de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
213de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    public static final String EXTRA_PREVIOUS_SCAN_MODE =
214005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE";
215de893f550301a60274e87aa8168225e7a7a42184Nick Pelly
216de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
217de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates that both inquiry scan and page scan are disabled on the local
218de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Bluetooth adapter. Therefore this device is neither discoverable
219de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * nor connectable from remote Bluetooth devices.
220de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
221005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int SCAN_MODE_NONE = 20;
222de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
223de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates that inquiry scan is disabled, but page scan is enabled on the
224de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * local Bluetooth adapter. Therefore this device is not discoverable from
225de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * remote Bluetooth devices, but is connectable from remote devices that
226de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * have previously discovered this device.
227de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
228005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int SCAN_MODE_CONNECTABLE = 21;
229de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
230de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Indicates that both inquiry scan and page scan are enabled on the local
231de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Bluetooth adapter. Therefore this device is both discoverable and
232de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * connectable from remote Bluetooth devices.
233de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
234005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 23;
235005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly
236005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly
237005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
238005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Broadcast Action: The local Bluetooth adapter has started the remote
239005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * device discovery process.
240005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>This usually involves an inquiry scan of about 12 seconds, followed
241005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * by a page scan of each new device to retrieve its Bluetooth name.
242005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Register for {@link BluetoothDevice#ACTION_FOUND} to be notified as
243005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * remote Bluetooth devices are found.
244005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Device discovery is a heavyweight procedure. New connections to
245005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * remote Bluetooth devices should not be attempted while discovery is in
246005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * progress, and existing connections will experience limited bandwidth
247005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
248005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * discovery.
249005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
250005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
251005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
252005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final String ACTION_DISCOVERY_STARTED =
253005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.action.DISCOVERY_STARTED";
254005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
255005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Broadcast Action: The local Bluetooth adapter has finished the device
256005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * discovery process.
257005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
258005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
259005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
260005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final String ACTION_DISCOVERY_FINISHED =
261005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.action.DISCOVERY_FINISHED";
262005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly
263005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
264005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Broadcast Action: The local Bluetooth adapter has changed its friendly
265005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Bluetooth name.
266005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>This name is visible to remote Bluetooth devices.
267005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Always contains the extra field {@link #EXTRA_LOCAL_NAME} containing
268005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * the name.
269005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
270005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
271005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
272005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final String ACTION_LOCAL_NAME_CHANGED =
273005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED";
274005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
275005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Used as a String extra field in {@link #ACTION_LOCAL_NAME_CHANGED}
276005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * intents to request the local Bluetooth name.
277005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
278005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME";
279bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
280f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    /** @hide */
281f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    public static final String BLUETOOTH_SERVICE = "bluetooth";
282f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly
283005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    private static final int ADDRESS_LENGTH = 17;
284bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
285f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    /**
286f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * Lazyily initialized singleton. Guaranteed final after first object
287f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * constructed.
288f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     */
289f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    private static BluetoothAdapter sAdapter;
290f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly
291bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    private final IBluetooth mService;
292bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
293bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
294f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * Get a handle to the default local Bluetooth adapter.
295f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * <p>Currently Android only supports one Bluetooth adapter, but the API
296f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * could be extended to support more. This will always return the default
297f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * adapter.
298f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * @return the default local adapter, or null if Bluetooth is not supported
299f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     *         on this hardware platform
300f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     */
301f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    public static synchronized BluetoothAdapter getDefaultAdapter() {
302f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly        if (sAdapter == null) {
303f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly            IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE);
304f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly            if (b != null) {
305f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly                IBluetooth service = IBluetooth.Stub.asInterface(b);
306f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly                sAdapter = new BluetoothAdapter(service);
307f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly            }
308f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly        }
309f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly        return sAdapter;
310f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    }
311f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly
312f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly    /**
313f242b7b931898856bcbcb7ec36cacf43098ba544Nick Pelly     * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.
314bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * @hide
315bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
316bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public BluetoothAdapter(IBluetooth service) {
317bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        if (service == null) {
318bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            throw new IllegalArgumentException("service is null");
319bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        }
320bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        mService = service;
321bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
322bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
323bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
32445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * Get a {@link BluetoothDevice} object for the given Bluetooth hardware
32545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * address.
32645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Valid Bluetooth hardware addresses must be upper case, in a format
327005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * such as "00:11:22:33:AA:BB". The helper {@link #checkBluetoothAddress} is
328005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * available to validate a Bluetooth address.
32945e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>A {@link BluetoothDevice} will always be returned for a valid
33045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * hardware address, even if this adapter has never seen that device.
331de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *
332bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * @param address valid Bluetooth MAC address
33345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @throws IllegalArgumentException if address is invalid
334bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
335bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public BluetoothDevice getRemoteDevice(String address) {
336bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return new BluetoothDevice(address);
337bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
338bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
339bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
340de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Return true if Bluetooth is currently enabled and ready for use.
341de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Equivalent to:
342de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <code>getBluetoothState() == STATE_ON</code>
343de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
344bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *
345de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return true if the local adapter is turned on
346bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
347bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public boolean isEnabled() {
348bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
349bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.isEnabled();
350bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
351bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return false;
352bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
353bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
354bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
355de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Get the current state of the local Bluetooth adapter.
356de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Possible return values are
357de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_OFF},
358de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_TURNING_ON},
359de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_ON},
360de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #STATE_TURNING_OFF}.
361de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
362bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *
363de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return current state of Bluetooth adapter
364bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
365de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    public int getState() {
366bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
367bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.getBluetoothState();
368bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
369de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        return STATE_OFF;
370bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
371bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
372bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
373ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * Turn on the local Bluetooth adapter&mdash;do not use without explicit
374ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * user action to turn on Bluetooth.
375de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>This powers on the underlying Bluetooth hardware, and starts all
376de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Bluetooth system services.
377ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * <p class="caution"><strong>Bluetooth should never be enabled without
378ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * direct user consent</strong>. If you want to turn on Bluetooth in order
379ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * to create a wireless connection, you should use the {@link
380ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * #ACTION_REQUEST_ENABLE} Intent, which will raise a dialog that requests
381ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * user permission to turn on Bluetooth. The {@link #enable()} method is
382ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * provided only for applications that include a user interface for changing
383ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * system settings, such as a "power manager" app.</p>
3843219ab4de27cd40b2149b0dd13edd412efe40244Brad Fitzpatrick     * <p>This is an asynchronous call: it will return immediately, and
385de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * clients should listen for {@link #ACTION_STATE_CHANGED}
386de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * to be notified of subsequent adapter state changes. If this call returns
387de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * true, then the adapter state will immediately transition from {@link
388de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * #STATE_OFF} to {@link #STATE_TURNING_ON}, and some time
389de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * later transition to either {@link #STATE_OFF} or {@link
390de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * #STATE_ON}. If this call returns false then there was an
391de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * immediate problem that will prevent the adapter from being turned on -
392de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * such as Airplane mode, or the adapter is already turned on.
393ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
394ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * permission
395de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *
396de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return true to indicate adapter startup has begun, or false on
397de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *         immediate error
398bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
399bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public boolean enable() {
400bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
401bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.enable();
402bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
403bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return false;
404bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
405bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
406bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
407ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * Turn off the local Bluetooth adapter&mdash;do not use without explicit
408ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * user action to turn off Bluetooth.
409de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>This gracefully shuts down all Bluetooth connections, stops Bluetooth
410de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * system services, and powers down the underlying Bluetooth hardware.
411ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * <p class="caution"><strong>Bluetooth should never be disbled without
412ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * direct user consent</strong>. The {@link #disable()} method is
413ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * provided only for applications that include a user interface for changing
414ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * system settings, such as a "power manager" app.</p>
4153219ab4de27cd40b2149b0dd13edd412efe40244Brad Fitzpatrick     * <p>This is an asynchronous call: it will return immediately, and
416de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * clients should listen for {@link #ACTION_STATE_CHANGED}
417de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * to be notified of subsequent adapter state changes. If this call returns
418de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * true, then the adapter state will immediately transition from {@link
419de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * #STATE_ON} to {@link #STATE_TURNING_OFF}, and some time
420de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * later transition to either {@link #STATE_OFF} or {@link
421de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * #STATE_ON}. If this call returns false then there was an
422de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * immediate problem that will prevent the adapter from being turned off -
423de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * such as the adapter already being turned off.
424ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
425ed2a70d6495b3f1928e36ad2b00ee4d33b2c0379Scott Main     * permission
426bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *
427de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return true to indicate adapter shutdown has begun, or false on
428de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *         immediate error
429bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
430bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public boolean disable() {
431bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
432bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.disable(true);
433bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
434bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return false;
435bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
436bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
437de893f550301a60274e87aa8168225e7a7a42184Nick Pelly    /**
438de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Returns the hardware address of the local Bluetooth adapter.
439de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>For example, "00:11:22:AA:BB:CC".
440de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
441de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *
442de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return Bluetooth hardware address as string
443de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     */
444bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public String getAddress() {
445bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
446bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.getAddress();
447bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
448bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return null;
449bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
450bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
451bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
452de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Get the friendly Bluetooth name of the local Bluetooth adapter.
453de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>This name is visible to remote Bluetooth devices.
454de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
455bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *
456de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return the Bluetooth name, or null on error
457bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
458bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public String getName() {
459bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
460bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.getName();
461bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
462bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return null;
463bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
464bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
465bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
466de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Set the friendly Bluetooth name of the local Bluetoth adapter.
467de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>This name is visible to remote Bluetooth devices.
468de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Valid Bluetooth names are a maximum of 248 UTF-8 characters, however
469de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * many remote devices can only display the first 40 characters, and some
470de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * may be limited to just 20.
471de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
472bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *
473de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @param name a valid Bluetooth name
474de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return     true if the name was set, false otherwise
475bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
476bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public boolean setName(String name) {
477bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
478bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.setName(name);
479bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
480bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return false;
481bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
482bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
483bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
484de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Get the current Bluetooth scan mode of the local Bluetooth adaper.
485de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>The Bluetooth scan mode determines if the local adapter is
486de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * connectable and/or discoverable from remote Bluetooth devices.
487de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Possible values are:
488de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_NONE},
489de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE},
490de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
491de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
492de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *
493de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return scan mode
494bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
495bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public int getScanMode() {
496bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
497bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.getScanMode();
498bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
499de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        return SCAN_MODE_NONE;
500bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
501bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
502bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
503de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * Set the Bluetooth scan mode of the local Bluetooth adapter.
504de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>The Bluetooth scan mode determines if the local adapter is
505de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * connectable and/or discoverable from remote Bluetooth devices.
50612835478ee687a493d1b5882e67b6725bd539c26Nick Pelly     * <p>For privacy reasons, discoverable mode is automatically turned off
50712835478ee687a493d1b5882e67b6725bd539c26Nick Pelly     * after <code>duration</code> seconds. For example, 120 seconds should be
50812835478ee687a493d1b5882e67b6725bd539c26Nick Pelly     * enough for a remote device to initiate and complete its discovery
50912835478ee687a493d1b5882e67b6725bd539c26Nick Pelly     * process.
510de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * <p>Valid scan mode values are:
511de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_NONE},
512de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE},
513de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}.
51418b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <p>Requires {@link android.Manifest.permission#WRITE_SECURE_SETTINGS}
51518b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <p>Applications cannot set the scan mode. They should use
51618b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * <code>startActivityForResult(
51718b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE})
51818b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * </code>instead.
519de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     *
520de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @param mode valid scan mode
52112835478ee687a493d1b5882e67b6725bd539c26Nick Pelly     * @param duration time in seconds to apply scan mode, only used for
52212835478ee687a493d1b5882e67b6725bd539c26Nick Pelly     *                 {@link #SCAN_MODE_CONNECTABLE_DISCOVERABLE}
523de893f550301a60274e87aa8168225e7a7a42184Nick Pelly     * @return     true if the scan mode was set, false otherwise
52418b1e79a123b979d25bfa5d0b0ee5d0382dbd64bNick Pelly     * @hide
525bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
52612835478ee687a493d1b5882e67b6725bd539c26Nick Pelly    public boolean setScanMode(int mode, int duration) {
527bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
52812835478ee687a493d1b5882e67b6725bd539c26Nick Pelly            return mService.setScanMode(mode, duration);
529bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
530de893f550301a60274e87aa8168225e7a7a42184Nick Pelly        return false;
531bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
532bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
53345e2704ff512d41e22af2801d76e96955469ce8dNick Pelly    /** @hide */
53412835478ee687a493d1b5882e67b6725bd539c26Nick Pelly    public boolean setScanMode(int mode) {
53512835478ee687a493d1b5882e67b6725bd539c26Nick Pelly        return setScanMode(mode, 120);
53612835478ee687a493d1b5882e67b6725bd539c26Nick Pelly    }
53712835478ee687a493d1b5882e67b6725bd539c26Nick Pelly
53812835478ee687a493d1b5882e67b6725bd539c26Nick Pelly    /** @hide */
539bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public int getDiscoverableTimeout() {
540bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
541bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.getDiscoverableTimeout();
542bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
543bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return -1;
544bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
545bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
54645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly    /** @hide */
547bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public void setDiscoverableTimeout(int timeout) {
548bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
549bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            mService.setDiscoverableTimeout(timeout);
550bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
551bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
552bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
553005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
554005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Start the remote device discovery process.
555005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>The discovery process usually involves an inquiry scan of about 12
556005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * seconds, followed by a page scan of each new device to retrieve its
557005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Bluetooth name.
558005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>This is an asynchronous call, it will return immediately. Register
559005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * for {@link #ACTION_DISCOVERY_STARTED} and {@link
560005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * #ACTION_DISCOVERY_FINISHED} intents to determine exactly when the
561005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * discovery starts and completes. Register for {@link
562005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * BluetoothDevice#ACTION_FOUND} to be notified as remote Bluetooth devices
563005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * are found.
564005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Device discovery is a heavyweight procedure. New connections to
565005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * remote Bluetooth devices should not be attempted while discovery is in
566005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * progress, and existing connections will experience limited bandwidth
567005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
5686d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * discovery. Discovery is not managed by the Activity,
5696d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * but is run as a system service, so an application should always call
5706d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * {@link BluetoothAdapter#cancelDiscovery()} even if it
5716d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * did not directly request a discovery, just to be sure.
572005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Device discovery will only find remote devices that are currently
573005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <i>discoverable</i> (inquiry scan enabled). Many Bluetooth devices are
574005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * not discoverable by default, and need to be entered into a special mode.
575005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
576005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     *
577005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * @return true on success, false on error
578005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
579bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public boolean startDiscovery() {
580bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
581bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.startDiscovery();
582bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
583bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return false;
584bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
585bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
586005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
587005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Cancel the current device discovery process.
588005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
5896d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * <p>Because discovery is a heavyweight precedure for the Bluetooth
5906d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * adapter, this method should always be called before attempting to connect
5916d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * to a remote device with {@link
5926d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * android.bluetooth.BluetoothSocket#connect()}. Discovery is not managed by
5936d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * the  Activity, but is run as a system service, so an application should
5946d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * always call cancel discovery even if it did not directly request a
5956d95fc0a2ca910212a43c4547c0ef000659b72dcScott Main     * discovery, just to be sure.
596005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     *
597005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * @return true on success, false on error
598005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
599005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public boolean cancelDiscovery() {
600bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
601bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            mService.cancelDiscovery();
602bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
603005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        return false;
604bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
605bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
606005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
607005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Return true if the local Bluetooth adapter is currently in the device
608005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * discovery process.
609005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Device discovery is a heavyweight procedure. New connections to
610005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * remote Bluetooth devices should not be attempted while discovery is in
611005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * progress, and existing connections will experience limited bandwidth
612005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * and high latency. Use {@link #cancelDiscovery()} to cancel an ongoing
613005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * discovery.
614005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * <p>Applications can also register for {@link #ACTION_DISCOVERY_STARTED}
615005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * or {@link #ACTION_DISCOVERY_FINISHED} to be notified when discovery
616005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * starts or completes.
617e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
618005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     *
619005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * @return true if discovering
620005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
621bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public boolean isDiscovering() {
622bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
623bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return mService.isDiscovering();
624bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
625bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return false;
626bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
627bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
628bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
629005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Return the set of {@link BluetoothDevice} objects that are bonded
630005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * (paired) to the local adapter.
631e6ee3be1c254404dad842298f6f56c11cc6c7ac8Nick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
632bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *
633005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * @return unmodifiable set of {@link BluetoothDevice}, or null on error
634bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
635bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public Set<BluetoothDevice> getBondedDevices() {
636bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
637bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            return toDeviceSet(mService.listBonds());
638bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
639bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return null;
640bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
641bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
642bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
64316fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * Picks RFCOMM channels until none are left.
64424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * Avoids reserved channels.
64524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     */
64624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private static class RfcommChannelPicker {
64724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        private static final int[] RESERVED_RFCOMM_CHANNELS =  new int[] {
64824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            10,  // HFAG
64924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            11,  // HSAG
65024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            12,  // OPUSH
65124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            19,  // PBAP
65224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        };
65324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        private static LinkedList<Integer> sChannels;  // master list of non-reserved channels
65424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        private static Random sRandom;
65524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
65624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        private final LinkedList<Integer> mChannels;  // local list of channels left to try
65724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
65816fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        private final UUID mUuid;
65916fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly
66016fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        public RfcommChannelPicker(UUID uuid) {
66124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            synchronized (RfcommChannelPicker.class) {
66224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                if (sChannels == null) {
66324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    // lazy initialization of non-reserved rfcomm channels
66424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    sChannels = new LinkedList<Integer>();
66524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    for (int i = 1; i <= BluetoothSocket.MAX_RFCOMM_CHANNEL; i++) {
66624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                        sChannels.addLast(new Integer(i));
66724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    }
66824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    for (int reserved : RESERVED_RFCOMM_CHANNELS) {
66924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                        sChannels.remove(new Integer(reserved));
67024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    }
67124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    sRandom = new Random();
67224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                }
67324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                mChannels = (LinkedList<Integer>)sChannels.clone();
67424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            }
67516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            mUuid = uuid;
67624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
6774b3db907f04d85cd20e0a0e35056ba44a10e8867Jaikumar Ganesh        /* Returns next random channel, or -1 if we're out */
67824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        public int nextChannel() {
6794b3db907f04d85cd20e0a0e35056ba44a10e8867Jaikumar Ganesh            if (mChannels.size() == 0) {
6804b3db907f04d85cd20e0a0e35056ba44a10e8867Jaikumar Ganesh                return -1;
68124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            }
6824b3db907f04d85cd20e0a0e35056ba44a10e8867Jaikumar Ganesh            return mChannels.remove(sRandom.nextInt(mChannels.size()));
68324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
68424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    }
68524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
68624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    /**
68745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * Create a listening, secure RFCOMM Bluetooth socket.
68845e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>A remote device connecting to this socket will be authenticated and
689bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * communication on this socket will be encrypted.
69045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
69124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * connections from a listening {@link BluetoothServerSocket}.
69245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * <p>Valid RFCOMM channels are in range 1 to 30.
69324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
69445e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @param channel RFCOMM channel to listen on
69545e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @return a listening RFCOMM BluetoothServerSocket
69645e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @throws IOException on error, for example Bluetooth not available, or
69745e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     *                     insufficient permissions, or channel in use.
69824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * @hide
699bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
70045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly    public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException {
701bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        BluetoothServerSocket socket = new BluetoothServerSocket(
70245e2704ff512d41e22af2801d76e96955469ce8dNick Pelly                BluetoothSocket.TYPE_RFCOMM, true, true, channel);
70324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int errno = socket.mSocket.bindListen();
70424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (errno != 0) {
70524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            try {
70624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                socket.close();
70724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } catch (IOException e) {}
70824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            socket.mSocket.throwErrnoNative(errno);
70924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
71024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        return socket;
71124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    }
71224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
71324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    /**
71424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * Create a listening, secure RFCOMM Bluetooth socket with Service Record.
71524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * <p>A remote device connecting to this socket will be authenticated and
71624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * communication on this socket will be encrypted.
71724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
71824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * connections from a listening {@link BluetoothServerSocket}.
71924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * <p>The system will assign an unused RFCOMM channel to listen on.
72024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * <p>The system will also register a Service Discovery
72124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * Protocol (SDP) record with the local SDP server containing the specified
72224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * UUID, service name, and auto-assigned channel. Remote Bluetooth devices
72324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * can use the same UUID to query our SDP server and discover which channel
72424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * to connect to. This SDP record will be removed when this socket is
72524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * closed, or if this application closes unexpectedly.
72616fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
72716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly     * connect to this socket from another device using the same {@link UUID}.
72824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
72924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * @param name service name for SDP record
73024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * @param uuid uuid for SDP record
73124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * @return a listening RFCOMM BluetoothServerSocket
73224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     * @throws IOException on error, for example Bluetooth not available, or
73324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     *                     insufficient permissions, or channel in use.
73424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly     */
73516fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly    public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUID uuid)
73624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            throws IOException {
73716fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly        RfcommChannelPicker picker = new RfcommChannelPicker(uuid);
73824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
73924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        BluetoothServerSocket socket;
74024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int channel;
74124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int errno;
74224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        while (true) {
74324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            channel = picker.nextChannel();
74424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
74524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            if (channel == -1) {
74624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                throw new IOException("No available channels");
74724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            }
74824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
74924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            socket = new BluetoothServerSocket(
75024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    BluetoothSocket.TYPE_RFCOMM, true, true, channel);
75124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            errno = socket.mSocket.bindListen();
75224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            if (errno == 0) {
75324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                if (DBG) Log.d(TAG, "listening on RFCOMM channel " + channel);
75424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                break;  // success
75524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } else if (errno == BluetoothSocket.EADDRINUSE) {
75624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                if (DBG) Log.d(TAG, "RFCOMM channel " + channel + " in use");
75724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                try {
75824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    socket.close();
75924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                } catch (IOException e) {}
76024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                continue;  // try another channel
76124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } else {
76224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                try {
76324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                    socket.close();
76424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                } catch (IOException e) {}
76524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                socket.mSocket.throwErrnoNative(errno);  // Exception as a result of bindListen()
76624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            }
76724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
76824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
76924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int handle = -1;
770bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        try {
77116fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly            handle = mService.addRfcommServiceRecord(name, new ParcelUuid(uuid), channel,
77216fb88a673c41b93c5d57ccb28c2697e7d87701aNick Pelly                    new Binder());
77324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        } catch (RemoteException e) {Log.e(TAG, "", e);}
77424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (handle == -1) {
775bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            try {
776bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly                socket.close();
77724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } catch (IOException e) {}
77824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            throw new IOException("Not able to register SDP record for " + name);
779bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        }
78024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        socket.setCloseHandler(mHandler, handle);
781bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return socket;
782bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
783bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
784bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
785bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * Construct an unencrypted, unauthenticated, RFCOMM server socket.
786bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * Call #accept to retrieve connections to this socket.
787bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * @return An RFCOMM BluetoothServerSocket
788bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * @throws IOException On error, for example Bluetooth not available, or
789bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *                     insufficient permissions.
79045e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @hide
791bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
792bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
793bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        BluetoothServerSocket socket = new BluetoothServerSocket(
794bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly                BluetoothSocket.TYPE_RFCOMM, false, false, port);
79524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int errno = socket.mSocket.bindListen();
79624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (errno != 0) {
797bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            try {
798bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly                socket.close();
79924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } catch (IOException e) {}
80024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            socket.mSocket.throwErrnoNative(errno);
801bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        }
802bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return socket;
803bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
804bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
805bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    /**
806bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * Construct a SCO server socket.
807bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * Call #accept to retrieve connections to this socket.
808bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * @return A SCO BluetoothServerSocket
809bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     * @throws IOException On error, for example Bluetooth not available, or
810bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     *                     insufficient permissions.
81145e2704ff512d41e22af2801d76e96955469ce8dNick Pelly     * @hide
812bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly     */
813bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    public static BluetoothServerSocket listenUsingScoOn() throws IOException {
814bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        BluetoothServerSocket socket = new BluetoothServerSocket(
815bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly                BluetoothSocket.TYPE_SCO, false, false, -1);
81624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        int errno = socket.mSocket.bindListen();
81724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        if (errno != 0) {
818bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            try {
819bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly                socket.close();
82024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } catch (IOException e) {}
82124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            socket.mSocket.throwErrnoNative(errno);
822bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        }
823bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return socket;
824bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
825bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
826bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    private Set<BluetoothDevice> toDeviceSet(String[] addresses) {
827bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        Set<BluetoothDevice> devices = new HashSet<BluetoothDevice>(addresses.length);
828bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        for (int i = 0; i < addresses.length; i++) {
829bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly            devices.add(getRemoteDevice(addresses[i]));
830bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        }
831bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly        return Collections.unmodifiableSet(devices);
832bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly    }
833005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly
83424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    private Handler mHandler = new Handler() {
83524bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        public void handleMessage(Message msg) {
83624bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            /* handle socket closing */
83724bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            int handle = msg.what;
83824bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            try {
83924bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                if (DBG) Log.d(TAG, "Removing service record " + Integer.toHexString(handle));
84024bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly                mService.removeServiceRecord(handle);
84124bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly            } catch (RemoteException e) {Log.e(TAG, "", e);}
84224bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly        }
84324bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly    };
84424bb9b8af4ff691538fe9e517e8156016b0da6cdNick Pelly
845005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
846005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * Validate a Bluetooth address, such as "00:43:A8:23:10:F0"
84755e66f1b739097095f6b3c47f88de258898fc1c9Nick Pelly     * <p>Alphabetic characters must be uppercase to be valid.
848005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     *
849005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * @param address Bluetooth address as string
850005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     * @return true if the address is valid, false otherwise
851005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
852005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static boolean checkBluetoothAddress(String address) {
853005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        if (address == null || address.length() != ADDRESS_LENGTH) {
854005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            return false;
855005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        }
856005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        for (int i = 0; i < ADDRESS_LENGTH; i++) {
857005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            char c = address.charAt(i);
858005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            switch (i % 3) {
859005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            case 0:
860005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            case 1:
86155e66f1b739097095f6b3c47f88de258898fc1c9Nick Pelly                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
86255e66f1b739097095f6b3c47f88de258898fc1c9Nick Pelly                    // hex character, OK
86355e66f1b739097095f6b3c47f88de258898fc1c9Nick Pelly                    break;
864005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                }
865005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                return false;
866005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            case 2:
867005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                if (c == ':') {
868005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                    break;  // OK
869005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                }
870005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly                return false;
871005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly            }
872005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        }
873005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly        return true;
874005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    }
875bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly}
876