Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 22 | import android.annotation.SuppressLint; |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
Artur Satayev | 76c1d9d | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 24 | import android.compat.annotation.UnsupportedAppUsage; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 26 | import android.os.Binder; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.RemoteException; |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 29 | import android.util.CloseGuard; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 35 | /** |
| 36 | * This class provides the APIs to control the Bluetooth MAP |
| 37 | * Profile. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 38 | * |
| 39 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 40 | */ |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 41 | @SystemApi |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 42 | public final class BluetoothMap implements BluetoothProfile, AutoCloseable { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String TAG = "BluetoothMap"; |
| 45 | private static final boolean DBG = true; |
| 46 | private static final boolean VDBG = false; |
| 47 | |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 48 | private CloseGuard mCloseGuard; |
| 49 | |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 50 | /** @hide */ |
| 51 | @SuppressLint("ActionValue") |
| 52 | @SystemApi |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 53 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 54 | "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED"; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 55 | |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 56 | /** |
| 57 | * There was an error trying to obtain the state |
| 58 | * |
| 59 | * @hide |
| 60 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 61 | public static final int STATE_ERROR = -1; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 62 | |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 63 | /** @hide */ |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 64 | public static final int RESULT_FAILURE = 0; |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 65 | /** @hide */ |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 66 | public static final int RESULT_SUCCESS = 1; |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Connection canceled before completion. |
| 69 | * |
| 70 | * @hide |
| 71 | */ |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 72 | public static final int RESULT_CANCELED = 2; |
| 73 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 74 | private BluetoothAdapter mAdapter; |
| 75 | private final BluetoothProfileConnector<IBluetoothMap> mProfileConnector = |
| 76 | new BluetoothProfileConnector(this, BluetoothProfile.MAP, |
| 77 | "BluetoothMap", IBluetoothMap.class.getName()) { |
| 78 | @Override |
| 79 | public IBluetoothMap getServiceInterface(IBinder service) { |
| 80 | return IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 81 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 82 | }; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Create a BluetoothMap proxy object. |
| 86 | */ |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 87 | /*package*/ BluetoothMap(Context context, ServiceListener listener) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 88 | if (DBG) Log.d(TAG, "Create BluetoothMap proxy object"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 89 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 90 | mProfileConnector.connect(context, listener); |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 91 | mCloseGuard = new CloseGuard(); |
| 92 | mCloseGuard.open("close"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 95 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 96 | protected void finalize() { |
| 97 | if (mCloseGuard != null) { |
| 98 | mCloseGuard.warnIfOpen(); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 99 | } |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 100 | close(); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Close the connection to the backing service. |
| 105 | * Other public functions of BluetoothMap will return default error |
| 106 | * results once close() has been called. Multiple invocations of close() |
| 107 | * are ok. |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 108 | * |
| 109 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 110 | */ |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 111 | @SystemApi |
| 112 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 113 | public void close() { |
| 114 | if (VDBG) log("close()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 115 | mProfileConnector.disconnect(); |
| 116 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 117 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 118 | private IBluetoothMap getService() { |
| 119 | return mProfileConnector.getService(); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the current state of the BluetoothMap service. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 124 | * |
| 125 | * @return One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not |
| 126 | * connected to the Map service. |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 127 | * |
| 128 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 129 | */ |
| 130 | public int getState() { |
| 131 | if (VDBG) log("getState()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 132 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 133 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 134 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 135 | return service.getState(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 136 | } catch (RemoteException e) { |
| 137 | Log.e(TAG, e.toString()); |
| 138 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 139 | } else { |
| 140 | Log.w(TAG, "Proxy not attached to service"); |
| 141 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 142 | } |
| 143 | return BluetoothMap.STATE_ERROR; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the currently connected remote Bluetooth device (PCE). |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 148 | * |
| 149 | * @return The remote Bluetooth device, or null if not in connected or connecting state, or if |
| 150 | * this proxy object is not connected to the Map service. |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 151 | * |
| 152 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 153 | */ |
| 154 | public BluetoothDevice getClient() { |
| 155 | if (VDBG) log("getClient()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 156 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 157 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 158 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 159 | return service.getClient(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 160 | } catch (RemoteException e) { |
| 161 | Log.e(TAG, e.toString()); |
| 162 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 163 | } else { |
| 164 | Log.w(TAG, "Proxy not attached to service"); |
| 165 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 166 | } |
| 167 | return null; |
| 168 | } |
| 169 | |
| 170 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 171 | * Returns true if the specified Bluetooth device is connected. |
| 172 | * Returns false if not connected, or if this proxy object is not |
| 173 | * currently connected to the Map service. |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 174 | * |
| 175 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 176 | */ |
| 177 | public boolean isConnected(BluetoothDevice device) { |
| 178 | if (VDBG) log("isConnected(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 179 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 180 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 181 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 182 | return service.isConnected(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 183 | } catch (RemoteException e) { |
| 184 | Log.e(TAG, e.toString()); |
| 185 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 186 | } else { |
| 187 | Log.w(TAG, "Proxy not attached to service"); |
| 188 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 194 | * Initiate connection. Initiation of outgoing connections is not |
| 195 | * supported for MAP server. |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 196 | * |
| 197 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 198 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 199 | public boolean connect(BluetoothDevice device) { |
| 200 | if (DBG) log("connect(" + device + ")" + "not supported for MAPS"); |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Initiate disconnect. |
| 206 | * |
| 207 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 208 | * @return false on error, true otherwise |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 209 | * |
| 210 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 211 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 212 | @UnsupportedAppUsage |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 213 | public boolean disconnect(BluetoothDevice device) { |
| 214 | if (DBG) log("disconnect(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 215 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 216 | if (service != null && isEnabled() && isValidDevice(device)) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 217 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 218 | return service.disconnect(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 219 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 220 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 221 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 222 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 223 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 224 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Check class bits for possible Map support. |
| 230 | * This is a simple heuristic that tries to guess if a device with the |
| 231 | * given class bits might support Map. It is not accurate for all |
| 232 | * devices. It tries to err on the side of false positives. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 233 | * |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 234 | * @return True if this device might support Map. |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 235 | * |
| 236 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 237 | */ |
| 238 | public static boolean doesClassMatchSink(BluetoothClass btClass) { |
| 239 | // TODO optimize the rule |
| 240 | switch (btClass.getDeviceClass()) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 241 | case BluetoothClass.Device.COMPUTER_DESKTOP: |
| 242 | case BluetoothClass.Device.COMPUTER_LAPTOP: |
| 243 | case BluetoothClass.Device.COMPUTER_SERVER: |
| 244 | case BluetoothClass.Device.COMPUTER_UNCATEGORIZED: |
| 245 | return true; |
| 246 | default: |
| 247 | return false; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 251 | /** |
| 252 | * Get the list of connected devices. Currently at most one. |
| 253 | * |
| 254 | * @return list of connected devices |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 255 | * |
| 256 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 257 | */ |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 258 | @SystemApi |
Rahul Sabnis | 946bf88 | 2020-02-25 11:33:43 -0800 | [diff] [blame] | 259 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 260 | public @NonNull List<BluetoothDevice> getConnectedDevices() { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 261 | if (DBG) log("getConnectedDevices()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 262 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 263 | if (service != null && isEnabled()) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 264 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 265 | return service.getConnectedDevices(); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 266 | } catch (RemoteException e) { |
| 267 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 268 | return new ArrayList<BluetoothDevice>(); |
| 269 | } |
| 270 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 271 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 272 | return new ArrayList<BluetoothDevice>(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get the list of devices matching specified states. Currently at most one. |
| 277 | * |
| 278 | * @return list of matching devices |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 279 | * |
| 280 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 281 | */ |
| 282 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 283 | if (DBG) log("getDevicesMatchingStates()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 284 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 285 | if (service != null && isEnabled()) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 286 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 287 | return service.getDevicesMatchingConnectionStates(states); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 288 | } catch (RemoteException e) { |
| 289 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 290 | return new ArrayList<BluetoothDevice>(); |
| 291 | } |
| 292 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 293 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 294 | return new ArrayList<BluetoothDevice>(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get connection state of device |
| 299 | * |
| 300 | * @return device connection state |
Rahul Sabnis | eccd5bd | 2020-01-08 11:08:42 -0800 | [diff] [blame] | 301 | * |
| 302 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 303 | */ |
| 304 | public int getConnectionState(BluetoothDevice device) { |
| 305 | if (DBG) log("getConnectionState(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 306 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 307 | if (service != null && isEnabled() && isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 308 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 309 | return service.getConnectionState(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 310 | } catch (RemoteException e) { |
| 311 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 312 | return BluetoothProfile.STATE_DISCONNECTED; |
| 313 | } |
| 314 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 315 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 316 | return BluetoothProfile.STATE_DISCONNECTED; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Set priority of the profile |
| 321 | * |
| 322 | * <p> The device should already be paired. |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 323 | * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF}, |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 324 | * |
| 325 | * @param device Paired bluetooth device |
| 326 | * @param priority |
| 327 | * @return true if priority is set, false on error |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 328 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 329 | */ |
Rahul Sabnis | 2c71d16 | 2020-03-18 17:46:33 -0700 | [diff] [blame] | 330 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 331 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 332 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 333 | return setConnectionPolicy(device, BluetoothAdapter.priorityToConnectionPolicy(priority)); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Set connection policy of the profile |
| 338 | * |
| 339 | * <p> The device should already be paired. |
| 340 | * Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED}, |
| 341 | * {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN} |
| 342 | * |
| 343 | * @param device Paired bluetooth device |
| 344 | * @param connectionPolicy is the connection policy to set to for this profile |
| 345 | * @return true if connectionPolicy is set, false on error |
| 346 | * @hide |
| 347 | */ |
| 348 | @SystemApi |
Rahul Sabnis | 2c71d16 | 2020-03-18 17:46:33 -0700 | [diff] [blame] | 349 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 350 | public boolean setConnectionPolicy(@NonNull BluetoothDevice device, |
Rahul Sabnis | 30c597f | 2020-01-22 14:26:35 -0800 | [diff] [blame] | 351 | @ConnectionPolicy int connectionPolicy) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 352 | if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 353 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 354 | if (service != null && isEnabled() && isValidDevice(device)) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 355 | if (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN |
| 356 | && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 357 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 358 | } |
| 359 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 360 | return service.setConnectionPolicy(device, connectionPolicy); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 361 | } catch (RemoteException e) { |
| 362 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 363 | return false; |
| 364 | } |
| 365 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 366 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Get the priority of the profile. |
| 372 | * |
| 373 | * <p> The priority can be any of: |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 374 | * {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 375 | * |
| 376 | * @param device Bluetooth device |
| 377 | * @return priority of the device |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 378 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 379 | */ |
Rahul Sabnis | 2c71d16 | 2020-03-18 17:46:33 -0700 | [diff] [blame] | 380 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 381 | public int getPriority(BluetoothDevice device) { |
| 382 | if (VDBG) log("getPriority(" + device + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 383 | return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device)); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Get the connection policy of the profile. |
| 388 | * |
| 389 | * <p> The connection policy can be any of: |
| 390 | * {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, |
| 391 | * {@link #CONNECTION_POLICY_UNKNOWN} |
| 392 | * |
| 393 | * @param device Bluetooth device |
| 394 | * @return connection policy of the device |
| 395 | * @hide |
| 396 | */ |
| 397 | @SystemApi |
Rahul Sabnis | 2c71d16 | 2020-03-18 17:46:33 -0700 | [diff] [blame] | 398 | @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) |
| 399 | public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 400 | if (VDBG) log("getConnectionPolicy(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 401 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 402 | if (service != null && isEnabled() && isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 403 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 404 | return service.getConnectionPolicy(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 405 | } catch (RemoteException e) { |
| 406 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 407 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 408 | } |
| 409 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 410 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 411 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 414 | private static void log(String msg) { |
| 415 | Log.d(TAG, msg); |
| 416 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 417 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 418 | private boolean isEnabled() { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 419 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 420 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 421 | log("Bluetooth is Not enabled"); |
| 422 | return false; |
| 423 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 424 | private static boolean isValidDevice(BluetoothDevice device) { |
| 425 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 428 | } |