1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
*Copyright (c) 2020, The Linux Foundation. All rights reserved.
*Not a contribution
*/
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.bluetooth;
import android.annotation.RequiresPermission;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.content.AttributionSource;
import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* This class provides the public APIs to control the Bluetooth VCP profile.
*
* <p>BluetoothVcp is a proxy object for controlling the Bluetooth VolumeControl
* Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get
* the BluetoothVcp proxy object.
*
* {@hide}
*/
public final class BluetoothVcp implements BluetoothProfile {
private static final String TAG = "BluetoothVcp";
private static final boolean DBG = true;
private static final boolean VDBG = true;
/**
* Intent used to broadcast the change in connection state of the VCP
* profile.
*
* <p>This intent will have 3 extras:
* <ul>
* <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
* <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* </ul>
*
* <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
* {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
* {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
* receive.
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.vcp.profile.action.CONNECTION_STATE_CHANGED";
/**
* Intent used to broadcast the volume change of the Volume Renderer device
*
* <p>This intent will have 1 extras:
* <ul>
* <li> {@link #EXTRA_VOLUME} - Current volume settings of Renderer device
* device. Range: 0 - 255.
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public static final String ACTION_VOLUME_CHANGED =
"android.bluetooth.vcp.profile.action.VOLUME_CHANGED";
/**
* A int extra field in {@link #ACTION_VOLUME_CHANGED}
* intents that contains the volume of the Volume Renderer device.
*/
public static final String EXTRA_VOLUME =
"android.bluetooth.vcp.profile.extra.VOLUME";
/**
* Intent used to broadcast the mute change of the Volume Renderer device
*
* <p>This intent will have 1 extras:
* <ul>
* <li> {@link #EXTRA_MUTE} - Current mute status of Renderer device
* device. False: unmute, True: mute.
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public static final String ACTION_MUTE_CHANGED =
"android.bluetooth.vcp.profile.action.MUTE_CHANGED";
/**
* A boolean extra field in {@link #ACTION_MUTE_CHANGED}
* intents that contains the mute status of the Volume Renderer device.
*/
public static final String EXTRA_MUTE =
"android.bluetooth.vcp.profile.extra.MUTE";
/**
* Intent used to broadcast the connection mode change of the VCP
*
* <p>This intent will have 1 extras:
* <ul>
* <li> {@link #EXTRA_MODE} - Current connection mode of VCP
* can be any of {@link #MODE_NONE}, {@link #MODE_UNICAST},
* {@link #MODE_BROADCAST}, {@link #MODE_UNICAST_BROADCAST},
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public static final String ACTION_CONNECTION_MODE_CHANGED =
"android.bluetooth.vcp.profile.action.CONNECTION_MODE_CHANGED";
/**
* A int extra field in {@link #ACTION_CONNECTION_MODE_CHANGED}
* intents that contains the connection mode of the VCP.
*/
public static final String EXTRA_MODE =
"android.bluetooth.vcp.profile.extra.MODE";
/** None VCP connection */
public static final int MODE_NONE = 0x00;
/** VCP connection setup with unicast mode */
public static final int MODE_UNICAST = 0x01;
/** VCP connection setup with broadcast mode */
public static final int MODE_BROADCAST = 0x02;
/** VCP connection setup with unicast and broadcast mode */
public static final int MODE_UNICAST_BROADCAST = 0x03;
public static final int A2DP = 0x0001;
public static final int HFP = 0x0002;
public static final int LE_MEDIA = 0x0010;
public static final int LE_VOICE = 0x2000;
public static final int CALL_STREAM = 0;
public static final int MEDIA_STREAM = 1;
private BluetoothAdapter mAdapter;
private final AttributionSource mAttributionSource;
private final BluetoothProfileConnector<IBluetoothVcp> mProfileConnector =
new BluetoothProfileConnector(this, BluetoothProfile.VCP,
"BluetoothVcp", IBluetoothVcp.class.getName()) {
@Override
public IBluetoothVcp getServiceInterface(IBinder service) {
return IBluetoothVcp.Stub.asInterface(
Binder.allowBlocking(service));
}
};
/**
* Create a BluetoothVcp proxy object for interacting with the local
* Bluetooth VCP service.
*/
/*package*/ BluetoothVcp(Context context, ServiceListener listener) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mProfileConnector.connect(context, listener);
mAttributionSource = mAdapter.getAttributionSource();
}
/*package*/ void close() {
mProfileConnector.disconnect();
}
private IBluetoothVcp getService() {
return mProfileConnector.getService();
}
@Override
public void finalize() {
close();
}
/**
* {@inheritDoc}
*/
@Override
public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()");
return null;
}
/**
* {@inheritDoc}
*/
@Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()");
return null;
}
/**
* {@inheritDoc}
*/
@Override
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getConnectionState(" + device + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.getConnectionState(device, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED;
}
/**
* Get current VCP Connection mode
*
* @param device: remote device instance
* @return current connection mode of VCP:
* {@link #BluetoothVcp.MODE_NONE} if none VCP connection
* {@link #BluetoothVcp.MODE_UNICAST} if VCP is connected for unicast
* {@link #BluetoothVcp.MODE_BROADCAST} if VCP is connected for broadcast
* {@link #BluetoothVcp.MODE_UNICAST_BROADCAST} if VCP
* is connected for unicast and broadcast
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getConnectionMode(BluetoothDevice device) {
if (VDBG) log("getConnectionMode(" + device + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.getConnectionMode(device, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return MODE_NONE;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return MODE_NONE;
}
/**
* Set absolute volume to remote device via VCP connection
*
* @param device: remote device instance
* @prarm volume: requested volume settings for remote device
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public void setAbsoluteVolume(BluetoothDevice device, int volume) {
if (VDBG) log("setAbsoluteVolume(" + device + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
service.setAbsoluteVolume(device, volume, mAttributionSource);
return;
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
}
/**
* Get current absolute volume of the remote device
*
* @param device: remote device instance
* @return current absolute volume of the remote device
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getAbsoluteVolume(BluetoothDevice device) {
if (VDBG) log("getAbsoluteVolume(" + device + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.getAbsoluteVolume(device, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return -1;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return -1;
}
/**
* Mute or unmute remote device via VCP connection
*
* @param device: remote device instance
* @prarm enableMute: true if mute, false if unmute
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public void setMute(BluetoothDevice device, boolean enableMute) {
if (VDBG) log("setMute(" + device + ")" +" enableMute: " + enableMute);
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
service.setMute(device, enableMute, mAttributionSource);
return;
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
}
/**
* Get mute status of remote device
*
* @param device: remote device instance
* @return current mute status of the remote device
* true if mute status, false if unmute status
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean isMute(BluetoothDevice device) {
if (VDBG) log("isMute(" + device + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.isMute(device, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* set active stream for a DuMo device
*
* @param device: remote device instance
* @param audioType: call/media audio
* @param profile: profile that is needed to be active
* @return success/failure
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean setActiveProfile(BluetoothDevice device, int audioType, int profile) {
if (VDBG) log("setActiveProfile(" + device + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled() && isValidDevice(device)) {
try {
return service.setActiveProfile(device, audioType, profile, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
/**
* set active stream for a DuMo device
*
* @param audioType: call/media audio
* @return ID of current active profile
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getActiveProfile(int audioType) {
if (VDBG) log("getActiveProfile(" + audioType + ")");
final IBluetoothVcp service =
getService();
if (service != null && isEnabled()) {
try {
return service.getActiveProfile(audioType, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return -1;
}
}
if (service == null) Log.w(TAG, "Proxy not attached to service");
return -1;
}
private boolean isEnabled() {
return mAdapter.getState() == BluetoothAdapter.STATE_ON;
}
private static boolean isValidDevice(BluetoothDevice device) {
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
}
private static void log(String msg) {
Log.d(TAG, msg);
}
}
|