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 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 import com.googlecode.android_scripting.rpc.RpcParameter; 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 @Rpc(description = "Is Avrcp profile ready.") bluetoothAvrcpIsReady()69 public Boolean bluetoothAvrcpIsReady() { 70 return sIsAvrcpReady; 71 } 72 73 @Rpc(description = "Get all the devices connected through AVRCP.") bluetoothAvrcpGetConnectedDevices()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.") bluetoothAvrcpDisconnect()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.") bluetoothAvrcpSendPassThroughCmd( @pcParametername = "deviceID", description = "Name or MAC address of a bluetooth device.") String deviceID, @RpcParameter(name = "keyCode") Integer keyCode, @RpcParameter(name = "keyState") Integer keyState)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 shutdown()114 public void shutdown() { 115 } 116 } 117