1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade.bluetooth; 18 19 import java.util.List; 20 21 import android.app.Service; 22 import android.bluetooth.BluetoothHeadsetClient; 23 import android.bluetooth.BluetoothAdapter; 24 import android.bluetooth.BluetoothDevice; 25 import android.bluetooth.BluetoothProfile; 26 import android.bluetooth.BluetoothUuid; 27 import android.os.ParcelUuid; 28 29 import com.googlecode.android_scripting.Log; 30 import com.googlecode.android_scripting.facade.FacadeManager; 31 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 32 import com.googlecode.android_scripting.rpc.Rpc; 33 import com.googlecode.android_scripting.rpc.RpcParameter; 34 35 public class BluetoothHfpClientFacade extends RpcReceiver { 36 static final ParcelUuid[] UUIDS = { 37 BluetoothUuid.Handsfree_AG, 38 }; 39 40 private final Service mService; 41 private final BluetoothAdapter mBluetoothAdapter; 42 43 private static boolean sIsHfpClientReady = false; 44 private static BluetoothHeadsetClient sHfpClientProfile = null; 45 BluetoothHfpClientFacade(FacadeManager manager)46 public BluetoothHfpClientFacade(FacadeManager manager) { 47 super(manager); 48 mService = manager.getService(); 49 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 50 mBluetoothAdapter.getProfileProxy(mService, new HfpClientServiceListener(), 51 BluetoothProfile.HEADSET_CLIENT); 52 } 53 54 class HfpClientServiceListener implements BluetoothProfile.ServiceListener { 55 @Override onServiceConnected(int profile, BluetoothProfile proxy)56 public void onServiceConnected(int profile, BluetoothProfile proxy) { 57 sHfpClientProfile = (BluetoothHeadsetClient) proxy; 58 sIsHfpClientReady = true; 59 } 60 61 @Override onServiceDisconnected(int profile)62 public void onServiceDisconnected(int profile) { 63 sIsHfpClientReady = false; 64 } 65 } 66 hfpClientConnect(BluetoothDevice device)67 public Boolean hfpClientConnect(BluetoothDevice device) { 68 if (sHfpClientProfile == null) return false; 69 return sHfpClientProfile.connect(device); 70 } 71 hfpClientDisconnect(BluetoothDevice device)72 public Boolean hfpClientDisconnect(BluetoothDevice device) { 73 if (sHfpClientProfile == null) return false; 74 return sHfpClientProfile.disconnect(device); 75 } 76 77 @Rpc(description = "Is HfpClient profile ready.") bluetoothHfpClientIsReady()78 public Boolean bluetoothHfpClientIsReady() { 79 return sIsHfpClientReady; 80 } 81 82 @Rpc(description = "Connect to an HFP Client device.") bluetoothHfpClientConnect( @pcParametername = "device", description = "Name or MAC address of a bluetooth device.") String deviceStr)83 public Boolean bluetoothHfpClientConnect( 84 @RpcParameter(name = "device", description = "Name or MAC address of a bluetooth device.") 85 String deviceStr) 86 throws Exception { 87 if (sHfpClientProfile == null) return false; 88 try { 89 BluetoothDevice device = 90 BluetoothFacade.getDevice(BluetoothFacade.DiscoveredDevices, deviceStr); 91 Log.d("Connecting to device " + device.getAliasName()); 92 return hfpClientConnect(device); 93 } catch (Exception e) { 94 Log.e("bluetoothHfpClientConnect failed on getDevice " + deviceStr + " with " + e); 95 return false; 96 } 97 } 98 99 @Rpc(description = "Disconnect an HFP Client device.") bluetoothHfpClientDisconnect( @pcParametername = "device", description = "Name or MAC address of a device.") String deviceStr)100 public Boolean bluetoothHfpClientDisconnect( 101 @RpcParameter(name = "device", description = "Name or MAC address of a device.") 102 String deviceStr) { 103 if (sHfpClientProfile == null) return false; 104 Log.d("Connected devices: " + sHfpClientProfile.getConnectedDevices()); 105 try { 106 BluetoothDevice device = 107 BluetoothFacade.getDevice(sHfpClientProfile.getConnectedDevices(), deviceStr); 108 return hfpClientDisconnect(device); 109 } catch (Exception e) { 110 // Do nothing since it is disconnect and this function should force disconnect. 111 Log.e("bluetoothHfpClientConnect getDevice failed " + e); 112 } 113 return false; 114 } 115 116 @Rpc(description = "Get all the devices connected through HFP Client.") bluetoothHfpClientGetConnectedDevices()117 public List<BluetoothDevice> bluetoothHfpClientGetConnectedDevices() { 118 return sHfpClientProfile.getConnectedDevices(); 119 } 120 121 @Rpc(description = "Get the connection status of a device.") bluetoothHfpClientGetConnectionStatus( @pcParametername = "deviceID", description = "Name or MAC address of a bluetooth device.") String deviceID)122 public Integer bluetoothHfpClientGetConnectionStatus( 123 @RpcParameter(name = "deviceID", 124 description = "Name or MAC address of a bluetooth device.") 125 String deviceID) { 126 if (sHfpClientProfile == null) { 127 return BluetoothProfile.STATE_DISCONNECTED; 128 } 129 List<BluetoothDevice> deviceList = sHfpClientProfile.getConnectedDevices(); 130 BluetoothDevice device; 131 try { 132 device = BluetoothFacade.getDevice(deviceList, deviceID); 133 } catch (Exception e) { 134 Log.e(e); 135 return BluetoothProfile.STATE_DISCONNECTED; 136 } 137 return sHfpClientProfile.getConnectionState(device); 138 } 139 140 @Override shutdown()141 public void shutdown() { 142 } 143 } 144