-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
132 lines (115 loc) · 3.34 KB
/
App.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { StatusBar } from "expo-status-bar";
import { useEffect, useReducer, useRef, useState } from "react";
import { FlatList, ScrollView, StyleSheet, Text, View } from "react-native";
import { BleManager } from "react-native-ble-plx";
import tw from "twrnc";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
let initialState = {
isScanning: false,
scanDone: false,
bleReady: false,
items: [],
};
const reducer = (state, action) => {
switch (action.type) {
case "setBLEReady":
return { ...state, bleReady: true };
break;
case "setBLEScan":
return { ...state, isScanning: action.payload };
case "addItem":
let items = state.items;
if (items.findIndex((x) => x.id == action.payload.id) == -1) {
items.push(action.payload);
}
return { ...state, items: items };
case "stopScan":
return { ...state, isScanning: false, scanDone: true };
case "reset":
return {
...state,
items: [],
bleReady: true,
isScanning: false,
scanDone: false,
};
default:
return state;
}
};
const manager = new BleManager();
export default function App() {
const [state, dispatch] = useReducer(reducer, initialState);
const stopBLEScan = () => {
manager.stopDeviceScan();
dispatch({ type: "stopScan" });
};
const timerRef = useRef(null);
const scanAndConnect = () => {
manager.startDeviceScan(null, null, (error, device) => {
dispatch({ type: "setBLEScan", payload: true });
if (error) {
// Handle error (scanning will be stopped automatically)
return;
}
dispatch({ type: "addItem", payload: device });
});
};
useEffect(() => {
if (state.bleReady && !state.isScanning && !state.scanDone) {
scanAndConnect();
}
if (state.scanTimeout <= 0 && state.isScanning && !state.scanDone) {
dispatch({ type: "stopScan" });
}
}, [state]);
useEffect(() => {
timerRef.current = setTimeout(() => {
stopBLEScan();
}, 1000 * 10);
return () => {
if (timerRef.current) clearTimeout(timerRef.current);
};
}, []);
useEffect(() => {
const subscription = manager.onStateChange((state) => {
if (state === "PoweredOn") {
dispatch({ type: "setBLEReady" });
subscription.remove();
}
}, true);
return () => subscription.remove();
}, [manager]);
const renderItem = ({ item }) => {
return (
<View style={tw`p-2 border rounded-md m-2`}>
<Text style={tw`text-[24px]`}>{item.name || "No name found"}</Text>
<Text style={tw`text-[12px]`}>{item.id}</Text>
</View>
);
};
return (
<SafeAreaProvider>
<StatusBar style="auto" />
<SafeAreaView
edges={["bottom", "left", "right", "top"]}
style={tw`text-red-200 flex`}
>
<View style={tw` h-full flex`}>
<View style={tw`p-4 border-b`}>
<Text style={tw`text-[22px]`}>
Available BLE devices in your area
</Text>
</View>
<FlatList
refreshing={state.isScanning}
onRefresh={() => dispatch({ type: "reset" })}
renderItem={renderItem}
data={state.items}
style={tw`flex border-b`}
/>
</View>
</SafeAreaView>
</SafeAreaProvider>
);
}