前言
最近在使用蓝牙4.0做单导联心电监测产品时遇到了一个问题。某些机型在蓝牙4.0传递数据的过程中出现丢包(比如最新华为mate10),导致无法展示正确的心电图。后来经过实测,原来是每秒数据量过大(大概975byte/s),但并不是所有的安卓设备都会丢包,这引起了我的注意。赶紧找硬件开发的同事一起分析,最终得出,mate10的蓝牙4.0通讯默认间隙时间较长,通讯频率降低,导致数据丢失。

1、BLE通讯 Connection paramter
Connection paramter有三种状态:

1.BluetoothGatt.CONNECTION_PRIORITY_BALANCED = 0 
使用Bluetooth SIG推荐的连接参数, 如果没有请求连接参数更新,这是默认值。

2.BluetoothGatt.CONNECTION_PRIORITY_HIGH = 1 
高优先级,低延迟连接。

3.BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER = 2 
低功耗,降低数据速率连接参数

随后,就以上模式进行了针对性的资料查找,蓝牙协议分析 给了我很大的帮助。

2、Android设置BLE连接参数
在BluetoothGattCallback抽象类中,处了有我们熟悉的onConnectionStateChange方法: 
/** 
* Callback indicating when GATT client has connected/disconnected to/from a remote 
* GATT server. 

* @param gatt GATT client 
* @param status Status of the connect or disconnect operation. 
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds. 
* @param newState Returns the new connection state. Can be one of 
* {@link BluetoothProfile#STATE_DISCONNECTED} or 
* {@link BluetoothProfile#STATE_CONNECTED} 
*/ 
public void onConnectionStateChange(BluetoothGatt gatt, int status, 
int newState) { 
}

还有一个当蓝牙连接参数改变的监听onConnectionUpdated 
/** 
* Callback indicating the connection parameters were updated. 

* @param gatt GATT client involved 
* @param interval Connection interval used on this connection, 1.25ms unit. Valid 
* range is from 6 (7.5ms) to 3200 (4000ms). 
* @param latency Slave latency for the connection in number of connection events. Valid 
* range is from 0 to 499 
* @param timeout Supervision timeout for this connection, in 10ms unit. Valid range is 
* from 10 (0.1s) to 3200 (32s) 
* @param status {@link BluetoothGatt#GATT_SUCCESS} if the connection has been updated 
* successfully 
* @hide 
*/ 
public void onConnectionUpdated(BluetoothGatt gatt, int interval, int latency, int timeout, 
int status) { 

也就是说,通过此回调函数,我们可以判断出当前BLE工作的状态。 
那么,如何去设置呢?

楼主发现目前处了Android O其他的默认为CONNECTION_PRIORITY_HIGH 。由此可以按照正常的步骤来操作:

if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.O) {
                   requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
                    mConnectionPriorityOperationInProgress = true;
                }

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
 public void requestConnectionPriority(int priority){
        if (mBluetoothGatt != null) {
            boolean requestConnectionPriority = mBluetoothGatt.requestConnectionPriority(priority);
            Log.w("wsh"," requestConnectionPriority : "+ requestConnectionPriority);
        }
    }

核心方法还是使用系统BluetoothGatt的requestConnectionPriority方法。

3、注意点
在蓝牙设备建立连接之后,系统会默认设定一些连接参数,这样就会多次执行onConnectionUpdated回调,我们一定要通过一些同步机制或延时设置的方式来确保设置参数成功。假如我们设置后,系统又设置了一次,就会出现参数被覆盖的情况。

  • liujinlei9109: 华为手机传大量数据遇到了同样问题,请问现在解决了吗?是修改的系统BluetoothGatt的requestConnectionPriority方法吗,谢谢解答(2个月前#1楼)收起回复

    • 王小_回复 liujinlei9109: 是的,已经解决了。如文章中所示,在蓝牙连接成功的回调中,判断是8.0系统则调用 requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)方法。 这个方法设置成功后会触发public void requestConnectionPriority(int priority)的回调。 这个是需要时间的,不是马上就后回调,不同机型时间不一样。(1个月前)

原文:https://blog.csdn.net/hello_json/article/details/79541853?utm_source=copy

