1 /* 2 * Copyright (C) 2017 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.googlecode.android_scripting.facade.bluetooth; 18 19 import java.util.List; 20 import java.util.ArrayList; 21 22 import android.app.Service; 23 import android.bluetooth.BluetoothHeadsetClient; 24 import android.bluetooth.BluetoothAdapter; 25 import android.bluetooth.BluetoothDevice; 26 import android.bluetooth.BluetoothProfile; 27 import android.bluetooth.BluetoothUuid; 28 import android.os.ParcelUuid; 29 30 import com.googlecode.android_scripting.Log; 31 import com.googlecode.android_scripting.facade.FacadeManager; 32 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 33 import com.googlecode.android_scripting.rpc.Rpc; 34 import com.googlecode.android_scripting.rpc.RpcParameter; 35 36 public class BluetoothHfpClientFacade extends RpcReceiver { 37 static final ParcelUuid[] UUIDS = { 38 BluetoothUuid.Handsfree_AG, 39 }; 40 41 private final Service mService; 42 private final BluetoothAdapter mBluetoothAdapter; 43 44 private static boolean sIsHfpClientReady = false; 45 private static BluetoothHeadsetClient sHfpClientProfile = null; 46 47 public BluetoothHfpClientFacade(FacadeManager manager) { 48 super(manager); 49 mService = manager.getService(); 50 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 51 mBluetoothAdapter.getProfileProxy(mService, new HfpClientServiceListener(), 52 BluetoothProfile.HEADSET_CLIENT); 53 } 54 55 class HfpClientServiceListener implements BluetoothProfile.ServiceListener { 56 @Override 57 public void onServiceConnected(int profile, BluetoothProfile proxy) { 58 sHfpClientProfile = (BluetoothHeadsetClient) proxy; 59 sIsHfpClientReady = true; 60 } 61 62 @Override 63 public void onServiceDisconnected(int profile) { 64 sIsHfpClientReady = false; 65 } 66 } 67 68 public Boolean hfpClientConnect(BluetoothDevice device) { 69 if (sHfpClientProfile == null) return false; 70 return sHfpClientProfile.connect(device); 71 } 72 73 public Boolean hfpClientDisconnect(BluetoothDevice device) { 74 if (sHfpClientProfile == null) return false; 75 return sHfpClientProfile.disconnect(device); 76 } 77 78 @Rpc(description = "Is HfpClient profile ready.") 79 public Boolean bluetoothHfpClientIsReady() { 80 return sIsHfpClientReady; 81 } 82 83 @Rpc(description = "Set priority of the profile") 84 public void bluetoothHfpClientSetPriority( 85 @RpcParameter(name = "device", description = "Mac address of a BT device.") 86 String deviceStr, 87 @RpcParameter(name = "priority", description = "Priority that needs to be set.") 88 Integer priority) 89 throws Exception { 90 if (sHfpClientProfile == null) return; 91 BluetoothDevice device = 92 BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(), deviceStr); 93 Log.d("Changing priority of device " + device.getAliasName() + " p: " + priority); 94 sHfpClientProfile.setPriority(device, priority); 95 } 96 97 @Rpc(description = "Get priority of the profile") 98 public Integer bluetoothHfpClientGetPriority( 99 @RpcParameter(name = "device", description = "Mac address of a BT device.") 100 String deviceStr) 101 throws Exception { 102 if (sHfpClientProfile == null) return BluetoothProfile.PRIORITY_UNDEFINED; 103 BluetoothDevice device = 104 BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(), deviceStr); 105 return sHfpClientProfile.getPriority(device); 106 } 107 108 @Rpc(description = "Connect to an HFP Client device.") 109 public Boolean bluetoothHfpClientConnect( 110 @RpcParameter(name = "device", description = "Name or MAC address of a bluetooth device.") 111 String deviceStr) 112 throws Exception { 113 if (sHfpClientProfile == null) return false; 114 try { 115 BluetoothDevice device = 116 BluetoothFacade.getDevice(BluetoothFacade.DiscoveredDevices, deviceStr); 117 Log.d("Connecting to device " + device.getAliasName()); 118 return hfpClientConnect(device); 119 } catch (Exception e) { 120 Log.e("bluetoothHfpClientConnect failed on getDevice " + deviceStr + " with " + e); 121 return false; 122 } 123 } 124 125 @Rpc(description = "Disconnect an HFP Client device.") 126 public Boolean bluetoothHfpClientDisconnect( 127 @RpcParameter(name = "device", description = "Name or MAC address of a device.") 128 String deviceStr) { 129 if (sHfpClientProfile == null) return false; 130 Log.d("Connected devices: " + sHfpClientProfile.getConnectedDevices()); 131 try { 132 BluetoothDevice device = 133 BluetoothFacade.getDevice(sHfpClientProfile.getConnectedDevices(), deviceStr); 134 return hfpClientDisconnect(device); 135 } catch (Exception e) { 136 // Do nothing since it is disconnect and this function should force disconnect. 137 Log.e("bluetoothHfpClientConnect getDevice failed " + e); 138 } 139 return false; 140 } 141 142 @Rpc(description = "Get all the devices connected through HFP Client.") 143 public List<BluetoothDevice> bluetoothHfpClientGetConnectedDevices() { 144 if (sHfpClientProfile == null) return new ArrayList<BluetoothDevice>(); 145 return sHfpClientProfile.getConnectedDevices(); 146 } 147 148 @Rpc(description = "Get the connection status of a device.") 149 public Integer bluetoothHfpClientGetConnectionStatus( 150 @RpcParameter(name = "deviceID", 151 description = "Name or MAC address of a bluetooth device.") 152 String deviceID) { 153 if (sHfpClientProfile == null) { 154 return BluetoothProfile.STATE_DISCONNECTED; 155 } 156 List<BluetoothDevice> deviceList = sHfpClientProfile.getConnectedDevices(); 157 BluetoothDevice device; 158 try { 159 device = BluetoothFacade.getDevice(deviceList, deviceID); 160 } catch (Exception e) { 161 Log.e(e); 162 return BluetoothProfile.STATE_DISCONNECTED; 163 } 164 return sHfpClientProfile.getConnectionState(device); 165 } 166 167 @Override 168 public void shutdown() { 169 } 170 } 171