Android BLE & BluetoothGattCallback.onServicesDiscovered不回调或部分回调解决

A.

如题,调用BluetoothGatt.discoverServices()返回true了,但是一直没走到onServicesDiscovered回调中,用Ble调试蓝牙助手测试了,也是一样的情况,所以应该是外设的问题,但是ios的没问题,蓝牙处理还是有差别,网上有一种方式说是延迟去多discoverServices几次,可能可以找到,尝试了一下还是不行,最后用了下面的方式基本可以解决:

加多个标志,就是连接里面加多个5s的延迟操作,如果5s后还没走回调,就重新发起连接,测试基本没问题,虽然解决了但是不知道什么原因导致的,有知道的老哥可以留言下

mHandler.postDelayed(new Runnable() {
   @Override public void run() { 
     if(!isDiscoverServices){//如果还没找到服务-执行重连 
        connectDevice(device,context);
      } 
   } 
},5000);

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    isDiscoverServices=true;
}

https://blog.csdn.net/Canra/article/details/124979522

 

B.

1.调用gatt.discoverService需要写在onConnectionStateChange方法里面。

2.延时多调用几次discoverService方法。

https://blog.csdn.net/aozhenmo2498/article/details/101411611

 

C.

在网上搜寻到方案是在gatt.discoverServices()之前加延时也只能部分解决问题,当时估计应该是线程问题。后来将涉及到UI线程的控件更新去除,无需延时完美解决。

 

https://blog.csdn.net/hujiusi185/article/details/106681636/

 

refs:

Android BLE 蓝牙编程(一)
https://www.cnblogs.com/wobeinianqing/p/5838273.html

Android BLE 蓝牙编程(二)
https://www.cnblogs.com/wobeinianqing/p/5879851.html

Android BLE 蓝牙编程(三)
https://www.cnblogs.com/wobeinianqing/p/5883135.html

Android BLE 蓝牙编程(四)
https://www.cnblogs.com/wobeinianqing/p/5886756.html

================================

Android BLE开发指南(一)入门基础
https://blog.csdn.net/kuaiguixs/article/details/90755223

Android BLE开发指南(二)中心设备端程序开发详解
https://blog.csdn.net/kuaiguixs/article/details/91437759

Android BLE 蓝牙开发指南(三)外围设备端开发详解
https://blog.csdn.net/kuaiguixs/article/details/91886524

posted @ 2024-09-05 18:23  petercao  阅读(73)  评论(0编辑  收藏  举报