diff options
Diffstat (limited to 'src/App.tsx')
-rw-r--r-- | src/App.tsx | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/App.tsx b/src/App.tsx new file mode 100644 index 0000000..7972da9 --- /dev/null +++ b/src/App.tsx | |||
@@ -0,0 +1,107 @@ | |||
1 | import React, {useEffect, useState} from 'react'; | ||
2 | import type {PropsWithChildren} from 'react'; | ||
3 | import { | ||
4 | Button, | ||
5 | FlatList, | ||
6 | SafeAreaView, | ||
7 | ScrollView, | ||
8 | StatusBar, | ||
9 | StyleSheet, | ||
10 | Text, | ||
11 | useColorScheme, | ||
12 | View, | ||
13 | } from 'react-native'; | ||
14 | |||
15 | import Toast from 'react-native-toast-message'; | ||
16 | import BleService, {Peripheral} from './ble-service'; | ||
17 | |||
18 | const bleService = new BleService(); | ||
19 | |||
20 | function App(): React.JSX.Element { | ||
21 | bleService: BleService; | ||
22 | |||
23 | const [isScanning, setIsScanning] = useState<boolean>(false); | ||
24 | const [peripherals, setPeripherals] = useState< | ||
25 | Map<Peripheral['id'], Peripheral> | ||
26 | >([]); | ||
27 | |||
28 | useEffect(() => { | ||
29 | bleService.setEvents({ | ||
30 | bleManagerConnectPeripheral: e => console.log(e.peripheral), | ||
31 | bleManagerStartSuccess: () => { | ||
32 | Toast.show({ | ||
33 | type: 'info', | ||
34 | text1: 'BleManager started..', | ||
35 | }); | ||
36 | }, | ||
37 | bleManagerDiscoverPeripheral: p => { | ||
38 | console.log(p.advertising.localName); | ||
39 | }, | ||
40 | bleManagerStopScan: () => { | ||
41 | setIsScanning(false); | ||
42 | setPeripherals(bleService.getPeripherals()); | ||
43 | |||
44 | console.log('scan: stop;'); | ||
45 | Toast.show({ | ||
46 | type: 'success', | ||
47 | text1: 'Scan stop..', | ||
48 | position: 'bottom', | ||
49 | }); | ||
50 | }, | ||
51 | bleManagerStartScan: () => { | ||
52 | setIsScanning(true); | ||
53 | console.log('scan: start;'); | ||
54 | Toast.show({ | ||
55 | type: 'info', | ||
56 | text1: 'Scan start..', | ||
57 | position: 'bottom', | ||
58 | }); | ||
59 | }, | ||
60 | bleManagerDidUpdateValueForCharacteristic: a => { | ||
61 | console.log(a.value); | ||
62 | }, | ||
63 | }); | ||
64 | |||
65 | return () => bleService.destroy(); | ||
66 | }); | ||
67 | |||
68 | return ( | ||
69 | <> | ||
70 | <Toast /> | ||
71 | <View> | ||
72 | <Text>Hello, World!!!!</Text> | ||
73 | {isScanning ? <Text>SKANUJE!</Text> : <Text>Juz nie</Text>} | ||
74 | <Button | ||
75 | onPress={() => { | ||
76 | bleService.scan(); | ||
77 | }} | ||
78 | title="Scan!" | ||
79 | /> | ||
80 | <FlatList | ||
81 | data={[...peripherals.values()]} | ||
82 | renderItem={({item}) => ( | ||
83 | <> | ||
84 | <Text>id: {item.id}</Text> | ||
85 | <Text>name: {item.name}</Text> | ||
86 | <Button | ||
87 | onPress={() => { | ||
88 | bleService.connect(item); | ||
89 | }} | ||
90 | title="Connect!" | ||
91 | /> | ||
92 | <Button | ||
93 | onPress={() => { | ||
94 | console.log(bleService.read(item)); | ||
95 | }} | ||
96 | title="Read!" | ||
97 | /> | ||
98 | <Text>-------</Text> | ||
99 | </> | ||
100 | )} | ||
101 | /> | ||
102 | </View> | ||
103 | </> | ||
104 | ); | ||
105 | } | ||
106 | |||
107 | export default App; | ||