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
|
import React, {useEffect, useState} from 'react';
import type {PropsWithChildren} from 'react';
import {
Button,
FlatList,
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import Toast from 'react-native-toast-message';
import BleService, {Peripheral} from './ble-service';
const bleService = new BleService();
function App(): React.JSX.Element {
bleService: BleService;
const [isScanning, setIsScanning] = useState<boolean>(false);
const [peripherals, setPeripherals] = useState<
Map<Peripheral['id'], Peripheral>
>([]);
useEffect(() => {
bleService.setEvents({
bleManagerConnectPeripheral: e => console.log(e.peripheral),
bleManagerStartSuccess: () => {
Toast.show({
type: 'info',
text1: 'BleManager started..',
});
},
bleManagerDiscoverPeripheral: p => {
console.log(p.advertising.localName);
},
bleManagerStopScan: () => {
setIsScanning(false);
setPeripherals(bleService.getPeripherals());
console.log('scan: stop;');
Toast.show({
type: 'success',
text1: 'Scan stop..',
position: 'bottom',
});
},
bleManagerStartScan: () => {
setIsScanning(true);
console.log('scan: start;');
Toast.show({
type: 'info',
text1: 'Scan start..',
position: 'bottom',
});
},
bleManagerDidUpdateValueForCharacteristic: a => {
console.log(a.value);
},
});
return () => bleService.destroy();
});
return (
<>
<Toast />
<View>
<Text>Hello, World!!!!</Text>
{isScanning ? <Text>SKANUJE!</Text> : <Text>Juz nie</Text>}
<Button
onPress={() => {
bleService.scan();
}}
title="Scan!"
/>
<FlatList
data={[...peripherals.values()]}
renderItem={({item}) => (
<>
<Text>id: {item.id}</Text>
<Text>name: {item.name}</Text>
<Button
onPress={() => {
bleService.connect(item);
}}
title="Connect!"
/>
<Button
onPress={() => {
console.log(bleService.read(item));
}}
title="Read!"
/>
<Text>-------</Text>
</>
)}
/>
</View>
</>
);
}
export default App;
|