BluetoothInstrumentation.java revision fa9dc683f18e68cc1170c149a40c33a191444af1
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 if ("getAddress".equals(command)) {
68            getAddress();
69        } else {
70            finish(null);
71        }
72    }
73
74    public void enable() {
75        getBluetoothTestUtils().enable(getBluetoothAdapter());
76        finish(mSuccessResult);
77    }
78
79    public void disable() {
80        getBluetoothTestUtils().disable(getBluetoothAdapter());
81        finish(mSuccessResult);
82    }
83
84    public void unpairAll() {
85        getBluetoothTestUtils().unpairAll(getBluetoothAdapter());
86        finish(mSuccessResult);
87    }
88
89    public void getName() {
90        String name = getBluetoothAdapter().getName();
91        mSuccessResult.putString("name", name);
92        finish(mSuccessResult);
93    }
94
95    public void getAddress() {
96        String name = getBluetoothAdapter().getAddress();
97        mSuccessResult.putString("address", name);
98        finish(mSuccessResult);
99    }
100
101    public void finish(Bundle result) {
102        if (result == null) {
103            result = new Bundle();
104        }
105        finish(Activity.RESULT_OK, result);
106    }
107}
108