1 /*
2  * Copyright (C) 2018 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 
17 package com.android.cts.verifier.bluetooth;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.util.Log;
26 
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.widget.Button;
30 import android.widget.EditText;
31 
32 import com.android.cts.verifier.PassFailButtons;
33 import com.android.cts.verifier.R;
34 import com.android.cts.verifier.TestResult;
35 
36 public class HidHostActivity extends PassFailButtons.Activity {
37     private static final String TAG = HidHostActivity.class.getSimpleName();
38 
39     BluetoothAdapter mBluetoothAdapter;
40 
41     private static final int ENABLE_BLUETOOTH_REQUEST = 1;
42     private static final int PICK_SERVER_DEVICE_REQUEST = 2;
43 
44     private String mDeviceAddress;
45 
46     private Button mPickDeviceButton;
47     private EditText mEditText;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.bt_hid_host);
53         setPassFailButtonClickListeners();
54         setInfoResources(R.string.bt_hid_host_test_name, R.string.bt_hid_host_test_info, -1);
55         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
56 
57         mEditText = (EditText) findViewById(R.id.bt_hid_host_edit_text);
58         mEditText.addTextChangedListener(new TextWatcher() {
59             @Override
60             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
61 
62             @Override
63             public void onTextChanged(CharSequence s, int start, int before, int count) {
64                 if (s.toString().equals(HidDeviceActivity.SAMPLE_INPUT)) {
65                     getPassButton().setEnabled(true);
66                 }
67             }
68 
69             @Override
70             public void afterTextChanged(Editable s) {}
71         });
72         mPickDeviceButton = (Button) findViewById(R.id.bt_hid_host_pick_device_button);
73         mPickDeviceButton.setOnClickListener(new OnClickListener() {
74             @Override
75             public void onClick(View v) {
76                 Intent intent = new Intent(HidHostActivity.this,
77                         DevicePickerActivity.class);
78                 startActivityForResult(intent, PICK_SERVER_DEVICE_REQUEST);
79                 mEditText.requestFocus();
80             }
81         });
82     }
83 
84     @Override
onActivityResult(int requestCode, int resultCode, Intent data)85     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
86         super.onActivityResult(requestCode, resultCode, data);
87         switch (requestCode) {
88             case PICK_SERVER_DEVICE_REQUEST:
89                 Log.d(TAG, "pick device: " + resultCode);
90                 if (resultCode == RESULT_OK) {
91                     mDeviceAddress = data.getStringExtra(DevicePickerActivity.EXTRA_DEVICE_ADDRESS);
92                     connectToDevice();
93                 } else {
94                     setResult(RESULT_CANCELED);
95                     finish();
96                 }
97                 break;
98         }
99     }
100 
connectToDevice()101     private boolean connectToDevice() {
102         BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
103         bluetoothDevice.createBond();
104         return true;
105     }
106 }
107