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
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;
35
36public class BluetoothAvrcpFacade extends RpcReceiver {
37  static final ParcelUuid[] AVRCP_UUIDS = {
38    BluetoothUuid.AvrcpTarget, BluetoothUuid.AvrcpController
39  };
40  private final Service mService;
41  private final BluetoothAdapter mBluetoothAdapter;
42
43  private static boolean sIsAvrcpReady = false;
44  private static BluetoothAvrcpController sAvrcpProfile = null;
45
46  public BluetoothAvrcpFacade(FacadeManager manager) {
47    super(manager);
48    mService = manager.getService();
49    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
50    mBluetoothAdapter.getProfileProxy(mService, new AvrcpServiceListener(),
51        BluetoothProfile.AVRCP_CONTROLLER);
52  }
53
54  class AvrcpServiceListener implements BluetoothProfile.ServiceListener {
55    @Override
56    public void onServiceConnected(int profile, BluetoothProfile proxy) {
57      sAvrcpProfile = (BluetoothAvrcpController) proxy;
58      sIsAvrcpReady = true;
59    }
60
61    @Override
62    public void onServiceDisconnected(int profile) {
63      sIsAvrcpReady = false;
64    }
65  }
66
67  @Rpc(description = "Is Avrcp profile ready.")
68  public Boolean bluetoothAvrcpIsReady() {
69    return sIsAvrcpReady;
70  }
71
72  @Rpc(description = "Get all the devices connected through AVRCP.")
73  public List<BluetoothDevice> bluetoothAvrcpGetConnectedDevices() {
74    if (!sIsAvrcpReady) {
75        Log.d("AVRCP profile is not ready.");
76        return null;
77    }
78    return sAvrcpProfile.getConnectedDevices();
79  }
80
81  @Rpc(description = "Close AVRCP connection.")
82  public void bluetoothAvrcpDisconnect() throws NoSuchMethodException,
83                                                IllegalAccessException,
84                                                IllegalArgumentException,
85                                                InvocationTargetException {
86      if (!sIsAvrcpReady) {
87          Log.d("AVRCP profile is not ready.");
88          return;
89      }
90      Method m = sAvrcpProfile.getClass().getMethod("close");
91      m.invoke(sAvrcpProfile);
92  }
93
94  @Override
95  public void shutdown() {
96  }
97}
98