-
Notifications
You must be signed in to change notification settings - Fork 56
/
App.tsx
executable file
·274 lines (259 loc) · 11.6 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
import React from 'react';
import { useState, useEffect } from 'react';
import {
View,
FlatList,
Text,
Button,
TextInput,
PermissionsAndroid,
Platform
} from 'react-native';
import CheckBox from '@react-native-community/checkbox';
import {
InterfaceType,
StarConnectionSettings,
StarPrinter
} from 'react-native-star-io10';
export default function App() {
const [interfaceType, setInterfaceType] = useState(InterfaceType.Lan);
const [identifier, setIdentifier] = useState("00:11:62:00:00:00");
const [statusList, setStatusList] = useState<String[]>([]);
const [printer, setPrinter] = useState<StarPrinter | undefined>(undefined);
const [isMonitoring, setIsMonitoring] = useState(false);
async function _onPressMonitorButton() {
var settings = new StarConnectionSettings();
settings.interfaceType = interfaceType;
settings.identifier = identifier;
// settings.autoSwitchInterface = true;
// If you are using Android 12 and targetSdkVersion is 31 or later,
// you have to request Bluetooth permission (Nearby devices permission) to use the Bluetooth printer.
// https://developer.android.com/about/versions/12/features/bluetooth-permissions
if (Platform.OS == 'android' && 31 <= Platform.Version) {
if (interfaceType == InterfaceType.Bluetooth || settings.autoSwitchInterface == true) {
var hasPermission = await _confirmBluetoothPermission();
if (!hasPermission) {
console.log(`PERMISSION ERROR: You have to allow Nearby devices to use the Bluetooth printer`);
return;
}
}
}
try {
if (isMonitoring) {
await printer?.close();
await printer?.dispose();
setPrinter(undefined);
} else {
setStatusList([]);
setPrinter(new StarPrinter(settings));
}
setIsMonitoring(!isMonitoring);
}
catch(error) {
console.log(`Error: ${String(error)}`);
}
}
useEffect(() => {
const _startMonitor = async () => {
if (printer == undefined) {
return;
}
printer.printerDelegate.onCommunicationError = (error) => {
var status = `Printer: Communication Error\n${String(error)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onReady = () => {
var status = `Printer: Ready`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onError = () => {
var status = `Printer: Error`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onPaperReady = () => {
var status = `Printer: Paper Ready`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onPaperNearEmpty = () => {
var status = `Printer: Paper Near Empty`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onPaperEmpty = () => {
var status = `Printer: Paper Empty`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onCoverOpened = () => {
var status = `Printer: Cover Opened`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.printerDelegate.onCoverClosed = () => {
var status = `Printer: Cover Closed`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.drawerDelegate.onCommunicationError = (error) => {
var status = `Drawer: Communication Error\n${String(error)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.drawerDelegate.onOpenCloseSignalSwitched = (openCloseSignal) => {
var status = `Drawer: Open Close Signal Switched: ${String(openCloseSignal)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.inputDeviceDelegate.onCommunicationError = (error) => {
var status = `Input Device: Communication Error\n${String(error)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.inputDeviceDelegate.onConnected = () => {
var status = `Input Device: Connected`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.inputDeviceDelegate.onDisconnected = () => {
var status = `Input Device: Disconnected`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.inputDeviceDelegate.onDataReceived = (data) => {
var status = `Input Device: DataReceived ${String(data)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.displayDelegate.onCommunicationError = (error) => {
var status = `Display: Communication Error\n${String(error)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.displayDelegate.onConnected = () => {
var status = `Display: Connected`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
printer.displayDelegate.onDisconnected = () => {
var status = `Display: Disconnected`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
try {
await printer.open();
}
catch(error) {
var status = `Error: ${String(error)}`;
console.log(status);
setStatusList((statusList) => [...statusList, status]);
}
}
_startMonitor();
}, [printer]);
async function _confirmBluetoothPermission(): Promise<boolean> {
var hasPermission = false;
try {
hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT);
if (!hasPermission) {
const status = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT);
hasPermission = status == PermissionsAndroid.RESULTS.GRANTED;
}
}
catch (err) {
console.warn(err);
}
return hasPermission;
}
return (
<View style={{ flex: 1, margin: 50 }}>
<View style={{ flexDirection: 'row' }}>
<Text style={{ width: 100 }}>Interface</Text>
<View style={{ margin: 10 }}>
<View style={{ flexDirection: 'row', marginTop: 0 }}>
<CheckBox
style={{ marginLeft: 20 }}
value={
interfaceType == InterfaceType.Lan
}
onValueChange={(isChecked) => {
if (isChecked) {
setInterfaceType(InterfaceType.Lan);
}
}}
/>
<Text style={{ marginLeft: 20 }}>LAN</Text>
</View>
<View style={{ flexDirection: 'row', marginTop: 10 }}>
<CheckBox
style={{ marginLeft: 20 }}
value={
interfaceType == InterfaceType.Bluetooth
}
onValueChange={(isChecked) => {
if (isChecked) {
setInterfaceType(InterfaceType.Bluetooth);
}
}}
/>
<Text style={{ marginLeft: 20 }}>Bluetooth</Text>
</View>
<View style={{ flexDirection: 'row', marginTop: 10 }}>
<CheckBox
style={{ marginLeft: 20 }}
value={
interfaceType == InterfaceType.BluetoothLE
}
onValueChange={(isChecked) => {
if (isChecked) {
setInterfaceType(InterfaceType.BluetoothLE);
}
}}
/>
<Text style={{ marginLeft: 20 }}>BluetoothLE</Text>
</View>
<View style={{ flexDirection: 'row', marginTop: 10 }}>
<CheckBox
style={{ marginLeft: 20 }}
value={
interfaceType == InterfaceType.Usb
}
onValueChange={(isChecked) => {
if (isChecked) {
setInterfaceType(InterfaceType.Usb);
}
}}
/>
<Text style={{ marginLeft: 20 }}>USB</Text>
</View>
</View>
</View>
<View style={{ flexDirection: 'row', marginTop: 30 }}>
<Text style={{ width: 100 }}>Identifier</Text>
<TextInput
style={{ width: 200, marginLeft: 20 }}
value={identifier}
onChangeText={(value) => {
setIdentifier(value);
}}
/>
</View>
<View style={{ width: 120, marginTop: 20 }}>
<Button
title={isMonitoring ? "Stop Monitoring" : "Start Monitoring"}
onPress={_onPressMonitorButton}
/>
</View>
<View style={{ flex: 1, alignSelf: 'stretch', marginTop: 20 }}>
<FlatList
style={{ marginTop: 30 }}
data={statusList}
renderItem={({item}) => <Text>{item}</Text>}
keyExtractor={(item, index) => index.toString()} />
</View>
</View>
);
};