Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.bluetooth; |
| 18 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.annotation.RequiresPermission; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 21 | import android.annotation.SystemService; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import 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 Janssen | 553c8c7 | 2017-01-12 16:00:30 -0800 | [diff] [blame] | 36 | * </p> |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 37 | * <div class="special reference"> |
| 38 | * <h3>Developer Guides</h3> |
Hemal Patel | 65813df | 2016-08-17 13:18:14 -0700 | [diff] [blame] | 39 | * <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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 44 | * </div> |
| 45 | * |
| 46 | * @see Context#getSystemService |
| 47 | * @see BluetoothAdapter#getDefaultAdapter() |
| 48 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 49 | @SystemService(Context.BLUETOOTH_SERVICE) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 50 | public 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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 87 | * @param device Remote bluetooth device. |
| 88 | * @param profile GATT or GATT_SERVER |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 89 | * @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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 92 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 93 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 94 | public int getConnectionState(BluetoothDevice device, int profile) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 95 | if (DBG) Log.d(TAG, "getConnectionState()"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 96 | |
| 97 | List<BluetoothDevice> connectedDevices = getConnectedDevices(profile); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 98 | for (BluetoothDevice connectedDevice : connectedDevices) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 99 | 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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 117 | * @param profile GATT or GATT_SERVER |
| 118 | * @return List of devices. The list will be empty on error. |
| 119 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 120 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 121 | public List<BluetoothDevice> getConnectedDevices(int profile) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 122 | if (DBG) Log.d(TAG, "getConnectedDevices"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 123 | 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 Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 131 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 132 | if (iGatt == null) return connectedDevices; |
| 133 | |
| 134 | connectedDevices = iGatt.getDevicesMatchingConnectionStates( |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 135 | new int[]{BluetoothProfile.STATE_CONNECTED}); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 136 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 137 | Log.e(TAG, "", e); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return connectedDevices; |
| 141 | } |
| 142 | |
| 143 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 144 | * 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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 155 | * @param profile GATT or GATT_SERVER |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 156 | * @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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 159 | * @return List of devices. The list will be empty on error. |
| 160 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 161 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 162 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int profile, int[] states) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 163 | if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 164 | |
| 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 Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 173 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 174 | if (iGatt == null) return devices; |
| 175 | devices = iGatt.getDevicesMatchingConnectionStates(states); |
| 176 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 177 | Log.e(TAG, "", e); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 178 | } |
| 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 He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 189 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 190 | * @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 He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 195 | BluetoothGattServerCallback callback) { |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 196 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 197 | return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO)); |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 198 | } |
| 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 He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 206 | * |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 207 | * @param context App context |
| 208 | * @param callback GATT server callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 209 | * @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 Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 212 | * @return BluetoothGattServer instance |
| 213 | * @hide |
| 214 | */ |
| 215 | public BluetoothGattServer openGattServer(Context context, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 216 | BluetoothGattServerCallback callback, int transport) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 217 | 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 Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 226 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 227 | if (iGatt == null) { |
| 228 | Log.e(TAG, "Fail to get GATT Server connection"); |
| 229 | return null; |
| 230 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 231 | BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 232 | Boolean regStatus = mGattServer.registerCallback(callback); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 233 | return regStatus ? mGattServer : null; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 234 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 235 | Log.e(TAG, "", e); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 236 | return null; |
| 237 | } |
| 238 | } |
| 239 | } |