ble 读写特征值特征值_BLE:使用Android / iOS读取长特征值

With a normal Characteristics Read only the MTU Size (20bytes) of data will be read.

My customer will offer a characteristics with a larger size (about 100bytes).

attclient_read_long command - Starts a procedure where the client first sends normal read request to the server, and if the server returns an attribute value with a length equal to the BLE MTU (22 bytes), then the client continues to send "read long" requests until rest of the attribute is read. This only applies if you are reading attributes that are longer than 22 bytes. It is often simpler to construct your GATT server such that there are no long attributes, for simplicity. Note that the BLE protocol still requires that data is packetized into max. 22-byte chunks, so using "read long" does not save transmission time.

But how can I use this feature in Android?

The BluetoothGatt class only offers a simple "Read()" - same for iOS.

Increasing the MTU is not possible since we need to support devices with AP Level < 21 (increaseMTU was introduced at API 21)

解决方案

I can confirm for iOS that a read operation as per standard will occur first. Then if the server returns a completely filled PDU, the iOS device will then continue to perform the blob read operation. Tested with iPhone 7 running iOS 11.2.x

You do NOT need to call the peripheral.readValue(characteristic) multiple times for long attributes. CoreBluetooth does all of this under the covers.

Refer to the Bluetooth Spec Core v5.0, specifically Vol 3, Part F. "Long Attribute Values".

Experiment to prove above.

I have an Android Thing acting as the server that I'm making return the maximum length with my iPhone during a read operation. iOS and my RPI3 exchange a MTU of 185. So the read response is (MTU - 1) 184 bytes long. The server (RPI) then receives a new read request with an offset of 184, which you can then return more data. This is continued until the offset is > 512, or the last read response returns less than the MTU - 1 length.

Based upon the fact that the BluetoothGattServer supports long attributes, I'd assume the BluetoothGatt object does as well. Since there is no way via the API to set the offset to be read, I'd assume you can invoke read just once.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#define DEVICE_NAME "BL618_GATT" // 设备名称 #define PROFILE_NUM 1 // 设备支持的服务数量 #define PROFILE_A_APP_ID 0 // 第一个服务的ID static void gap_event_handler(ble_event_t *event) { // 处理 GAP 事件 } static void gatt_event_handler(ble_event_t *event) { // 处理 GATT 事件 } int main(void) { // 初始化蓝牙协议栈 bluetooth_init(gap_event_handler, gatt_event_handler); // 设置设备名称 bluetooth_set_device_name(DEVICE_NAME); // 创建一个服务 bluetooth_gatt_create_service(PROFILE_NUM); // 添加服务的特征值 bluetooth_gatt_add_char(PROFILE_A_APP_ID, "CHAR_A", 0xFF01, 0x20, NULL); // 开始广播 bluetooth_start_advertising(); while (1) { // 等待事件 bluetooth_wait_for_event(); } return 0; static void gap_event_handler(ble_event_t *event) { switch (event->type) { case BLE_GAP_EVENT_ADV_IND: { // 收到广播包,可以连接该设备 ble_gap_connect(&event->gap_event.adv_ind.address); break; } case BLE_GAP_EVENT_CONNECTED: { // 连接成功,可以开始 GATT 操作 break; } case BLE_GAP_EVENT_DISCONNECTED: { // 断开连接,重新开始广播 bluetooth_start_advertising(); break; } default: break; } } static void gatt_event_handler(ble_event_t *event) { switch (event->type) { case BLE_GATT_EVENT_READ: { // 处理操作 break; } case BLE_GATT_EVENT_WRITE: { // 处理操作 ble_gatt_server_send_indication(event->conn_handle, 0x1234, raw_data, sizeof(raw_data)); // 发送通知给主机 break; } default: break; } } } }优化
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值