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. --- App.tsx | 118 ------------------- __tests__/App.test.tsx | 2 +- android/app/src/debug/AndroidManifest.xml | 42 ++++++- android/app/src/main/AndroidManifest.xml | 53 +++++++-- dev-env | 10 ++ index.js | 2 +- package-lock.json | 21 +++- package.json | 4 +- src/App.tsx | 107 +++++++++++++++++ src/ble-service.ts | 186 ++++++++++++++++++++++++++++++ 10 files changed, 412 insertions(+), 133 deletions(-) delete mode 100644 App.tsx create mode 100755 dev-env create mode 100644 src/App.tsx create mode 100644 src/ble-service.ts diff --git a/App.tsx b/App.tsx deleted file mode 100644 index 125fe1b..0000000 --- a/App.tsx +++ /dev/null @@ -1,118 +0,0 @@ -/** - * Sample React Native App - * https://github.com/facebook/react-native - * - * @format - */ - -import React from 'react'; -import type {PropsWithChildren} from 'react'; -import { - SafeAreaView, - ScrollView, - StatusBar, - StyleSheet, - Text, - useColorScheme, - View, -} from 'react-native'; - -import { - Colors, - DebugInstructions, - Header, - LearnMoreLinks, - ReloadInstructions, -} from 'react-native/Libraries/NewAppScreen'; - -type SectionProps = PropsWithChildren<{ - title: string; -}>; - -function Section({children, title}: SectionProps): React.JSX.Element { - const isDarkMode = useColorScheme() === 'dark'; - return ( - - - {title} - - - {children} - - - ); -} - -function App(): React.JSX.Element { - const isDarkMode = useColorScheme() === 'dark'; - - const backgroundStyle = { - backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, - }; - - return ( - - - -
- -
- Edit App.tsx to change this - screen and then come back to see your edits. -
-
- -
-
- -
-
- Read the docs to discover what to do next: -
- -
- - - ); -} - -const styles = StyleSheet.create({ - sectionContainer: { - marginTop: 32, - paddingHorizontal: 24, - }, - sectionTitle: { - fontSize: 24, - fontWeight: '600', - }, - sectionDescription: { - marginTop: 8, - fontSize: 18, - fontWeight: '400', - }, - highlight: { - fontWeight: '700', - }, -}); - -export default App; diff --git a/__tests__/App.test.tsx b/__tests__/App.test.tsx index 9eac6fb..d5d3244 100644 --- a/__tests__/App.test.tsx +++ b/__tests__/App.test.tsx @@ -4,7 +4,7 @@ import 'react-native'; import React from 'react'; -import App from '../App'; +import App from '../src/App.tsx'; // Note: import explicitly to use the types shipped with jest. import {it} from '@jest/globals'; diff --git a/android/app/src/debug/AndroidManifest.xml b/android/app/src/debug/AndroidManifest.xml index eb98c01..b7c3a3c 100644 --- a/android/app/src/debug/AndroidManifest.xml +++ b/android/app/src/debug/AndroidManifest.xml @@ -1,6 +1,46 @@ + xmlns:tools="http://schemas.android.com/tools"> + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + diff --git a/dev-env b/dev-env new file mode 100755 index 0000000..452ee7a --- /dev/null +++ b/dev-env @@ -0,0 +1,10 @@ +#!/bin/sh + +docker run -it \ + --rm \ + -v "$(pwd)":/app -w /app \ + -v react-native-android-data:/root \ + --privileged \ + reactnativecommunity/react-native-android \ + $@ + diff --git a/index.js b/index.js index a850d03..85f14e6 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ */ import {AppRegistry} from 'react-native'; -import App from './App'; +import App from './src/App.tsx'; import {name as appName} from './app.json'; AppRegistry.registerComponent(appName, () => App); diff --git a/package-lock.json b/package-lock.json index 930ef30..f52af31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,9 @@ "version": "0.0.1", "dependencies": { "react": "18.2.0", - "react-native": "0.73.0" + "react-native": "0.73.0", + "react-native-ble-manager": "^11.0.5", + "react-native-toast-message": "^2.1.8" }, "devDependencies": { "@babel/core": "^7.20.0", @@ -11769,6 +11771,23 @@ "react": "18.2.0" } }, + "node_modules/react-native-ble-manager": { + "version": "11.0.5", + "resolved": "https://registry.npmjs.org/react-native-ble-manager/-/react-native-ble-manager-11.0.5.tgz", + "integrity": "sha512-d8q6YEKyNkiygCy+qHa+N139UHRSWtbz7S+29mL0ZVjCSdzA6PRPsnSVDeRQJEVrWzP8ut2O6py57b6mPnBBFA==", + "peerDependencies": { + "react-native": ">=0.60.0" + } + }, + "node_modules/react-native-toast-message": { + "version": "2.1.8", + "resolved": "https://registry.npmjs.org/react-native-toast-message/-/react-native-toast-message-2.1.8.tgz", + "integrity": "sha512-vlDQVkvpSq9L3/GRfoKaxVQyenj/A7yHToC9M0nrSCTR9XPXzoc2AOgRQnM6GVvDhetDld9ZfGtFcucyWcGQNA==", + "peerDependencies": { + "react": "*", + "react-native": "*" + } + }, "node_modules/react-native/node_modules/@jest/types": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/@jest/types/-/types-26.6.2.tgz", diff --git a/package.json b/package.json index c51391e..7455929 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ }, "dependencies": { "react": "18.2.0", - "react-native": "0.73.0" + "react-native": "0.73.0", + "react-native-ble-manager": "^11.0.5", + "react-native-toast-message": "^2.1.8" }, "devDependencies": { "@babel/core": "^7.20.0", 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} +