blob: cc2b6155a7eaeeff1124170dd1c0db88d189bf71 [file] [log] [blame]
Matthew Xiefe3807a2013-07-18 17:31:50 -07001/*
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
17package android.bluetooth;
18
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -080019import android.Manifest;
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080020import android.annotation.NonNull;
21import android.annotation.Nullable;
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -080022import android.annotation.RequiresPermission;
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080023import android.annotation.SuppressLint;
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -080024import android.annotation.SystemApi;
Artur Satayev76c1d9d2019-12-10 17:47:52 +000025import android.compat.annotation.UnsupportedAppUsage;
Matthew Xiefe3807a2013-07-18 17:31:50 -070026import android.content.Context;
Jack Hea355e5e2017-08-22 16:06:54 -070027import android.os.Binder;
28import android.os.IBinder;
29import android.os.RemoteException;
Rahul Sabnis946bf882020-02-25 11:33:43 -080030import android.util.CloseGuard;
Matthew Xiefe3807a2013-07-18 17:31:50 -070031import android.util.Log;
32
Jack Hea355e5e2017-08-22 16:06:54 -070033import java.util.ArrayList;
34import java.util.List;
35
Matthew Xiefe3807a2013-07-18 17:31:50 -070036/**
37 * This class provides the APIs to control the Bluetooth MAP
38 * Profile.
Jack Hea355e5e2017-08-22 16:06:54 -070039 *
40 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -070041 */
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080042@SystemApi
Rahul Sabnis946bf882020-02-25 11:33:43 -080043public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
Matthew Xiefe3807a2013-07-18 17:31:50 -070044
45 private static final String TAG = "BluetoothMap";
46 private static final boolean DBG = true;
47 private static final boolean VDBG = false;
48
Rahul Sabnis946bf882020-02-25 11:33:43 -080049 private CloseGuard mCloseGuard;
50
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080051 /** @hide */
52 @SuppressLint("ActionValue")
53 @SystemApi
Kim Schulz0d376052013-08-22 11:18:02 +020054 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070055 "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED";
Matthew Xiefe3807a2013-07-18 17:31:50 -070056
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080057 /**
58 * There was an error trying to obtain the state
59 *
60 * @hide
61 */
Jack Hea355e5e2017-08-22 16:06:54 -070062 public static final int STATE_ERROR = -1;
Matthew Xiefe3807a2013-07-18 17:31:50 -070063
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080064 /** @hide */
Matthew Xiefe3807a2013-07-18 17:31:50 -070065 public static final int RESULT_FAILURE = 0;
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080066 /** @hide */
Matthew Xiefe3807a2013-07-18 17:31:50 -070067 public static final int RESULT_SUCCESS = 1;
Rahul Sabniseccd5bd2020-01-08 11:08:42 -080068 /**
69 * Connection canceled before completion.
70 *
71 * @hide
72 */
Matthew Xiefe3807a2013-07-18 17:31:50 -070073 public static final int RESULT_CANCELED = 2;
74
Ugo Yud0115462019-03-26 21:38:08 +080075 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 Xiefe3807a2013-07-18 17:31:50 -070082 }
Ugo Yud0115462019-03-26 21:38:08 +080083 };
Matthew Xiefe3807a2013-07-18 17:31:50 -070084
85 /**
86 * Create a BluetoothMap proxy object.
87 */
Ugo Yud0115462019-03-26 21:38:08 +080088 /*package*/ BluetoothMap(Context context, ServiceListener listener) {
Kim Schulz0d376052013-08-22 11:18:02 +020089 if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
Matthew Xiefe3807a2013-07-18 17:31:50 -070090 mAdapter = BluetoothAdapter.getDefaultAdapter();
Ugo Yud0115462019-03-26 21:38:08 +080091 mProfileConnector.connect(context, listener);
Rahul Sabnis946bf882020-02-25 11:33:43 -080092 mCloseGuard = new CloseGuard();
93 mCloseGuard.open("close");
Matthew Xiefe3807a2013-07-18 17:31:50 -070094 }
95
Rahul Sabnis946bf882020-02-25 11:33:43 -080096 @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
97 protected void finalize() {
98 if (mCloseGuard != null) {
99 mCloseGuard.warnIfOpen();
Matthew Xiefe3807a2013-07-18 17:31:50 -0700100 }
Rahul Sabnis946bf882020-02-25 11:33:43 -0800101 close();
Matthew Xiefe3807a2013-07-18 17:31:50 -0700102 }
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 Sabniseccd5bd2020-01-08 11:08:42 -0800109 *
110 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -0700111 */
Rahul Sabnis946bf882020-02-25 11:33:43 -0800112 @SystemApi
113 @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
114 public void close() {
115 if (VDBG) log("close()");
Ugo Yud0115462019-03-26 21:38:08 +0800116 mProfileConnector.disconnect();
117 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700118
Ugo Yud0115462019-03-26 21:38:08 +0800119 private IBluetoothMap getService() {
120 return mProfileConnector.getService();
Matthew Xiefe3807a2013-07-18 17:31:50 -0700121 }
122
123 /**
124 * Get the current state of the BluetoothMap service.
Jack Hea355e5e2017-08-22 16:06:54 -0700125 *
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 Sabniseccd5bd2020-01-08 11:08:42 -0800128 *
129 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -0700130 */
131 public int getState() {
132 if (VDBG) log("getState()");
Ugo Yud0115462019-03-26 21:38:08 +0800133 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700134 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700135 try {
Jack He16eeac32017-08-17 12:11:18 -0700136 return service.getState();
Jack Hea355e5e2017-08-22 16:06:54 -0700137 } catch (RemoteException e) {
138 Log.e(TAG, e.toString());
139 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700140 } 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 Hea355e5e2017-08-22 16:06:54 -0700149 *
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 Sabniseccd5bd2020-01-08 11:08:42 -0800152 *
153 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -0700154 */
155 public BluetoothDevice getClient() {
156 if (VDBG) log("getClient()");
Ugo Yud0115462019-03-26 21:38:08 +0800157 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700158 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700159 try {
Jack He16eeac32017-08-17 12:11:18 -0700160 return service.getClient();
Jack Hea355e5e2017-08-22 16:06:54 -0700161 } catch (RemoteException e) {
162 Log.e(TAG, e.toString());
163 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700164 } 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 Schulz0d376052013-08-22 11:18:02 +0200172 * 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 Sabniseccd5bd2020-01-08 11:08:42 -0800175 *
176 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -0700177 */
178 public boolean isConnected(BluetoothDevice device) {
179 if (VDBG) log("isConnected(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800180 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700181 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700182 try {
Jack He16eeac32017-08-17 12:11:18 -0700183 return service.isConnected(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700184 } catch (RemoteException e) {
185 Log.e(TAG, e.toString());
186 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700187 } 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 Schulz0d376052013-08-22 11:18:02 +0200195 * Initiate connection. Initiation of outgoing connections is not
196 * supported for MAP server.
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800197 *
198 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -0700199 */
Kim Schulz0d376052013-08-22 11:18:02 +0200200 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 Hea355e5e2017-08-22 16:06:54 -0700209 * @return false on error, true otherwise
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800210 *
211 * @hide
Kim Schulz0d376052013-08-22 11:18:02 +0200212 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100213 @UnsupportedAppUsage
Kim Schulz0d376052013-08-22 11:18:02 +0200214 public boolean disconnect(BluetoothDevice device) {
215 if (DBG) log("disconnect(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800216 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700217 if (service != null && isEnabled() && isValidDevice(device)) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700218 try {
Jack He16eeac32017-08-17 12:11:18 -0700219 return service.disconnect(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200220 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700221 Log.e(TAG, Log.getStackTraceString(new Throwable()));
222 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200223 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700224 }
Jack He16eeac32017-08-17 12:11:18 -0700225 if (service == null) Log.w(TAG, "Proxy not attached to service");
Matthew Xiefe3807a2013-07-18 17:31:50 -0700226 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 Hea355e5e2017-08-22 16:06:54 -0700234 *
Matthew Xiefe3807a2013-07-18 17:31:50 -0700235 * @return True if this device might support Map.
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800236 *
237 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -0700238 */
239 public static boolean doesClassMatchSink(BluetoothClass btClass) {
240 // TODO optimize the rule
241 switch (btClass.getDeviceClass()) {
Jack Hea355e5e2017-08-22 16:06:54 -0700242 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 Xiefe3807a2013-07-18 17:31:50 -0700249 }
250 }
251
Kim Schulz0d376052013-08-22 11:18:02 +0200252 /**
253 * Get the list of connected devices. Currently at most one.
254 *
255 * @return list of connected devices
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800256 *
257 * @hide
Kim Schulz0d376052013-08-22 11:18:02 +0200258 */
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800259 @SystemApi
Rahul Sabnis946bf882020-02-25 11:33:43 -0800260 @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800261 public @NonNull List<BluetoothDevice> getConnectedDevices() {
Kim Schulz0d376052013-08-22 11:18:02 +0200262 if (DBG) log("getConnectedDevices()");
Ugo Yud0115462019-03-26 21:38:08 +0800263 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700264 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200265 try {
Jack He16eeac32017-08-17 12:11:18 -0700266 return service.getConnectedDevices();
Kim Schulz0d376052013-08-22 11:18:02 +0200267 } catch (RemoteException e) {
268 Log.e(TAG, Log.getStackTraceString(new Throwable()));
269 return new ArrayList<BluetoothDevice>();
270 }
271 }
Jack He16eeac32017-08-17 12:11:18 -0700272 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200273 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 Sabniseccd5bd2020-01-08 11:08:42 -0800280 *
281 * @hide
Kim Schulz0d376052013-08-22 11:18:02 +0200282 */
283 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
284 if (DBG) log("getDevicesMatchingStates()");
Ugo Yud0115462019-03-26 21:38:08 +0800285 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700286 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200287 try {
Jack He16eeac32017-08-17 12:11:18 -0700288 return service.getDevicesMatchingConnectionStates(states);
Kim Schulz0d376052013-08-22 11:18:02 +0200289 } catch (RemoteException e) {
290 Log.e(TAG, Log.getStackTraceString(new Throwable()));
291 return new ArrayList<BluetoothDevice>();
292 }
293 }
Jack He16eeac32017-08-17 12:11:18 -0700294 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200295 return new ArrayList<BluetoothDevice>();
296 }
297
298 /**
299 * Get connection state of device
300 *
301 * @return device connection state
Rahul Sabniseccd5bd2020-01-08 11:08:42 -0800302 *
303 * @hide
Kim Schulz0d376052013-08-22 11:18:02 +0200304 */
305 public int getConnectionState(BluetoothDevice device) {
306 if (DBG) log("getConnectionState(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800307 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700308 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200309 try {
Jack He16eeac32017-08-17 12:11:18 -0700310 return service.getConnectionState(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200311 } catch (RemoteException e) {
312 Log.e(TAG, Log.getStackTraceString(new Throwable()));
313 return BluetoothProfile.STATE_DISCONNECTED;
314 }
315 }
Jack He16eeac32017-08-17 12:11:18 -0700316 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200317 return BluetoothProfile.STATE_DISCONNECTED;
318 }
319
320 /**
321 * Set priority of the profile
322 *
323 * <p> The device should already be paired.
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800324 * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF},
Kim Schulz0d376052013-08-22 11:18:02 +0200325 *
326 * @param device Paired bluetooth device
327 * @param priority
328 * @return true if priority is set, false on error
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800329 * @hide
Kim Schulz0d376052013-08-22 11:18:02 +0200330 */
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800331 @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
Kim Schulz0d376052013-08-22 11:18:02 +0200332 public boolean setPriority(BluetoothDevice device, int priority) {
333 if (DBG) log("setPriority(" + device + ", " + priority + ")");
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800334 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 Sabnis30c597f2020-01-22 14:26:35 -0800351 public boolean setConnectionPolicy(@Nullable BluetoothDevice device,
352 @ConnectionPolicy int connectionPolicy) {
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800353 if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800354 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700355 if (service != null && isEnabled() && isValidDevice(device)) {
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800356 if (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
357 && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
Jack Hea355e5e2017-08-22 16:06:54 -0700358 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200359 }
360 try {
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800361 return service.setConnectionPolicy(device, connectionPolicy);
Kim Schulz0d376052013-08-22 11:18:02 +0200362 } catch (RemoteException e) {
363 Log.e(TAG, Log.getStackTraceString(new Throwable()));
364 return false;
365 }
366 }
Jack He16eeac32017-08-17 12:11:18 -0700367 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200368 return false;
369 }
370
371 /**
372 * Get the priority of the profile.
373 *
374 * <p> The priority can be any of:
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800375 * {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
Kim Schulz0d376052013-08-22 11:18:02 +0200376 *
377 * @param device Bluetooth device
378 * @return priority of the device
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800379 * @hide
Kim Schulz0d376052013-08-22 11:18:02 +0200380 */
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800381 @RequiresPermission(Manifest.permission.BLUETOOTH)
Kim Schulz0d376052013-08-22 11:18:02 +0200382 public int getPriority(BluetoothDevice device) {
383 if (VDBG) log("getPriority(" + device + ")");
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800384 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 Sabnis30c597f2020-01-22 14:26:35 -0800400 public @ConnectionPolicy int getConnectionPolicy(@Nullable BluetoothDevice device) {
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800401 if (VDBG) log("getConnectionPolicy(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800402 final IBluetoothMap service = getService();
Jack He16eeac32017-08-17 12:11:18 -0700403 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200404 try {
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800405 return service.getConnectionPolicy(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200406 } catch (RemoteException e) {
407 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800408 return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
Kim Schulz0d376052013-08-22 11:18:02 +0200409 }
410 }
Jack He16eeac32017-08-17 12:11:18 -0700411 if (service == null) Log.w(TAG, "Proxy not attached to service");
Rahul Sabnisdf1ef4a2019-11-27 18:09:33 -0800412 return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
Kim Schulz0d376052013-08-22 11:18:02 +0200413 }
414
Matthew Xiefe3807a2013-07-18 17:31:50 -0700415 private static void log(String msg) {
416 Log.d(TAG, msg);
417 }
Kim Schulz0d376052013-08-22 11:18:02 +0200418
Jack Hea355e5e2017-08-22 16:06:54 -0700419 private boolean isEnabled() {
Kim Schulz0d376052013-08-22 11:18:02 +0200420 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 He16eeac32017-08-17 12:11:18 -0700425 private static boolean isValidDevice(BluetoothDevice device) {
426 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Kim Schulz0d376052013-08-22 11:18:02 +0200427 }
428
Matthew Xiefe3807a2013-07-18 17:31:50 -0700429}