BLE总结,flutter开发工具

这篇博客详细介绍了在Flutter中使用蓝牙低功耗(BLE)进行开发的相关操作,包括蓝牙连接、数据读写、回调函数的使用,并探讨了如何优化写入操作的效率,以及BLE的自动重连策略。内容涵盖了蓝牙GattCallback的实现,以及在实际操作中需要注意的延时问题和设备端数据接收的验证。同时,文章提供了Android学习笔记和移动架构的资源链接。
摘要由CSDN通过智能技术生成

所有操作的回调函数。

private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {

@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); //收到设备notify值 (设备上报值) } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); //读取到值 } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { //write成功(发送值成功) } } @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothGatt.STATE_CONNECTED) { // 连接成功 } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { // 断开连接 } } } @Override public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorRead(gatt, descriptor, status); } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorWrite(gatt, descriptor, status); } @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { super.onReadRemoteRssi(gatt, rssi, status); if (status == BluetoothGatt.GATT_SUCCESS) { //获取到RSSI, RSSI 正常情况下 是 一个 负值,如 -33 ; 这个值的绝对值越小,代表设备离手机越近 //通过mBluetoothGatt.readRemoteRssi();来获取 } } @Override public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { super.onReliableWriteCompleted(gatt, status); } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { //寻找到服务 } } }; --------------------------------------------------------------------------------------------------------------------------------------- 当调用了连接函数 mBluetoothGatt = bluetoothDevice.connectGatt(this.context, false, gattCallback);之后, 如果连接成功就会 走到 连接状态回调: @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (status == BluetoothGatt.GATT_SUCCESS) { //首先判断这个status 如果等于 BluetoothGatt.GATT_SUCCESS(value=0)代表这个回调是正常的, //如果不等于 0,那边就代表没有成功,也不需要进行其他操作了, // 连接成功和断开连接都会走到这里 if (newState == BluetoothGatt.STATE_CONNECTED) { // 连接成功 //连接成功之后,我们应该立刻去寻找服务(上面提到的BluetoothGattService),只有寻找到服务之后,才可以和设备进行通信 gatt.discoverServices();// 寻找服务 } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { // 断开连接 } } } 当判断到连接成功之后,会去寻找服务, 这个过程是异步的,会耗点时间,当寻找到服务之后,会走到回调: @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { //寻找到服务 //寻找服务之后,我们就可以和设备进行通信,比如下发配置值,获取设备电量什么的 readBatrery();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值