Skip to content

Commit de759f6

Browse files
Koalkball-hayden
andcommitted
feat: adding settings capabilities
Co-authored-by: Hayden Ball <[email protected]>
1 parent 24ae6e3 commit de759f6

File tree

10 files changed

+1278
-486
lines changed

10 files changed

+1278
-486
lines changed

example/app.config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config: ExpoConfig = {
77
version: "1.0.0",
88
orientation: "portrait",
99
assetBundlePatterns: ["**/*"],
10+
scheme: "rnmcumgr",
1011
splash: {
1112
image: ".assets/images/pd.png",
1213
backgroundColor: "#FFFFFF",
@@ -27,7 +28,15 @@ const config: ExpoConfig = {
2728
},
2829
plugins: [
2930
["expo-document-picker"],
30-
["./gradlePlugin.ts"]
31+
["./gradlePlugin.ts"],
32+
[
33+
'expo-build-properties',
34+
{
35+
android: {
36+
minSdkVersion: 26,
37+
},
38+
},
39+
],
3140
]
3241
};
3342

example/app/_layout.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Tabs } from 'expo-router';
2+
import 'react-native-reanimated';
3+
4+
export default function Home() {
5+
return (
6+
<Tabs>
7+
<Tabs.Screen
8+
name="update"
9+
options={{ title: 'Update' }}
10+
/>
11+
<Tabs.Screen
12+
name="settings"
13+
options={{ title: 'Settings' }}
14+
/>
15+
</Tabs>
16+
);
17+
}

example/app/settings.tsx

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import {
2+
readSetting,
3+
writeSetting,
4+
} from '@playerdata/react-native-mcu-manager';
5+
import { Buffer } from 'buffer';
6+
7+
import React, { useState } from 'react';
8+
import {
9+
Button,
10+
FlatList,
11+
Modal,
12+
SafeAreaView,
13+
ScrollView,
14+
StyleSheet,
15+
Text,
16+
TextInput,
17+
View,
18+
} from 'react-native';
19+
20+
import useBluetoothDevices from '../src/useBluetoothDevices';
21+
22+
const styles = StyleSheet.create({
23+
root: {
24+
padding: 16,
25+
},
26+
27+
block: {
28+
marginBottom: 16,
29+
},
30+
31+
list: {
32+
padding: 16,
33+
},
34+
});
35+
36+
export default function settings() {
37+
const [devicesListVisible, setDevicesListVisible] = useState(false);
38+
const [selectedDeviceId, setSelectedDeviceId] = useState<string>('');
39+
const [selectedDeviceName, setSelectedDeviceName] = useState<string | null>(
40+
null
41+
);
42+
43+
const [settingName, setSettingName] = useState<string>('');
44+
const [settingValue, setSettingValue] = useState<string>('');
45+
46+
const { devices, error: scanError } = useBluetoothDevices();
47+
48+
return (
49+
<SafeAreaView>
50+
<ScrollView contentContainerStyle={styles.root}>
51+
<Text style={styles.block}>Step 1 - Select Device to Update</Text>
52+
53+
<View style={styles.block}>
54+
{selectedDeviceId && (
55+
<>
56+
<Text>Selected:</Text>
57+
<Text>{selectedDeviceName}</Text>
58+
</>
59+
)}
60+
<Button
61+
onPress={() => setDevicesListVisible(true)}
62+
title="Select Device"
63+
/>
64+
</View>
65+
66+
<Modal visible={devicesListVisible}>
67+
<FlatList
68+
contentContainerStyle={styles.list}
69+
data={devices}
70+
keyExtractor={({ id }) => id}
71+
renderItem={({ item }) => (
72+
<View>
73+
<Text>{item.name || item.id}</Text>
74+
75+
<Button
76+
title="Select"
77+
onPress={() => {
78+
setSelectedDeviceId(item.id);
79+
setSelectedDeviceName(item.name);
80+
setDevicesListVisible(false);
81+
}}
82+
/>
83+
</View>
84+
)}
85+
ListHeaderComponent={() => <Text>{scanError}</Text>}
86+
/>
87+
</Modal>
88+
89+
<Text style={styles.block}>
90+
Step 2 - Read or Write settings to the device
91+
</Text>
92+
93+
<View style={styles.block}>
94+
<Text>Setting Name</Text>
95+
96+
<TextInput
97+
value={settingName || ''}
98+
onChangeText={setSettingName}
99+
placeholder="Enter setting name"
100+
/>
101+
</View>
102+
103+
<View style={styles.block}>
104+
<Text>Setting Value</Text>
105+
106+
<TextInput
107+
value={settingValue || ''}
108+
onChangeText={setSettingValue}
109+
placeholder="Enter setting name"
110+
/>
111+
</View>
112+
113+
<View style={styles.block}>
114+
<Button
115+
onPress={async () => {
116+
const valueB64 = await readSetting(selectedDeviceId, settingName);
117+
const decodedValue = Buffer.from(valueB64, 'base64').toString(
118+
'binary'
119+
);
120+
setSettingValue(decodedValue);
121+
}}
122+
title="Read Setting"
123+
/>
124+
</View>
125+
126+
<View style={styles.block}>
127+
<Text>{settingValue}</Text>
128+
<Button
129+
onPress={async () => {
130+
const encodedValue = Buffer.from(settingValue, 'binary').toString(
131+
'base64'
132+
);
133+
await writeSetting(selectedDeviceId, settingName, encodedValue);
134+
}}
135+
title="Write Setting"
136+
/>
137+
</View>
138+
</ScrollView>
139+
</SafeAreaView>
140+
);
141+
}

