• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 = "Set priority of the profile")
bluetoothHspSetPriority( @pcParametername = "device", description = "Mac address of a BT device.") String deviceStr, @RpcParameter(name = "priority", description = "Priority that needs to be set.") Integer priority)83   public void bluetoothHspSetPriority(
84       @RpcParameter(name = "device", description = "Mac address of a BT device.")
85       String deviceStr,
86       @RpcParameter(name = "priority", description = "Priority that needs to be set.")
87       Integer priority)
88       throws Exception {
89     if (sHspProfile == null) return;
90     BluetoothDevice device =
91         BluetoothFacade.getDevice(mBluetoothAdapter.getBondedDevices(), deviceStr);
92     Log.d("Changing priority of device " + device.getAliasName() + " p: " + priority);
93     sHspProfile.setPriority(device, priority);
94   }
95 
96   @Rpc(description = "Connect to an HSP device.")
bluetoothHspConnect( @pcParametername = "device", description = "Name or MAC address of a bluetooth device.") String device)97   public Boolean bluetoothHspConnect(
98       @RpcParameter(name = "device", description = "Name or MAC address of a bluetooth device.")
99       String device)
100       throws Exception {
101     if (sHspProfile == null)
102       return false;
103     BluetoothDevice mDevice = BluetoothFacade.getDevice(BluetoothFacade.DiscoveredDevices, device);
104     Log.d("Connecting to device " + mDevice.getAliasName());
105     return hspConnect(mDevice);
106   }
107 
108   @Rpc(description = "Disconnect an HSP device.")
bluetoothHspDisconnect( @pcParametername = "device", description = "Name or MAC address of a device.") String device)109   public Boolean bluetoothHspDisconnect(
110       @RpcParameter(name = "device", description = "Name or MAC address of a device.")
111       String device)
112       throws Exception {
113     if (sHspProfile == null)
114       return false;
115     Log.d("Connected devices: " + sHspProfile.getConnectedDevices());
116     BluetoothDevice mDevice = BluetoothFacade.getDevice(sHspProfile.getConnectedDevices(),
117                                                         device);
118     return hspDisconnect(mDevice);
119   }
120 
121   @Rpc(description = "Get all the devices connected through HSP.")
bluetoothHspGetConnectedDevices()122   public List<BluetoothDevice> bluetoothHspGetConnectedDevices() {
123     while (!sIsHspReady);
124     return sHspProfile.getConnectedDevices();
125   }
126 
127   @Rpc(description = "Get the connection status of a device.")
bluetoothHspGetConnectionStatus( @pcParametername = "deviceID", description = "Name or MAC address of a bluetooth device.") String deviceID)128   public Integer bluetoothHspGetConnectionStatus(
129           @RpcParameter(name = "deviceID",
130                         description = "Name or MAC address of a bluetooth device.")
131           String deviceID) {
132       if (sHspProfile == null) {
133           return BluetoothProfile.STATE_DISCONNECTED;
134       }
135       List<BluetoothDevice> deviceList = sHspProfile.getConnectedDevices();
136       BluetoothDevice device;
137       try {
138           device = BluetoothFacade.getDevice(deviceList, deviceID);
139       } catch (Exception e) {
140           return BluetoothProfile.STATE_DISCONNECTED;
141       }
142       return sHspProfile.getConnectionState(device);
143   }
144 
145   @Override
shutdown()146   public void shutdown() {
147   }
148 }
149