forked from skierkowski/react-native-wifi-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (50 loc) · 1.48 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
/**
* @providesModule react-native-open-wifi
*/
var { RNOpenWifi } = require('react-native').NativeModules;
var OpenWifi = {
connect: connectToSsid,
isMobileDataEnabled: isMobileDataEnabled
};
const ONE_SECOND = 1000;
const POLLING_FREQUENCY = 500;
function connectToSsid (ssid, { timeout = 10 * ONE_SECOND } = {}, debug) {
return new Promise((resolve, reject) => {
RNOpenWifi.getSSID((currentSsid) => {
if (currentSsid === ssid) {
resolve();
return;
}
// Connect to WiFi network
RNOpenWifi.connect(ssid);
// Check every 500 milliseconds if we are connected to the right SSID
var timeElapsed = 0;
function checkSSID () {
setTimeout(function () {
timeElapsed += POLLING_FREQUENCY;
RNOpenWifi.getSSID((currentSsid) => {
RNOpenWifi.status(status => {
// console.log('ssid:', currentSsid, 'status:', status);
if (currentSsid === ssid && status === 'COMPLETED') {
resolve();
} else if (timeElapsed >= timeout) {
reject(new Error('Couldn\'t find the network'));
} else {
checkSSID();
}
});
});
}, POLLING_FREQUENCY);
}
checkSSID();
});
});
}
function isMobileDataEnabled () {
return new Promise((resolve, reject) => {
RNOpenWifi.isMobileDataEnabled(isEnabled => {
resolve(isEnabled);
});
});
}
module.exports = OpenWifi;