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 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 _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 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 comming from an HID device. That device i 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, what is actually read are:
two 0x00
one 0x35
twenty nine 0x00
What I expected to read is only a 0x36.
What is wrong with this?
Thanks