forked from kirillzyusko/react-native-wifi-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (119 loc) · 4.91 KB
/
index.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
import { DeviceEventEmitter, NativeModules } from 'react-native';
import { getError } from './reason-code';
const WiFiP2PManager = NativeModules.WiFiP2PManagerModule;
// ACTIONS
const PEERS_UPDATED_ACTION = 'PEERS_UPDATED';
const CONNECTION_INFO_UPDATED_ACTION = 'CONNECTION_INFO_UPDATED';
const THIS_DEVICE_CHANGED_ACTION = 'THIS_DEVICE_CHANGED_ACTION';
// CONSTS
const MODULE_NAME = 'WIFI_P2P';
const initialize = () => WiFiP2PManager.init();
const startDiscoveringPeers = () => new Promise((resolve, reject) => {
WiFiP2PManager.discoverPeers((reasonCode) => {
reasonCode === undefined ? resolve('success') : reject(getError(reasonCode));
})
});
const subscribeOnEvent = (event, callback) => {
return DeviceEventEmitter.addListener(`${MODULE_NAME}:${event}`, callback);
};
/**
* @deprecated since RN 0.65 because of favour to new API.
* @see https://github.com/kirillzyusko/react-native-wifi-p2p/releases/tag/3.3.0 for migration process.
*/
const unsubscribeFromEvent = (event, callback) => {
DeviceEventEmitter.removeListener(`${MODULE_NAME}:${event}`, callback);
};
const subscribeOnThisDeviceChanged = (callback) => subscribeOnEvent(THIS_DEVICE_CHANGED_ACTION, callback);
/**
* @deprecated since RN 0.65 because of favour to new API.
* @see https://github.com/kirillzyusko/react-native-wifi-p2p/releases/tag/3.3.0 for migration process.
*/
const unsubscribeFromThisDeviceChanged = (callback) => unsubscribeFromEvent(THIS_DEVICE_CHANGED_ACTION, callback);
const subscribeOnPeersUpdates = (callback) => subscribeOnEvent(PEERS_UPDATED_ACTION, callback);
/**
* @deprecated since RN 0.65 because of favour to new API.
* @see https://github.com/kirillzyusko/react-native-wifi-p2p/releases/tag/3.3.0 for migration process.
*/
const unsubscribeFromPeersUpdates = (callback) => unsubscribeFromEvent(PEERS_UPDATED_ACTION, callback);
const subscribeOnConnectionInfoUpdates = (callback) => subscribeOnEvent(CONNECTION_INFO_UPDATED_ACTION, callback);
/**
* @deprecated since RN 0.65 because of favour to new API.
* @see https://github.com/kirillzyusko/react-native-wifi-p2p/releases/tag/3.3.0 for migration process.
*/
const unsubscribeFromConnectionInfoUpdates = (callback) => unsubscribeFromEvent(CONNECTION_INFO_UPDATED_ACTION, callback);
const connect = (deviceAddress) => connectWithConfig({ deviceAddress });
const connectWithConfig = (args) => new Promise((resolve, reject) => {
WiFiP2PManager.connectWithConfig(args, status => {
status === undefined ? resolve() : reject(getError(status));
})
});
const cancelConnect = () => new Promise((resolve, reject) => {
WiFiP2PManager.cancelConnect(status => {
status === undefined ? resolve() : reject(getError(status));
})
});
const createGroup = () => new Promise((resolve, reject) => {
WiFiP2PManager.createGroup(reasonCode => {
reasonCode === undefined ? resolve() : reject(getError(reasonCode));
})
});
const removeGroup = () => new Promise((resolve, reject) => {
WiFiP2PManager.removeGroup(reasonCode => {
reasonCode === undefined ? resolve() : reject(getError(reasonCode));
})
});
const getAvailablePeers = () => WiFiP2PManager.getAvailablePeersList();
const stopDiscoveringPeers = () => new Promise((resolve, reject) => {
WiFiP2PManager.stopPeerDiscovery(reasonCode => {
reasonCode === undefined ? resolve() : reject(getError(reasonCode));
})
});
const sendFile = (pathToFile) => WiFiP2PManager.sendFile(pathToFile);
const sendFileTo = (pathToFile, address) => WiFiP2PManager.sendFileTo(pathToFile, address);
const receiveFile = (folder, fileName, forceToScanGallery = false) => new Promise((resolve, reject) => {
WiFiP2PManager.receiveFile(folder, fileName, forceToScanGallery, (pathToFile) => {
resolve(pathToFile);
});
});
const sendMessage = (message) => WiFiP2PManager.sendMessage(message);
const sendMessageTo = (message, address) => WiFiP2PManager.sendMessageTo(message, address);
const receiveMessage = () => new Promise((resolve, reject) => {
WiFiP2PManager.receiveMessage((message) => {
resolve(message);
});
});
const getConnectionInfo = () => WiFiP2PManager.getConnectionInfo();
const getGroupInfo = () => WiFiP2PManager.getGroupInfo();
export {
// public methods
initialize,
startDiscoveringPeers,
stopDiscoveringPeers,
subscribeOnThisDeviceChanged,
unsubscribeFromThisDeviceChanged,
subscribeOnPeersUpdates,
unsubscribeFromPeersUpdates,
subscribeOnConnectionInfoUpdates,
unsubscribeFromConnectionInfoUpdates,
getAvailablePeers,
connect,
connectWithConfig,
cancelConnect,
createGroup,
removeGroup,
getConnectionInfo,
getGroupInfo,
sendFile,
sendFileTo,
receiveFile,
sendMessage,
sendMessageTo,
receiveMessage,
// system methods
subscribeOnEvent,
unsubscribeFromEvent,
// const
PEERS_UPDATED_ACTION,
CONNECTION_INFO_UPDATED_ACTION,
THIS_DEVICE_CHANGED_ACTION,
};