-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarcodeExample.js
More file actions
166 lines (152 loc) · 4.43 KB
/
Copy pathBarcodeExample.js
File metadata and controls
166 lines (152 loc) · 4.43 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
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
import React, { useEffect, useState } from 'react';
import { View, Text, Button, StyleSheet, Alert, ScrollView } from 'react-native';
import { BarcodeScanner } from 'react-native-newland';
const BarcodeExample = () => {
const [scannedData, setScannedData] = useState(null);
const [isEnabled, setIsEnabled] = useState(false);
const [scanHistory, setScanHistory] = useState([]);
useEffect(() => {
// Set up barcode event listeners
const barcodeSubscription = BarcodeScanner.onBarcode((data) => {
console.log('Barcode scanned:', data);
setScannedData(data);
// Add to history
setScanHistory(prev => [data, ...prev.slice(0, 9)]); // Keep last 10 scans
Alert.alert(
'Barcode Scanned',
`Data: ${data.data}\nType: ${data.type}\nCategory: ${data.category}\nType ID: ${data.typeId}\nStatus: ${data.status}`
);
});
// Cleanup subscription on unmount
return () => {
barcodeSubscription.remove();
BarcodeScanner.release();
};
}, []);
const handleEnable = () => {
BarcodeScanner.enable();
setIsEnabled(true);
};
const handleDisable = () => {
BarcodeScanner.disable();
setIsEnabled(false);
};
const handleScan = () => {
BarcodeScanner.read({
// Optional configuration
timeout: 30000, // 30 seconds timeout
continuous: false // Single scan mode
});
};
const formatTimestamp = (timestamp) => {
return new Date(parseInt(timestamp)).toLocaleString();
};
return (
<ScrollView style={styles.container}>
<Text style={styles.title}>Newland Barcode Scanner</Text>
<View style={styles.buttonContainer}>
<Button
title={isEnabled ? "Disable Scanner" : "Enable Scanner"}
onPress={isEnabled ? handleDisable : handleEnable}
/>
</View>
<View style={styles.buttonContainer}>
<Button
title="Scan Barcode"
onPress={handleScan}
disabled={!isEnabled}
/>
</View>
{scannedData && (
<View style={styles.resultContainer}> <Text style={styles.resultTitle}>Last Scanned:</Text>
<Text style={styles.resultText}>Data: {scannedData.data}</Text>
<Text style={styles.resultText}>Type: {scannedData.type}</Text>
<Text style={styles.resultText}>Category: {scannedData.category}</Text>
<Text style={styles.resultText}>Type ID: {scannedData.typeId}</Text>
<Text style={styles.resultText}>Status: {scannedData.status}</Text>
<Text style={styles.resultText}>Time: {formatTimestamp(scannedData.timestamp)}</Text>
</View>
)}
{scanHistory.length > 0 && (
<View style={styles.historyContainer}>
<Text style={styles.historyTitle}>Scan History:</Text>
{scanHistory.map((scan, index) => ( <View key={index} style={styles.historyItem}>
<Text style={styles.historyData}>{scan.data}</Text>
<Text style={styles.historyType}>{scan.type} ({scan.category})</Text>
<Text style={styles.historyType}>ID: {scan.typeId}</Text>
<Text style={styles.historyTime}>{formatTimestamp(scan.timestamp)}</Text>
</View>
))}
</View>
)}
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
},
title: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 30,
},
buttonContainer: {
marginVertical: 10,
},
resultContainer: {
marginTop: 30,
padding: 15,
backgroundColor: '#e8f5e8',
borderRadius: 8,
borderWidth: 1,
borderColor: '#4caf50',
},
resultTitle: {
fontSize: 18,
fontWeight: 'bold',
marginBottom: 10,
color: '#2e7d32',
},
resultText: {
fontSize: 16,
marginVertical: 2,
color: '#333',
},
historyContainer: {
marginTop: 20,
padding: 15,
backgroundColor: '#f5f5f5',
borderRadius: 8,
},
historyTitle: {
fontSize: 18,
fontWeight: 'bold',
marginBottom: 10,
color: '#333',
},
historyItem: {
padding: 10,
marginVertical: 2,
backgroundColor: '#fff',
borderRadius: 4,
borderLeftWidth: 3,
borderLeftColor: '#2196f3',
},
historyData: {
fontSize: 16,
fontWeight: 'bold',
color: '#333',
},
historyType: {
fontSize: 14,
color: '#666',
},
historyTime: {
fontSize: 12,
color: '#999',
},
});
export default BarcodeExample;