blob: b82680c1cae99d8b0404cf51bff612d7df34ff6d [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
Mathew Inwood4dc66d32018-08-01 15:07:20 +010019import android.annotation.UnsupportedAppUsage;
Matthew Xiefe3807a2013-07-18 17:31:50 -070020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
Jack Hea355e5e2017-08-22 16:06:54 -070024import android.os.Binder;
25import android.os.IBinder;
26import android.os.RemoteException;
Matthew Xiefe3807a2013-07-18 17:31:50 -070027import android.util.Log;
28
Jack Hea355e5e2017-08-22 16:06:54 -070029import java.util.ArrayList;
30import java.util.List;
31
Matthew Xiefe3807a2013-07-18 17:31:50 -070032/**
33 * This class provides the APIs to control the Bluetooth MAP
34 * Profile.
Jack Hea355e5e2017-08-22 16:06:54 -070035 *
36 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -070037 */
Kim Schulz0d376052013-08-22 11:18:02 +020038public final class BluetoothMap implements BluetoothProfile {
Matthew Xiefe3807a2013-07-18 17:31:50 -070039
40 private static final String TAG = "BluetoothMap";
41 private static final boolean DBG = true;
42 private static final boolean VDBG = false;
43
Kim Schulz0d376052013-08-22 11:18:02 +020044 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070045 "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED";
Matthew Xiefe3807a2013-07-18 17:31:50 -070046
Jack He16eeac32017-08-17 12:11:18 -070047 private volatile IBluetoothMap mService;
Matthew Xiefe3807a2013-07-18 17:31:50 -070048 private final Context mContext;
49 private ServiceListener mServiceListener;
50 private BluetoothAdapter mAdapter;
51
52 /** There was an error trying to obtain the state */
Jack Hea355e5e2017-08-22 16:06:54 -070053 public static final int STATE_ERROR = -1;
Matthew Xiefe3807a2013-07-18 17:31:50 -070054
55 public static final int RESULT_FAILURE = 0;
56 public static final int RESULT_SUCCESS = 1;
57 /** Connection canceled before completion. */
58 public static final int RESULT_CANCELED = 2;
59
Jack He2992cd02017-08-22 21:21:23 -070060 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
Matthew Xiefe3807a2013-07-18 17:31:50 -070061 new IBluetoothStateChangeCallback.Stub() {
62 public void onBluetoothStateChange(boolean up) {
63 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
64 if (!up) {
Jack Hea355e5e2017-08-22 16:06:54 -070065 if (VDBG) Log.d(TAG, "Unbinding service...");
Matthew Xiefe3807a2013-07-18 17:31:50 -070066 synchronized (mConnection) {
67 try {
68 mService = null;
69 mContext.unbindService(mConnection);
70 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -070071 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -070072 }
73 }
74 } else {
75 synchronized (mConnection) {
76 try {
77 if (mService == null) {
Jack Hea355e5e2017-08-22 16:06:54 -070078 if (VDBG) Log.d(TAG, "Binding service...");
Kim Schulz0d376052013-08-22 11:18:02 +020079 doBind();
Matthew Xiefe3807a2013-07-18 17:31:50 -070080 }
81 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -070082 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -070083 }
84 }
85 }
86 }
Jack Hea355e5e2017-08-22 16:06:54 -070087 };
Matthew Xiefe3807a2013-07-18 17:31:50 -070088
89 /**
90 * Create a BluetoothMap proxy object.
91 */
Kim Schulz0d376052013-08-22 11:18:02 +020092 /*package*/ BluetoothMap(Context context, ServiceListener l) {
93 if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
Matthew Xiefe3807a2013-07-18 17:31:50 -070094 mContext = context;
95 mServiceListener = l;
96 mAdapter = BluetoothAdapter.getDefaultAdapter();
97 IBluetoothManager mgr = mAdapter.getBluetoothManager();
98 if (mgr != null) {
99 try {
100 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
101 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700102 Log.e(TAG, "", e);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700103 }
104 }
Kim Schulz0d376052013-08-22 11:18:02 +0200105 doBind();
106 }
107
108 boolean doBind() {
109 Intent intent = new Intent(IBluetoothMap.class.getName());
110 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
111 intent.setComponent(comp);
Dianne Hackborn466ce962014-03-19 18:06:58 -0700112 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
113 android.os.Process.myUserHandle())) {
Kim Schulz0d376052013-08-22 11:18:02 +0200114 Log.e(TAG, "Could not bind to Bluetooth MAP Service with " + intent);
115 return false;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700116 }
Kim Schulz0d376052013-08-22 11:18:02 +0200117 return true;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700118 }
119
120 protected void finalize() throws Throwable {
121 try {
122 close();
123 } finally {
124 super.finalize();
125 }
126 }
127
128 /**
129 * Close the connection to the backing service.
130 * Other public functions of BluetoothMap will return default error
131 * results once close() has been called. Multiple invocations of close()
132 * are ok.
133 */
134 public synchronized void close() {
135 IBluetoothManager mgr = mAdapter.getBluetoothManager();
136 if (mgr != null) {
137 try {
138 mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
139 } catch (Exception e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700140 Log.e(TAG, "", e);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700141 }
142 }
143
144 synchronized (mConnection) {
145 if (mService != null) {
146 try {
147 mService = null;
148 mContext.unbindService(mConnection);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700149 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700150 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700151 }
152 }
153 }
154 mServiceListener = null;
155 }
156
157 /**
158 * Get the current state of the BluetoothMap service.
Jack Hea355e5e2017-08-22 16:06:54 -0700159 *
160 * @return One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not
161 * connected to the Map service.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700162 */
163 public int getState() {
164 if (VDBG) log("getState()");
Jack He16eeac32017-08-17 12:11:18 -0700165 final IBluetoothMap service = mService;
166 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700167 try {
Jack He16eeac32017-08-17 12:11:18 -0700168 return service.getState();
Jack Hea355e5e2017-08-22 16:06:54 -0700169 } catch (RemoteException e) {
170 Log.e(TAG, e.toString());
171 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700172 } else {
173 Log.w(TAG, "Proxy not attached to service");
174 if (DBG) log(Log.getStackTraceString(new Throwable()));
175 }
176 return BluetoothMap.STATE_ERROR;
177 }
178
179 /**
180 * Get the currently connected remote Bluetooth device (PCE).
Jack Hea355e5e2017-08-22 16:06:54 -0700181 *
182 * @return The remote Bluetooth device, or null if not in connected or connecting state, or if
183 * this proxy object is not connected to the Map service.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700184 */
185 public BluetoothDevice getClient() {
186 if (VDBG) log("getClient()");
Jack He16eeac32017-08-17 12:11:18 -0700187 final IBluetoothMap service = mService;
188 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700189 try {
Jack He16eeac32017-08-17 12:11:18 -0700190 return service.getClient();
Jack Hea355e5e2017-08-22 16:06:54 -0700191 } catch (RemoteException e) {
192 Log.e(TAG, e.toString());
193 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700194 } else {
195 Log.w(TAG, "Proxy not attached to service");
196 if (DBG) log(Log.getStackTraceString(new Throwable()));
197 }
198 return null;
199 }
200
201 /**
Kim Schulz0d376052013-08-22 11:18:02 +0200202 * Returns true if the specified Bluetooth device is connected.
203 * Returns false if not connected, or if this proxy object is not
204 * currently connected to the Map service.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700205 */
206 public boolean isConnected(BluetoothDevice device) {
207 if (VDBG) log("isConnected(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700208 final IBluetoothMap service = mService;
209 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700210 try {
Jack He16eeac32017-08-17 12:11:18 -0700211 return service.isConnected(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700212 } catch (RemoteException e) {
213 Log.e(TAG, e.toString());
214 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700215 } else {
216 Log.w(TAG, "Proxy not attached to service");
217 if (DBG) log(Log.getStackTraceString(new Throwable()));
218 }
219 return false;
220 }
221
222 /**
Kim Schulz0d376052013-08-22 11:18:02 +0200223 * Initiate connection. Initiation of outgoing connections is not
224 * supported for MAP server.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700225 */
Kim Schulz0d376052013-08-22 11:18:02 +0200226 public boolean connect(BluetoothDevice device) {
227 if (DBG) log("connect(" + device + ")" + "not supported for MAPS");
228 return false;
229 }
230
231 /**
232 * Initiate disconnect.
233 *
234 * @param device Remote Bluetooth Device
Jack Hea355e5e2017-08-22 16:06:54 -0700235 * @return false on error, true otherwise
Kim Schulz0d376052013-08-22 11:18:02 +0200236 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100237 @UnsupportedAppUsage
Kim Schulz0d376052013-08-22 11:18:02 +0200238 public boolean disconnect(BluetoothDevice device) {
239 if (DBG) log("disconnect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700240 final IBluetoothMap service = mService;
241 if (service != null && isEnabled() && isValidDevice(device)) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700242 try {
Jack He16eeac32017-08-17 12:11:18 -0700243 return service.disconnect(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200244 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700245 Log.e(TAG, Log.getStackTraceString(new Throwable()));
246 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200247 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700248 }
Jack He16eeac32017-08-17 12:11:18 -0700249 if (service == null) Log.w(TAG, "Proxy not attached to service");
Matthew Xiefe3807a2013-07-18 17:31:50 -0700250 return false;
251 }
252
253 /**
254 * Check class bits for possible Map support.
255 * This is a simple heuristic that tries to guess if a device with the
256 * given class bits might support Map. It is not accurate for all
257 * devices. It tries to err on the side of false positives.
Jack Hea355e5e2017-08-22 16:06:54 -0700258 *
Matthew Xiefe3807a2013-07-18 17:31:50 -0700259 * @return True if this device might support Map.
260 */
261 public static boolean doesClassMatchSink(BluetoothClass btClass) {
262 // TODO optimize the rule
263 switch (btClass.getDeviceClass()) {
Jack Hea355e5e2017-08-22 16:06:54 -0700264 case BluetoothClass.Device.COMPUTER_DESKTOP:
265 case BluetoothClass.Device.COMPUTER_LAPTOP:
266 case BluetoothClass.Device.COMPUTER_SERVER:
267 case BluetoothClass.Device.COMPUTER_UNCATEGORIZED:
268 return true;
269 default:
270 return false;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700271 }
272 }
273
Kim Schulz0d376052013-08-22 11:18:02 +0200274 /**
275 * Get the list of connected devices. Currently at most one.
276 *
277 * @return list of connected devices
278 */
279 public List<BluetoothDevice> getConnectedDevices() {
280 if (DBG) log("getConnectedDevices()");
Jack He16eeac32017-08-17 12:11:18 -0700281 final IBluetoothMap service = mService;
282 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200283 try {
Jack He16eeac32017-08-17 12:11:18 -0700284 return service.getConnectedDevices();
Kim Schulz0d376052013-08-22 11:18:02 +0200285 } catch (RemoteException e) {
286 Log.e(TAG, Log.getStackTraceString(new Throwable()));
287 return new ArrayList<BluetoothDevice>();
288 }
289 }
Jack He16eeac32017-08-17 12:11:18 -0700290 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200291 return new ArrayList<BluetoothDevice>();
292 }
293
294 /**
295 * Get the list of devices matching specified states. Currently at most one.
296 *
297 * @return list of matching devices
298 */
299 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
300 if (DBG) log("getDevicesMatchingStates()");
Jack He16eeac32017-08-17 12:11:18 -0700301 final IBluetoothMap service = mService;
302 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200303 try {
Jack He16eeac32017-08-17 12:11:18 -0700304 return service.getDevicesMatchingConnectionStates(states);
Kim Schulz0d376052013-08-22 11:18:02 +0200305 } catch (RemoteException e) {
306 Log.e(TAG, Log.getStackTraceString(new Throwable()));
307 return new ArrayList<BluetoothDevice>();
308 }
309 }
Jack He16eeac32017-08-17 12:11:18 -0700310 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200311 return new ArrayList<BluetoothDevice>();
312 }
313
314 /**
315 * Get connection state of device
316 *
317 * @return device connection state
318 */
319 public int getConnectionState(BluetoothDevice device) {
320 if (DBG) log("getConnectionState(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700321 final IBluetoothMap service = mService;
322 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200323 try {
Jack He16eeac32017-08-17 12:11:18 -0700324 return service.getConnectionState(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200325 } catch (RemoteException e) {
326 Log.e(TAG, Log.getStackTraceString(new Throwable()));
327 return BluetoothProfile.STATE_DISCONNECTED;
328 }
329 }
Jack He16eeac32017-08-17 12:11:18 -0700330 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200331 return BluetoothProfile.STATE_DISCONNECTED;
332 }
333
334 /**
335 * Set priority of the profile
336 *
337 * <p> The device should already be paired.
Jack Hea355e5e2017-08-22 16:06:54 -0700338 * Priority can be one of {@link #PRIORITY_ON} or
Kim Schulz0d376052013-08-22 11:18:02 +0200339 * {@link #PRIORITY_OFF},
340 *
341 * @param device Paired bluetooth device
342 * @param priority
343 * @return true if priority is set, false on error
344 */
345 public boolean setPriority(BluetoothDevice device, int priority) {
346 if (DBG) log("setPriority(" + device + ", " + priority + ")");
Jack He16eeac32017-08-17 12:11:18 -0700347 final IBluetoothMap service = mService;
348 if (service != null && isEnabled() && isValidDevice(device)) {
Jack He2992cd02017-08-22 21:21:23 -0700349 if (priority != BluetoothProfile.PRIORITY_OFF
350 && priority != BluetoothProfile.PRIORITY_ON) {
Jack Hea355e5e2017-08-22 16:06:54 -0700351 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200352 }
353 try {
Jack He16eeac32017-08-17 12:11:18 -0700354 return service.setPriority(device, priority);
Kim Schulz0d376052013-08-22 11:18:02 +0200355 } catch (RemoteException e) {
356 Log.e(TAG, Log.getStackTraceString(new Throwable()));
357 return false;
358 }
359 }
Jack He16eeac32017-08-17 12:11:18 -0700360 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200361 return false;
362 }
363
364 /**
365 * Get the priority of the profile.
366 *
367 * <p> The priority can be any of:
368 * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
369 * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
370 *
371 * @param device Bluetooth device
372 * @return priority of the device
373 */
374 public int getPriority(BluetoothDevice device) {
375 if (VDBG) log("getPriority(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700376 final IBluetoothMap service = mService;
377 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200378 try {
Jack He16eeac32017-08-17 12:11:18 -0700379 return service.getPriority(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200380 } catch (RemoteException e) {
381 Log.e(TAG, Log.getStackTraceString(new Throwable()));
382 return PRIORITY_OFF;
383 }
384 }
Jack He16eeac32017-08-17 12:11:18 -0700385 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200386 return PRIORITY_OFF;
387 }
388
Matthew Xie9b693992013-10-10 11:21:40 -0700389 private final ServiceConnection mConnection = new ServiceConnection() {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700390 public void onServiceConnected(ComponentName className, IBinder service) {
391 if (DBG) log("Proxy object connected");
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600392 mService = IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service));
Matthew Xiefe3807a2013-07-18 17:31:50 -0700393 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200394 mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700395 }
396 }
Jack Hea355e5e2017-08-22 16:06:54 -0700397
Matthew Xiefe3807a2013-07-18 17:31:50 -0700398 public void onServiceDisconnected(ComponentName className) {
399 if (DBG) log("Proxy object disconnected");
400 mService = null;
401 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200402 mServiceListener.onServiceDisconnected(BluetoothProfile.MAP);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700403 }
404 }
405 };
406
407 private static void log(String msg) {
408 Log.d(TAG, msg);
409 }
Kim Schulz0d376052013-08-22 11:18:02 +0200410
Jack Hea355e5e2017-08-22 16:06:54 -0700411 private boolean isEnabled() {
Kim Schulz0d376052013-08-22 11:18:02 +0200412 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
413 if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
414 log("Bluetooth is Not enabled");
415 return false;
416 }
Jack He16eeac32017-08-17 12:11:18 -0700417 private static boolean isValidDevice(BluetoothDevice device) {
418 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Kim Schulz0d376052013-08-22 11:18:02 +0200419 }
420
Matthew Xiefe3807a2013-07-18 17:31:50 -0700421}