android - 如果值 len > MTU 大小,如何发送 BLE 特性通知?

标签 android bluetooth bluetooth-lowenergy gatt bluetooth-gatt

我有一个 Android GATT 服务器,代码如下:

// Data length = 24 bytes
void onNewData(GattServerDelegate gattServer, byte[] data) { // data = 24 bytes
    characteristic.setValue(data);

    // MTU = 23
    gattServer.notifyCharacteristicChanged(mNotifyDevice, characteristic, false);
}

由于数据比MTU大小长,客户端只收到部分消息。我可以吗:

  • 向服务器端请求新的 MTU?或者,
  • 将数据分成两个数据包?

最佳答案

这两个问题的答案都是肯定的,但有一个小细节。

MTU 只能从 GATT 客户端请求,而不能从 GATT 服务器请求。只有当客户端发起 MTU 交换时,GATT 服务器才能响应其 MTU。

但是,同一设备可以同时具有 GATT 服务器角色和 GATT 客户端角色。两者使用相同的 MTU。因此,只需打开客户端连接并调用 https://developer.android.com/reference/android/bluetooth/BluetoothGatt#requestMtu(int) 。或者只需确保远程设备在发送通知之前启动 MTU 交换。

您还可以拆分数据并发送多个通知。执行此操作时,请确保等待此回调 https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback#onNotificationSent(android.bluetooth.BluetoothDevice,%20int)在发送下一个通知之前。

另请注意,最大通知大小为 MTU - 3,因为 ATT header 也必须适合。

关于android - 如果值 len > MTU 大小,如何发送 BLE 特性通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75379837/

相关文章:

java - Android BLE 设备无法相互检测

android - 在 eclipse 的 androidmanifest 中使用 TODO 注释

java - 更新到 Realm 0.89.0(从 0.87.2)

iOS CoreBluetooth 突然处于 CBManagerStateResetting 状态

android - BLE 需要打开蓝牙吗?

iphone - iPhone 上的蓝牙低功耗连接间隔

Android Google Maps API API23 上的安全异常

android - 如何运行两个单独运行的异步任务?

java - 如何读取通过蓝牙发送的传入值?

linux - 低功耗蓝牙 : Passive Scan - but do not loop forever?