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