1

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

4

4 回答 4

8

经过几天的挣扎,我现在已经成功了,干杯:)

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

    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 中的你们 :)

于 2011-05-29T16:16:25.283 回答
2

好的,我得到了更新以支持 Honeycomb 及更高版本。您需要向界面添加新功能。我在这里做到了:

interface IBluetoothA2dp {
boolean connectSink(in BluetoothDevice device); // Pre API 11 only
boolean disconnectSink(in BluetoothDevice device); // Pre API 11 only
boolean connect(in BluetoothDevice device); // API 11 and up only
boolean disconnect(in BluetoothDevice device); // API 11 and up only
boolean suspendSink(in BluetoothDevice device); // all
boolean resumeSink(in BluetoothDevice device); // all
BluetoothDevice[] getConnectedSinks();  // change to Set<> once AIDL supports, pre API 11 only
BluetoothDevice[] getNonDisconnectedSinks();  // change to Set<> once AIDL supports, 
int getSinkState(in BluetoothDevice device);
boolean setSinkPriority(in BluetoothDevice device, int priority); // Pre API 11 only
boolean setPriority(in BluetoothDevice device, int priority); // API 11 and up only
int getPriority(in BluetoothDevice device); // API 11 and up only
int getSinkPriority(in BluetoothDevice device); // Pre API 11 only
boolean isA2dpPlaying(in BluetoothDevice device); // API 11 and up only

}

然后在这个接口调用函数前需要检查API版本。这是我的例子:

            if (android.os.Build.VERSION.SDK_INT < 11) {

            IBluetoothA2dp ibta = getIBluetoothA2dp();
            try {
                Log.d(LOG_TAG, "Here: " + ibta.getSinkPriority(device));
                if (ibta != null)
                    ibta.connectSink(device);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Error " + e.getMessage());
            }
        } else {
            IBluetoothA2dp ibta = getIBluetoothA2dp();
            try {
                Log.d(LOG_TAG, "Here: " + ibta.getPriority(device));
                if (ibta != null)
                    ibta.connect(device);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Error " + e.getMessage());
            }
        }

希望这可以帮助。我能够让同一个应用程序同时使用这两个界面。

于 2012-01-11T04:09:43.847 回答
2

我在 Android 4.2 上试过这个,下面的行返回 null。它正在 4.1 上运行,有什么想法吗?

IBinder b = (IBinder) m2.invoke(null, "bluetooth_a2dp");
于 2012-11-20T02:15:06.813 回答
-2

这与连接/重新连接问题有关(答案是苹果脚本)..

我刚买了一个 android htc one V 并通过应用程序 PdaNet 将其用作热点(安装在我的手机和我的 mac os 10.5.8 ppc 笔记本电脑上)。

我似乎无法通过 wifi 或 USB 连接热点,但它确实适用于 BLUETOOTH!唯一的问题是连接最多只能持续 2 分钟到 40 分钟(现在看看它,有记录),我必须手动重新连接,这只需要 2 秒,但如果我的 mac 的网络方面可以自动重新连接。

我的手机不是问题,因为它会发出恒定的信号(尽管我的手机可能会暂时丢失信号,这是正常的连接问题)..问题是我的笔记本电脑自动重新连接。我的笔记本电脑和 htc one v DO 保持配对,笔记本电脑端没有自动重新连接。

我希望我知道苹果脚本 bcs 然后我可以编写一个自动重新连接丢失的蓝牙连接的苹果脚本..或者一个小部件可以做到这一点?如果是这样,我会把它放在阴凉处,因为蓝牙网络共享工作得很好。

我希望这能帮助我自己和其他人寻找相同的答案..如果你保持这个线程打开,我可以稍后返回一些可能的苹果脚本解决方案(我必须快速学习)..谢谢-marcus

于 2013-06-24T10:58:33.100 回答