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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/*
* Copyright (C) 2007 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 com.android.unit_tests;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothIntent;
import android.bluetooth.BluetoothClass;
import android.bluetooth.IBluetoothDeviceCallback;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemProperties;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import junit.framework.Assert;
import java.util.List;
import java.util.HashSet;
public class BluetoothTest extends AndroidTestCase {
private static final String TAG = "BluetoothTest";
@MediumTest
public void testBluetoothSmokeTest() throws Exception {
BluetoothDevice btDevice =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
// TODO: Use a more reliable check to see if this product should
// support Bluetooth - see bug 988521
boolean shouldSupportBluetooth = SystemProperties.get("ro.kernel.qemu").equals("0");
assertFalse(shouldSupportBluetooth && btDevice == null);
if (!shouldSupportBluetooth) {
Log.i(TAG, "Skipping test - this device does not have bluetooth.");
return;
}
boolean bluetoothWasEnabled = btDevice.isEnabled();
if (bluetoothWasEnabled) {
Log.i(TAG, "Bluetooth already enabled");
} else {
Log.i(TAG, "Enabling Bluetooth...");
btDevice.enable();
Log.i(TAG, "Bluetooth enabled");
}
Assert.assertTrue(btDevice.isEnabled());
String myAddress = btDevice.getAddress();
Assert.assertTrue(myAddress != null);
Log.i(TAG, "My Bluetooth Address is " + myAddress);
Assert.assertFalse(myAddress.equals("00:00:00:00:00:00"));
if (!bluetoothWasEnabled) {
Log.i(TAG, "Disabling Bluetooth...");
btDevice.disable();
Log.i(TAG, "Bluetooth disabled");
}
}
private boolean listenA2dp = false;
private void listenA2dp() {
if (!listenA2dp) {
listenA2dp = true;
getContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(BluetoothA2dp.SINK_STATE, -1);
int oldState = intent.getIntExtra(BluetoothA2dp.SINK_PREVIOUS_STATE, -1);
Log.e(TAG, "A2DP INTENT: state = " + state + " oldState = " + oldState);
}
}, new IntentFilter(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION));
}
}
@MediumTest
public void testA2dpSmokeTest() throws Exception {
listenA2dp();
BluetoothA2dp a2dp = new BluetoothA2dp(getContext());
List<String> sinks = a2dp.listConnectedSinks();
Log.e(TAG, "listConnectedSinks()...");
for (String sink : sinks) {
Log.e(TAG, sink + " state = " + a2dp.getSinkState(sink));
}
}
@MediumTest
public void testA2dpConnect() throws Exception {
listenA2dp();
String address = SystemProperties.get("debug.a2dp.address", "<none>");
BluetoothA2dp a2dp = new BluetoothA2dp(getContext());
int result = a2dp.connectSink(address);
Log.e(TAG, "connectSink(" + address + ") = " + result);
}
@MediumTest
public void testA2dpDisconnect() throws Exception {
listenA2dp();
String address = SystemProperties.get("debug.a2dp.address", "<none>");
BluetoothA2dp a2dp = new BluetoothA2dp(getContext());
int result = a2dp.disconnectSink(address);
Log.e(TAG, "disconnectSink(" + address + ") = " + result);
}
@MediumTest
public void testBluetoothEnabled() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
if (device.isEnabled()) {
Log.i(TAG, "isEnabled() = yes");
} else {
Log.i(TAG, "isEnabled() = no");
}
}
@MediumTest
public void testEnableBluetooth() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
device.enable();
}
@MediumTest
public void testEnableBluetoothWithCallback() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
if (!device.enable(mCallback)) {
Log.e(TAG, "enable() failed");
}
}
@MediumTest
public void testDisableBluetooth() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
device.disable();
}
@LargeTest
public void testDiscovery() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
if (device.isEnabled()) {
getContext().registerReceiver((BroadcastReceiver)new TestDiscoveryReceiver(),
new IntentFilter(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION));
Log.i(TAG, "Starting discovery...");
String result = device.startDiscovery() ? "true" : "false";
Log.i(TAG, "startDiscovery() = " + result);
}
}
@LargeTest
public void testMultipleDiscovery() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
if (device.isEnabled()) {
getContext().registerReceiver((BroadcastReceiver)new TestDiscoveryReceiver(),
new IntentFilter(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION));
String result;
Log.i(TAG, "Starting multiple discovery...");
for (int i = 0; i < 5; i++) {
result = device.startDiscovery() ? "true" : "false";
Log.i(TAG, "startDiscovery() = " + result);
}
}
}
private class TestDiscoveryReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
String address = intent.getStringExtra(BluetoothIntent.ADDRESS);
int deviceClass = intent.getIntExtra(BluetoothIntent.CLASS, -1);
short rssi = intent.getShortExtra(BluetoothIntent.RSSI, (short)-1);
Log.i(TAG, "Discovered Device: " + address + " " + deviceClass + " " + rssi);
}
}
private IBluetoothDeviceCallback mCallback = new IBluetoothDeviceCallback.Stub() {
public void onEnableResult(int res) {
String result = "unknown";
switch (res) {
case BluetoothDevice.RESULT_SUCCESS:
result = "success";
break;
case BluetoothDevice.RESULT_FAILURE:
result = "FAILURE";
break;
}
Log.i(TAG, "onEnableResult(" + result + ")");
}
public void onGetRemoteServiceChannelResult(String device, int channel) {}
};
@SmallTest
public void testCreateBond() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
if (!device.createBond("01:23:45:67:89:AB")) {
Log.e(TAG, "createBonding() failed");
}
}
@SmallTest
public void testIsPeriodicDiscovery() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
boolean ret = device.isPeriodicDiscovery();
if (ret) {
Log.i(TAG, "isPeriodicDiscovery() = TRUE");
} else {
Log.i(TAG, "isPeriodicDiscovery() = FALSE");
}
}
@LargeTest
public void testListBondings() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
String[] addresses = device.listBonds();
if (addresses == null) {
Log.i(TAG, "Bluetooth disabled");
return;
}
for (String address : addresses) {
String name = device.getRemoteName(address);
Log.i(TAG, "BONDING: " + address + " (" + name + ")");
}
}
@LargeTest
public void testListAclConnections() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
String[] addresses = device.listAclConnections();
if (addresses == null) {
Log.i(TAG, "Bluetooth disabled");
return;
}
for (String address : addresses) {
String name = device.getRemoteName(address);
Log.i(TAG, "CONNECTION: " + address + " (" + name + ")");
}
}
@LargeTest
public void testListRemoteDevices() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
String[] addresses = device.listRemoteDevices();
if (addresses == null) {
Log.i(TAG, "Bluetooth disabled");
return;
}
for (String address : addresses) {
String name = device.getRemoteName(address);
Log.i(TAG, "KNOWN DEVICE: " + address + " (" + name + ")");
}
}
@MediumTest
public void testSetupBTIntentRecv() throws Exception {
BluetoothDevice device =
(BluetoothDevice)getContext().getSystemService(Context.BLUETOOTH_SERVICE);
if (device == null) {
Log.i(TAG, "Device not Bluetooth capable, skipping test");
return;
}
if (device.isEnabled()) {
IntentFilter filter = new IntentFilter(BluetoothIntent.ENABLED_ACTION);
filter.addAction(BluetoothIntent.ENABLED_ACTION);
filter.addAction(BluetoothIntent.DISABLED_ACTION);
filter.addAction(BluetoothIntent.NAME_CHANGED_ACTION);
filter.addAction(BluetoothIntent.DISCOVERY_STARTED_ACTION);
filter.addAction(BluetoothIntent.DISCOVERY_COMPLETED_ACTION);
filter.addAction(BluetoothIntent.PAIRING_REQUEST_ACTION);
filter.addAction(BluetoothIntent.PAIRING_CANCEL_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_CLASS_UPDATED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_DISCONNECT_REQUESTED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION);
filter.addAction(BluetoothIntent.REMOTE_NAME_FAILED_ACTION);
filter.addAction(BluetoothIntent.BOND_STATE_CHANGED_ACTION);
filter.addAction(BluetoothIntent.HEADSET_STATE_CHANGED_ACTION);
getContext().registerReceiver(
(BroadcastReceiver)new BluetoothIntentReceiver(), filter);
Log.i(TAG, "Listening for BLUETOOTH INTENTS....");
} else {
Log.e(TAG, "BT not enabled");
}
}
private class BluetoothIntentReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
String msg = "";
String address = intent.getStringExtra(BluetoothIntent.ADDRESS);
if (address != null) {
msg += " address=" + address;
}
int deviceClass = intent.getIntExtra(BluetoothIntent.CLASS, BluetoothClass.ERROR);
if (deviceClass != BluetoothClass.ERROR) {
msg += " class=" + deviceClass;
}
int rssi = intent.getIntExtra(BluetoothIntent.RSSI, -1);
if (rssi != -1) {
msg += " rssi=" + rssi;
}
String name = intent.getStringExtra(BluetoothIntent.NAME);
if (name != null) {
msg += " name=" + name;
}
int state = intent.getIntExtra(BluetoothIntent.HEADSET_STATE, -10);
if (state != -10) {
msg += " headset state=" + state;
}
Log.i(TAG, "BLUETOOTH INTENT: " + intent.getAction() + msg);
}
}
private static final int[] ALL_SERVICE_CLASSES = new int[] {
BluetoothClass.Service.LIMITED_DISCOVERABILITY,
BluetoothClass.Service.POSITIONING,
BluetoothClass.Service.NETWORKING,
BluetoothClass.Service.RENDER,
BluetoothClass.Service.CAPTURE,
BluetoothClass.Service.OBJECT_TRANSFER,
BluetoothClass.Service.AUDIO,
BluetoothClass.Service.TELEPHONY,
BluetoothClass.Service.INFORMATION
};
private void assertOnlyTheseServiceClassesAreSupported(int deviceClass, HashSet<Integer> serviceClasses) {
for (int serviceClassType : ALL_SERVICE_CLASSES) {
Assert.assertEquals(serviceClasses.contains(new Integer(serviceClassType)),
BluetoothClass.Service.hasService(deviceClass, serviceClassType));
}
}
@SmallTest
public void testDeviceClass() throws Exception {
// This test does not require bluetooth hardware
int deviceClass;
HashSet<Integer> serviceClasses;
deviceClass = BluetoothClass.ERROR; // bogus class
serviceClasses = new HashSet<Integer>();
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.ERROR, BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(BluetoothClass.ERROR, BluetoothClass.Device.getDevice(deviceClass));
deviceClass = 0x10210C; // mac book pro
serviceClasses = new HashSet<Integer>();
serviceClasses.add(BluetoothClass.Service.OBJECT_TRANSFER);
serviceClasses.add(BluetoothClass.Service.LIMITED_DISCOVERABILITY);
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.Device.Major.COMPUTER,
BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(0x10C, BluetoothClass.Device.getDevice(deviceClass));
// mac book pro with some unused bits set. Expecting the same results
deviceClass = 0xFF10210F;
serviceClasses = new HashSet<Integer>();
serviceClasses.add(BluetoothClass.Service.OBJECT_TRANSFER);
serviceClasses.add(BluetoothClass.Service.LIMITED_DISCOVERABILITY);
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.Device.Major.COMPUTER,
BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(0x10C, BluetoothClass.Device.getDevice(deviceClass));
deviceClass = 0x3E0100; // droid.corp.google.com
serviceClasses = new HashSet<Integer>();
serviceClasses.add(BluetoothClass.Service.AUDIO);
serviceClasses.add(BluetoothClass.Service.OBJECT_TRANSFER);
serviceClasses.add(BluetoothClass.Service.CAPTURE);
serviceClasses.add(BluetoothClass.Service.RENDER);
serviceClasses.add(BluetoothClass.Service.NETWORKING);
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.Device.Major.COMPUTER,
BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(0x100, BluetoothClass.Device.getDevice(deviceClass));
deviceClass = 0x40020C; // Android
serviceClasses = new HashSet<Integer>();
serviceClasses.add(BluetoothClass.Service.TELEPHONY);
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.Device.Major.PHONE, BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(0x20C, BluetoothClass.Device.getDevice(deviceClass));
// Motorola T305 & Jabra BT125 & Jabra BT250V
// This seems to be a very common headset & handsfree device code
deviceClass = 0x200404;
serviceClasses = new HashSet<Integer>();
serviceClasses.add(BluetoothClass.Service.AUDIO);
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.Device.Major.AUDIO_VIDEO,
BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET,
BluetoothClass.Device.getDevice(deviceClass));
// Audi UHV 0128
deviceClass = 0x200408;
serviceClasses = new HashSet<Integer>();
serviceClasses.add(BluetoothClass.Service.AUDIO);
assertOnlyTheseServiceClassesAreSupported(deviceClass, serviceClasses);
Assert.assertEquals(BluetoothClass.Device.Major.AUDIO_VIDEO,
BluetoothClass.Device.Major.getDeviceMajor(deviceClass));
Assert.assertEquals(BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE,
BluetoothClass.Device.getDevice(deviceClass));
}
}
|