需求:Android 平板做为一个音响的功能使用。已经移植 BT 的 a2dp sink 的功能。现在需要加上 平板控制手机的声音播放,暂停,音量大小等功能


移植:
    
平台: rk3368 系统 android6.0 


    按网上的资料,http://blog.csdn.net/shichaog/article/details/52182987 。在 Android stdio 里移植,发现
import android.bluetooth.BluetoothAvrcpController;
import android.bluetooth.BluetoothAvrcp;
这两个 import 是找不到。查看源码,其实里面有的。网上查出原因,是因为用了 @hide 来隐藏,不暴露给app

于是想到在系统app 里尝试能否使用。

测试 在系统的源码里  package/app/Bluetooth 里的 src/com/android/bluetooth/a2dp/A2dpSinkStateMachine.java 加上
+import android.bluetooth.BluetoothAvrcpController;
+import android.bluetooth.BluetoothAvrcp;
     编译,能够通过。证明系统app 是可以调用的。
 
于是在 A2dpSinkStateMachine.java 加了一个 
+           mAvrcpController.sendPassThroughCmd(device, BluetoothAvrcp.PASSTHROUGH_ID_STOP, BluetoothAvrcp.PASSTHROUGH_STATE_PRESS);
+           mAvrcpController.sendPassThroughCmd(device, BluetoothAvrcp.PASSTHROUGH_ID_STOP, BluetoothAvrcp.PASSTHROUGH_STATE_RELEASE);
来暂停
 
那么问题来了,如何获取 mAvrcpController
 
看例子发现,需要一个 Listener
于是加了
 
 
+    private BluetoothProfile.ServiceListener mAvrcpServiceListener = new BluetoothProfile.ServiceListener(){
+        @Override
+        public void onServiceConnected(int profile, BluetoothProfile proxy) {
+        Log.d(TAG, "BT profile Service connected");
+            if (profile == BluetoothProfile.AVRCP_CONTROLLER){
+                Log.d(TAG, "AvrcpControllerService connected");
+
+                mAvrcpController = (BluetoothAvrcpController) proxy;
+//                mAvrcpController.setCallback(new AvrcpControllerCallback());
+
+                Log.d(TAG, "Avrcp devices: ");
+                List devices = mAvrcpController.getConnectedDevices();
+                for (BluetoothDevice device : devices)
+                    Log.d(TAG, " - " + device.getName() + " " + device.getAddress());
+            }
+        }
+
+        @Override
+        public void onServiceDisconnected(int profile) {
+            if (profile == BluetoothProfile.AVRCP_CONTROLLER) {
+                Log.d(TAG, "AvrcpControllerService disconnected");
+                //mAvrcpController.removeCallback();
+                mAvrcpController = null;
+            }
+        }
+    };
+


   然而程序进不了 onServiceConnected ,mAvrcpController 为空
   
   在framework 里加 log 发现 在 frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java 里的 BluetoothAvrcpController avrcp = new BluetoothAvrcpController(context, listener);出了错。怀疑是 服务没有启动。
   
12-12 21:02:16.207  1700  1700 D BluetoothAdapter: wade getProfileProxy12
12-12 21:02:16.207  1700  1700 D BluetoothAdapter: wade new BluetoothAvrcpController
12-12 21:02:16.210  1700  1700 E BluetoothAvrcpController: Could not bind to Bluetooth AVRCP Controller Service with Intent { act=android.bluetooth.IBluetoothAvrcpController }


  怎么启动 avrcp 的服务呢?没有想到很好的办法。比较以前有的一些补丁和源码。发现了问题。
  
--- a/packages/apps/Bluetooth/res/values/config.xml
+++ b/packages/apps/Bluetooth/res/values/config.xml
@@ -26,7 +26,7 @@
     false
     true
     true
-    false
+    true
     false
 
   在修改了这个之后,终于可以暂停手机端的音乐暂停了。
   
   
 总结:
    1. 编译要在源码里,编译系统app ,android stdio 用不了。
2. mAvrcpController 的获取需要加一个 BluetoothProfile.ServiceListener
3.系统本身要修改 config.xml 以打开 BluetoothAvrcpController 服务。
4. 只做到了一播放就暂停。关于场景的工作,(何时暂停,播放,或者按钮暂停播放没有做)

附所有的修改
app :
diff --git a/packages/apps/Bluetooth/res/values/config.xml b/packages/apps/Bluetooth/res/values/config.xml
index 0262064..57c8345 100644
--- a/packages/apps/Bluetooth/res/values/config.xml
+++ b/packages/apps/Bluetooth/res/values/config.xml
@@ -26,7 +26,7 @@
     false
     true
     true
-    false
+    true
     false
 
     

更多相关文章

  1. Android提供的系统服务之--LayoutInflater(布局服务)
  2. Android编译系统详解(二)——命令执行流程
  3. [置顶] Android 开发即时聊天工具 YQ 《更新》(源码下载)
  4. android系统编译adt插件
  5. Android 系统编译移除应用
  6. Android源码博文集锦1
  7. Android 4.0源码编译不生成odex

随机推荐

  1. 使用Repo在Windows平台下載Android原始碼
  2. android API1.6--2.1变更摘要
  3. 【Android(安卓)开发教程】隐藏Activity
  4. Android权限注解
  5. android 模拟器 使用SD卡
  6. Android开发之InstanceState详解
  7. This template depends on the Android(
  8. android adt 最新下载地址
  9. React Native 中文版(含最新Android章节)
  10. android访问本地web(不是127.0.0.1而是10