-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.js
206 lines (188 loc) · 5.01 KB
/
print.js
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
import React, { useEffect, useState } from 'react';
import {
View,
Text,
TextInput,
TouchableOpacity,
FlatList,
StyleSheet,
Alert,
PermissionsAndroid, Platform
} from 'react-native';
import { NativeModules } from 'react-native';
const { ZSDKModule } = NativeModules;
const PrinterScreen = () => {
const [macAddress, setMacAddress] = useState('');
const [zplText, setZplText] = useState('^XA^FO50,50^A0N,50,50^FDHello World^FS^XZ');
const [printers, setPrinters] = useState([]);
const [loading, setLoading] = useState(false);
async function requestBluetoothPermissions() {
if (Platform.OS === 'android' && Platform.Version >= 31) {
try {
const granted = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
]);
if (
granted[PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT] === PermissionsAndroid.RESULTS.GRANTED &&
granted[PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN] === PermissionsAndroid.RESULTS.GRANTED
) {
console.log("Bluetooth permissions granted.");
} else {
console.log("Bluetooth permissions denied.");
}
} catch (err) {
console.warn(err);
}
}
}
useEffect(() => {
requestBluetoothPermissions();
}, []);
// Discover Printers
const discoverPrinters = () => {
setLoading(true);
ZSDKModule.zsdkPrinterDiscoveryBluetooth((error, result) => {
setLoading(false);
if (error) {
Alert.alert('Error', `Printer Discovery Failed: ${error}`);
} else {
const discoveredPrinters = JSON.parse(result);
setPrinters(discoveredPrinters);
Alert.alert('Success', `Found ${discoveredPrinters.length} printer(s)`);
}
});
};
// Send Print Job
const sendPrintJob = () => {
if (!macAddress) {
Alert.alert('Error', 'Please enter a MAC address.');
return;
}
ZSDKModule.zsdkWriteBluetooth(macAddress, zplText);
Alert.alert('Success', 'Print job sent.');
};
return (
<View style={styles.container}>
<Text style={styles.title}>Printer Management</Text>
{/* MAC Address Input */}
<Text style={styles.label}>Printer MAC Address:</Text>
<TextInput
style={styles.input}
placeholder="Enter MAC Address"
value={macAddress}
onChangeText={setMacAddress}
/>
{/* ZPL Text Input */}
<Text style={styles.label}>ZPL Data:</Text>
<TextInput
style={[styles.input, styles.textArea]}
placeholder="Enter ZPL"
value={zplText}
onChangeText={setZplText}
multiline
/>
{/* Buttons */}
<View style={styles.buttonContainer}>
<TouchableOpacity style={styles.button} onPress={discoverPrinters}>
<Text style={styles.buttonText}>
{loading ? 'Discovering...' : 'Discover Printers'}
</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.secondaryButton]} onPress={sendPrintJob}>
<Text style={styles.buttonText}>Send Print Job</Text>
</TouchableOpacity>
</View>
{/* Printer List */}
<Text style={styles.label}>Discovered Printers:</Text>
<FlatList
data={printers}
keyExtractor={(item, index) => `${item.address}-${index}`}
renderItem={({ item }) => (
<View style={styles.printerItem}>
<Text style={styles.printerText}>{item.friendlyName}</Text>
<Text style={styles.printerSubText}>{item.address}</Text>
</View>
)}
ListEmptyComponent={
<Text style={styles.emptyText}>No printers discovered yet.</Text>
}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
height: "100%",
backgroundColor:"white",
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
color: 'red',
},
label: {
fontSize: 16,
marginBottom: 5,
color: '#555',
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
padding: 10,
marginBottom: 15,
backgroundColor: '#fff',
fontSize: 16,
},
textArea: {
height: 100,
textAlignVertical: 'top',
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 20,
},
button: {
flex: 1,
padding: 15,
backgroundColor: '#007bff',
borderRadius: 8,
alignItems: 'center',
marginHorizontal: 5,
},
secondaryButton: {
backgroundColor: '#28a745',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: 'bold',
},
printerItem: {
padding: 15,
backgroundColor: '#fff',
marginBottom: 10,
borderRadius: 8,
borderColor: '#ccc',
borderWidth: 1,
},
printerText: {
fontSize: 16,
fontWeight: 'bold',
color: '#333',
},
printerSubText: {
fontSize: 14,
},
emptyText: {
textAlign: 'center',
fontSize: 16,
marginTop: 20,
},
});
export default PrinterScreen;