blob: 7e3bb05fe024d617069b459e968706ac009aed14 [file] [log] [blame]
Matthew Xieddf7e472013-03-01 18:41:02 -08001/*
2 * Copyright (C) 2013 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
Tor Norbye2d497522015-04-23 17:10:21 -070019import android.Manifest;
20import android.annotation.RequiresPermission;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060021import android.annotation.SystemService;
Matthew Xieddf7e472013-03-01 18:41:02 -080022import android.content.Context;
23import android.os.RemoteException;
24import android.util.Log;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * High level manager used to obtain an instance of an {@link BluetoothAdapter}
31 * and to conduct overall Bluetooth Management.
32 * <p>
33 * Use {@link android.content.Context#getSystemService(java.lang.String)}
34 * with {@link Context#BLUETOOTH_SERVICE} to create an {@link BluetoothManager},
35 * then call {@link #getAdapter} to obtain the {@link BluetoothAdapter}.
Marie Janssen553c8c72017-01-12 16:00:30 -080036 * </p>
Matthew Xieddf7e472013-03-01 18:41:02 -080037 * <div class="special reference">
38 * <h3>Developer Guides</h3>
Hemal Patel65813df2016-08-17 13:18:14 -070039 * <p>
40 * For more information about using BLUETOOTH, read the <a href=
41 * "{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer
42 * guide.
43 * </p>
Matthew Xieddf7e472013-03-01 18:41:02 -080044 * </div>
45 *
46 * @see Context#getSystemService
47 * @see BluetoothAdapter#getDefaultAdapter()
48 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060049@SystemService(Context.BLUETOOTH_SERVICE)
Matthew Xieddf7e472013-03-01 18:41:02 -080050public final class BluetoothManager {
51 private static final String TAG = "BluetoothManager";
52 private static final boolean DBG = true;
53 private static final boolean VDBG = true;
54
55 private final BluetoothAdapter mAdapter;
56
57 /**
58 * @hide
59 */
60 public BluetoothManager(Context context) {
61 context = context.getApplicationContext();
62 if (context == null) {
63 throw new IllegalArgumentException(
64 "context not associated with any application (using a mock context?)");
65 }
66 // Legacy api - getDefaultAdapter does not take in the context
67 mAdapter = BluetoothAdapter.getDefaultAdapter();
68 }
69
70 /**
71 * Get the default BLUETOOTH Adapter for this device.
72 *
73 * @return the default BLUETOOTH Adapter
74 */
75 public BluetoothAdapter getAdapter() {
76 return mAdapter;
77 }
78
79 /**
80 * Get the current connection state of the profile to the remote device.
81 *
82 * <p>This is not specific to any application configuration but represents
83 * the connection state of the local Bluetooth adapter for certain profile.
84 * This can be used by applications like status bar which would just like
85 * to know the state of Bluetooth.
86 *
Matthew Xieddf7e472013-03-01 18:41:02 -080087 * @param device Remote bluetooth device.
88 * @param profile GATT or GATT_SERVER
Jack Hea355e5e2017-08-22 16:06:54 -070089 * @return State of the profile connection. One of {@link BluetoothProfile#STATE_CONNECTED},
90 * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED},
91 * {@link BluetoothProfile#STATE_DISCONNECTING}
Matthew Xieddf7e472013-03-01 18:41:02 -080092 */
Tor Norbye2d497522015-04-23 17:10:21 -070093 @RequiresPermission(Manifest.permission.BLUETOOTH)
Matthew Xieddf7e472013-03-01 18:41:02 -080094 public int getConnectionState(BluetoothDevice device, int profile) {
Jack Hea355e5e2017-08-22 16:06:54 -070095 if (DBG) Log.d(TAG, "getConnectionState()");
Matthew Xieddf7e472013-03-01 18:41:02 -080096
97 List<BluetoothDevice> connectedDevices = getConnectedDevices(profile);
Jack Hea355e5e2017-08-22 16:06:54 -070098 for (BluetoothDevice connectedDevice : connectedDevices) {
Matthew Xieddf7e472013-03-01 18:41:02 -080099 if (device.equals(connectedDevice)) {
100 return BluetoothProfile.STATE_CONNECTED;
101 }
102 }
103
104 return BluetoothProfile.STATE_DISCONNECTED;
105 }
106
107 /**
108 * Get connected devices for the specified profile.
109 *
110 * <p> Return the set of devices which are in state {@link BluetoothProfile#STATE_CONNECTED}
111 *
112 * <p>This is not specific to any application configuration but represents
113 * the connection state of Bluetooth for this profile.
114 * This can be used by applications like status bar which would just like
115 * to know the state of Bluetooth.
116 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800117 * @param profile GATT or GATT_SERVER
118 * @return List of devices. The list will be empty on error.
119 */
Tor Norbye2d497522015-04-23 17:10:21 -0700120 @RequiresPermission(Manifest.permission.BLUETOOTH)
Matthew Xieddf7e472013-03-01 18:41:02 -0800121 public List<BluetoothDevice> getConnectedDevices(int profile) {
Jack Hea355e5e2017-08-22 16:06:54 -0700122 if (DBG) Log.d(TAG, "getConnectedDevices");
Matthew Xieddf7e472013-03-01 18:41:02 -0800123 if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) {
124 throw new IllegalArgumentException("Profile not supported: " + profile);
125 }
126
127 List<BluetoothDevice> connectedDevices = new ArrayList<BluetoothDevice>();
128
129 try {
130 IBluetoothManager managerService = mAdapter.getBluetoothManager();
Matthew Xiecdd94e32013-04-11 16:36:26 -0700131 IBluetoothGatt iGatt = managerService.getBluetoothGatt();
Matthew Xieddf7e472013-03-01 18:41:02 -0800132 if (iGatt == null) return connectedDevices;
133
134 connectedDevices = iGatt.getDevicesMatchingConnectionStates(
Jack Hea355e5e2017-08-22 16:06:54 -0700135 new int[]{BluetoothProfile.STATE_CONNECTED});
Matthew Xieddf7e472013-03-01 18:41:02 -0800136 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700137 Log.e(TAG, "", e);
Matthew Xieddf7e472013-03-01 18:41:02 -0800138 }
139
140 return connectedDevices;
141 }
142
143 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800144 * Get a list of devices that match any of the given connection
145 * states.
146 *
147 * <p> If none of the devices match any of the given states,
148 * an empty list will be returned.
149 *
150 * <p>This is not specific to any application configuration but represents
151 * the connection state of the local Bluetooth adapter for this profile.
152 * This can be used by applications like status bar which would just like
153 * to know the state of the local adapter.
154 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800155 * @param profile GATT or GATT_SERVER
Jack Hea355e5e2017-08-22 16:06:54 -0700156 * @param states Array of states. States can be one of {@link BluetoothProfile#STATE_CONNECTED},
157 * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED},
158 * {@link BluetoothProfile#STATE_DISCONNECTING},
Matthew Xieddf7e472013-03-01 18:41:02 -0800159 * @return List of devices. The list will be empty on error.
160 */
Tor Norbye2d497522015-04-23 17:10:21 -0700161 @RequiresPermission(Manifest.permission.BLUETOOTH)
Matthew Xieddf7e472013-03-01 18:41:02 -0800162 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int profile, int[] states) {
Jack Hea355e5e2017-08-22 16:06:54 -0700163 if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates");
Matthew Xieddf7e472013-03-01 18:41:02 -0800164
165 if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) {
166 throw new IllegalArgumentException("Profile not supported: " + profile);
167 }
168
169 List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
170
171 try {
172 IBluetoothManager managerService = mAdapter.getBluetoothManager();
Matthew Xiecdd94e32013-04-11 16:36:26 -0700173 IBluetoothGatt iGatt = managerService.getBluetoothGatt();
Matthew Xieddf7e472013-03-01 18:41:02 -0800174 if (iGatt == null) return devices;
175 devices = iGatt.getDevicesMatchingConnectionStates(states);
176 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700177 Log.e(TAG, "", e);
Matthew Xieddf7e472013-03-01 18:41:02 -0800178 }
179
180 return devices;
181 }
182
183 /**
184 * Open a GATT Server
185 * The callback is used to deliver results to Caller, such as connection status as well
186 * as the results of any other GATT server operations.
187 * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
188 * to conduct GATT server operations.
Jack Hea355e5e2017-08-22 16:06:54 -0700189 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800190 * @param context App context
191 * @param callback GATT server callback handler that will receive asynchronous callbacks.
192 * @return BluetoothGattServer instance
193 */
194 public BluetoothGattServer openGattServer(Context context,
Jack Hea355e5e2017-08-22 16:06:54 -0700195 BluetoothGattServerCallback callback) {
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700196
Jack Hea355e5e2017-08-22 16:06:54 -0700197 return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO));
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700198 }
199
200 /**
201 * Open a GATT Server
202 * The callback is used to deliver results to Caller, such as connection status as well
203 * as the results of any other GATT server operations.
204 * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
205 * to conduct GATT server operations.
Jack Hea355e5e2017-08-22 16:06:54 -0700206 *
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700207 * @param context App context
208 * @param callback GATT server callback handler that will receive asynchronous callbacks.
Jack Hea355e5e2017-08-22 16:06:54 -0700209 * @param transport preferred transport for GATT connections to remote dual-mode devices {@link
210 * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link
211 * BluetoothDevice#TRANSPORT_LE}
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700212 * @return BluetoothGattServer instance
213 * @hide
214 */
215 public BluetoothGattServer openGattServer(Context context,
Jack Hea355e5e2017-08-22 16:06:54 -0700216 BluetoothGattServerCallback callback, int transport) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800217 if (context == null || callback == null) {
218 throw new IllegalArgumentException("null parameter: " + context + " " + callback);
219 }
220
221 // TODO(Bluetooth) check whether platform support BLE
222 // Do the check here or in GattServer?
223
224 try {
225 IBluetoothManager managerService = mAdapter.getBluetoothManager();
Matthew Xiecdd94e32013-04-11 16:36:26 -0700226 IBluetoothGatt iGatt = managerService.getBluetoothGatt();
Matthew Xieddf7e472013-03-01 18:41:02 -0800227 if (iGatt == null) {
228 Log.e(TAG, "Fail to get GATT Server connection");
229 return null;
230 }
Jack Hea355e5e2017-08-22 16:06:54 -0700231 BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport);
Matthew Xieddf7e472013-03-01 18:41:02 -0800232 Boolean regStatus = mGattServer.registerCallback(callback);
Jack Hea355e5e2017-08-22 16:06:54 -0700233 return regStatus ? mGattServer : null;
Matthew Xieddf7e472013-03-01 18:41:02 -0800234 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700235 Log.e(TAG, "", e);
Matthew Xieddf7e472013-03-01 18:41:02 -0800236 return null;
237 }
238 }
239}