example/src/App.tsx renamed to example/app/update.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import {
1515
View,
1616
} from 'react-native';
1717

18-
import useBluetoothDevices from './useBluetoothDevices';
19-
import useFilePicker from './useFilePicker';
20-
import useFirmwareUpdate from './useFirmwareUpdate';
18+
import useBluetoothDevices from './../src/useBluetoothDevices';
19+
import useFilePicker from './../src/useFilePicker';
20+
import useFirmwareUpdate from './../src/useFirmwareUpdate';
2121

2222
const styles = StyleSheet.create({
2323
root: {
@@ -33,7 +33,7 @@ const styles = StyleSheet.create({
3333
},
3434
});
3535

36-
export default function App() {
36+
export default function update() {
3737
const [devicesListVisible, setDevicesListVisible] = useState(false);
3838
const [selectedDeviceId, setSelectedDeviceId] = useState<string | null>(null);
3939
const [selectedDeviceName, setSelectedDeviceName] = useState<string | null>(

example/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerRootComponent } from 'expo';
22

3-
import App from './src/App';
3+
import App from './src/Update';
44

55
// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
66
// It also ensures that whether you load the app in Expo Go or in a native build,

example/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "react-native-mcu-manager-example",
33
"description": "Example app for react-native-mcu-manager",
4+
"main": "expo-router/entry",
45
"version": "0.0.1",
56
"private": true,
67
"scripts": {
@@ -12,13 +13,21 @@
1213
},
1314
"dependencies": {
1415
"@playerdata/react-native-mcu-manager": "workspace:*",
15-
"expo": "52.0.25",
16+
"expo": "^52.0.25",
17+
"expo-build-properties": "0.12.5",
18+
"expo-constants": "~17.0.4",
1619
"expo-document-picker": "13.0.2",
20+
"expo-linking": "~7.0.4",
21+
"expo-router": "~4.0.16",
1722
"expo-splash-screen": "0.29.20",
23+
"expo-status-bar": "~2.0.1",
1824
"lodash": "4.17.21",
1925
"react": "18.3.1",
2026
"react-native": "0.76.3",
2127
"react-native-ble-plx": "3.4.0",
28+
"react-native-reanimated": "^3.16.7",
29+
"react-native-safe-area-context": "4.12.0",
30+
"react-native-screens": "~4.4.0",
2231
"react-native-toast-message": "2.2.1"
2332
},
2433
"devDependencies": {

0 commit comments

Comments
 (0)