John Du | 3214737 | 2013-07-26 12:28:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.os.Bundle; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.widget.Button; |
| 30 | import android.widget.EditText; |
| 31 | import android.widget.Toast; |
| 32 | |
| 33 | public class BleReliableWriteActivity extends PassFailButtons.Activity { |
| 34 | |
| 35 | EditText mEditText; |
| 36 | |
| 37 | @Override |
| 38 | public void onCreate(Bundle savedInstanceState) { |
| 39 | super.onCreate(savedInstanceState); |
| 40 | setContentView(R.layout.ble_reliable_write); |
| 41 | setPassFailButtonClickListeners(); |
| 42 | setInfoResources(R.string.ble_reliable_write_name, R.string.ble_reliable_write_info, -1); |
| 43 | getPassButton().setEnabled(false); |
| 44 | ((Button) findViewById(R.id.ble_execute)).setEnabled(false); |
| 45 | |
| 46 | mEditText = (EditText) findViewById(R.id.write_text); |
| 47 | |
| 48 | ((Button) findViewById(R.id.ble_begin)).setOnClickListener(new OnClickListener() { |
| 49 | @Override |
| 50 | public void onClick(View v) { |
| 51 | Intent intent = new Intent(BleReliableWriteActivity.this, BleClientService.class); |
| 52 | intent.putExtra(BleClientService.EXTRA_COMMAND, |
| 53 | BleClientService.COMMAND_BEGIN_WRITE); |
| 54 | startService(intent); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | ((Button) findViewById(R.id.ble_write)).setOnClickListener(new OnClickListener() { |
| 59 | @Override |
| 60 | public void onClick(View v) { |
| 61 | String writeValue = mEditText.getText().toString(); |
| 62 | Intent intent = new Intent(BleReliableWriteActivity.this, BleClientService.class); |
| 63 | intent.putExtra(BleClientService.EXTRA_COMMAND, |
| 64 | BleClientService.COMMAND_WRITE_CHARACTERISTIC); |
| 65 | intent.putExtra(BleClientService.EXTRA_WRITE_VALUE, writeValue); |
| 66 | startService(intent); |
| 67 | mEditText.setText(""); |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | ((Button) findViewById(R.id.ble_execute)).setOnClickListener(new OnClickListener() { |
| 72 | @Override |
| 73 | public void onClick(View v) { |
| 74 | Intent intent = new Intent(BleReliableWriteActivity.this, BleClientService.class); |
| 75 | intent.putExtra(BleClientService.EXTRA_COMMAND, |
| 76 | BleClientService.COMMAND_EXECUTE_WRITE); |
| 77 | startService(intent); |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onResume() { |
| 84 | super.onResume(); |
| 85 | IntentFilter filter = new IntentFilter(); |
| 86 | filter.addAction(BleClientService.BLE_CHARACTERISTIC_WRITE); |
| 87 | filter.addAction(BleClientService.BLE_RELIABLE_WRITE_COMPLETED); |
| 88 | registerReceiver(onBroadcast, filter); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void onPause() { |
| 93 | super.onPause(); |
| 94 | unregisterReceiver(onBroadcast); |
| 95 | Intent intent = new Intent(this, BleClientService.class); |
| 96 | intent.putExtra(BleClientService.EXTRA_COMMAND, BleClientService.COMMAND_ABORT_RELIABLE); |
| 97 | startService(intent); |
| 98 | } |
| 99 | |
| 100 | private void showMessage(String msg) { |
| 101 | Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); |
| 102 | } |
| 103 | |
| 104 | private BroadcastReceiver onBroadcast = new BroadcastReceiver() { |
| 105 | @Override |
| 106 | public void onReceive(Context context, Intent intent) { |
| 107 | String action = intent.getAction(); |
| 108 | if (action == BleClientService.BLE_CHARACTERISTIC_WRITE) { |
| 109 | showMessage("Write value verified."); |
| 110 | ((Button) findViewById(R.id.ble_execute)).setEnabled(true); |
| 111 | } else if (action == BleClientService.BLE_RELIABLE_WRITE_COMPLETED) { |
| 112 | showMessage("Reliable write completed."); |
| 113 | getPassButton().setEnabled(true); |
| 114 | } |
| 115 | } |
| 116 | }; |
Yicheng Fan | c888220 | 2014-11-10 12:10:10 -0800 | [diff] [blame] | 117 | } |