blob: fc5f830a89402c63282a8f3cc4492f63d20f0661 [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 Inwood7acad5e2018-08-01 15:00:35 +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;
jovanak2a9131f2018-10-15 17:50:19 -070027import android.os.UserHandle;
Matthew Xiefe3807a2013-07-18 17:31:50 -070028import android.util.Log;
29
Jack Hea355e5e2017-08-22 16:06:54 -070030import java.util.ArrayList;
31import java.util.List;
32
Matthew Xiefe3807a2013-07-18 17:31:50 -070033/**
34 * This class provides the APIs to control the Bluetooth MAP
35 * Profile.
Jack Hea355e5e2017-08-22 16:06:54 -070036 *
37 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -070038 */
Kim Schulz0d376052013-08-22 11:18:02 +020039public final class BluetoothMap implements BluetoothProfile {
Matthew Xiefe3807a2013-07-18 17:31:50 -070040
41 private static final String TAG = "BluetoothMap";
42 private static final boolean DBG = true;
43 private static final boolean VDBG = false;
44
Kim Schulz0d376052013-08-22 11:18:02 +020045 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070046 "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED";
Matthew Xiefe3807a2013-07-18 17:31:50 -070047
Jack He16eeac32017-08-17 12:11:18 -070048 private volatile IBluetoothMap mService;
Matthew Xiefe3807a2013-07-18 17:31:50 -070049 private final Context mContext;
50 private ServiceListener mServiceListener;
51 private BluetoothAdapter mAdapter;
52
53 /** There was an error trying to obtain the state */
Jack Hea355e5e2017-08-22 16:06:54 -070054 public static final int STATE_ERROR = -1;
Matthew Xiefe3807a2013-07-18 17:31:50 -070055
56 public static final int RESULT_FAILURE = 0;
57 public static final int RESULT_SUCCESS = 1;
58 /** Connection canceled before completion. */
59 public static final int RESULT_CANCELED = 2;
60
Jack He2992cd02017-08-22 21:21:23 -070061 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
Matthew Xiefe3807a2013-07-18 17:31:50 -070062 new IBluetoothStateChangeCallback.Stub() {
63 public void onBluetoothStateChange(boolean up) {
64 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
65 if (!up) {
Jack Hea355e5e2017-08-22 16:06:54 -070066 if (VDBG) Log.d(TAG, "Unbinding service...");
Matthew Xiefe3807a2013-07-18 17:31:50 -070067 synchronized (mConnection) {
68 try {
69 mService = null;
70 mContext.unbindService(mConnection);
71 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -070072 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -070073 }
74 }
75 } else {
76 synchronized (mConnection) {
77 try {
78 if (mService == null) {
Jack Hea355e5e2017-08-22 16:06:54 -070079 if (VDBG) Log.d(TAG, "Binding service...");
Kim Schulz0d376052013-08-22 11:18:02 +020080 doBind();
Matthew Xiefe3807a2013-07-18 17:31:50 -070081 }
82 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -070083 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -070084 }
85 }
86 }
87 }
Jack Hea355e5e2017-08-22 16:06:54 -070088 };
Matthew Xiefe3807a2013-07-18 17:31:50 -070089
90 /**
91 * Create a BluetoothMap proxy object.
92 */
Kim Schulz0d376052013-08-22 11:18:02 +020093 /*package*/ BluetoothMap(Context context, ServiceListener l) {
94 if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
Matthew Xiefe3807a2013-07-18 17:31:50 -070095 mContext = context;
96 mServiceListener = l;
97 mAdapter = BluetoothAdapter.getDefaultAdapter();
98 IBluetoothManager mgr = mAdapter.getBluetoothManager();
99 if (mgr != null) {
100 try {
101 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
102 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700103 Log.e(TAG, "", e);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700104 }
105 }
Kim Schulz0d376052013-08-22 11:18:02 +0200106 doBind();
107 }
108
109 boolean doBind() {
110 Intent intent = new Intent(IBluetoothMap.class.getName());
111 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
112 intent.setComponent(comp);
Dianne Hackborn466ce962014-03-19 18:06:58 -0700113 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
jovanak2a9131f2018-10-15 17:50:19 -0700114 UserHandle.CURRENT_OR_SELF)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200115 Log.e(TAG, "Could not bind to Bluetooth MAP Service with " + intent);
116 return false;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700117 }
Kim Schulz0d376052013-08-22 11:18:02 +0200118 return true;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700119 }
120
121 protected void finalize() throws Throwable {
122 try {
123 close();
124 } finally {
125 super.finalize();
126 }
127 }
128
129 /**
130 * Close the connection to the backing service.
131 * Other public functions of BluetoothMap will return default error
132 * results once close() has been called. Multiple invocations of close()
133 * are ok.
134 */
135 public synchronized void close() {
136 IBluetoothManager mgr = mAdapter.getBluetoothManager();
137 if (mgr != null) {
138 try {
139 mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
140 } catch (Exception e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700141 Log.e(TAG, "", e);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700142 }
143 }
144
145 synchronized (mConnection) {
146 if (mService != null) {
147 try {
148 mService = null;
149 mContext.unbindService(mConnection);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700150 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700151 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700152 }
153 }
154 }
155 mServiceListener = null;
156 }
157
158 /**
159 * Get the current state of the BluetoothMap service.
Jack Hea355e5e2017-08-22 16:06:54 -0700160 *
161 * @return One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not
162 * connected to the Map service.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700163 */
164 public int getState() {
165 if (VDBG) log("getState()");
Jack He16eeac32017-08-17 12:11:18 -0700166 final IBluetoothMap service = mService;
167 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700168 try {
Jack He16eeac32017-08-17 12:11:18 -0700169 return service.getState();
Jack Hea355e5e2017-08-22 16:06:54 -0700170 } catch (RemoteException e) {
171 Log.e(TAG, e.toString());
172 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700173 } else {
174 Log.w(TAG, "Proxy not attached to service");
175 if (DBG) log(Log.getStackTraceString(new Throwable()));
176 }
177 return BluetoothMap.STATE_ERROR;
178 }
179
180 /**
181 * Get the currently connected remote Bluetooth device (PCE).
Jack Hea355e5e2017-08-22 16:06:54 -0700182 *
183 * @return The remote Bluetooth device, or null if not in connected or connecting state, or if
184 * this proxy object is not connected to the Map service.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700185 */
186 public BluetoothDevice getClient() {
187 if (VDBG) log("getClient()");
Jack He16eeac32017-08-17 12:11:18 -0700188 final IBluetoothMap service = mService;
189 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700190 try {
Jack He16eeac32017-08-17 12:11:18 -0700191 return service.getClient();
Jack Hea355e5e2017-08-22 16:06:54 -0700192 } catch (RemoteException e) {
193 Log.e(TAG, e.toString());
194 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700195 } else {
196 Log.w(TAG, "Proxy not attached to service");
197 if (DBG) log(Log.getStackTraceString(new Throwable()));
198 }
199 return null;
200 }
201
202 /**
Kim Schulz0d376052013-08-22 11:18:02 +0200203 * Returns true if the specified Bluetooth device is connected.
204 * Returns false if not connected, or if this proxy object is not
205 * currently connected to the Map service.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700206 */
207 public boolean isConnected(BluetoothDevice device) {
208 if (VDBG) log("isConnected(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700209 final IBluetoothMap service = mService;
210 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700211 try {
Jack He16eeac32017-08-17 12:11:18 -0700212 return service.isConnected(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700213 } catch (RemoteException e) {
214 Log.e(TAG, e.toString());
215 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700216 } else {
217 Log.w(TAG, "Proxy not attached to service");
218 if (DBG) log(Log.getStackTraceString(new Throwable()));
219 }
220 return false;
221 }
222
223 /**
Kim Schulz0d376052013-08-22 11:18:02 +0200224 * Initiate connection. Initiation of outgoing connections is not
225 * supported for MAP server.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700226 */
Kim Schulz0d376052013-08-22 11:18:02 +0200227 public boolean connect(BluetoothDevice device) {
228 if (DBG) log("connect(" + device + ")" + "not supported for MAPS");
229 return false;
230 }
231
232 /**
233 * Initiate disconnect.
234 *
235 * @param device Remote Bluetooth Device
Jack Hea355e5e2017-08-22 16:06:54 -0700236 * @return false on error, true otherwise
Kim Schulz0d376052013-08-22 11:18:02 +0200237 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100238 @UnsupportedAppUsage
Kim Schulz0d376052013-08-22 11:18:02 +0200239 public boolean disconnect(BluetoothDevice device) {
240 if (DBG) log("disconnect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700241 final IBluetoothMap service = mService;
242 if (service != null && isEnabled() && isValidDevice(device)) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700243 try {
Jack He16eeac32017-08-17 12:11:18 -0700244 return service.disconnect(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200245 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700246 Log.e(TAG, Log.getStackTraceString(new Throwable()));
247 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200248 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700249 }
Jack He16eeac32017-08-17 12:11:18 -0700250 if (service == null) Log.w(TAG, "Proxy not attached to service");
Matthew Xiefe3807a2013-07-18 17:31:50 -0700251 return false;
252 }
253
254 /**
255 * Check class bits for possible Map support.
256 * This is a simple heuristic that tries to guess if a device with the
257 * given class bits might support Map. It is not accurate for all
258 * devices. It tries to err on the side of false positives.
Jack Hea355e5e2017-08-22 16:06:54 -0700259 *
Matthew Xiefe3807a2013-07-18 17:31:50 -0700260 * @return True if this device might support Map.
261 */
262 public static boolean doesClassMatchSink(BluetoothClass btClass) {
263 // TODO optimize the rule
264 switch (btClass.getDeviceClass()) {
Jack Hea355e5e2017-08-22 16:06:54 -0700265 case BluetoothClass.Device.COMPUTER_DESKTOP:
266 case BluetoothClass.Device.COMPUTER_LAPTOP:
267 case BluetoothClass.Device.COMPUTER_SERVER:
268 case BluetoothClass.Device.COMPUTER_UNCATEGORIZED:
269 return true;
270 default:
271 return false;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700272 }
273 }
274
Kim Schulz0d376052013-08-22 11:18:02 +0200275 /**
276 * Get the list of connected devices. Currently at most one.
277 *
278 * @return list of connected devices
279 */
280 public List<BluetoothDevice> getConnectedDevices() {
281 if (DBG) log("getConnectedDevices()");
Jack He16eeac32017-08-17 12:11:18 -0700282 final IBluetoothMap service = mService;
283 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200284 try {
Jack He16eeac32017-08-17 12:11:18 -0700285 return service.getConnectedDevices();
Kim Schulz0d376052013-08-22 11:18:02 +0200286 } catch (RemoteException e) {
287 Log.e(TAG, Log.getStackTraceString(new Throwable()));
288 return new ArrayList<BluetoothDevice>();
289 }
290 }
Jack He16eeac32017-08-17 12:11:18 -0700291 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200292 return new ArrayList<BluetoothDevice>();
293 }
294
295 /**
296 * Get the list of devices matching specified states. Currently at most one.
297 *
298 * @return list of matching devices
299 */
300 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
301 if (DBG) log("getDevicesMatchingStates()");
Jack He16eeac32017-08-17 12:11:18 -0700302 final IBluetoothMap service = mService;
303 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200304 try {
Jack He16eeac32017-08-17 12:11:18 -0700305 return service.getDevicesMatchingConnectionStates(states);
Kim Schulz0d376052013-08-22 11:18:02 +0200306 } catch (RemoteException e) {
307 Log.e(TAG, Log.getStackTraceString(new Throwable()));
308 return new ArrayList<BluetoothDevice>();
309 }
310 }
Jack He16eeac32017-08-17 12:11:18 -0700311 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200312 return new ArrayList<BluetoothDevice>();
313 }
314
315 /**
316 * Get connection state of device
317 *
318 * @return device connection state
319 */
320 public int getConnectionState(BluetoothDevice device) {
321 if (DBG) log("getConnectionState(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700322 final IBluetoothMap service = mService;
323 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200324 try {
Jack He16eeac32017-08-17 12:11:18 -0700325 return service.getConnectionState(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200326 } catch (RemoteException e) {
327 Log.e(TAG, Log.getStackTraceString(new Throwable()));
328 return BluetoothProfile.STATE_DISCONNECTED;
329 }
330 }
Jack He16eeac32017-08-17 12:11:18 -0700331 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200332 return BluetoothProfile.STATE_DISCONNECTED;
333 }
334
335 /**
336 * Set priority of the profile
337 *
338 * <p> The device should already be paired.
Jack Hea355e5e2017-08-22 16:06:54 -0700339 * Priority can be one of {@link #PRIORITY_ON} or
Kim Schulz0d376052013-08-22 11:18:02 +0200340 * {@link #PRIORITY_OFF},
341 *
342 * @param device Paired bluetooth device
343 * @param priority
344 * @return true if priority is set, false on error
345 */
346 public boolean setPriority(BluetoothDevice device, int priority) {
347 if (DBG) log("setPriority(" + device + ", " + priority + ")");
Jack He16eeac32017-08-17 12:11:18 -0700348 final IBluetoothMap service = mService;
349 if (service != null && isEnabled() && isValidDevice(device)) {
Jack He2992cd02017-08-22 21:21:23 -0700350 if (priority != BluetoothProfile.PRIORITY_OFF
351 && priority != BluetoothProfile.PRIORITY_ON) {
Jack Hea355e5e2017-08-22 16:06:54 -0700352 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200353 }
354 try {
Jack He16eeac32017-08-17 12:11:18 -0700355 return service.setPriority(device, priority);
Kim Schulz0d376052013-08-22 11:18:02 +0200356 } catch (RemoteException e) {
357 Log.e(TAG, Log.getStackTraceString(new Throwable()));
358 return false;
359 }
360 }
Jack He16eeac32017-08-17 12:11:18 -0700361 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200362 return false;
363 }
364
365 /**
366 * Get the priority of the profile.
367 *
368 * <p> The priority can be any of:
369 * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
370 * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
371 *
372 * @param device Bluetooth device
373 * @return priority of the device
374 */
375 public int getPriority(BluetoothDevice device) {
376 if (VDBG) log("getPriority(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700377 final IBluetoothMap service = mService;
378 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200379 try {
Jack He16eeac32017-08-17 12:11:18 -0700380 return service.getPriority(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200381 } catch (RemoteException e) {
382 Log.e(TAG, Log.getStackTraceString(new Throwable()));
383 return PRIORITY_OFF;
384 }
385 }
Jack He16eeac32017-08-17 12:11:18 -0700386 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200387 return PRIORITY_OFF;
388 }
389
Matthew Xie9b693992013-10-10 11:21:40 -0700390 private final ServiceConnection mConnection = new ServiceConnection() {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700391 public void onServiceConnected(ComponentName className, IBinder service) {
392 if (DBG) log("Proxy object connected");
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600393 mService = IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service));
Matthew Xiefe3807a2013-07-18 17:31:50 -0700394 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200395 mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700396 }
397 }
Jack Hea355e5e2017-08-22 16:06:54 -0700398
Matthew Xiefe3807a2013-07-18 17:31:50 -0700399 public void onServiceDisconnected(ComponentName className) {
400 if (DBG) log("Proxy object disconnected");
401 mService = null;
402 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200403 mServiceListener.onServiceDisconnected(BluetoothProfile.MAP);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700404 }
405 }
406 };
407
408 private static void log(String msg) {
409 Log.d(TAG, msg);
410 }
Kim Schulz0d376052013-08-22 11:18:02 +0200411
Jack Hea355e5e2017-08-22 16:06:54 -0700412 private boolean isEnabled() {
Kim Schulz0d376052013-08-22 11:18:02 +0200413 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
414 if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
415 log("Bluetooth is Not enabled");
416 return false;
417 }
Jack He16eeac32017-08-17 12:11:18 -0700418 private static boolean isValidDevice(BluetoothDevice device) {
419 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Kim Schulz0d376052013-08-22 11:18:02 +0200420 }
421
Matthew Xiefe3807a2013-07-18 17:31:50 -0700422}