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