requestMtu method

Future<int> requestMtu(
  1. int desiredMtu, {
  2. int timeout = 15,
})

Implementation

Future<int> requestMtu(int desiredMtu, {int timeout = 15}) async {
  if (isDisconnected) {
    throw const BleException(
      operation: 'requestMtu',
      message: 'device is not connected',
    );
  }

  final Future<BleMtuEvent> responseFuture = BlePlatform.instance.mtuChanged
      .firstWhere((event) => event.remoteId == remoteId);

  final bool started = await BlePlatform.instance.requestMtu(
    remoteId: remoteId.str,
    mtu: desiredMtu,
  );

  if (!started) {
    throw const BleException(
      operation: 'requestMtu',
      message: 'native requestMtu returned false',
    );
  }

  final BleMtuEvent response = await responseFuture.timeout(
    Duration(seconds: timeout),
    onTimeout: () {
      throw BleException(
        operation: 'requestMtu',
        message: 'Timed out after ${timeout}s',
      );
    },
  );

  if (!response.success) {
    throw BleException(
      operation: 'requestMtu',
      code: response.errorCode,
      message: response.errorString,
    );
  }

  return response.mtu;
}