AndroidBLE读写开发回顾

作者: Ugly_K | 来源:发表于2019-06-18 18:12 被阅读1次

Android BLE 读写开发回顾

这里主要记录BLE的通讯方面的笔记

1.发现BluetoothGattService回调

BLE 调用gatt.connect()连接成功之后调用:

gatt.discoverServices()

触发回调void onServicesDiscovered(BluetoothGatt gatt, int status)

通过硬件规定的uuid获取对应的BluetoothGattService:

BluetoothGattService service = gatt.getService(mServiceUuid);

BluetoothGattService中包含一个或多个BluetoothGattCharacteristic,这些特征字将是后续用来读写数据的载体,而

通过硬件给定的uuid筛选出对应的特征字,譬如有些特征字用于收发简单命令,而有些特征字用于批量发送数据:

if (characteristic.getUuid().equals(UUID.fromString(String.format(MEDTRUM_BASE_UUID, MEDTRUM_UUID_CHARACTERISTIC_BULK)))) {
    //获取批量发送数据特征字
    mBulkCharacteristics = characteristic;
} else if (characteristic.getUuid().equals(UUID.fromString(String.format(MEDTRUM_BASE_UUID, MEDTRUM_UUID_CHARACTERISTIC_CTRLPT)))) {
    //获取收发命令特征字
    mCommandCharacteristics = characteristic;
}

在拿到特征字之后并不能直接使用,如果特征字具有通知功能,那么还需要对其进行设置:

Log.d(TAG, "setCharacteristicNotification: "+gatt.setCharacteristicNotification(characteristic, true));

当然,这还不够,你会发现这个特征字可以正常的发送数据,但是接受不到返回数据,划重点,这里别忘了对特征字描述符BluetoothGattDescriptor进行设置:

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
} else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

设置描述符调用了mBluetoothGatt.writeDescriptor(descriptor)方法,对应的回调为:

void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)

在回调中可以对对应的描述符进行验证是否设置成功,然后就可以正常的使用特征字收发命令了;

注意,通常BluetoothGattService并不只包含一个特征字,而对特征字的设置动作底层其实也是一个写的命令,理论上也会触发:

void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)

但是在开发过程中通常并不能在这一步接收到回调,这里的原因我还不是很清楚,大牛看到请,烦请指点一二,嘻嘻。

既然是写的操作,就必须遵循串行操作的原则,有序的写,设置一个特征字之后延迟一段时间,再设置下一个。

这里的延迟时间比较讲究,Android手机厂家众多,所以需要给定的延迟时间也各不相同,开发经验发现如果间隔时间超过250ms SAMSUNG 2s内连接不上会被动断开,最终测试结果定在了700ms,可以适配目前所有的测试机型,如果有大牛有更好的解决办法,烦请指点一二,嘻嘻。

2.读写数据

在拿到了特征字之后,就可以进行读写操作了,写:

if (mCommandCharacteristics != null) {
    if (value.length > 0) {
        mCommandCharacteristics.setValue(value);
        mBluetoothGatt.writeCharacteristic(mCommandCharacteristics);
    }
}

发送数据之后,可以通过回调:

onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)

接收反馈,判断命令是否发送成功。

对于数据的读取,通过回调:

void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)

来获取到反馈的内容,可以看到这个回调中包含了特征字BluetoothGattCharacteristic很显然,我么您可以通过特征字的uuid来判断反馈的数据是命令返回,或者是通知消息;

注意,在读写交互中,请一定遵守串行传输的准则,有序的收发命令,避免出现奇奇怪怪的bug;

相关文章

  • AndroidBLE读写开发回顾

    Android BLE 读写开发回顾 这里主要记录BLE的通讯方面的笔记 1.发现BluetoothGattSer...

  • iOS获取本地路径: Documents, Caches,tmp

    App内目录结构:AppData.png 读写权限与功能: iOS开发是在沙盒中,对于开发者有读写权限的目录,如下...

  • Java NIO 底层原理

    1.1. JavaIO读写原理 无论是Socket的读写还是文件的读写,在Java层面的应用开发或者是linux系...

  • Sharding-JDBC:单库分表的实现

    剧情回顾 前面,我们一共学习了读写分离,垂直拆分,垂直拆分+读写分离。对应的文章分别如下: Sharding-JD...

  • ios5~沙盒

    2018.01.17 沙盒 iOS开发是在沙盒中开发的,对一些部分的文件的读写进行了限制,只能在几个目录下读写文件...

  • 2021年读写回顾

    2021年文学历程回顾 今天是2021年最后一天的上班日,也是年末、月末与周末同日。倚窗眺望,闽江上清流潺湲。当夕...

  • AndroidBLE开发过程中遇到的问题记录

    Android BLE 开发过程中遇到的问题记录 1.断开连接后出现133错误 在断开连接之后再次连接经常会出现1...

  • 开发回顾

    今年的开发工作在欢乐的春节气氛中突然就开始了。春节前两周,就当大家还在筹划着春节回家团圆之时,被强行留下。年前...

  • 2018-02-08

    IOS蓝牙开发SDK通信笔记连接和读写 当下蓝牙开发可谓是越来越火,不论是...

  • Kotlin笔记(58) — 文件读写

    前言 在Android开发过程中,我们还涉及到文件读写的问题,Kotlin在文件读写方面,更是有很多优势,下面就来...

网友评论

    本文标题:AndroidBLE读写开发回顾

    本文链接:https://www.haomeiwen.com/subject/mpdqqctx.html