ns blue_print_service.dart
parent
988d6a6c6b
commit
7b06e4c390
|
|
@ -33,7 +33,7 @@ class BluePrintService extends BaseService {
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
await _bluetooth.connect(_device!);
|
await _bluetooth.connect(_device!);
|
||||||
await Future.delayed(Duration(seconds: 2));
|
await Future.delayed(Duration(seconds: 5));
|
||||||
response = true;
|
response = true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Error connect $e');
|
print('Error connect $e');
|
||||||
|
|
@ -55,8 +55,21 @@ class BluePrintService extends BaseService {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> printBytes(Uint8List bytes) async {
|
Future<bool> printBytes(Uint8List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20 }) async {
|
||||||
Map<String, dynamic> config = Map();
|
Map<String, dynamic> config = Map();
|
||||||
return await _bluetooth.rawBytes(config, bytes);
|
final len = bytes.length;
|
||||||
|
List<List<int>> chunks = [];
|
||||||
|
for (var i = 0; i < len; i += chunkSizeBytes) {
|
||||||
|
var end = (i + chunkSizeBytes < len) ? i + chunkSizeBytes : len;
|
||||||
|
chunks.add(bytes.sublist(i, end));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < chunks.length; i += 1) {
|
||||||
|
await _bluetooth.rawBytes(config, chunks[i]);
|
||||||
|
await Future.delayed(Duration(milliseconds: queueSleepTimeMs));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,10 +40,8 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_permission();
|
_permission();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void _testPrint() async {
|
void _testPrint() async {
|
||||||
setState(() {
|
setState(() {
|
||||||
_printing = true;
|
_printing = true;
|
||||||
|
|
@ -64,33 +62,35 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||||
log.i(chunkSizeBytes);
|
log.i(chunkSizeBytes);
|
||||||
log.i(queueSleepTimeMs);
|
log.i(queueSleepTimeMs);
|
||||||
|
|
||||||
|
|
||||||
// TODO Don't forget to choose printer's paper
|
// TODO Don't forget to choose printer's paper
|
||||||
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58;
|
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80
|
||||||
|
? PaperSize.mm80
|
||||||
|
: PaperSize.mm58;
|
||||||
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
||||||
final bool res = await printerManager.printBytes(
|
final bool res = await printerManager.printBytes(
|
||||||
Uint8List.fromList(await testTicketImage(paper)
|
Uint8List.fromList(await testTicketImage(paper)),
|
||||||
));
|
chunkSizeBytes: chunkSizeBytes,
|
||||||
|
queueSleepTimeMs: queueSleepTimeMs);
|
||||||
_dialogService.showDialog(description: 'result is $res');
|
_dialogService.showDialog(description: 'result is $res');
|
||||||
} else {
|
} else {
|
||||||
final bool res = await printerManager.printBytes(
|
final bool res = await printerManager.printBytes(
|
||||||
Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text']))
|
Uint8List.fromList(await printTextCheck(
|
||||||
);
|
paper, state.printerEncoding!, exampleJson['check_text'])),
|
||||||
|
chunkSizeBytes: chunkSizeBytes,
|
||||||
|
queueSleepTimeMs: queueSleepTimeMs);
|
||||||
_dialogService.showDialog(description: 'result is $res');
|
_dialogService.showDialog(description: 'result is $res');
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('ERROR');
|
print('ERROR');
|
||||||
print(e);
|
print(e);
|
||||||
}
|
}
|
||||||
|
//10 sec safe disconnect
|
||||||
//7 sec safe disconnect
|
await Future.delayed(Duration(seconds: 14));
|
||||||
await Future.delayed(Duration(seconds: 15));
|
|
||||||
await printerManager.disconnect();
|
await printerManager.disconnect();
|
||||||
|
await Future.delayed(Duration(seconds: 3));
|
||||||
setState(() {
|
setState(() {
|
||||||
_printing = false;
|
_printing = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
@ -117,13 +117,17 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||||
}),
|
}),
|
||||||
SettingItem(
|
SettingItem(
|
||||||
title: 'Кодировка',
|
title: 'Кодировка',
|
||||||
name: vm.printerEncoding != null ? encoding[vm.printerEncoding] : '' ,
|
name: vm.printerEncoding != null
|
||||||
|
? encoding[vm.printerEncoding]
|
||||||
|
: '',
|
||||||
onTap: () {
|
onTap: () {
|
||||||
_navigatorService.push(SettingsPrinterEncodingRoute);
|
_navigatorService.push(SettingsPrinterEncodingRoute);
|
||||||
}),
|
}),
|
||||||
SettingItem(
|
SettingItem(
|
||||||
title: 'Ширина ленты',
|
title: 'Ширина ленты',
|
||||||
name: vm.printerPaperSize != null ? paperSize[vm.printerPaperSize] : null ,
|
name: vm.printerPaperSize != null
|
||||||
|
? paperSize[vm.printerPaperSize]
|
||||||
|
: null,
|
||||||
onTap: () {
|
onTap: () {
|
||||||
_navigatorService.push(SettingsPrinterPaperRoute);
|
_navigatorService.push(SettingsPrinterPaperRoute);
|
||||||
}),
|
}),
|
||||||
|
|
@ -156,24 +160,24 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||||
//Метод для получения постоянного доступа к местополения
|
//Метод для получения постоянного доступа к местополения
|
||||||
//только для Android
|
//только для Android
|
||||||
void _permission() async {
|
void _permission() async {
|
||||||
if( Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
var status = await Permission.location.status;
|
var status = await Permission.location.status;
|
||||||
log.i(status);
|
log.i(status);
|
||||||
if ( status.isDenied || status.isPermanentlyDenied) {
|
if (status.isDenied || status.isPermanentlyDenied) {
|
||||||
DialogResponse response = await _dialogService.showConfirmationDialog(
|
DialogResponse response = await _dialogService.showConfirmationDialog(
|
||||||
title: 'Доступ',
|
title: 'Доступ',
|
||||||
description: 'Для поиска устройств Bluetooth необходимо предоставить доступ к отслеживанию геолокации.',
|
description:
|
||||||
|
'Для поиска устройств Bluetooth необходимо предоставить доступ к отслеживанию геолокации.',
|
||||||
cancelTitle: 'Нет',
|
cancelTitle: 'Нет',
|
||||||
confirmationTitle: 'Хорошо',
|
confirmationTitle: 'Хорошо',
|
||||||
);
|
);
|
||||||
if (response.confirmed) {
|
if (response.confirmed) {
|
||||||
if (await Permission.location
|
if (await Permission.location.request().isGranted) {
|
||||||
.request()
|
|
||||||
.isGranted) {
|
|
||||||
print('Granted');
|
print('Granted');
|
||||||
} else {
|
} else {
|
||||||
_dialogService.showDialog(
|
_dialogService.showDialog(
|
||||||
description: 'Необходимо указать постоянный доступ к местоположении для поиска принтера');
|
description:
|
||||||
|
'Необходимо указать постоянный доступ к местоположении для поиска принтера');
|
||||||
_navigatorService.pop();
|
_navigatorService.pop();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -184,8 +188,6 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _startInitialPrint() async {
|
void _startInitialPrint() async {
|
||||||
|
|
||||||
|
|
||||||
if (Platform.isIOS) {
|
if (Platform.isIOS) {
|
||||||
_testPrint();
|
_testPrint();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -195,11 +197,11 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||||
if (val == 12) {
|
if (val == 12) {
|
||||||
print('on');
|
print('on');
|
||||||
_testPrint();
|
_testPrint();
|
||||||
|
|
||||||
} else if (val == 10) {
|
} else if (val == 10) {
|
||||||
print('off');
|
print('off');
|
||||||
_dialogService.showDialog(
|
_dialogService.showDialog(
|
||||||
description: 'Отсутвует соеденение Bluetooth или он отключен' , title: 'Bluetooth');
|
description: 'Отсутвует соеденение Bluetooth или он отключен',
|
||||||
|
title: 'Bluetooth');
|
||||||
}
|
}
|
||||||
print('state is $val');
|
print('state is $val');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue