-
Notifications
You must be signed in to change notification settings - Fork 9
/
ScanHotspots.tsx
149 lines (133 loc) · 3.4 KB
/
ScanHotspots.tsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { useCallback, useEffect, useState } from 'react'
import {
Alert,
Button,
FlatList,
Platform,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native'
import { SafeAreaView } from 'react-native-safe-area-context'
import { Device, useHotspotBle } from '@helium/react-native-sdk'
import {
request,
check,
PERMISSIONS,
RESULTS,
PermissionStatus,
} from 'react-native-permissions'
import { useNavigation } from '@react-navigation/native'
import type { HotspotBleNavProp } from './HotspotBLENav'
const ScanHotspots = () => {
const { startScan, stopScan, connect, scannedDevices } = useHotspotBle()
const [scanning, setScanning] = useState(false)
const [canScan, setCanScan] = useState<boolean | undefined>(undefined)
const navigation = useNavigation<HotspotBleNavProp>()
const showError = (error: any) => {
console.log(error)
Alert.alert(error.toString())
}
const updateCanScan = useCallback((result: PermissionStatus) => {
switch (result) {
case RESULTS.UNAVAILABLE:
case RESULTS.BLOCKED:
case RESULTS.DENIED:
case RESULTS.LIMITED:
setCanScan(false)
break
case RESULTS.GRANTED:
setCanScan(true)
break
}
}, [])
useEffect(() => {
if (Platform.OS === 'ios') {
setCanScan(true)
return
}
check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
.then(updateCanScan)
.catch(showError)
}, [updateCanScan])
useEffect(() => {
if (canScan !== false) return
request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
.then(updateCanScan)
.catch(showError)
}, [canScan, updateCanScan])
const handleScanPress = useCallback(() => {
const shouldScan = !scanning
setScanning(shouldScan)
if (shouldScan) {
startScan((error) => {
if (error) {
showError(error)
}
})
} else {
stopScan()
}
}, [scanning, startScan, stopScan])
const navNext = useCallback(
() => navigation.push('HotspotSettings'),
[navigation]
)
const connectDevice = useCallback(
(d: Device) => async () => {
try {
await connect(d)
if (scanning) {
stopScan()
setScanning(false)
}
navNext()
} catch (e) {
console.log(e)
}
},
[connect, navNext, scanning, stopScan]
)
const renderItem = React.useCallback(
({ item: device }: { index: number; item: Device }) => {
return (
<TouchableOpacity
onPress={connectDevice(device)}
style={styles.listItemContainer}
>
<Text>{device.name}</Text>
</TouchableOpacity>
)
},
[connectDevice]
)
const keyExtractor = React.useCallback(({ id }: Device) => id, [])
return (
<SafeAreaView style={styles.container} edges={['bottom']}>
<FlatList
contentContainerStyle={styles.container}
data={scannedDevices}
renderItem={renderItem}
keyExtractor={keyExtractor}
/>
{canScan && (
<Button
title={scanning ? 'Stop Scan' : 'Start Scan'}
onPress={handleScanPress}
/>
)}
</SafeAreaView>
)
}
const styles = StyleSheet.create({
container: { flex: 1 },
listItemContainer: {
height: 60,
padding: 16,
backgroundColor: 'white',
justifyContent: 'center',
borderBottomColor: 'lightgray',
borderBottomWidth: 1,
},
})
export default ScanHotspots