write method
Implementation
Future<void> write(
List<int> value, {
bool withoutResponse = false,
int timeout = 15,
}) async {
if (device.isDisconnected) {
throw const BleException(
operation: 'writeCharacteristic',
message: 'device is not connected',
);
}
final Future<BleCharacteristicWriteEvent> responseFuture = BlePlatform
.instance
.characteristicWritten
.firstWhere(
(event) =>
event.remoteId == remoteId &&
event.primaryServiceUuid == primaryServiceUuid &&
event.serviceUuid == serviceUuid &&
event.characteristicUuid == characteristicUuid &&
event.instanceId == instanceId,
);
final bool started = await BlePlatform.instance.writeCharacteristic(
remoteId: remoteId.str,
primaryServiceUuid: primaryServiceUuid,
serviceUuid: serviceUuid,
characteristicUuid: characteristicUuid,
instanceId: instanceId,
value: value,
withoutResponse: withoutResponse,
);
if (!started) {
throw const BleException(
operation: 'writeCharacteristic',
message: 'native writeCharacteristic returned false',
);
}
final BleCharacteristicWriteEvent response = await responseFuture.timeout(
Duration(seconds: timeout),
onTimeout: () {
throw BleException(
operation: 'writeCharacteristic',
message: 'Timed out after ${timeout}s',
);
},
);
if (!response.success) {
throw BleException(
operation: 'writeCharacteristic',
code: response.errorCode,
message: response.errorString,
);
}
}