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.BluetoothHeadset;
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 BluetoothHspFacade extends RpcReceiver {
36   static final ParcelUuid[] UUIDS = {
37     BluetoothUuid.HSP, BluetoothUuid.Handsfree
38   };
39 
40   private final Service mService;
41   private final BluetoothAdapter mBluetoothAdapter;
42 
43   private static boolean sIsHspReady = false;
44   private static BluetoothHeadset sHspProfile = null;
45 
BluetoothHspFacade(FacadeManager manager)46   public BluetoothHspFacade(FacadeManager manager) {
47     super(manager);
48     mService = manager.getService();
49     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
50     mBluetoothAdapter.getProfileProxy(mService, new HspServiceListener(),
51         BluetoothProfile.HEADSET);
52   }
53 
54   class HspServiceListener implements BluetoothProfile.ServiceListener {
55     @Override
onServiceConnected(int profile, BluetoothProfile proxy)56     public void onServiceConnected(int profile, BluetoothProfile proxy) {
57       sHspProfile = (BluetoothHeadset) proxy;
58       sIsHspReady = true;
59     }
60 
61     @Override
onServiceDisconnected(int profile)62     public void onServiceDisconnected(int profile) {
63       sIsHspReady = false;
64     }
65   }
66 
hspConnect(BluetoothDevice device)67   public Boolean hspConnect(BluetoothDevice device) {
68     if (sHspProfile == null) return false;
69     return sHspProfile.connect(device);
70   }
71 
hspDisconnect(BluetoothDevice device)72   public Boolean hspDisconnect(BluetoothDevice device) {
73     if (sHspProfile == null) return false;
74     return sHspProfile.disconnect(device);
75   }
76 
77   @Rpc(description = "Is Hsp profile ready.")
bluetoothHspIsReady()78   public Boolean bluetoothHspIsReady() {
79     return sIsHspReady;
80   }
81 
82   @Rpc(description = "Connect to an HSP device.")
bluetoothHspConnect( @pcParametername = "device", description = "Name or MAC address of a bluetooth device.") String device)83   public Boolean bluetoothHspConnect(
84       @RpcParameter(name = "device", description = "Name or MAC address of a bluetooth device.")
85       String device)
86       throws Exception {
87     if (sHspProfile == null)
88       return false;
89     BluetoothDevice mDevice = BluetoothFacade.getDevice(BluetoothFacade.DiscoveredDevices, device);
90     Log.d("Connecting to device " + mDevice.getAliasName());
91     return hspConnect(mDevice);
92   }
93 
94   @Rpc(description = "Disconnect an HSP device.")
bluetoothHspDisconnect( @pcParametername = "device", description = "Name or MAC address of a device.") String device)95   public Boolean bluetoothHspDisconnect(
96       @RpcParameter(name = "device", description = "Name or MAC address of a device.")
97       String device)
98       throws Exception {
99     if (sHspProfile == null)
100       return false;
101     Log.d("Connected devices: " + sHspProfile.getConnectedDevices());
102     BluetoothDevice mDevice = BluetoothFacade.getDevice(sHspProfile.getConnectedDevices(),
103                                                         device);
104     return hspDisconnect(mDevice);
105   }
106 
107   @Rpc(description = "Get all the devices connected through HSP.")
bluetoothHspGetConnectedDevices()108   public List<BluetoothDevice> bluetoothHspGetConnectedDevices() {
109     while (!sIsHspReady);
110     return sHspProfile.getConnectedDevices();
111   }
112 
113   @Rpc(description = "Get the connection status of a device.")
bluetoothHspGetConnectionStatus( @pcParametername = "deviceID", description = "Name or MAC address of a bluetooth device.") String deviceID)114   public Integer bluetoothHspGetConnectionStatus(
115           @RpcParameter(name = "deviceID",
116                         description = "Name or MAC address of a bluetooth device.")
117           String deviceID) {
118       if (sHspProfile == null) {
119           return BluetoothProfile.STATE_DISCONNECTED;
120       }
121       List<BluetoothDevice> deviceList = sHspProfile.getConnectedDevices();
122       BluetoothDevice device;
123       try {
124           device = BluetoothFacade.getDevice(deviceList, deviceID);
125       } catch (Exception e) {
126           return BluetoothProfile.STATE_DISCONNECTED;
127       }
128       return sHspProfile.getConnectionState(device);
129   }
130 
131   @Override
shutdown()132   public void shutdown() {
133   }
134 }
135