• 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.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.List;
22 
23 import android.app.Service;
24 import android.bluetooth.BluetoothAdapter;
25 import android.bluetooth.BluetoothAvrcpController;
26 import android.bluetooth.BluetoothDevice;
27 import android.bluetooth.BluetoothProfile;
28 import android.bluetooth.BluetoothUuid;
29 import android.os.ParcelUuid;
30 
31 import com.googlecode.android_scripting.Log;
32 import com.googlecode.android_scripting.facade.FacadeManager;
33 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
34 import com.googlecode.android_scripting.rpc.Rpc;
35 
36 public 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 
BluetoothAvrcpFacade(FacadeManager manager)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
onServiceConnected(int profile, BluetoothProfile proxy)56     public void onServiceConnected(int profile, BluetoothProfile proxy) {
57       sAvrcpProfile = (BluetoothAvrcpController) proxy;
58       sIsAvrcpReady = true;
59     }
60 
61     @Override
onServiceDisconnected(int profile)62     public void onServiceDisconnected(int profile) {
63       sIsAvrcpReady = false;
64     }
65   }
66 
67   @Rpc(description = "Is Avrcp profile ready.")
bluetoothAvrcpIsReady()68   public Boolean bluetoothAvrcpIsReady() {
69     return sIsAvrcpReady;
70   }
71 
72   @Rpc(description = "Get all the devices connected through AVRCP.")
bluetoothAvrcpGetConnectedDevices()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.")
bluetoothAvrcpDisconnect()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
shutdown()95   public void shutdown() {
96   }
97 }
98