• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package android.bluetooth;
17 
18 import android.app.Activity;
19 import android.app.Instrumentation;
20 import android.content.Context;
21 import android.os.Bundle;
22 
23 import junit.framework.Assert;
24 
25 import java.util.Set;
26 
27 public class BluetoothInstrumentation extends Instrumentation {
28 
29     private BluetoothTestUtils mUtils = null;
30     private BluetoothAdapter mAdapter = null;
31     private Bundle mArgs = null;
32     private Bundle mSuccessResult = null;
33 
getBluetoothTestUtils()34     private BluetoothTestUtils getBluetoothTestUtils() {
35         if (mUtils == null) {
36             mUtils = new BluetoothTestUtils(getContext(),
37                     BluetoothInstrumentation.class.getSimpleName());
38         }
39         return mUtils;
40     }
41 
getBluetoothAdapter()42     private BluetoothAdapter getBluetoothAdapter() {
43         if (mAdapter == null) {
44             mAdapter = ((BluetoothManager)getContext().getSystemService(
45                     Context.BLUETOOTH_SERVICE)).getAdapter();
46         }
47         return mAdapter;
48     }
49 
50     @Override
onCreate(Bundle arguments)51     public void onCreate(Bundle arguments) {
52         super.onCreate(arguments);
53         mArgs = arguments;
54         // create the default result response, but only use it in success code path
55         mSuccessResult = new Bundle();
56         mSuccessResult.putString("result", "SUCCESS");
57         start();
58     }
59 
60     @Override
onStart()61     public void onStart() {
62         String command = mArgs.getString("command");
63         if ("enable".equals(command)) {
64             enable();
65         } else if ("disable".equals(command)) {
66             disable();
67         } else if ("unpairAll".equals(command)) {
68             unpairAll();
69         } else if ("getName".equals(command)) {
70             getName();
71         } else if ("getAddress".equals(command)) {
72             getAddress();
73         } else if ("getBondedDevices".equals(command)) {
74             getBondedDevices();
75         } else if ("enableBtSnoop".equals(command)) {
76             enableBtSnoop();
77         } else {
78             finish(null);
79         }
80     }
81 
enable()82     public void enable() {
83         getBluetoothTestUtils().enable(getBluetoothAdapter());
84         finish(mSuccessResult);
85     }
86 
disable()87     public void disable() {
88         getBluetoothTestUtils().disable(getBluetoothAdapter());
89         finish(mSuccessResult);
90     }
91 
unpairAll()92     public void unpairAll() {
93         getBluetoothTestUtils().unpairAll(getBluetoothAdapter());
94         finish(mSuccessResult);
95     }
96 
getName()97     public void getName() {
98         String name = getBluetoothAdapter().getName();
99         mSuccessResult.putString("name", name);
100         finish(mSuccessResult);
101     }
102 
getAddress()103     public void getAddress() {
104         String name = getBluetoothAdapter().getAddress();
105         mSuccessResult.putString("address", name);
106         finish(mSuccessResult);
107     }
108 
getBondedDevices()109     public void getBondedDevices() {
110         Set<BluetoothDevice> devices = getBluetoothAdapter().getBondedDevices();
111         int i = 0;
112         for (BluetoothDevice device : devices) {
113             mSuccessResult.putString(String.format("device-%02d", i), device.getAddress());
114             i++;
115         }
116         finish(mSuccessResult);
117     }
118 
enableBtSnoop()119     public void enableBtSnoop() {
120         Assert.assertTrue("failed to enable snoop log",
121                 getBluetoothAdapter().configHciSnoopLog(true));
122         finish(mSuccessResult);
123     }
124 
finish(Bundle result)125     public void finish(Bundle result) {
126         if (result == null) {
127             result = new Bundle();
128         }
129         finish(Activity.RESULT_OK, result);
130     }
131 }
132