Files
swiftcontrol/lib/bluetooth/ble_string.dart
skyiea314 fa56e76a9b fix(ble): match Onelap Key faces and discover it on Android (#440)
* fix(ble): match Onelap Key faces and strip firmware padding

The settings page drew the firmware revision's trailing padding as tofu after 0.105. Device Information strings now stop at the first NUL and drop 0xFF or non-UTF-8 tails. The controller diagram uses the molded cross: circle, triangle, square, dot pad, and ONELAP.

* fix(ble): show legacy Onelap Key advertisements on Android

universal_ble's default Android scan asks only for BLE 5 extended advertisements, so a BLE 4.x Onelap Key never appears. Request legacy advertisements at low latency, and accept a later packet that is the first one to carry the controller name.

* style: keep the Onelap scan change to the discovery lines

The previous commit reformatted connection.dart. Restore the surrounding file and leave the legacy-scan and name-arrival checks.

* fix(ble): preserve valid UTF-8 prefixes

Keep the valid device-information prefix before malformed UTF-8.
Discard printable bytes after the error and cover truncated sequences
and valid replacement characters with regression tests.

* fix(ble): retain extended advertisements

Allow both legacy and extended Android advertisements while keeping
low-latency scanning. Verify the configuration at the BLE platform
boundary and remove the incorrect extended-only explanation.

* style: use English comments and test descriptions

---------

Co-authored-by: haoxiang.yang <haoxiang.yang@megatronix.co>
2026-11-06 08:38:36 +01:00

33 lines
1.1 KiB
Dart

import 'dart:convert';
/// Device Information strings are C strings, often in a fixed buffer.
/// Bytes after the NUL, 0xFF fill, or a non-UTF-8 tail render as tofu boxes
/// after an otherwise readable version such as `0.105`.
String? decodeBleDeviceInfoString(List<int> data) {
if (data.isEmpty) return null;
var end = data.length;
final nul = data.indexOf(0);
if (nul != -1) end = nul;
while (end > 0 && data[end - 1] == 0xFF) {
end--;
}
if (end == 0) return null;
final bytes = data.sublist(0, end);
final decoded = _decode(bytes);
final cleaned = decoded.replaceAll(_unprintable, '').trim();
return cleaned.isEmpty ? null : cleaned;
}
final _unprintable = RegExp(r'[\u0000-\u001F\u007F-\u009F\uE000-\uF8FF\uFFFD-\uFFFF]');
String _decode(List<int> bytes) {
try {
return utf8.decode(bytes);
} on FormatException catch (error) {
// An incomplete multibyte character may precede the error offset.
// The caller removes any resulting replacement characters during cleanup.
return utf8.decode(bytes.sublist(0, error.offset ?? 0), allowMalformed: true);
}
}