From 1749bd5e7fa959a022472defb8ba78c689c994af Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Mon, 11 Dec 2023 15:31:44 +0100 Subject: Add initial ble-service. --- src/App.tsx | 107 ++++++++++++++++++++++++++++++ src/ble-service.ts | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 src/App.tsx create mode 100644 src/ble-service.ts (limited to 'src') 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 @@ +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(false); + const [peripherals, setPeripherals] = useState< + Map + >([]); + + 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 ( + <> + + + Hello, World!!!! + {isScanning ? SKANUJE! : Juz nie} +