Android蓝牙4.0之传输速率的提升相关推荐

  1. ym——物联网入口之中的一个Android蓝牙4.0

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 假设还有同学不知道蓝牙4.0能够做什么请查看Android+蓝牙 4.0 将带来什么? ...

  2. ym——物联网入口之一Android蓝牙4.0

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 如果还有同学不知道蓝牙4.0可以做什么请查看Android+蓝牙 4.0 将带来什么? ...

  3. android蓝牙4.0 BLE低功耗应用

    转自    http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html 谈谈几个月以来开发android蓝 ...

  4. 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受

    谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受,注明下时间:2012-10-17写的博客,后期更新的也注明了时间 ...

  5. 开发android蓝牙4.0 BLE低功耗应用的感受

    文章转自: http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html 谈谈几个月以来开发android蓝 ...

  6. Android 蓝牙4.0(BLE)Demo

    给大家分享一下蓝牙4.0的Demo,本篇介绍基本使用 最终效果 如何获取UUID 使用 Android 蓝牙4.0(BLE) Demo下载 最终效果 如何获取UUID 去手机华为应用市场下载一个叫蓝牙 ...

  7. Android蓝牙4.0的数据通讯

    我在两家公司都用到了app与BLE设备通讯,当时也是什么都不懂,在网上各种搜索,各种查资料,总算弄出来了.在这里记录下来,希望对有需要的人有些帮助.   1.检测手机是否支持蓝牙4.0(一般手机4.3 ...

  8. android 蓝牙4.0(BLE) 开发

    简介 蓝牙发展至今经历了9个版本的更新.1.1.1.2.2.0.2.1.3.0.4.0.4.1.4.2.5.0.那么在1.x~3.0之间的我们称之为传统蓝牙,4.x开始的蓝牙我们称之为低功耗蓝牙也就是 ...

  9. Android蓝牙4.0 BLE开发坑总结

    onServicesDiscovered 回调里不能直接执行 write /readDataFromCharacteristic() 或者 enableNotificationOfCharacteri ...

  10. Android蓝牙4.0开发及发送指令到蓝牙硬件设备,简单好用,方法已写好直接可用

    近日,接到需要用到蓝牙解锁硬件设备的新需求,开发过程中呢也遇到许多硬件的坑,开发协议文档较简单,几句话就完了,第一次搞得我自己一脸懵逼,本来一两个小时就能写完并测试完成的过程用了两三天.哎!默默地回到 ...

最新文章

  1. 2015/6/1站立会议(补发)
  2. 一个高效的定时任务系统
  3. MariaDB配置慢日志
  4. iOS底层面试题--RunLoop
  5. sqlserver browser无法启动_SQL Server中非sysadmin权限用户在SQL Agent的权限
  6. apache php mysql 整合_PHP+Apache+MySQL整合
  7. 使用增强回归树和随机森林模型进行溪流水质预测--文献阅读
  8. python列表有哪些操作_python列表的基本操作有哪些
  9. 研发全球化再升级,华为已悄然布局16家海外研究所
  10. 迷途的羔羊?-SBO市场发展之我见
  11. Python实现简易局域网视频聊天工具
  12. ubuntu下锐捷客户端提示多个ip地址
  13. 谷粒学院(十六)OAuth2 | 微信扫码登录 | QQ扫码登录
  14. (转)loff_t *ppos是什么东东
  15. CCF201509-1数列分段(C语言)
  16. 前端怎么画三角形_css画出三角形和梯形
  17. [RK3399][Android7.1] 调试笔记 --- sdcard守护进程启动位置
  18. jQuery_删除及清空节点
  19. MOSFET和IGBT栅极驱动器电路的基本原理的学习(1)
  20. FGMap学习之--天气预报

热门文章

  1. 去哪下载python项目_Python 项目实践二(下载数据)第三篇
  2. Google mediapipe 人脸识别应用
  3. Sutton and Barto 教材中多臂老虎机(k-armed bandit testbed)模拟
  4. python interpreter下载_Piton - Python interpreter
  5. BAPI_PO_CREATE1 创建采购订单
  6. Adolescent Suicidal Risk Assessment in Clinician-Patient Interaction
  7. 变频器调速工作原理实训装置QY-SP12
  8. linux 心脏滴血漏洞,漏洞bash近日“破壳”,当心再次“心脏出血”
  9. Android Killer反编译时遇到的异常
  10. 交响曲、协奏曲、奏鸣曲