1 /* 2 * Copyright (C) 2020 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.server.telecom.companionapp; 18 19 import android.app.Activity; 20 import android.bluetooth.BluetoothDevice; 21 import android.companion.AssociationRequest; 22 import android.companion.BluetoothDeviceFilter; 23 import android.companion.CompanionDeviceManager; 24 import android.content.Intent; 25 import android.content.IntentSender; 26 import android.os.Bundle; 27 import android.os.ParcelUuid; 28 import android.util.Log; 29 import android.view.View; 30 import android.view.View.OnClickListener; 31 import android.widget.Button; 32 import android.widget.Toast; 33 34 import java.util.UUID; 35 import java.util.regex.Pattern; 36 37 /** 38 * Companion Test App in Telecom 39 */ 40 public class CompanionTestApp extends Activity { 41 42 private static String logtag = "CompanionTestApp"; 43 private CompanionDeviceManager mDeviceManager; 44 private AssociationRequest mPairingRequest; 45 private BluetoothDeviceFilter mDeviceFilter; 46 47 private static final int SELECT_DEVICE_REQUEST_CODE = 42; 48 49 @Override onCreate(Bundle savedInstanceState)50 public void onCreate(Bundle savedInstanceState) { 51 setupDeviceAssociation(); 52 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.main); 55 56 Button buttonStart = (Button)findViewById(R.id.buttonStart); 57 buttonStart.setOnClickListener(startListener); 58 59 Button buttonStop = (Button)findViewById(R.id.buttonStop); 60 buttonStop.setOnClickListener(stopListener); 61 } 62 63 private OnClickListener startListener = new OnClickListener() { 64 public void onClick(View v) { 65 Log.d(logtag,"onClick() called - start button"); 66 Toast.makeText(CompanionTestApp.this, "Start Association", Toast.LENGTH_LONG).show(); 67 assosicate(); 68 Log.d(logtag,"onClick() ended - start button"); 69 } 70 }; 71 72 private OnClickListener stopListener = new OnClickListener() { 73 public void onClick(View v) { 74 Log.d(logtag,"onClick() called - stop button"); 75 Toast.makeText(CompanionTestApp.this, "Stop Association", Toast.LENGTH_LONG).show(); 76 disassosicate(); 77 Log.d(logtag,"onClick() ended - stop button"); 78 } 79 }; 80 81 // Setup Device Association Preparation, per 82 // https://developer.android.com/guide/topics/connectivity/companion-device-pairing setupDeviceAssociation()83 private void setupDeviceAssociation() { 84 mDeviceManager = getSystemService(CompanionDeviceManager.class); 85 86 mDeviceFilter = new BluetoothDeviceFilter.Builder() 87 .build(); 88 89 mPairingRequest = new AssociationRequest.Builder() 90 .addDeviceFilter(mDeviceFilter) 91 .setDeviceProfile(AssociationRequest.DEVICE_PROFILE_WATCH) 92 .build(); 93 94 } 95 96 // Associate bluetooth device, per 97 // https://developer.android.com/guide/topics/connectivity/companion-device-pairing assosicate()98 private void assosicate() { 99 // When the app tries to pair with the Bluetooth device, show the 100 // appropriate pairing request dialog to the user. 101 mDeviceManager.associate(mPairingRequest, 102 new CompanionDeviceManager.Callback() { 103 @Override 104 public void onDeviceFound(IntentSender chooserLauncher) { 105 try { 106 startIntentSenderForResult(chooserLauncher, 107 SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0); 108 } catch (Exception ex) { 109 Log.d(logtag, "Callback onDeviceFound() Exception: " + ex); 110 } 111 } 112 113 @Override 114 public void onFailure(CharSequence error) { 115 Log.d(logtag, "Callback onFailure() called - error: " + error); 116 } 117 }, 118 null); 119 } 120 121 // Disassociate the associated bluetooth device disassosicate()122 private void disassosicate() { 123 for (String macAddress : mDeviceManager.getAssociations()) { 124 mDeviceManager.disassociate(macAddress); 125 } 126 } 127 128 @Override onActivityResult(int requestCode, int resultCode, Intent data)129 public void onActivityResult(int requestCode, int resultCode, Intent data) { 130 if (requestCode == SELECT_DEVICE_REQUEST_CODE && 131 resultCode == Activity.RESULT_OK) { 132 // User has chosen to pair with the Bluetooth device. 133 BluetoothDevice deviceToPair = 134 data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE); 135 deviceToPair.createBond(); 136 } 137 } 138 139 @Override onStart()140 protected void onStart() { 141 Log.d(logtag,"onStart() called"); 142 super.onStart(); 143 } 144 145 @Override onResume()146 protected void onResume() { 147 Log.d(logtag,"onResume() called"); 148 super.onResume(); 149 } 150 151 @Override onPause()152 protected void onPause() { 153 Log.d(logtag,"onPause() called"); 154 super.onPause(); 155 } 156 157 @Override onStop()158 protected void onStop() { 159 Log.d(logtag,"onStop() called"); 160 super.onStop(); 161 } 162 163 @Override onDestroy()164 protected void onDestroy() { 165 Log.d(logtag,"onDestroy() called"); 166 super.onDestroy(); 167 } 168 } 169