-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
295 lines (262 loc) · 7.66 KB
/
App.tsx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* eslint-disable react-native/no-inline-styles */
import React, { useEffect } from 'react';
import {
Alert,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
import BluetoothPrinter, {
type iDevice,
} from '@linvix-sistemas/react-native-bluetooth-printer';
import {
Printer,
Style,
Align,
Model,
InMemory,
} from '@linvix-sistemas/react-native-escpos-buffer';
export default function App() {
const [Devices, setDevices] = React.useState<iDevice[]>([]);
const goScanDevices = async () => {
try {
setDevices([]);
const devices = await BluetoothPrinter.scanDevices();
(devices?.paired ?? []).map((device) => {
setDevices((old) => {
if (!old.some((d) => d.address === device.address)) {
old.push(device);
}
return [...old];
});
});
(devices?.found ?? []).map((device) => {
setDevices((old) => {
if (!old.some((d) => d.address === device.address)) {
old.push(device);
}
return [...old];
});
});
console.log(devices);
} catch (error: Error | any) {
Alert.alert(error.message);
}
};
const goEnableBluetooth = async () => {
try {
await BluetoothPrinter.requestPermission();
await BluetoothPrinter.enableBluetooth();
} catch (error: Error | any) {
Alert.alert(error.message);
}
};
const goConnect = async (device: iDevice) => {
try {
const connect = await BluetoothPrinter.connect(device.address);
console.log(connect);
} catch (error: Error | any) {
Alert.alert(error.message);
}
};
const goPrint = async () => {
/**
* Classe de geração do Impresso.
*/
const connection = new InMemory();
const printer = await Printer.CONNECT('TM-T20', connection);
const capability = Model.EXPAND(Model.FIND('TM-T20'));
const { feed } = capability;
// printer.setColumns(48);
await printer.writeln('TESTÉ');
await printer.writeln('Align Center', 0, Align.Center);
await printer.writeln('Align Left', 0, Align.Left);
await printer.writeln('Align Right', 0, Align.Right);
await printer.writeln();
await printer.writeln('Bold Text', Style.Bold, Align.Center);
await printer.writeln('Italic Text', Style.Italic, Align.Center);
await printer.writeln('Underline Text', Style.Underline, Align.Center);
await printer.writeln('Condensed Text', Style.Condensed, Align.Center);
await printer.writeln(
'All Styles',
Style.Bold + Style.Italic + Style.Underline + Style.Condensed,
Align.Center
);
await printer.writeln();
await printer.writeln('DOUBLE WIDTH', Style.DoubleWidth, Align.Center);
await printer.writeln('DOUBLE HEIGHT', Style.DoubleHeight, Align.Center);
await printer.writeln(
'WIDTH AND HEIGHT',
Style.DoubleWidth + Style.DoubleHeight,
Align.Center
);
await printer.writeln();
await printer.writeln('With bold', 0, Align.Center);
await printer.writeln(
'DOUBLE WIDTH',
Style.DoubleWidth + Style.Bold,
Align.Center
);
await printer.writeln(
'DOUBLE HEIGHT',
Style.DoubleHeight + Style.Bold,
Align.Center
);
await printer.writeln(
'WIDTH AND HEIGHT',
Style.DoubleWidth + Style.DoubleHeight + Style.Bold,
Align.Center
);
await printer.writeln();
await printer.writeln(
'Áçênts $ 5',
Style.DoubleWidth | Style.DoubleWidth,
Align.Center
);
await printer.writeln();
await printer.writeln(`Columns: ${printer.columns}`, 0, Align.Center);
await printer.writeln(
'1 10 20 30 40 50 60 70 80'
);
await printer.writeln('| |');
await printer.writeln(
'12345678901234567890123456789012345678901234567890123456789012345678901234567890'
);
await printer.writeln('');
await printer.writeln('QR Code', 0, Align.Center);
await printer.setAlignment(Align.Center);
await printer.qrcode(
'https://github.com/linvix-sistemas/react-native-escpos-buffer',
11
);
await printer.setAlignment(Align.Left);
await printer.writeln('End QR Code', 0, Align.Center);
await printer.writeln(`Last Line - Feed: ${feed}`, 0, Align.Center);
await printer.cutter();
if (feed) {
await printer.feed(feed);
}
const buffer = connection.buffer();
const bytes = convertBufferToBytes(buffer);
try {
const print = await BluetoothPrinter.printRaw(bytes);
console.log(print);
} catch (error: Error | any) {
Alert.alert(error.message);
}
};
const convertBufferToBytes = (buffer: Buffer) => {
const bytes: number[] = [];
Array.from(buffer).map((byte) => {
bytes.push(byte);
});
return bytes;
};
useEffect(() => {
const listener = BluetoothPrinter.onDeviceFound((device: iDevice) => {
console.log(device);
setDevices((old) => {
if (!old.some((d) => d.address === device.address)) {
old.push(device);
}
return [...old];
});
});
return () => listener?.remove();
}, []);
useEffect(() => {
const listener = BluetoothPrinter.onDeviceAlreadyPaired(
(devices: iDevice[]) => {
(devices ?? []).map((device) => {
setDevices((old) => {
if (!old.some((d) => d.address === device.address)) {
old.push(device);
}
return [...old];
});
});
}
);
return () => listener?.remove();
}, []);
return (
<View style={styles.container}>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
margin: 20,
}}
>
<TouchableOpacity
onPress={goEnableBluetooth}
style={{
borderColor: '#405e7b',
borderWidth: 1,
borderRadius: 5,
padding: 10,
}}
>
<Text style={{ color: '#405e7b' }}>Ligar bluetooth</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={goScanDevices}
style={{
borderColor: '#405e7b',
borderWidth: 1,
borderRadius: 5,
padding: 10,
}}
>
<Text style={{ color: '#405e7b' }}>Buscar dispositivos</Text>
</TouchableOpacity>
</View>
<View
style={{
flexDirection: 'row',
justifyContent: 'center',
marginBottom: 20,
}}
>
<TouchableOpacity
onPress={goPrint}
style={{
borderColor: '#405e7b',
borderWidth: 1,
borderRadius: 5,
padding: 10,
}}
>
<Text style={{ color: '#405e7b' }}>Imprimir texto</Text>
</TouchableOpacity>
</View>
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={{ flexGrow: 1, padding: 10 }}
>
{Devices.map((device) => {
return (
<View key={device.address} style={{ flexDirection: 'row' }}>
<View
style={{ flex: 1, flexDirection: 'column', marginBottom: 10 }}
>
<Text>{device.name}</Text>
<Text>{device.address}</Text>
</View>
<TouchableOpacity onPress={() => goConnect(device)}>
<Text style={{ color: '#405e7b' }}>Conectar</Text>
</TouchableOpacity>
</View>
);
})}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});