BluetoothInstrumentation.java revision e47ebbeec672ece19e260ccd7f4b83a297bce61e
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    private Bundle mSuccessResult = null;
29
30    private BluetoothTestUtils getBluetoothTestUtils() {
31        if (mUtils == null) {
32            mUtils = new BluetoothTestUtils(getContext(),
33                    BluetoothInstrumentation.class.getSimpleName());
34        }
35        return mUtils;
36    }
37
38    private BluetoothAdapter getBluetoothAdapter() {
39        if (mAdapter == null) {
40            mAdapter = ((BluetoothManager)getContext().getSystemService(
41                    Context.BLUETOOTH_SERVICE)).getAdapter();
42        }
43        return mAdapter;
44    }
45
46    @Override
47    public void onCreate(Bundle arguments) {
48        super.onCreate(arguments);
49        mArgs = arguments;
50        // create the default result response, but only use it in success code path
51        mSuccessResult = new Bundle();
52        mSuccessResult.putString("result", "SUCCESS");
53        start();
54    }
55
56    @Override
57    public void onStart() {
58        String command = mArgs.getString("command");
59        if ("enable".equals(command)) {
60            enable();
61        } else if ("disable".equals(command)) {
62            disable();
63        } else if ("unpairAll".equals(command)) {
64            unpairAll();
65        } else if ("getName".equals(command)) {
66            getName();
67        } else {
68            finish(null);
69        }
70    }
71
72    public void enable() {
73        getBluetoothTestUtils().enable(getBluetoothAdapter());
74        finish(mSuccessResult);
75    }
76
77    public void disable() {
78        getBluetoothTestUtils().disable(getBluetoothAdapter());
79        finish(mSuccessResult);
80    }
81
82    public void unpairAll() {
83        getBluetoothTestUtils().unpairAll(getBluetoothAdapter());
84        finish(mSuccessResult);
85    }
86
87    public void getName() {
88        String name = getBluetoothAdapter().getName();
89        mSuccessResult.putString("name", name);
90        finish(mSuccessResult);
91    }
92
93    public void finish(Bundle result) {
94        if (result == null) {
95            result = new Bundle();
96        }
97        finish(Activity.RESULT_OK, result);
98    }
99}
100