1、问题描述

最近进行Android ble的开发,遇到最大的问题就是在往characteristic中写入数据的时候,会有一个成功率抖动的问题,常会出现第一次写入失败,必须再写一次才成功的情况。

每次往characteristic中写入数据的时候,应该要回调onCharacteristicWrite这个方法的,如果失败了,根本就不回调这个方法,导致我甚至无出判断成功还是失败,无法再逻辑代码中去重写,只能在交互界面重复操作。

2、具体代码

下面我贴出我大致代码。

2.1BluetoothGattCallback

private void connectDevice() {

mDevice.connectGatt(this, false, new BluetoothGattCallback() {

@Override

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (newState == BluetoothProfile.STATE_CONNECTED) {

Log.d("MainActivity", "连接成功");

mGatt = gatt;

mGatt.connect();

gatt.discoverServices();

}

}

@Override

public void onServicesDiscovered(final BluetoothGatt gatt, int status) {

if (status == BluetoothGatt.GATT_SUCCESS) {

List<BluetoothGattService> services = gatt.getServices();

mGattService = services.get(4);

BluetoothGattCharacteristic notificationCharacteristic = mGattService.getCharacteristics().get(0);

mWriteCharacteristic = services.get(4).getCharacteristics().get(1);

mSimpleIO.setString("notificationUUID", notificationCharacteristic.getUuid().toString());

gatt.setCharacteristicNotification(notificationCharacteristic, true);

}

}

@Override

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

if (status == BluetoothGatt.GATT_SUCCESS) {

switch (mCurrState) {

case ConstantContainer.VERIFY_DEFAULT_KEY:

alterNewKey(gatt, characteristic);

break;

case ConstantContainer.ALTER_KEY:

bondSuccess();

break;

case ConstantContainer.REMOVE_BOND:

mGatt.disconnect();

try {

if (removeBond(mDevice)) {

mSimpleIO.remove("deviceAddress");

mSimpleIO.remove("notificationUUID");

runOnUiThread(new Runnable() {

@Override

public void run() {

showFindLayout();

}

});

}

} catch (Exception e) {

e.printStackTrace();

}

break;

}

} else {

switch (mCurrState) {

case ConstantContainer.VERIFY_DEFAULT_KEY:

Log.d("BluetoothActivity", "验证初始密码失败");

break;

case ConstantContainer.ALTER_KEY:

Log.d("BluetoothActivity", "修改密码失败");

break;

case ConstantContainer.REMOVE_BOND:

Log.d("BluetoothActivity", "删除配对失败");

break;

}

}

}

@Override

public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

}

});

上面的代码是BluetoothGattCallback的几个主要回调,可以看到我在onCharacteristicWrite中对他的status做了判断,想要定位写入是否成功,但结果是失败的情况下,并不回调这个方法。

2.2 写入部分

private void alterNewKey(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

mWriteCharacteristic.setValue("BBTM=" + mBlueKey);

mCurrState = ConstantContainer.ALTER_KEY;

Log.d("BluetoothActivity", "gatt.writeCharacteristic(characteristic) alterNewKey:" + gatt.writeCharacteristic(characteristic));

}

private void writeDefaultKey(BluetoothGatt gatt, BluetoothGattCharacteristic mWriteCharacteristic) {

mCurrState = ConstantContainer.VERIFY_DEFAULT_KEY;

mWriteCharacteristic.setValue(ConstantContainer.DEFAULT_KEY);

Log.d("BluetoothActivity", "gatt.writeCharacteristic(mWriteCharacteristic) writeDefaultKey:" + gatt.writeCharacteristic(mWriteCharacteristic));

}

这两个方法是我写入的部分,我把它们的返回值打印出来可发现,无论成功失败,返回的都是true。

