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 static android.content.Context.RECEIVER_EXPORTED; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.widget.Button; 29 import android.widget.Toast; 30 31 import com.android.cts.verifier.PassFailButtons; 32 import com.android.cts.verifier.R; 33 34 public class BleButtonActivity extends PassFailButtons.Activity { 35 36 static final int DISCOVER_SERVICE = 0; 37 static final int DISCONNECT = 1; 38 39 private int mName; 40 private int mInfo; 41 private int mButtonText; 42 private int mCommand; 43 private String mFilter; 44 private String mMessage; 45 BleButtonActivity(int target)46 BleButtonActivity(int target) { 47 if (target == DISCOVER_SERVICE) { 48 mName = R.string.ble_discover_service_name; 49 mInfo = R.string.ble_discover_service_info; 50 mButtonText = R.string.ble_discover_service; 51 mCommand = BleClientService.COMMAND_DISCOVER_SERVICE; 52 mFilter = BleClientService.BLE_SERVICES_DISCOVERED; 53 mMessage = "Service discovered."; 54 } else if (target == DISCONNECT) { 55 mName = R.string.ble_client_disconnect_name; 56 mInfo = R.string.ble_client_disconnect_name; 57 mButtonText = R.string.ble_disconnect; 58 mCommand = BleClientService.COMMAND_DISCONNECT; 59 mFilter = BleClientService.BLE_BLUETOOTH_DISCONNECTED; 60 mMessage = "Bluetooth LE disconnected."; 61 } 62 } 63 64 @Override onCreate(Bundle savedInstanceState)65 public void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 setContentView(R.layout.ble_button); 68 setPassFailButtonClickListeners(); 69 setInfoResources(mName, mInfo, -1); 70 getPassButton().setEnabled(false); 71 72 ((Button) findViewById(R.id.ble_button)).setText(mButtonText); 73 ((Button) findViewById(R.id.ble_button)).setOnClickListener(new OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 Intent intent = new Intent(BleButtonActivity.this, BleClientService.class); 77 intent.putExtra(BleClientService.EXTRA_COMMAND, mCommand); 78 startService(intent); 79 } 80 }); 81 } 82 83 @Override onResume()84 public void onResume() { 85 super.onResume(); 86 IntentFilter filter = new IntentFilter(); 87 filter.addAction(mFilter); 88 registerReceiver(onBroadcast, filter, RECEIVER_EXPORTED); 89 } 90 91 @Override onPause()92 public void onPause() { 93 super.onPause(); 94 unregisterReceiver(onBroadcast); 95 } 96 showMessage(String msg)97 private void showMessage(String msg) { 98 Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 99 } 100 101 private BroadcastReceiver onBroadcast = new BroadcastReceiver() { 102 @Override 103 public void onReceive(Context context, Intent intent) { 104 showMessage(mMessage); 105 getPassButton().setEnabled(true); 106 } 107 }; 108 }