android编程连接蓝牙BLE音箱,onServiceConnected()方法得不到执行,请问什么原因?

anil1973 2017-01-04 12:03:21
我在写一个小程序,效果是点击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的句柄。
有没有哪位熟悉蓝牙开发的老师能告诉我,我的代码里出了什么问题?非常感谢!
...全文
525 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice_seven 2020-09-05
  • 打赏
  • 举报
回复
仍然没找到结果啊?最后怎么实现的
Birds2018 2017-01-04
  • 打赏
  • 举报
回复
看看是否有权限,然后你换手机试试,
anil1973 2017-01-04
  • 打赏
  • 举报
回复
对应的logcat如下: 01-04 22:15:11.366 3436-3436/? D/anil: waiting for BluetoothAdapter is turned on.0 01-04 22:15:11.369 3436-3436/? D/anil: Mi Bluetooth Speaker is found. 01-04 22:15:11.375 3436-3436/? D/anil: connect运行。 可以看见,蓝牙设备是找到了。getBluetoothA2dp() 里面的语句没执行,直接执行了connect()方法,然后程序退出。 到底是什么原因?为什么从api例子里复制的获得A2DP的代码都执行不了?很困惑。期待大家能给我指点。谢谢!
anil1973 2017-01-04
  • 打赏
  • 举报
回复
为了查找问题,我把listview蓝牙设备列表等都去掉,搭建了一个最简的环境,activity里只干了几件事: 1、打开手机的蓝牙。 2、getProfileA2dp() 3、connect() (用一个小米音箱做测试。用它的mac地址找到mBluetoothDevice) 但就是这么最简的代码,依然是一楼提到的故障,onSerivceConnected没有执行,拿不到A2DP的句柄。 代码很短,如下: package com.qx.wanke.bletest2; import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class MainActivity extends AppCompatActivity { private static final String TAG = "anil"; private BluetoothA2dp mBluetoothA2dp; private BluetoothAdapter mBluetoothAdapter; private BluetoothDevice mBluetoothDevice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.enable(); } int i = 0; while (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) { i = i++; } Log.d(TAG, "waiting for BluetoothAdapter is turned on." + String.valueOf(i)); mBluetoothDevice = mBluetoothAdapter.getRemoteDevice("E8:07:BF:00:C5:8D"); if (mBluetoothDevice != null) { Log.d("anil", mBluetoothDevice.getName() + " is found."); } getBluetoothA2dp(); connect(); } private void getBluetoothA2dp(){ mBluetoothAdapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { Log.d(TAG, "onServiceConnected运行。"); if(profile==BluetoothProfile.A2DP){ mBluetoothA2dp=(BluetoothA2dp)proxy; Log.d(TAG, "onServiceConnected: 获得A2dp"); } } @Override public void onServiceDisconnected(int profile) { } },BluetoothProfile.A2DP); } private void connect(){ try { Log.d(TAG, "connect运行。"); Method connect = mBluetoothA2dp.getClass().getDeclaredMethod("connect", BluetoothDevice.class); connect.setAccessible(true); connect.invoke(mBluetoothA2dp,mBluetoothDevice); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { Log.e(TAG,"connect exception:"+e); e.printStackTrace(); } } }
anil1973 2017-01-04
  • 打赏
  • 举报
回复
谢谢楼上各位朋友的指点。我再把大家提到的问题说明一下: 1、我手边几台手机(mi5、魅族等)都试了,都出现同样的问题。 2、蓝牙设备是两个蓝牙4.0 BLE音箱,都无法连接。(不过按我对这段代码的理解,这里还没走到连接蓝牙设备的地方,只是要获取手机的A2DP协议的句柄) 3、权限都声明了。一个Bluetooth、一个Bluetooth-Admin 4、mBluetoothAdapter应该是正常的,在活动的前面就定义了BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();。而且一楼这段出错代码之前,我还用它获取了.getBondedDevices()。 5、@bigtree_mfc , 您所说的“绑定服务”,具体是指哪个服务?我看api例子里的这段代码,好象就是“getProfileProxy”方法后就直接onServiceConnected()。因为才接触蓝牙开发,所以也不太清楚这些写,算不算已经绑定服务了呢? 6、我又参考了一些网上的帖子,把一楼代码里的getProfileProxy()的句子,分开写成2行: 1 private void getBluetoothA2dp(){ 2 Log.d("anil","getBluetoothA2dp()开始执行。"); 3 // if (mBluetoothAdapter==null) Log.d("anil", "mBluetoothAdapter为空"); 运行后这句没打印,说明mBA不为空 4 mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.A2DP); 5 private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { 这时候更奇怪的是:编译时就直接提示: 第4行里面的mProfileListener没定义(我参考网上帖子的代码里,这个mProfileListener前面都没有声明啊。) 第5行里面的mProfileListener没使用。 谢谢楼上各位朋友的指导,希望还能帮我再看看,到底是哪里出了问题?(如果有必要的话,我把完整的代码贴出来)
大树学长 2017-01-04
  • 打赏
  • 举报
回复
绑定服务了么?
R_ine 2017-01-04
  • 打赏
  • 举报
回复
对这块表示很了解=-= 你可以这么做=-=去随便下个蓝牙DEMO。看看其是否能正常运行=-=然后再将其的主要代码拷贝过来=-=,当然我觉得楼上所说=-=权限问题,也是一个问题。你可以先下载个蓝牙小DEMO运行下。
严振杰 2017-01-04
  • 打赏
  • 举报
回复
这个还真不是很熟悉,提供几个参考意见吧。1. 那个蓝牙设备在身边吗?2. 楼上说的权限问题。 如果连接不到,mBluetoothAdapter调用getProfileProxy()是很简单没有问题,那么mBluetoothAdapter本身有没有问题呢,检查一下这个。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