Revert "fix: ble write handling"#3725
Conversation
There was a problem hiding this comment.
Code Review
The pull request reverts a previous change related to BLE write handling. The original comment highlighting critical issues with the reverted BLE write functionality, including lack of long write support, writeWithoutResponse optimization, concurrency issues, and retries, has been kept as it does not contradict any existing rules and provides valuable feedback for robust BLE communication. The comment suggests re-introducing a more robust write mechanism.
| _writeQueue = task.catchError((_) {}); | ||
| await task; | ||
| try { | ||
| await characteristic.write(data); |
There was a problem hiding this comment.
This revert simplifies the write logic, but it re-introduces several critical issues that the previous implementation was trying to solve:
- Lack of long write support: The call to
characteristic.write(data)doesn't setallowLongWrite: true. This will cause writes larger than the MTU (typically 20 bytes) to fail on Android. - No
writeWithoutResponseoptimization: The previous code would use write without response if the characteristic supported it, which improves throughput. This implementation will always use write with response, which is slower. - Concurrency issues: Without a write queue (like the
_writeQueuethat was removed), concurrent calls towriteCharacteristiccan lead to race conditions andGATT_BUSYerrors, making the BLE communication unreliable. - No retries: The lack of a retry mechanism for transient errors makes the connection more fragile.
While I understand the reverted code may have had issues, simply reverting it makes the BLE write functionality significantly less robust. I'd strongly recommend re-introducing a more robust write mechanism that handles these cases. At a minimum, please consider enabling long writes and using write without response where appropriate.
await characteristic.write(
data,
withoutResponse: characteristic.properties.writeWithoutResponse,
allowLongWrite: true,
);
Reverts #3471