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 
17 package com.android.cts.verifier.bluetooth;
18 
19 import com.android.cts.verifier.PassFailButtons;
20 import com.android.cts.verifier.R;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.BaseAdapter;
35 import android.widget.ImageView;
36 import android.widget.ListView;
37 import android.widget.TextView;
38 import android.widget.Toast;
39 
40 public class BleClientStartActivity extends PassFailButtons.Activity {
41 
42     private TestAdapter mTestAdapter;
43     private int mAllPassed;
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         setContentView(R.layout.ble_server_start);
49         setPassFailButtonClickListeners();
50         setInfoResources(R.string.ble_server_start_name,
51                          R.string.ble_server_start_info, -1);
52         getPassButton().setEnabled(false);
53 
54         mTestAdapter = new TestAdapter(this, setupTestList());
55         ListView listView = (ListView) findViewById(R.id.ble_server_tests);
56         listView.setAdapter(mTestAdapter);
57 
58         mAllPassed = 0;
59         startService(new Intent(this, BleClientService.class));
60     }
61 
62     @Override
onResume()63     public void onResume() {
64         super.onResume();
65 
66         IntentFilter filter = new IntentFilter();
67         filter.addAction(BleClientService.BLE_BLUETOOTH_CONNECTED);
68         filter.addAction(BleClientService.BLE_BLUETOOTH_DISCONNECTED);
69         filter.addAction(BleClientService.BLE_SERVICES_DISCOVERED);
70         filter.addAction(BleClientService.BLE_CHARACTERISTIC_READ);
71         filter.addAction(BleClientService.BLE_CHARACTERISTIC_WRITE);
72         filter.addAction(BleClientService.BLE_CHARACTERISTIC_CHANGED);
73         filter.addAction(BleClientService.BLE_DESCRIPTOR_READ);
74         filter.addAction(BleClientService.BLE_DESCRIPTOR_WRITE);
75         filter.addAction(BleClientService.BLE_RELIABLE_WRITE_COMPLETED);
76         filter.addAction(BleClientService.BLE_READ_REMOTE_RSSI);
77         registerReceiver(onBroadcast, filter);
78     }
79 
80     @Override
onPause()81     public void onPause() {
82         super.onPause();
83         unregisterReceiver(onBroadcast);
84     }
85 
86     @Override
onDestroy()87     public void onDestroy() {
88         super.onDestroy();
89         stopService(new Intent(this, BleClientService.class));
90     }
91 
setupTestList()92     private List<Integer> setupTestList() {
93         ArrayList<Integer> testList = new ArrayList<Integer>();
94         testList.add(R.string.ble_client_connect_name);
95         testList.add(R.string.ble_discover_service_name);
96         testList.add(R.string.ble_read_characteristic_name);
97         testList.add(R.string.ble_write_characteristic_name);
98         testList.add(R.string.ble_reliable_write_name);
99         testList.add(R.string.ble_notify_characteristic_name);
100         testList.add(R.string.ble_read_descriptor_name);
101         testList.add(R.string.ble_write_descriptor_name);
102         testList.add(R.string.ble_read_rssi_name);
103         testList.add(R.string.ble_client_disconnect_name);
104         return testList;
105     }
106 
107     private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
108         @Override
109         public void onReceive(Context context, Intent intent) {
110             String action = intent.getAction();
111             if (action == BleClientService.BLE_BLUETOOTH_CONNECTED) {
112                 mTestAdapter.setTestPass(0);
113                 mAllPassed |= 0x01;
114             } else if (action == BleClientService.BLE_SERVICES_DISCOVERED) {
115                 mTestAdapter.setTestPass(1);
116                 mAllPassed |= 0x02;
117             } else if (action == BleClientService.BLE_CHARACTERISTIC_READ) {
118                 mTestAdapter.setTestPass(2);
119                 mAllPassed |= 0x04;
120             } else if (action == BleClientService.BLE_CHARACTERISTIC_WRITE) {
121                 mTestAdapter.setTestPass(3);
122                 mAllPassed |= 0x08;
123             } else if (action == BleClientService.BLE_RELIABLE_WRITE_COMPLETED) {
124                 mTestAdapter.setTestPass(4);
125                 mAllPassed |= 0x10;
126             } else if (action == BleClientService.BLE_CHARACTERISTIC_CHANGED) {
127                 mTestAdapter.setTestPass(5);
128                 mAllPassed |= 0x20;
129             } else if (action == BleClientService.BLE_DESCRIPTOR_READ) {
130                 mTestAdapter.setTestPass(6);
131                 mAllPassed |= 0x40;
132             } else if (action == BleClientService.BLE_DESCRIPTOR_WRITE) {
133                 mTestAdapter.setTestPass(7);
134                 mAllPassed |= 0x80;
135             } else if (action == BleClientService.BLE_READ_REMOTE_RSSI) {
136                 mTestAdapter.setTestPass(8);
137                 mAllPassed |= 0x100;
138             } else if (action == BleClientService.BLE_BLUETOOTH_DISCONNECTED) {
139                 mTestAdapter.setTestPass(9);
140                 mAllPassed |= 0x200;
141             }
142             mTestAdapter.notifyDataSetChanged();
143             if (mAllPassed == 0x3FF) getPassButton().setEnabled(true);
144         }
145     };
146 
147 }
148