BluetoothInstrumentation.java revision 03800b8873f70223dcfcf396a3762775dfc1a8ab
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 */
16package android.bluetooth;
17
18import android.app.Activity;
19import android.app.Instrumentation;
20import android.content.Context;
21import android.os.Bundle;
22
23public class BluetoothInstrumentation extends Instrumentation {
24
25    private BluetoothTestUtils mUtils = null;
26    private BluetoothAdapter mAdapter = null;
27    private Bundle mArgs = null;
28
29    private BluetoothTestUtils getBluetoothTestUtils() {
30        if (mUtils == null) {
31            mUtils = new BluetoothTestUtils(getContext(),
32                    BluetoothInstrumentation.class.getSimpleName());
33        }
34        return mUtils;
35    }
36
37    private BluetoothAdapter getBluetoothAdapter() {
38        if (mAdapter == null) {
39            mAdapter = ((BluetoothManager)getContext().getSystemService(
40                    Context.BLUETOOTH_SERVICE)).getAdapter();
41        }
42        return mAdapter;
43    }
44
45    @Override
46    public void onCreate(Bundle arguments) {
47        super.onCreate(arguments);
48        mArgs = arguments;
49        start();
50    }
51
52    @Override
53    public void onStart() {
54        String command = mArgs.getString("command");
55        if ("enable".equals(command)) {
56            enable();
57        } else if ("disable".equals(command)) {
58            disable();
59        } else if ("unpairAll".equals(command)) {
60            unpairAll();
61        } else if ("getName".equals(command)) {
62            getName();
63        } else {
64            finish(null);
65        }
66    }
67
68    public void enable() {
69        getBluetoothTestUtils().enable(getBluetoothAdapter());
70        finish(null);
71    }
72
73    public void disable() {
74        getBluetoothTestUtils().disable(getBluetoothAdapter());
75        finish(null);
76    }
77
78    public void unpairAll() {
79        getBluetoothTestUtils().unpairAll(getBluetoothAdapter());
80        finish(null);
81    }
82
83    public void getName() {
84        String name = getBluetoothAdapter().getName();
85        Bundle bundle = new Bundle();
86        bundle.putString("name", name);
87        finish(bundle);
88    }
89
90    public void finish(Bundle result) {
91        if (result == null) {
92            result = new Bundle();
93        }
94        finish(Activity.RESULT_OK, result);
95    }
96}
97