Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 20 | import android.annotation.RequiresPermission; |
| 21 | import android.annotation.SystemApi; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 23 | import android.app.PendingIntent; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 26 | import android.os.Binder; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * This class provides the APIs to control the Bluetooth MAP MCE Profile. |
| 36 | * |
| 37 | * @hide |
| 38 | */ |
| 39 | public final class BluetoothMapClient implements BluetoothProfile { |
| 40 | |
| 41 | private static final String TAG = "BluetoothMapClient"; |
| 42 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
| 43 | private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE); |
| 44 | |
| 45 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 46 | "android.bluetooth.mapmce.profile.action.CONNECTION_STATE_CHANGED"; |
| 47 | public static final String ACTION_MESSAGE_RECEIVED = |
| 48 | "android.bluetooth.mapmce.profile.action.MESSAGE_RECEIVED"; |
| 49 | /* Actions to be used for pending intents */ |
| 50 | public static final String ACTION_MESSAGE_SENT_SUCCESSFULLY = |
| 51 | "android.bluetooth.mapmce.profile.action.MESSAGE_SENT_SUCCESSFULLY"; |
| 52 | public static final String ACTION_MESSAGE_DELIVERED_SUCCESSFULLY = |
| 53 | "android.bluetooth.mapmce.profile.action.MESSAGE_DELIVERED_SUCCESSFULLY"; |
| 54 | |
Srinivas Visvanathan | 86a8c1c | 2017-03-07 10:22:53 -0800 | [diff] [blame] | 55 | /* Extras used in ACTION_MESSAGE_RECEIVED intent. |
| 56 | * NOTE: HANDLE is only valid for a single session with the device. */ |
| 57 | public static final String EXTRA_MESSAGE_HANDLE = |
| 58 | "android.bluetooth.mapmce.profile.extra.MESSAGE_HANDLE"; |
Sal Savage | 5d145c0 | 2019-05-14 11:09:19 -0700 | [diff] [blame] | 59 | public static final String EXTRA_MESSAGE_TIMESTAMP = |
| 60 | "android.bluetooth.mapmce.profile.extra.MESSAGE_TIMESTAMP"; |
| 61 | public static final String EXTRA_MESSAGE_READ_STATUS = |
| 62 | "android.bluetooth.mapmce.profile.extra.MESSAGE_READ_STATUS"; |
Srinivas Visvanathan | ad90338 | 2017-03-03 09:57:18 -0800 | [diff] [blame] | 63 | public static final String EXTRA_SENDER_CONTACT_URI = |
| 64 | "android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_URI"; |
| 65 | public static final String EXTRA_SENDER_CONTACT_NAME = |
| 66 | "android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_NAME"; |
| 67 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 68 | /** There was an error trying to obtain the state */ |
| 69 | public static final int STATE_ERROR = -1; |
| 70 | |
| 71 | public static final int RESULT_FAILURE = 0; |
| 72 | public static final int RESULT_SUCCESS = 1; |
| 73 | /** Connection canceled before completion. */ |
| 74 | public static final int RESULT_CANCELED = 2; |
| 75 | |
Vasu Nori | 694752d | 2018-08-17 17:25:28 -0700 | [diff] [blame] | 76 | private static final int UPLOADING_FEATURE_BITMASK = 0x08; |
| 77 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 78 | private BluetoothAdapter mAdapter; |
| 79 | private final BluetoothProfileConnector<IBluetoothMapClient> mProfileConnector = |
| 80 | new BluetoothProfileConnector(this, BluetoothProfile.MAP_CLIENT, |
| 81 | "BluetoothMapClient", IBluetoothMapClient.class.getName()) { |
| 82 | @Override |
| 83 | public IBluetoothMapClient getServiceInterface(IBinder service) { |
| 84 | return IBluetoothMapClient.Stub.asInterface(Binder.allowBlocking(service)); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 85 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 86 | }; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Create a BluetoothMapClient proxy object. |
| 90 | */ |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 91 | /*package*/ BluetoothMapClient(Context context, ServiceListener listener) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 92 | if (DBG) Log.d(TAG, "Create BluetoothMapClient proxy object"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 93 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 94 | mProfileConnector.connect(context, listener); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | protected void finalize() throws Throwable { |
| 98 | try { |
| 99 | close(); |
| 100 | } finally { |
| 101 | super.finalize(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Close the connection to the backing service. |
| 107 | * Other public functions of BluetoothMap will return default error |
| 108 | * results once close() has been called. Multiple invocations of close() |
| 109 | * are ok. |
| 110 | */ |
| 111 | public void close() { |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 112 | mProfileConnector.disconnect(); |
| 113 | } |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 114 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 115 | private IBluetoothMapClient getService() { |
| 116 | return mProfileConnector.getService(); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns true if the specified Bluetooth device is connected. |
| 121 | * Returns false if not connected, or if this proxy object is not |
| 122 | * currently connected to the Map service. |
| 123 | */ |
| 124 | public boolean isConnected(BluetoothDevice device) { |
| 125 | if (VDBG) Log.d(TAG, "isConnected(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 126 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 127 | if (service != null) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 128 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 129 | return service.isConnected(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 130 | } catch (RemoteException e) { |
| 131 | Log.e(TAG, e.toString()); |
| 132 | } |
| 133 | } else { |
| 134 | Log.w(TAG, "Proxy not attached to service"); |
| 135 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Initiate connection. Initiation of outgoing connections is not |
| 142 | * supported for MAP server. |
| 143 | */ |
| 144 | public boolean connect(BluetoothDevice device) { |
| 145 | if (DBG) Log.d(TAG, "connect(" + device + ")" + "for MAPS MCE"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 146 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 147 | if (service != null) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 148 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 149 | return service.connect(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 150 | } catch (RemoteException e) { |
| 151 | Log.e(TAG, e.toString()); |
| 152 | } |
| 153 | } else { |
| 154 | Log.w(TAG, "Proxy not attached to service"); |
| 155 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Initiate disconnect. |
| 162 | * |
| 163 | * @param device Remote Bluetooth Device |
| 164 | * @return false on error, true otherwise |
| 165 | */ |
| 166 | public boolean disconnect(BluetoothDevice device) { |
| 167 | if (DBG) Log.d(TAG, "disconnect(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 168 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 169 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 170 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 171 | return service.disconnect(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 174 | } |
| 175 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 176 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get the list of connected devices. Currently at most one. |
| 182 | * |
| 183 | * @return list of connected devices |
| 184 | */ |
| 185 | @Override |
| 186 | public List<BluetoothDevice> getConnectedDevices() { |
| 187 | if (DBG) Log.d(TAG, "getConnectedDevices()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 188 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 189 | if (service != null && isEnabled()) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 190 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 191 | return service.getConnectedDevices(); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 192 | } catch (RemoteException e) { |
| 193 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 194 | return new ArrayList<>(); |
| 195 | } |
| 196 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 197 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 198 | return new ArrayList<>(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get the list of devices matching specified states. Currently at most one. |
| 203 | * |
| 204 | * @return list of matching devices |
| 205 | */ |
| 206 | @Override |
| 207 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 208 | if (DBG) Log.d(TAG, "getDevicesMatchingStates()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 209 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 210 | if (service != null && isEnabled()) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 211 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 212 | return service.getDevicesMatchingConnectionStates(states); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 213 | } catch (RemoteException e) { |
| 214 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 215 | return new ArrayList<>(); |
| 216 | } |
| 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"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 219 | return new ArrayList<>(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get connection state of device |
| 224 | * |
| 225 | * @return device connection state |
| 226 | */ |
| 227 | @Override |
| 228 | public int getConnectionState(BluetoothDevice device) { |
| 229 | if (DBG) Log.d(TAG, "getConnectionState(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 230 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 231 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 232 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 233 | return service.getConnectionState(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 234 | } catch (RemoteException e) { |
| 235 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 236 | return BluetoothProfile.STATE_DISCONNECTED; |
| 237 | } |
| 238 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 239 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 240 | return BluetoothProfile.STATE_DISCONNECTED; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Set priority of the profile |
| 245 | * |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 246 | * <p> The device should already be paired. |
| 247 | * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF}, |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 248 | * |
| 249 | * @param device Paired bluetooth device |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 250 | * @param priority |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 251 | * @return true if priority is set, false on error |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 252 | * @hide |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 253 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 254 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 255 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 256 | if (DBG) Log.d(TAG, "setPriority(" + device + ", " + priority + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 257 | return setConnectionPolicy(device, BluetoothAdapter.priorityToConnectionPolicy(priority)); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Set connection policy of the profile |
| 262 | * |
| 263 | * <p> The device should already be paired. |
| 264 | * Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED}, |
| 265 | * {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN} |
| 266 | * |
| 267 | * @param device Paired bluetooth device |
| 268 | * @param connectionPolicy is the connection policy to set to for this profile |
| 269 | * @return true if connectionPolicy is set, false on error |
| 270 | * @hide |
| 271 | */ |
| 272 | @SystemApi |
| 273 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 274 | public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) { |
| 275 | if (DBG) Log.d(TAG, "setConnectionPolicy(" + device + ", " + connectionPolicy + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 276 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 277 | if (service != null && isEnabled() && isValidDevice(device)) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 278 | if (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN |
| 279 | && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 283 | return service.setConnectionPolicy(device, connectionPolicy); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 284 | } catch (RemoteException e) { |
| 285 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 286 | return false; |
| 287 | } |
| 288 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 289 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Get the priority of the profile. |
| 295 | * |
| 296 | * <p> The priority can be any of: |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 297 | * {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 298 | * |
| 299 | * @param device Bluetooth device |
| 300 | * @return priority of the device |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 301 | * @hide |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 302 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 303 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 304 | public int getPriority(BluetoothDevice device) { |
| 305 | if (VDBG) Log.d(TAG, "getPriority(" + device + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 306 | return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device)); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Get the connection policy of the profile. |
| 311 | * |
| 312 | * <p> The connection policy can be any of: |
| 313 | * {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, |
| 314 | * {@link #CONNECTION_POLICY_UNKNOWN} |
| 315 | * |
| 316 | * @param device Bluetooth device |
| 317 | * @return connection policy of the device |
| 318 | * @hide |
| 319 | */ |
| 320 | @SystemApi |
| 321 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 322 | public int getConnectionPolicy(BluetoothDevice device) { |
| 323 | if (VDBG) Log.d(TAG, "getConnectionPolicy(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 324 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 325 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 326 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 327 | return service.getConnectionPolicy(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 328 | } catch (RemoteException e) { |
| 329 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 330 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 333 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 334 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Send a message. |
| 339 | * |
| 340 | * Send an SMS message to either the contacts primary number or the telephone number specified. |
| 341 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 342 | * @param device Bluetooth device |
| 343 | * @param contacts Uri[] of the contacts |
| 344 | * @param message Message to be sent |
| 345 | * @param sentIntent intent issued when message is sent |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 346 | * @param deliveredIntent intent issued when message is delivered |
| 347 | * @return true if the message is enqueued, false on error |
| 348 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 349 | @UnsupportedAppUsage |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 350 | public boolean sendMessage(BluetoothDevice device, Uri[] contacts, String message, |
| 351 | PendingIntent sentIntent, PendingIntent deliveredIntent) { |
| 352 | if (DBG) Log.d(TAG, "sendMessage(" + device + ", " + contacts + ", " + message); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 353 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 354 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 355 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 356 | return service.sendMessage(device, contacts, message, sentIntent, deliveredIntent); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 357 | } catch (RemoteException e) { |
| 358 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | /** |
Joseph Pirozzo | b8fc067 | 2016-10-06 11:44:53 -0700 | [diff] [blame] | 366 | * Get unread messages. Unread messages will be published via {@link #ACTION_MESSAGE_RECEIVED}. |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 367 | * |
| 368 | * @param device Bluetooth device |
| 369 | * @return true if the message is enqueued, false on error |
| 370 | */ |
| 371 | public boolean getUnreadMessages(BluetoothDevice device) { |
| 372 | if (DBG) Log.d(TAG, "getUnreadMessages(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 373 | final IBluetoothMapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 374 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 375 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 376 | return service.getUnreadMessages(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 377 | } catch (RemoteException e) { |
| 378 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | return false; |
| 383 | } |
| 384 | |
Vasu Nori | 694752d | 2018-08-17 17:25:28 -0700 | [diff] [blame] | 385 | /** |
| 386 | * Returns the "Uploading" feature bit value from the SDP record's |
| 387 | * MapSupportedFeatures field (see Bluetooth MAP 1.4 spec, page 114). |
| 388 | * @param device The Bluetooth device to get this value for. |
| 389 | * @return Returns true if the Uploading bit value in SDP record's |
| 390 | * MapSupportedFeatures field is set. False is returned otherwise. |
| 391 | */ |
| 392 | public boolean isUploadingSupported(BluetoothDevice device) { |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 393 | final IBluetoothMapClient service = getService(); |
Vasu Nori | 694752d | 2018-08-17 17:25:28 -0700 | [diff] [blame] | 394 | try { |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 395 | return (service != null && isEnabled() && isValidDevice(device)) |
| 396 | && ((service.getSupportedFeatures(device) & UPLOADING_FEATURE_BITMASK) > 0); |
Vasu Nori | 694752d | 2018-08-17 17:25:28 -0700 | [diff] [blame] | 397 | } catch (RemoteException e) { |
| 398 | Log.e(TAG, e.getMessage()); |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 403 | private boolean isEnabled() { |
| 404 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 405 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 406 | if (DBG) Log.d(TAG, "Bluetooth is Not enabled"); |
| 407 | return false; |
| 408 | } |
| 409 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 410 | private static boolean isValidDevice(BluetoothDevice device) { |
| 411 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 414 | } |