forked from kirillzyusko/react-native-wifi-p2p-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
296 lines (271 loc) · 8.6 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { PureComponent } from 'react';
import {
StyleSheet,
View,
Button
} from 'react-native';
import {
initialize,
startDiscoveringPeers,
stopDiscoveringPeers,
unsubscribeFromPeersUpdates,
unsubscribeFromThisDeviceChanged,
unsubscribeFromConnectionInfoUpdates,
subscribeOnConnectionInfoUpdates,
subscribeOnThisDeviceChanged,
subscribeOnPeersUpdates,
connect,
cancelConnect,
createGroup,
removeGroup,
getAvailablePeers,
sendFile,
receiveFile,
getConnectionInfo,
getGroupInfo,
receiveMessage,
sendMessage,
} from 'react-native-wifi-p2p';
import { PermissionsAndroid } from 'react-native';
type Props = {};
export default class App extends PureComponent<Props> {
state = {
devices: []
};
async componentDidMount() {
try {
await initialize();
// since it's required in Android >= 6.0
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
{
'title': 'Access to wi-fi P2P mode',
'message': 'ACCESS_COARSE_LOCATION'
}
);
console.log(granted === PermissionsAndroid.RESULTS.GRANTED ? "You can use the p2p mode" : "Permission denied: p2p mode will not work");
subscribeOnPeersUpdates(this.handleNewPeers);
subscribeOnConnectionInfoUpdates(this.handleNewInfo);
subscribeOnThisDeviceChanged(this.handleThisDeviceChanged);
const status = await startDiscoveringPeers();
console.log('startDiscoveringPeers status: ', status);
} catch (e) {
console.error(e);
}
}
componentWillUnmount() {
unsubscribeFromConnectionInfoUpdates(this.handleNewInfo);
unsubscribeFromPeersUpdates(this.handleNewPeers);
unsubscribeFromThisDeviceChanged(this.handleThisDeviceChanged)
}
handleNewInfo = (info) => {
console.log('OnConnectionInfoUpdated', info);
};
handleNewPeers = ({ devices }) => {
console.log('OnPeersUpdated', devices);
this.setState({ devices: devices });
};
handleThisDeviceChanged = (groupInfo) => {
console.log('THIS_DEVICE_CHANGED_ACTION', groupInfo);
};
connectToFirstDevice = () => {
console.log('Connect to: ', this.state.devices[0]);
connect(this.state.devices[0].deviceAddress)
.then(() => console.log('Successfully connected'))
.catch(err => console.error('Something gone wrong. Details: ', err));
};
onCancelConnect = () => {
cancelConnect()
.then(() => console.log('cancelConnect', 'Connection successfully canceled'))
.catch(err => console.error('cancelConnect', 'Something gone wrong. Details: ', err));
};
onCreateGroup = () => {
createGroup()
.then(() => console.log('Group created successfully!'))
.catch(err => console.error('Something gone wrong. Details: ', err));
};
onRemoveGroup = () => {
removeGroup()
.then(() => console.log('Currently you don\'t belong to group!'))
.catch(err => console.error('Something gone wrong. Details: ', err));
};
onStopInvestigation = () => {
stopDiscoveringPeers()
.then(() => console.log('Stopping of discovering was successful'))
.catch(err => console.error(`Something is gone wrong. Maybe your WiFi is disabled? Error details`, err));
};
onStartInvestigate = () => {
startDiscoveringPeers()
.then(status => console.log('startDiscoveringPeers', `Status of discovering peers: ${status}`))
.catch(err => console.error(`Something is gone wrong. Maybe your WiFi is disabled? Error details: ${err}`));
};
onGetAvailableDevices = () => {
getAvailablePeers()
.then(peers => console.log(peers));
};
onSendFile = () => {
//const url = '/storage/sdcard0/Music/Rammstein:Amerika.mp3';
const url = '/storage/emulated/0/Music/Bullet For My Valentine:Letting You Go.mp3';
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
{
'title': 'Access to read',
'message': 'READ_EXTERNAL_STORAGE'
}
)
.then(granted => {
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the storage")
} else {
console.log("Storage permission denied")
}
})
.then(() => {
return PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
'title': 'Access to write',
'message': 'WRITE_EXTERNAL_STORAGE'
}
)
})
.then(() => {
return sendFile(url)
.then((metaInfo) => console.log('File sent successfully', metaInfo))
.catch(err => console.log('Error while file sending', err));
})
.catch(err => console.log(err));
};
onReceiveFile = () => {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
{
'title': 'Access to read',
'message': 'READ_EXTERNAL_STORAGE'
}
)
.then(granted => {
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the storage")
} else {
console.log("Storage permission denied")
}
})
.then(() => {
return PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
'title': 'Access to write',
'message': 'WRITE_EXTERNAL_STORAGE'
}
)
})
.then(() => {
return receiveFile('/storage/emulated/0/Music/', 'BFMV:Letting You Go.mp3')
.then(() => console.log('File received successfully'))
.catch(err => console.log('Error while file receiving', err))
})
.catch(err => console.log(err));
};
onSendMessage = () => {
sendMessage("Hello world!")
.then((metaInfo) => console.log('Message sent successfully', metaInfo))
.catch(err => console.log('Error while message sending', err));
};
onReceiveMessage = () => {
receiveMessage()
.then((msg) => console.log('Message received successfully', msg))
.catch(err => console.log('Error while message receiving', err))
};
onGetConnectionInfo = () => {
getConnectionInfo()
.then(info => console.log('getConnectionInfo', info));
};
onGetGroupInfo = () => {
getGroupInfo()
.then(info => console.log('getGroupInfo', info));
};
render() {
return (
<View style={styles.container}>
<Button
title="Connect"
onPress={this.connectToFirstDevice}
/>
<Button
title="Cancel connect"
onPress={this.onCancelConnect}
/>
<Button
title="Create group"
onPress={this.onCreateGroup}
/>
<Button
title="Remove group"
onPress={this.onRemoveGroup}
/>
<Button
title="Investigate"
onPress={this.onStartInvestigate}
/>
<Button
title="Prevent Investigation"
onPress={this.onStopInvestigation}
/>
<Button
title="Get Available Devices"
onPress={this.onGetAvailableDevices}
/>
<Button
title="Get connection Info"
onPress={this.onGetConnectionInfo}
/>
<Button
title="Get group info"
onPress={this.onGetGroupInfo}
/>
<Button
title="Send file"
onPress={this.onSendFile}
/>
<Button
title="Receive file"
onPress={this.onReceiveFile}
/>
<Button
title="Send message"
onPress={this.onSendMessage}
/>
<Button
title="Receive message"
onPress={this.onReceiveMessage}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});