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
17package com.googlecode.android_scripting.facade.bluetooth;
18
19import java.lang.reflect.InvocationTargetException;
20import java.lang.reflect.Method;
21import java.util.List;
22
23import android.app.Service;
24import android.bluetooth.BluetoothAdapter;
25import android.bluetooth.BluetoothAvrcpController;
26import android.bluetooth.BluetoothDevice;
27import android.bluetooth.BluetoothProfile;
28import android.bluetooth.BluetoothUuid;
29import android.os.ParcelUuid;
30
31import com.googlecode.android_scripting.Log;
32import com.googlecode.android_scripting.facade.FacadeManager;
33import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
34import com.googlecode.android_scripting.rpc.Rpc;
35import com.googlecode.android_scripting.rpc.RpcParameter;
36
37public class BluetoothAvrcpFacade extends RpcReceiver {
38  static final ParcelUuid[] AVRCP_UUIDS = {
39    BluetoothUuid.AvrcpTarget, BluetoothUuid.AvrcpController
40  };
41  private final Service mService;
42  private final BluetoothAdapter mBluetoothAdapter;
43
44  private static boolean sIsAvrcpReady = false;
45  private static BluetoothAvrcpController sAvrcpProfile = null;
46
47  public BluetoothAvrcpFacade(FacadeManager manager) {
48    super(manager);
49    mService = manager.getService();
50    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
51    mBluetoothAdapter.getProfileProxy(mService, new AvrcpServiceListener(),
52        BluetoothProfile.AVRCP_CONTROLLER);
53  }
54
55  class AvrcpServiceListener implements BluetoothProfile.ServiceListener {
56    @Override
57    public void onServiceConnected(int profile, BluetoothProfile proxy) {
58      sAvrcpProfile = (BluetoothAvrcpController) proxy;
59      sIsAvrcpReady = true;
60    }
61
62    @Override
63    public void onServiceDisconnected(int profile) {
64      sIsAvrcpReady = false;
65    }
66  }
67
68  @Rpc(description = "Is Avrcp profile ready.")
69  public Boolean bluetoothAvrcpIsReady() {
70    return sIsAvrcpReady;
71  }
72
73  @Rpc(description = "Get all the devices connected through AVRCP.")
74  public List<BluetoothDevice> bluetoothAvrcpGetConnectedDevices() {
75    if (!sIsAvrcpReady) {
76        Log.d("AVRCP profile is not ready.");
77        return null;
78    }
79    return sAvrcpProfile.getConnectedDevices();
80  }
81
82  @Rpc(description = "Close AVRCP connection.")
83  public void bluetoothAvrcpDisconnect() throws NoSuchMethodException,
84                                                IllegalAccessException,
85                                                IllegalArgumentException,
86                                                InvocationTargetException {
87      if (!sIsAvrcpReady) {
88          Log.d("AVRCP profile is not ready.");
89          return;
90      }
91      Method m = sAvrcpProfile.getClass().getMethod("close");
92      m.invoke(sAvrcpProfile);
93  }
94
95  @Rpc(description = "Send AVRPC passthrough command.")
96  public void bluetoothAvrcpSendPassThroughCmd(
97          @RpcParameter(name = "deviceID",
98                        description = "Name or MAC address of a bluetooth device.")
99          String deviceID,
100          @RpcParameter(name = "keyCode")
101          Integer keyCode,
102          @RpcParameter(name = "keyState")
103          Integer keyState) throws Exception {
104      if (!sIsAvrcpReady) {
105          Log.d("AVRCP profile is not ready.");
106          return;
107      }
108      BluetoothDevice mDevice = BluetoothFacade.getDevice(sAvrcpProfile.getConnectedDevices(),
109                                                          deviceID);
110      sAvrcpProfile.sendPassThroughCmd(mDevice, keyCode, keyState);
111  }
112
113  @Override
114  public void shutdown() {
115  }
116}
117