android编程连接蓝牙BLE音箱,onServiceConnected()方法得不到执行,请问什么原因?
我在写一个小程序,效果是点击listView里的一项(列表是手机配对的蓝牙设备表),程序把被点击的(如蓝牙音箱)设备连接起来。
其中出现问题的代码段如下:
private void getBluetoothA2dp(){
Log.d("anil","getBluetoothA2dp()开始执行。");
mBluetoothAdapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Log.d("anil","onServiceConnected(){}开始执行。");
//调试中,这句log.d就执行不到了,onServiceConnected服务始终没有被连接,为什么?
if (profile == BluetoothProfile.A2DP) {
mBluetoothA2dp = (BluetoothA2dp) proxy;
}
}
@Override
public void onServiceDisconnected(int profile) {
}
},BluetoothProfile.A2DP);
if (mBluetoothA2dp==null) Log.d("anil", "getBluetoothA2dp: 没获得mBluetoothA2dp");
}
这段代码是网上很多例子实现连接蓝牙设备的基础代码,我没有做任何不合适的改动。但运行后,根据我log出来的信息看,onServiceConneted()方法一直得不到执行,也就是说得不到BluetoothA2dp的句柄。
有没有哪位熟悉蓝牙开发的老师能告诉我,我的代码里出了什么问题?非常感谢!