-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
155 lines (145 loc) · 3.94 KB
/
App.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
import React, {useEffect, useState, useMemo} from 'react';
import {
Text,
View,
NativeModules,
TouchableOpacity,
FlatList,
DeviceEventEmitter,
} from 'react-native';
// Khai báo Native module
const rfidModule = NativeModules.RfidAndroidNative;
function App(props) {
const [devicesConnect, setDevicesConnect] = useState('');
const [devicesList, setDevicesList] = useState([]);
const [listRFID, setListRFID] = useState([]);
const [clearFfid, setClearRfid] = useState(false);
//Thêm event listener khi read rfid
//Khi clear tạo 1 state refresh lại list rfid để listener receive state list
useEffect(() => {
DeviceEventEmitter.removeAllListeners();
DeviceEventEmitter.addListener('ScanBLEListenner', res => {
devicesList.push(res);
setDevicesList([...devicesList]);
});
DeviceEventEmitter.addListener('ReadRFIDListenner', res => {
listRFID.push(res);
setListRFID([...listRFID]);
});
}, [clearFfid]);
//connect sled reader khi didmount
useEffect(() => {
rfidModule.connectAddress('EA:E2:7C:B8:2E:2F').then(res => {
setDevicesConnect(res);
});
}, []);
function renderItemBLE({item, index}) {
return (
<TouchableOpacity
style={{
width: '100%',
height: 30,
backgroundColor: '#FFFFFF',
marginHorizontal: 10,
marginBottom: 3,
}}
onPress={() => {
rfidModule.stopScanBLE();
rfidModule.connectAddress(item.address_device).then(res => {
setDevicesConnect(res);
});
}}>
<Text>{item.address_device}</Text>
</TouchableOpacity>
);
}
function renderItemRFID({item, index}) {
return (
<TouchableOpacity
style={{
width: '100%',
height: 30,
backgroundColor: '#FFFFFF',
marginHorizontal: 10,
marginBottom: 3,
}}>
<Text>{`${index + 1}. ${item.rfid_tag}`}</Text>
</TouchableOpacity>
);
}
function renderBTEvent(title, color, callback) {
return (
<TouchableOpacity
style={{
marginTop: 30,
backgroundColor: color,
width: '20%',
height: 40,
alignItems: 'center',
justifyContent: 'center',
}}
onPress={() => callback()}>
<Text style={{color: 'white'}}>{title}</Text>
</TouchableOpacity>
);
}
const renderListDevices = useMemo(() => {
return (
<FlatList
style={{flex: 1, width: '100%', marginTop: 20}}
data={devicesList}
renderItem={renderItemBLE}
/>
);
}, [devicesList]);
const renderListRFID = useMemo(() => {
return (
<FlatList
style={{flex: 1, width: '100%', marginTop: 20}}
data={listRFID}
renderItem={renderItemRFID}
/>
);
}, [listRFID]);
return (
<View style={{flex: 1, alignItems: 'center'}}>
<Text style={{marginTop: 20, fontWeight: '600'}}>
{devicesConnect ? `Connect: ${devicesConnect}` : ''}
</Text>
<View
style={{
flexDirection: 'row',
width: '100%',
justifyContent: 'space-around',
}}>
{renderBTEvent('Scan', 'gray', () => {
{
setDevicesList([]);
setClearRfid(!clearFfid);
rfidModule.ScanBLE();
}
})}
{renderBTEvent('Stop', 'red', () => {
{
rfidModule.stopScanBLE()
}
})}
{renderBTEvent('Start', 'blue', () => {
rfidModule.startScanRFID();
})}
{renderBTEvent('Stop', 'red', () => rfidModule.stop())}
{renderBTEvent('Clear', 'green', () =>
rfidModule.clearData().then(res => {
if (res) {
setListRFID([]);
setClearRfid(!clearFfid);
}
}),
)}
</View>
{/* {renderListDevices} */}
{renderListRFID}
</View>
);
}
export default App;