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 20 import android.app.Service; 21 import android.bluetooth.BluetoothAdapter; 22 import android.bluetooth.BluetoothAvrcpController; 23 import android.bluetooth.BluetoothDevice; 24 import android.bluetooth.BluetoothProfile; 25 import android.bluetooth.BluetoothUuid; 26 import android.os.ParcelUuid; 27 28 import com.googlecode.android_scripting.Log; 29 import com.googlecode.android_scripting.facade.FacadeManager; 30 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 31 import com.googlecode.android_scripting.rpc.Rpc; 32 33 import java.lang.reflect.InvocationTargetException; 34 import java.lang.reflect.Method; 35 import java.util.List; 36 37 public 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 BluetoothAvrcpFacade(FacadeManager manager)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 onServiceConnected(int profile, BluetoothProfile proxy)57 public void onServiceConnected(int profile, BluetoothProfile proxy) { 58 sAvrcpProfile = (BluetoothAvrcpController) proxy; 59 sIsAvrcpReady = true; 60 } 61 62 @Override onServiceDisconnected(int profile)63 public void onServiceDisconnected(int profile) { 64 sIsAvrcpReady = false; 65 } 66 } 67 68 /** 69 * Is Avrcp profile ready. 70 * @return Avrcp profile is ready or not. 71 */ 72 @Rpc(description = "Is Avrcp profile ready.") bluetoothAvrcpIsReady()73 public Boolean bluetoothAvrcpIsReady() { 74 return sIsAvrcpReady; 75 } 76 77 /** 78 * Get all the devices connected through AVRCP. 79 * @return Lsit of the devices connected through AVRCP. 80 */ 81 @Rpc(description = "Get all the devices connected through AVRCP.") bluetoothAvrcpGetConnectedDevices()82 public List<BluetoothDevice> bluetoothAvrcpGetConnectedDevices() { 83 if (!sIsAvrcpReady) { 84 Log.d("AVRCP profile is not ready."); 85 return null; 86 } 87 return sAvrcpProfile.getConnectedDevices(); 88 } 89 90 /** 91 * Close AVRCP connection. 92 */ 93 @Rpc(description = "Close AVRCP connection.") bluetoothAvrcpDisconnect()94 public void bluetoothAvrcpDisconnect() throws NoSuchMethodException, 95 IllegalAccessException, IllegalArgumentException, 96 InvocationTargetException { 97 if (!sIsAvrcpReady) { 98 Log.d("AVRCP profile is not ready."); 99 return; 100 } 101 Method m = sAvrcpProfile.getClass().getMethod("close"); 102 m.invoke(sAvrcpProfile); 103 } 104 105 @Override shutdown()106 public void shutdown() { 107 } 108 } 109