android ble 写失败,Android低功耗蓝牙BLE写入数据很大几率会失败 求解相关推荐

  1. Android 蓝牙4.0(BLE)开发实现对蓝牙的写入数据和读取数据

    由于最近学校组织了一个移动APP(安卓)设计大赛,自己也学习安卓有一段时间了,就跟同学商量一起去参加试试,一拍即合,然后我们就开始想idea,因为最近可穿戴设备比较火,我们也就想试试.经过商量,我负责 ...

  2. 【IoT】加密与安全:CC254x 低功耗蓝牙 BLE 之 AES-128 加密算法

    蓝牙数据是可以通过空中抓包而被抓取到的,因此需要将通信数据进行加密,即使别人截获了加密后的数据,也无法利用该数据. AES 加密原理 CC254x 支持对称加密 AES: 加密过程: 需要加密的数据 ...

  3. Android低功耗蓝牙(BLE)开发(二)

    在上一篇文章Android低功耗蓝牙(BLE)开发(一)中我们了解了BLE的相关概念,这里我们来实际用代码演示安卓进行BLE连接和通讯的功能.本文代码基于Android5.0以上(API 21) 1. ...

  4. Android低功耗蓝牙BLE

    低功耗蓝牙BLE与传统的蓝牙相比最大的优势是功耗降低90%,同时传输距离增大(超过100米).安全和稳定性提高(支持AES加密和CRC验证),允许Android应用程序与具有更严格电源要求的BLE设备 ...

  5. Android 低功耗蓝牙BLE连接通信

    目录 简介 蓝牙 4.0 BLE与蓝牙4.0的区别 BLE的特点 主要特性 技术细节 BLE的应用 BLE的体系结构 BLE设备链路层状态 就绪态 广播态 扫描态 发起态 连接状态 通信基本过程 两种 ...

  6. 泰凌微ble mesh蓝牙模组天猫精灵学习之旅④如何在Android开发低功耗蓝牙ble控制 TB-02 模块,代码工程全部开源!(附带Demo)

    本<泰凌微ble mesh蓝牙模组天猫精灵学习之旅>系列博客学习由半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1.小白也痴迷,如 ...

  7. 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发详解

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发详解 ...

  8. 低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端

    低功耗蓝牙BLE外围模式(peripheral)-使用BLE作为服务端 Android对外模模式(peripheral)的支持 从Android5.0开始才支持 关键术语和概念 以下是关键BLE术语和 ...

  9. 蓝牙:深入浅出低功耗蓝牙(BLE)协议栈

    深入浅出低功耗蓝牙(BLE)协议栈 BLE协议栈为什么要分层?怎么理解BLE"连接"?如果BLE协议只有ATT层没有GATT层会发生什么? 协议栈框架 一般而言,我们把某个协议的实 ...

  10. PyQt5之QtBluetooth模块:低功耗蓝牙BLE通信

    PyQt5之QtBluetooth模块:低功耗蓝牙BLE通信 最近使用PyQt5开发PC端工具,正巧手上有一个富芮坤的低功耗蓝牙,于是想在PC端试试与之通信,不过发现使用PyQt5开发低功耗蓝牙的教程 ...

最新文章

  1. $(shell expr $(MAKE_VERSION) \= 3.81) 这里“\”的解释
  2. 95后程序员月薪2万背着电脑送外卖,送单途中改Bug
  3. 【实践】简洁大方的summernote 富文本编辑器插件的用发——导入篇
  4. 学习ASP.NET-初探Application状态
  5. 用少于10行代码训练前沿深度学习新药研发模型
  6. 洛谷——P1219 八皇后
  7. Spring Boot 操作 Memcache
  8. 测试工程师职业素养:懂得拒绝无效工作
  9. java get请求带参数报错 java.io.IOException: Server returned HTTP response code: 400 for URL
  10. jQuery知识点学习整理
  11. 《ARM嵌入式Linux系统开发从入门到精通》勘误
  12. 水经注叠加cad_如何在CAD中将矢量地图与卫星影像完美叠加
  13. 条形码编码规则及标准
  14. Java多线程面试题(面试必备)
  15. 阿里再度联手数据港合建数据中心 服务金额至少40亿
  16. 低深度测序下的拷贝数变异文章阅读笔记
  17. 工信部颁发“免死金牌” 抢票软件继续存活
  18. NMOS和PMOS的DCIV仿真
  19. 一种基于XGBOOST的企业员工稳定性(员工离职)预测方法
  20. springcloud常见问题

热门文章

  1. linux fdisk ntfs,2014.1.2 学习记录(fdisk、ntfs)
  2. VS2015打开cshtml视图页文件报错 未将对象引用设置为实例 解决办法
  3. 浅谈Event Loop
  4. Flutter ListView以及GridView的列表展示与Item点击事件
  5. 20.10 for循环 20.11/20.12 while循环 20.13 break跳出循环 20.14 continue结束本次循环 20.15 exit退出整个脚本...
  6. 04-树4. Root of AVL Tree (25)
  7. C语言 整人程序“我是猪”
  8. 黑客攻击成网络安全大患 危害长久
  9. matlab gui怎样将结果保存在excel中_声发射简单使用matlab导入参数波形数据并绘图...
  10. sleuth zipkin mysql_springCloud的使用08-----服务链路追踪(sleuth+zipkin)