Flutter中Bluetooth LE特征读写成功但响应解析异常
Flutter BLE设备通信:写入成功但响应解析问题
我正在开发一款与BLE设备通信的Flutter应用,写入操作看似成功,但在解析响应时遇到问题。
日志片段
/flutter (27667): [FBP] [[ OnCharacteristicWritten ]] result: {error_string: GATT_SUCCESS, service_uuid: ffe0, success: 1, remote_id: A4:DA:32:4F:BA:29, error_code: 0, characteristic_uuid: ffe1, value: 626f6f745f66617374626f6f74} I/flutter (27667): Success writing /flutter (27667): [FBP] <readCharacteristic> args: {remote_id: A4:DA:32:4F:BA:29, service_uuid: ffe0, secondary_service_uuid: null, characteristic_uuid: ffe1} D/[FBP-Android](27667): [FBP] onMethodCall: readCharacteristic /flutter (27667): [FBP] <readCharacteristic> result: true D/[FBP-Android](27667): [FBP] onCharacteristicRead: ffe1 status: 0 /flutter (27667): [FBP] [[ OnCharacteristicReceived ]] result: {error_string: GATT_SUCCESS, service_uuid: ffe0, success: 1, remote_id: A4:DA:32:4F:BA:29, error_code: 0, characteristic_uuid: ffe1, value: a4da324fba29fb} /flutter (27667): Immediate Device Response Hex: a4da324fba29fb /flutter (27667): Device Response: a4da324fba29fb D/BluetoothGatt(27667): onConnectionUpdated() - Device=A4:DA:32:4F:BA:29 interval=24 latency=0 timeout=600 status=0
函数代码片段
String hexCommand = HEX.encode(Utf8Encoder().convert(command)); await characteristic.write((hex.decode(hexCommand))).then((value) { print('Success writing'); }); List<int> response; try { response = await characteristic.read().timeout(Duration(seconds: 5)); } catch (timeoutError) { setState(() { string = 'Error: Timeout'; }); return; } print('Immediate Device Response Hex: ${hex.encode(Uint8List.fromList(response))}'); if (response.isEmpty) { setState(() { string = 'Error: No response'; }); return; } String responseHex = hex.encode(Uint8List.fromList(response)); setState(() { string = responseHex; }); print('Device Response: $responseHex');
已尝试操作
- 在Flutter中实现了向BLE设备特征写入命令的函数。
- 写入操作看似成功,日志中
[FBP] [[ OnCharacteristicWritten ]]结果可佐证。 - 随后尝试使用
readCharacteristic函数读取响应。 - 读取操作似乎也成功,日志中
[FBP] result: true和[FBP] onCharacteristicRead等条目可证明。
预期与实际结果
预期
- 写入命令后,BLE设备能做出恰当响应。
- 接收到可解析的有意义响应。
实际结果
- 写入操作成功,但不确定返回数据的格式。
- 响应以十六进制格式呈现(如
a4da324fba29fb),无法解析。
问题
请问该数据应为何种格式,以及如何在Flutter中正确解析响应?
内容的提问来源于stack exchange,提问作者leith inoubli
相关产品推荐
相关产品推荐

