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; |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 20 | import android.annotation.RequiresFeature; |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 22 | import android.annotation.SystemService; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * High level manager used to obtain an instance of an {@link BluetoothAdapter} |
| 33 | * and to conduct overall Bluetooth Management. |
| 34 | * <p> |
| 35 | * Use {@link android.content.Context#getSystemService(java.lang.String)} |
| 36 | * with {@link Context#BLUETOOTH_SERVICE} to create an {@link BluetoothManager}, |
| 37 | * then call {@link #getAdapter} to obtain the {@link BluetoothAdapter}. |
Marie Janssen | 553c8c7 | 2017-01-12 16:00:30 -0800 | [diff] [blame] | 38 | * </p> |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 39 | * <div class="special reference"> |
| 40 | * <h3>Developer Guides</h3> |
Hemal Patel | 65813df | 2016-08-17 13:18:14 -0700 | [diff] [blame] | 41 | * <p> |
| 42 | * For more information about using BLUETOOTH, read the <a href= |
| 43 | * "{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer |
| 44 | * guide. |
| 45 | * </p> |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 46 | * </div> |
| 47 | * |
| 48 | * @see Context#getSystemService |
| 49 | * @see BluetoothAdapter#getDefaultAdapter() |
| 50 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 51 | @SystemService(Context.BLUETOOTH_SERVICE) |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 52 | @RequiresFeature(PackageManager.FEATURE_BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 53 | public final class BluetoothManager { |
| 54 | private static final String TAG = "BluetoothManager"; |
| 55 | private static final boolean DBG = true; |
| 56 | private static final boolean VDBG = true; |
| 57 | |
| 58 | private final BluetoothAdapter mAdapter; |
| 59 | |
| 60 | /** |
| 61 | * @hide |
| 62 | */ |
| 63 | public BluetoothManager(Context context) { |
| 64 | context = context.getApplicationContext(); |
| 65 | if (context == null) { |
| 66 | throw new IllegalArgumentException( |
| 67 | "context not associated with any application (using a mock context?)"); |
| 68 | } |
| 69 | // Legacy api - getDefaultAdapter does not take in the context |
| 70 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the default BLUETOOTH Adapter for this device. |
| 75 | * |
| 76 | * @return the default BLUETOOTH Adapter |
| 77 | */ |
| 78 | public BluetoothAdapter getAdapter() { |
| 79 | return mAdapter; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the current connection state of the profile to the remote device. |
| 84 | * |
| 85 | * <p>This is not specific to any application configuration but represents |
| 86 | * the connection state of the local Bluetooth adapter for certain profile. |
| 87 | * This can be used by applications like status bar which would just like |
| 88 | * to know the state of Bluetooth. |
| 89 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 90 | * @param device Remote bluetooth device. |
| 91 | * @param profile GATT or GATT_SERVER |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 92 | * @return State of the profile connection. One of {@link BluetoothProfile#STATE_CONNECTED}, |
| 93 | * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, |
| 94 | * {@link BluetoothProfile#STATE_DISCONNECTING} |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 95 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 96 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 97 | public int getConnectionState(BluetoothDevice device, int profile) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 98 | if (DBG) Log.d(TAG, "getConnectionState()"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 99 | |
| 100 | List<BluetoothDevice> connectedDevices = getConnectedDevices(profile); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 101 | for (BluetoothDevice connectedDevice : connectedDevices) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 102 | if (device.equals(connectedDevice)) { |
| 103 | return BluetoothProfile.STATE_CONNECTED; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return BluetoothProfile.STATE_DISCONNECTED; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get connected devices for the specified profile. |
| 112 | * |
| 113 | * <p> Return the set of devices which are in state {@link BluetoothProfile#STATE_CONNECTED} |
| 114 | * |
| 115 | * <p>This is not specific to any application configuration but represents |
| 116 | * the connection state of Bluetooth for this profile. |
| 117 | * This can be used by applications like status bar which would just like |
| 118 | * to know the state of Bluetooth. |
| 119 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 120 | * @param profile GATT or GATT_SERVER |
| 121 | * @return List of devices. The list will be empty on error. |
| 122 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 123 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 124 | public List<BluetoothDevice> getConnectedDevices(int profile) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 125 | if (DBG) Log.d(TAG, "getConnectedDevices"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 126 | if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { |
| 127 | throw new IllegalArgumentException("Profile not supported: " + profile); |
| 128 | } |
| 129 | |
| 130 | List<BluetoothDevice> connectedDevices = new ArrayList<BluetoothDevice>(); |
| 131 | |
| 132 | try { |
| 133 | IBluetoothManager managerService = mAdapter.getBluetoothManager(); |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 134 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 135 | if (iGatt == null) return connectedDevices; |
| 136 | |
| 137 | connectedDevices = iGatt.getDevicesMatchingConnectionStates( |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 138 | new int[]{BluetoothProfile.STATE_CONNECTED}); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 139 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 140 | Log.e(TAG, "", e); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return connectedDevices; |
| 144 | } |
| 145 | |
| 146 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 147 | * Get a list of devices that match any of the given connection |
| 148 | * states. |
| 149 | * |
| 150 | * <p> If none of the devices match any of the given states, |
| 151 | * an empty list will be returned. |
| 152 | * |
| 153 | * <p>This is not specific to any application configuration but represents |
| 154 | * the connection state of the local Bluetooth adapter for this profile. |
| 155 | * This can be used by applications like status bar which would just like |
| 156 | * to know the state of the local adapter. |
| 157 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 158 | * @param profile GATT or GATT_SERVER |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 159 | * @param states Array of states. States can be one of {@link BluetoothProfile#STATE_CONNECTED}, |
| 160 | * {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED}, |
| 161 | * {@link BluetoothProfile#STATE_DISCONNECTING}, |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 162 | * @return List of devices. The list will be empty on error. |
| 163 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 164 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 165 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int profile, int[] states) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 166 | if (DBG) Log.d(TAG, "getDevicesMatchingConnectionStates"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 167 | |
| 168 | if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) { |
| 169 | throw new IllegalArgumentException("Profile not supported: " + profile); |
| 170 | } |
| 171 | |
| 172 | List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>(); |
| 173 | |
| 174 | try { |
| 175 | IBluetoothManager managerService = mAdapter.getBluetoothManager(); |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 176 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 177 | if (iGatt == null) return devices; |
| 178 | devices = iGatt.getDevicesMatchingConnectionStates(states); |
| 179 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 180 | Log.e(TAG, "", e); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return devices; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Open a GATT Server |
| 188 | * The callback is used to deliver results to Caller, such as connection status as well |
| 189 | * as the results of any other GATT server operations. |
| 190 | * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer |
| 191 | * to conduct GATT server operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 192 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 193 | * @param context App context |
| 194 | * @param callback GATT server callback handler that will receive asynchronous callbacks. |
| 195 | * @return BluetoothGattServer instance |
| 196 | */ |
| 197 | public BluetoothGattServer openGattServer(Context context, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 198 | BluetoothGattServerCallback callback) { |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 199 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 200 | return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO)); |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Open a GATT Server |
| 205 | * The callback is used to deliver results to Caller, such as connection status as well |
| 206 | * as the results of any other GATT server operations. |
| 207 | * The method returns a BluetoothGattServer instance. You can use BluetoothGattServer |
| 208 | * to conduct GATT server operations. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 209 | * |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 210 | * @param context App context |
| 211 | * @param callback GATT server callback handler that will receive asynchronous callbacks. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 212 | * @param transport preferred transport for GATT connections to remote dual-mode devices {@link |
| 213 | * BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link |
| 214 | * BluetoothDevice#TRANSPORT_LE} |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 215 | * @return BluetoothGattServer instance |
| 216 | * @hide |
| 217 | */ |
| 218 | public BluetoothGattServer openGattServer(Context context, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 219 | BluetoothGattServerCallback callback, int transport) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 220 | if (context == null || callback == null) { |
| 221 | throw new IllegalArgumentException("null parameter: " + context + " " + callback); |
| 222 | } |
| 223 | |
| 224 | // TODO(Bluetooth) check whether platform support BLE |
| 225 | // Do the check here or in GattServer? |
| 226 | |
| 227 | try { |
| 228 | IBluetoothManager managerService = mAdapter.getBluetoothManager(); |
Matthew Xie | cdd94e3 | 2013-04-11 16:36:26 -0700 | [diff] [blame] | 229 | IBluetoothGatt iGatt = managerService.getBluetoothGatt(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 230 | if (iGatt == null) { |
| 231 | Log.e(TAG, "Fail to get GATT Server connection"); |
| 232 | return null; |
| 233 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 234 | BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 235 | Boolean regStatus = mGattServer.registerCallback(callback); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 236 | return regStatus ? mGattServer : null; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 237 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | Log.e(TAG, "", e); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 239 | return null; |
| 240 | } |
| 241 | } |
| 242 | } |