forked from star-micronics/react-native-star-io10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
192 lines (170 loc) · 6.63 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
import React from 'react';
import {
View,
Text,
Button,
TextInput,
PermissionsAndroid,
Platform
} from 'react-native';
import {
Picker
} from '@react-native-picker/picker';
import {
InterfaceType,
StarConnectionSettings,
StarPrinter
} from 'react-native-star-io10';
interface AppProps {
}
interface AppState {
interfaceType: InterfaceType;
identifier: string;
}
class App extends React.Component<AppProps, AppState> {
private _printer: StarPrinter;
private _onPressMonitorButton = async() => {
await this._printer.close();
await this._printer.dispose();
var settings = new StarConnectionSettings();
settings.interfaceType = this.state.interfaceType;
settings.identifier = this.state.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 (this.state.interfaceType == InterfaceType.Bluetooth || settings.autoSwitchInterface == true) {
var hasPermission = await this._confirmBluetoothPermission();
if (!hasPermission) {
console.log(`PERMISSION ERROR: You have to allow Nearby devices to use the Bluetooth printer`);
return;
}
}
}
this._printer = new StarPrinter(settings);
this._printer.printerDelegate.onCommunicationError = (error) => {
console.log(`Printer: Communication Error`);
console.log(error);
}
this._printer.printerDelegate.onReady = () => {
console.log(`Printer: Ready`);
}
this._printer.printerDelegate.onError = () => {
console.log(`Printer: Error`);
}
this._printer.printerDelegate.onPaperReady = () => {
console.log(`Printer: Paper Ready`);
}
this._printer.printerDelegate.onPaperNearEmpty = () => {
console.log(`Printer: Paper Near Empty`);
}
this._printer.printerDelegate.onPaperEmpty = () => {
console.log(`Printer: Paper Empty`);
}
this._printer.printerDelegate.onCoverOpened = () => {
console.log(`Printer: Cover Opened`);
}
this._printer.printerDelegate.onCoverClosed = () => {
console.log(`Printer: Cover Closed`);
}
this._printer.drawerDelegate.onCommunicationError = (error) => {
console.log(`Drawer: Communication Error`);
console.log(error);
}
this._printer.drawerDelegate.onOpenCloseSignalSwitched = (openCloseSignal) => {
console.log(`Drawer: Open Close Signal Switched: ${String(openCloseSignal)}`);
}
this._printer.inputDeviceDelegate.onCommunicationError = (error) => {
console.log(`Input Device: Communication Error`);
console.log(error);
}
this._printer.inputDeviceDelegate.onConnected = () => {
console.log(`Input Device: Connected`);
}
this._printer.inputDeviceDelegate.onDisconnected = () => {
console.log(`Input Device: Disconnected`);
}
this._printer.inputDeviceDelegate.onDataReceived = (data) => {
console.log(`Input Device: DataReceived ${String(data)}`);
}
this._printer.displayDelegate.onCommunicationError = (error) => {
console.log(`Display: Communication Error`);
console.log(error);
}
this._printer.displayDelegate.onConnected = () => {
console.log(`Display: Connected`);
}
this._printer.displayDelegate.onDisconnected = () => {
console.log(`Display: Disconnected`);
}
try {
await this._printer.open();
}
catch(error) {
console.log(`Error: ${String(error)}`);
}
}
private async _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;
}
constructor(props: any) {
super(props);
this.state = {
interfaceType: InterfaceType.Lan,
identifier: '00:11:62:00:00:00'
};
var settings = new StarConnectionSettings();
settings.interfaceType = this.state.interfaceType;
settings.identifier = this.state.identifier;
this._printer = new StarPrinter(settings);
}
render() {
return (
<View style={{ margin: 50 }}>
<View style={{ flexDirection: 'row' }}>
<Text style={{ width: 100 }}>Interface</Text>
<Picker
style={{ width: 200, marginLeft: 20, justifyContent: 'center' }}
selectedValue={this.state.interfaceType}
onValueChange={(value) => {
this.setState({ interfaceType: value });
}}>
<Picker.Item label='LAN' value={InterfaceType.Lan} />
<Picker.Item label='Bluetooth' value={InterfaceType.Bluetooth}/>
<Picker.Item label='Bluetooth LE' value={InterfaceType.BluetoothLE}/>
<Picker.Item label='USB' value={InterfaceType.Usb} />
</Picker>
</View>
<View style={{ flexDirection: 'row', marginTop: 30 }}>
<Text style={{ width: 100 }}>Identifier</Text>
<TextInput
style={{ width: 200, marginLeft: 20 }}
value={this.state.identifier}
onChangeText={(value) => {
this.setState({ identifier: value });
}}
/>
</View>
<View style={{ width: 100, marginTop: 20 }}>
<Button
title="Monitor"
onPress={this._onPressMonitorButton}
/>
</View>
</View>
);
}
};
export default App;