By using hid_tool package in flutter using dart language strange data is read (Android app)

10 hours ago 1
ARTICLE AD BOX

I have created the following class in flutter that uses hid_tool package:

import 'dart:async'; import 'package:hid_tool/hid_tool.dart'; class ShiftDispenser { late HidDevice _device; late StreamSubscription? _subscription; final Function(String) onDataReceived; final Function(String)? onError; bool _isConnected = false; ShiftDispenser({required this.onDataReceived, this.onError}); Future<bool> connect() async { try { final devices = await Hid.getDevices(); _device = devices.isNotEmpty ? devices.first : throw Exception('No HID device found'); await _device.open(); _isConnected = true; await _readData(); return true; } catch (e) { onError?.call('Failed to connect: $e'); return false; } } Future<void> _readData() async { try { if (!_device.isOpen) { throw Exception('Failed to open HID device'); } _subscription = _device.inputStream().listen( (byte) { print('Buffer: ${String.fromCharCode(byte)}'); }, onError: (Object e) => print('Stream error: $e'), onDone: () => print('Stream closed'), ); } catch (e) { onError?.call('Error reading data: $e'); } } Future<void> disconnect() async { if (_subscription != null) { await _subscription!.cancel(); _subscription = null; } if (_isConnected) { await _device.close(); _isConnected = false; } } bool get isConnected => _isConnected; }

To get the device I call connect method. This method also start listening for data coming from an HID device. That device is actually a QR code reader.

For testing purposes, I created a QR code that contains only a text with this content: 6.

When I run the app in a real device (an Android tablet) and read that QR, this is actually read:

two 0x00

one 35

fifteen 0x00

one 40

twenty nine 0x00

enter image description here

What I expected to read is only a 0x36.

enter image description here

I have added permissions in AndroidManifest.xml file, but the same happens with or without permissions.

What is wrong with this?

Thanks

Read Entire Article