安卓蓝牙 : knowing MTU on the GattServer side

标签 android bluetooth bluetooth-lowenergy

我已经在 Android 上实现了 BLE 通信。我正在使用 requestMtu 在客户端协商 MTU .这很有效,因为客户端在 onMtuChanged 上获得了 MTU 更改.但是,我仍然没有办法知道服务器端的 MTU,只有客户端。我实现了自己的 fragment 化/分段机制,因为有时我会发送合理数量的数据。我想根据 MTU 大小对数据进行分段以加快通信速度。我目前在服务器端一次发送 20 个字节,这可以工作但速度较慢。有没有办法获取服务器上的 MTU?

最佳答案

我完成这项工作的方法是等待连接,然后与通过 onConnectionStateChange(...) 函数连接的设备建立连接。

private final BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
  @Override
  public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
      if (newState == Bluetooth.STATE_CONNECTED)
        device.connectGatt(MainActivity.this, false, mGattCallback);
      super.onConnectionStateChange(device, status, newState);
}

这允许我在成功连接后请求 Mtu()。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
      if (newState == Bluetooth.STATE_CONNECTED) {
        gatt.requestMtu(512);
      }
      super.onConnectionStateChange(gatt, status, newState);

    }
  };

关于安卓蓝牙 : knowing MTU on the GattServer side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38880523/

相关文章:

bluetooth - CC2540 USB - 使可发现 : Not ready to perform task

core-location - iBeacon 和 XCode - 使用 CoreLocation 进行发现并与 CoreBluetooth 连接

ios - 如何在 Objective C 中将 CBCharacteristic.value 转换为 NSString

android - 设备间近场通信

android - 如何在工具栏中添加带有编辑文本的搜索栏

c++ - 检查蓝牙套接字中缓冲区的状态

android - startLeScan 替换为当前 api

android - 当我随机检索它们时,如何防止 Firebase 数据库中的 key 冗余?

BroadcastReceiver的onReceive函数中的Android startActivity

android - Arduino 到 Android 的安全蓝牙连接