• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.bluetooth.tests;
2 
3 import android.annotation.TargetApi;
4 import android.bluetooth.BluetoothAdapter;
5 import android.os.Build;
6 import android.test.AndroidTestCase;
7 import android.util.Log;
8 
9 
10 @TargetApi(Build.VERSION_CODES.ECLAIR)
11 public class BluetoothTestUtils extends AndroidTestCase {
12 
13     protected static String TAG = "BluetoothTestUtils";
14     protected static final boolean D = true;
15 
16     static final int POLL_TIME = 500;
17     static final int ENABLE_TIMEOUT = 5000;
18 
19     /** Helper to turn BT on.
20      * This method will either fail on an assert, or return with BT turned on.
21      * Behavior of getState() and isEnabled() are validated along the way.
22      */
enableBt(BluetoothAdapter adapter)23     public static void enableBt(BluetoothAdapter adapter) {
24         if (adapter.getState() == BluetoothAdapter.STATE_ON) {
25             assertTrue(adapter.isEnabled());
26             return;
27         }
28         assertEquals(BluetoothAdapter.STATE_OFF, adapter.getState());
29         assertFalse(adapter.isEnabled());
30         adapter.enable();
31         for (int i=0; i<ENABLE_TIMEOUT/POLL_TIME; i++) {
32             int state = adapter.getState();
33             switch (state) {
34             case BluetoothAdapter.STATE_ON:
35                 assertTrue(adapter.isEnabled());
36                 Log.i(TAG, "Bluetooth enabled...");
37                 return;
38             case BluetoothAdapter.STATE_OFF:
39                 Log.i(TAG, "STATE_OFF: Still waiting for enable to begin...");
40                 break;
41             default:
42                 Log.i(TAG, "Status is: " + state);
43                 assertEquals(BluetoothAdapter.STATE_TURNING_ON, adapter.getState());
44                 assertFalse(adapter.isEnabled());
45                 break;
46             }
47             try {
48                 Thread.sleep(POLL_TIME);
49             } catch (InterruptedException e) {}
50         }
51         fail("enable() timeout");
52     }
53 
54 }
55