安卓连接配对的蓝牙耳机

标签 android bluetooth headset

我想模拟通过“设置”->“无线”->“蓝牙”并以编程方式连接配对的蓝牙耳机的操作。我在 Stackoverflow 和 Google 上做了一些搜索,都表明在 API 级别 11 之前没有可用的解决方案。但是,我有兴趣通过查看 Android 蓝牙实现的源代码来解决这个问题。问题是我不知道应该查看哪个特定源代码。有什么建议么?非常感谢。

最佳答案

经过几天的努力,我现在成功了,干杯:)

  1. 将 android.bluetooth.IBluetoothA2dp.aidl 添加到您应用的/src 目录中;
  2. 在您的代码中添加此私有(private)方法:

    private IBluetoothA2dp getIBluetoothA2dp() {
    
    IBluetoothA2dp ibta = null;
    
    try {
    
        Class c2 = Class.forName("android.os.ServiceManager");
    
        Method m2 = c2.getDeclaredMethod("getService", String.class);
        IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp");
    
        Log.d("Felix", "Test2: " + b.getInterfaceDescriptor());
    
        Class c3 = Class.forName("android.bluetooth.IBluetoothA2dp");
    
        Class[] s2 = c3.getDeclaredClasses();
    
        Class c = s2[0];
        // printMethods(c);
        Method m = c.getDeclaredMethod("asInterface", IBinder.class);
    
        m.setAccessible(true);
        ibta = (IBluetoothA2dp) m.invoke(null, b);
    
    } catch (Exception e) {
        Log.e("flowlab", "Erroraco!!! " + e.getMessage());
    }
    
  3. 用这个测试它:

    private void testBluetoothA2dp(BluetoothDevice device) {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub
    IBluetoothA2dp ibta = getIBluetoothA2dp();
    try {
        Log.d("Felix", "Here: " + ibta.getSinkPriority(device));
        ibta.connectSink(device);
    } catch (RemoteException e) {
        // * TODO Auto-generated catch block
        e.printStackTrace();
    }
    

我无法提供这些代码的引用,因为我花了很多时间谷歌搜索、检查 stackoverflow 和审查 Android 源代码,但未能跟踪源代码。非常感谢 Stackoverflow 的你们 :)

关于安卓连接配对的蓝牙耳机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161870/

相关文章:

android - PowerMock 模拟 MoreAsserts

android - 当我从垂直 View 更改为水平 View 时,为什么我的歌曲会重新开始播放?

python - 尝试通过蓝牙将串行数据从 arduino 传感器发送到 python

java - Android 蓝牙-MAC 地址

android - 如何检测耳机功能

c# - 连接到电脑上的蓝牙耳机

Android BluetoothGatt - 状态 133 - 注册回调

ios - 如何将 iPhone 与外部设备连接?

android - Android2.2应用启动时如何获取蓝牙耳机连接状态?

android - 带虚线的 Google Static Maps API 路径