-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.html
More file actions
115 lines (100 loc) · 3.71 KB
/
printer.html
File metadata and controls
115 lines (100 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<p>
<button class="btn btn-lg btn-default" onclick="conectar()">Conectar37()</button>
</p>
<p>
<button class="btn btn-lg btn-default" onclick="sendTextData('testes....testes\n....testes\n....testes....testes\n....testes....testes\n....testes....testes....')">Escreva Teste</button>
</p>
<p>
<button class="btn btn-lg btn-default" onclick="sendtextarea()">sendtextarea</button>
</p>
<textarea id="target" ></textarea>
<script type="text/javascript">
let printCharacteristic;
let texto = "";
let dist = 20; //Buffer enviado por ver para impressora
let x = 0;
function handleError(error) {
console.log(error);
document.querySelector('#target').value += 'error' + error ;
}
function sendtextarea(){
sendTextData(document.querySelector('#target').value);
}
function mostralog(line){
document.querySelector('#target').value += line ;
};
function printTextQuebrado(resolve, reject) {
// let text = encoder.encode(linha[i] + '\u000A\u000D');
// Can only write 512 bytes at a time to the characteristic
// Need to send the image data in 512 byte batches
let encoder = new TextEncoder("utf-8");
let text = encoder.encode(texto.substring(0 + x * dist, (x + 1) * dist));
//alert(texto.substring(0 + x * dist, (x + 1) * dist));
if ( x < texto.length / dist) {
printCharacteristic.writeValue(text).then(() => {
x++;
printTextQuebrado(resolve, reject)
})
.catch(error => reject(error));
} else {
resolve();
}
}
function sendTextData(t) {
let ESC = "\u001B";
let GS = "\u001D";
let InitializePrinter = ESC + "@";
let BoldOn = ESC + "E" + "\u0001";
let BoldOff = ESC + "E" + "\0";
let DoubleOn = GS + "!" + "\u0011"; // 2x sized text (double-high + double-wide)
let DoubleOff = GS + "!" + "\0";
x = 0;
texto = InitializePrinter + t + + '\u000A\u000A\u000A\u000A\u000A\u000A';
return new Promise(function(resolve, reject) {
printTextQuebrado(resolve, reject);
});
}
//e7810a71-73ae-499d-8c15-faa9aef0c3f2
//000018f0-0000-1000-8000-00805f9b34fb
function conectar () {
if (printCharacteristic == null) {
let options = {
optionalServices: ['000018f0-0000-1000-8000-00805f9b34fb'],
"acceptAllDevices": true
};
navigator.bluetooth.requestDevice(options)
.then(device => {
mostralog('> Found ' + device.name);
mostralog('Connecting to GATT Server...');
return device.gatt.connect();
})
// 000018f0-0000-1000-8000-00805f9b34fb
// 00002af1-0000-1000-8000-00805f9b34fb
.then(server => server.getPrimaryService("000018f0-0000-1000-8000-00805f9b34fb"))
.then(service => service.getCharacteristic("00002af1-0000-1000-8000-00805f9b34fb"))
.then(characteristic => {
// Cache the characteristic
printCharacteristic = characteristic;
sendTextData(`Lorem Ipsum simplesmente
uma simulação de texto da indústria
tipográfica e de impressos, e vem sendo
utilizado desde o século XVI, quando um
impressor desconhecido pegou uma bandeja de
tipos e os embaralhou para fazer um livro de modelos de
tipos. Lorem Ipsum sobreviveu não
só a cinco séculos, como também ao
salto para a editoração eletrônica,
permanecendo essencialmente inalterado.
Se popularizou na década de 60, quando a Letraset
lançou decalques contendo passagens de Lorem Ipsum, e
mais recentemente quando passou a ser integrado a
softwares de editoração eletrônica como Aldus
PageMaker.`)
})
.catch(handleError);
} else {
mostralog('Ja conectado');
sendTextData('Segundo testeSegundo testeSegundontesteSegundo testeSegundo testeSegundo teste');
}
};
</script>