-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApp.tsx
146 lines (134 loc) · 3.63 KB
/
App.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
/* eslint-disable react-native/no-inline-styles */
import * as React from "react"
import {
StyleSheet,
View,
Text,
Button,
PermissionsAndroid,
Platform,
NativeEventEmitter,
NativeModules,
} from "react-native"
import {
startCentral,
startPeripheral,
advertise,
scan,
connect,
write,
notify,
} from "react-native-ble-didcomm-sdk"
import { presentationMsg } from "./presentationMsg"
const bleDidcommSdkEmitter = new NativeEventEmitter(NativeModules.BleDidcommSdk)
const Spacer = () => <View style={{ height: 20, width: 20 }} />
const msg = JSON.stringify(presentationMsg)
const requestPermissions = async () => {
await PermissionsAndroid.requestMultiple([
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.BLUETOOTH_CONNECT",
"android.permission.BLUETOOTH_SCAN",
"android.permission.BLUETOOTH_ADVERTISE",
"android.permission.ACCESS_COARSE_LOCATION",
])
}
export default function App() {
const [isCentral, setIsCentral] = React.useState(false)
const [isPeripheral, setIsPeripheral] = React.useState(false)
const [peripheralId, setPeripheralId] = React.useState<string>()
const [connected, setConnected] = React.useState(false)
React.useEffect(() => {
const onDiscoverPeripheralListener = bleDidcommSdkEmitter.addListener(
"onDiscoverPeripheral",
({
peripheralId: pId,
name,
}: {
peripheralId: string
name?: string
}) => {
console.log(`Discovered: ${pId} ${name ? "with name:" + name : ""}`)
setPeripheralId(pId)
}
)
const onConnectedPeripheralListener = bleDidcommSdkEmitter.addListener(
"onConnectedPeripheral",
({ peripheralId: pId }: { peripheralId: string }) => {
console.log(`Connected to: ${pId}`)
setConnected(true)
}
)
const onReceivedNotificationListener = bleDidcommSdkEmitter.addListener(
"onReceivedNotification",
console.log
)
const onReceivedWriteWithoutResponseListener = bleDidcommSdkEmitter.addListener(
"onReceivedWriteWithoutResponse",
console.log
)
return () => {
onDiscoverPeripheralListener.remove()
onConnectedPeripheralListener.remove()
onReceivedNotificationListener.remove()
onReceivedWriteWithoutResponseListener.remove()
}
}, [])
return (
<View style={styles.container}>
<Text>Bluetooth demo screen</Text>
<Spacer />
{Platform.OS === "android" && (
<>
<Button
title="requestPermissions"
onPress={async () => {
await requestPermissions()
}}
/>
<Spacer />
</>
)}
<Button
title="start: central"
onPress={async () => {
await startCentral()
setIsCentral(true)
}}
/>
<Button
title="start: peripheral"
onPress={async () => {
await startPeripheral()
setIsPeripheral(true)
}}
/>
{isCentral && (
<>
<Button title="scan" onPress={scan} />
{peripheralId && (
<Button title="connect" onPress={() => connect(peripheralId)} />
)}
{connected && <Button title="write" onPress={() => write(msg)} />}
</>
)}
{isPeripheral && (
<>
<Button title="advertise" onPress={advertise} />
<Button title="notify" onPress={() => notify(msg)} />
</>
)}
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
box: {
width: 60,
height: 60,
marginVertical: 20,
},
})