Home | History | Annotate | Download | only in bluetooth
      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 
     34     private BluetoothTestUtils getBluetoothTestUtils() {
     35         if (mUtils == null) {
     36             mUtils = new BluetoothTestUtils(getContext(),
     37                     BluetoothInstrumentation.class.getSimpleName());
     38         }
     39         return mUtils;
     40     }
     41 
     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
     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
     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 {
     76             finish(null);
     77         }
     78     }
     79 
     80     public void enable() {
     81         getBluetoothTestUtils().enable(getBluetoothAdapter());
     82         finish(mSuccessResult);
     83     }
     84 
     85     public void disable() {
     86         getBluetoothTestUtils().disable(getBluetoothAdapter());
     87         finish(mSuccessResult);
     88     }
     89 
     90     public void unpairAll() {
     91         getBluetoothTestUtils().unpairAll(getBluetoothAdapter());
     92         finish(mSuccessResult);
     93     }
     94 
     95     public void getName() {
     96         String name = getBluetoothAdapter().getName();
     97         mSuccessResult.putString("name", name);
     98         finish(mSuccessResult);
     99     }
    100 
    101     public void getAddress() {
    102         String name = getBluetoothAdapter().getAddress();
    103         mSuccessResult.putString("address", name);
    104         finish(mSuccessResult);
    105     }
    106 
    107     public void getBondedDevices() {
    108         Set<BluetoothDevice> devices = getBluetoothAdapter().getBondedDevices();
    109         int i = 0;
    110         for (BluetoothDevice device : devices) {
    111             mSuccessResult.putString(String.format("device-%02d", i), device.getAddress());
    112             i++;
    113         }
    114         finish(mSuccessResult);
    115     }
    116 
    117     public void finish(Bundle result) {
    118         if (result == null) {
    119             result = new Bundle();
    120         }
    121         finish(Activity.RESULT_OK, result);
    122     }
    123 }
    124