Android 连接到配对的蓝牙耳机 [英] Android connect to a paired bluetooth headset

查看:29
本文介绍了Android 连接到配对的蓝牙耳机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to simulate the action of going throgh Settings->Wireless->Bluetooth and connect a paired bluetooth headset programmatically. I've done some search in Stackoverflow and Google, both indicating that there is no solution available before API level 11. However, I'm interested in working it out by peeking in the source code of bluetooth implementation of Android. The problem is that I'm not aware of which specific source code I should review. Any suggestions? Many thanks.

推荐答案

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

OK, I got this updated to support Honeycomb and up. You need to add new functions to the interface. I did that here:

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版本.这是我的例子:

Then you need to check the API version before calling functions in this interface. Here is my example:

            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());
            }
        }

希望这会有所帮助.我能够让同一个应用程序与两个界面一起使用.

Hope this helps. I was able to get the same app to work with both interfaces.

这篇关于Android 连接到配对的蓝牙耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