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