printData method
Implementation
Future<void> printData(
Printer device,
List<int> bytes, {
bool longData = false,
}) async {
if (device.connectionType == ConnectionType.USB) {
using((Arena alloc) {
final printer = RawPrinter(device.name!, alloc);
printer.printbytes(bytes);
});
return;
}
if (!isInitialized) {
throw Exception('WindowBluetoothManager is not initialized');
}
final services = await WinBle.discoverServices(device.address!);
final service = services.first;
final characteristics = await WinBle.discoverCharacteristics(
address: device.address!,
serviceId: service,
);
final characteristic = characteristics
.firstWhere((element) => element.properties.write ?? false)
.uuid;
final mtusize = await WinBle.getMaxMtuSize(device.address!);
if (longData) {
int mtu = mtusize - 50;
if (mtu.isNegative) {
mtu = 20;
}
final numberOfTimes = bytes.length / mtu;
final numberOfTimesInt = numberOfTimes.toInt();
int timestoPrint = 0;
if (numberOfTimes > numberOfTimesInt) {
timestoPrint = numberOfTimesInt + 1;
} else {
timestoPrint = numberOfTimesInt;
}
for (var i = 0; i < timestoPrint; i++) {
final data = bytes.sublist(i * mtu,
((i + 1) * mtu) > bytes.length ? bytes.length : ((i + 1) * mtu));
await WinBle.write(
address: device.address!,
service: service,
characteristic: characteristic,
data: Uint8List.fromList(data),
writeWithResponse: false,
);
}
} else {
await WinBle.write(
address: device.address!,
service: service,
characteristic: characteristic,
data: Uint8List.fromList(bytes),
writeWithResponse: false,
);
}
}