2

我们正在将 mtu 请求从 android 发送到 iOS

Android - 从此函数 onServicesDiscovered 回调请求 mtu

但我不知道如何找出对等设备支持是否请求 MTU 以及实际协商的 MTU 是什么。所需功能:BluetoothGattCallback.onMtuChanged(BluetoothGatt gatt, int mtu, int status) 仅在 API 级别 22 (Android L 5.1) 中添加。

我的问题是我不知道我可以发送多少字节的数据包。

 @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                //requestPriorityHigh();

                    gatt.requestMtu(182);

                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
                List<BluetoothGattService> Services = gatt.getServices();
                for (BluetoothGattService gattService : Services) {
                    if (SERVICE_UUID.equals(gattService.getUuid())) {
                        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
                        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                            if (CHARACTERISTIC_UUID.equals(gattCharacteristic.getUuid())) {
                                gatt.writeCharacteristic(gattCharacteristic);
                                List<BluetoothGattDescriptor> gattDescriptors = gattCharacteristic.getDescriptors();
                                for (BluetoothGattDescriptor gattDescriptor : gattDescriptors) {
                                    gatt.readDescriptor(gattDescriptor);
                                }
                            }
                        }
                    }
                }
            } else {
                Log.w(MainActivity.TAG, "onServicesDiscovered received: " + status);
          }
    }

例如:gatt.requestMtu(182)

IOS - 未触发didSubscribeTo 特征回调

- (void)peripheralManager:(CBPeripheralManager )peripheral central:(CBCentral )central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
{
  NOTIFY_MTU = central.maximumUpdateValueLength;
  NSLog(@"Central subscribed to characteristic");
  NSLog(@"Supported to BLE Device Info:--> %lu",(unsigned long)[central maximumUpdateValueLength]);
  [peripheral setDesiredConnectionLatency:CBPeripheralManagerConnectionLatencyLow forCentral:central];
}

我们需要根据连接的 BLE 设备设置数据包大小。如果未请求 MTU,我们会在didSubscribeTo 特性上进行回调,最小 MTU 大小为 20。如何从 android 获取和设置此 mtu 大小。

我们如何设置MTU?

4

1 回答 1

2

方法requestMtu启动两个蓝牙设备之间的 MTU 交换。他们将就两个设备都支持的值达成一致。

所以你可以要求一个高 MTU:

gatt.requestMtu(2000);

然后等待onMtuChanged回调。它会告诉您已同意的 MTU:

@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
    Log.i(TAG, "MTU: " + mtu);
}
于 2019-08-07T13:54:45.397 回答