其他分享
首页 > 其他分享> > Android连接到配对的蓝牙耳机

Android连接到配对的蓝牙耳机

作者:互联网

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

解决方法:

好的,我更新了这个以支持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());
            }
        }

希望这可以帮助.我能够使用相同的应用程序来使用这两个界面.

标签:android,bluetooth,headset
来源: https://codeday.me/bug/20191005/1855020.html