node-tutorial
node-tutorial copied to clipboard
node的wifi模块
创建配置文件
导出已有配置文件,会显示你曾经输入过的WIFI密码的配置文件
# clear表示以明文方式显示密码
netsh wlan export profile key=clear
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>10086</name>
<SSIDConfig>
<SSID>
<!-- <hex>3130303836</hex> -->
<name>10086</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<autoSwitch>true</autoSwitch>
<MSM>
<security>
<authEncryption>
<!-- WPA2PSK,WPAPSK,open -->
<authentication>WPA2PSK</authentication>
<!-- AES,TKIP,WEP -->
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>false</protected>
<keyMaterial>{password}</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
name和SSID可以不同(最好设为一致)
- name是配置文件名称
- SSID是要连接的wifi名
connectionMode可以为手动连接的manual或者自动连接的auto; keyMaterial处填写密码,无密码状态如下:
<security>
<authEncryption>
<authentication>open</authentication>
<encryption>none</encryption>
<useOneX>false</useOneX>
</authEncryption>
</security>
检查配置文件是否已经存在:
netsh wlan show profile
删除配置文件:
netsh wlan delete profile name="10086"
netsh wlan delete profile name="Eno"
添加配置文件:
netsh wlan add profile filename="WLAN-10086.xml"
注意这里面参数是文件名,默认路径为当前目录,添加成功后提示:已将配置文件 10086 添加到接口 WLAN 可以在命令行运行一下命令扫描WiFi信息:
netsh wlan show networks mode=Bssid
进行连接:
netsh wlan connect name="10086"
重要的事情说三遍:这里的name是刚刚添加过的配置文件中的name,而不是配置文件名! 连接成功的提示信息为已成功完成连接请求。
配合Node
我们可以使用Node编写JS去实现连接,读取配置文件并连接
var execFile = require('child_process').execFile;
var env = Object.assign(process.env, {
LANG: 'en_US.UTF-8',
LC_ALL: 'en_US.UTF-8',
LC_MESSAGES: 'en_US.UTF-8'
});
// 执行命令
function execCommand(cmd, params) {
return new Promise(function (resolve, reject) {
execFile(cmd, params, {
env
}, function (err, stdout, stderr) {
if (err) {
// Add command output to error, so it's easier to handle
err.stdout = stdout;
err.stderr = stderr;
reject(err);
} else {
resolve(stdout);
}
});
});
}
execCommand('netsh', [
'wlan',
'add',
'profile',
// 读取wifi配置文件
'filename="nodeWifiConnect.xml"'
]);
常用netsh命令总结:
- 列出配置文件:
netsh wlan show profile - 导出配置文件:
netsh wlan export profile key=clear - 删除配置文件:
netsh wlan delete profile name="" - 添加配置文件:
netsh wlan add profile filename="" - 连接wifi:
netsh wlan connect name="" - 列出接口:
netsh wlan show interface - 开启接口:
netsh interface set interface "Interface Name" enabled - 列出所有可连接wifi详细信息:
netsh wlan show networks mode=bssid
node-wifi
var wifi = require('node-wifi');
// Initialize wifi module
// Absolutely necessary even to set interface to null
wifi.init({
iface : null // network interface, choose a random wifi interface if set to null
});
// Scan networks
wifi.scan(function(err, networks) {
if (err) {
console.log(err);
} else {
console.log(networks);
/*
networks = [
{
ssid: '...',
bssid: '...',
mac: '...', // equals to bssid (for retrocompatibility)
channel: <number>,
frequency: <number>, // in MHz
signal_level: <number>, // in dB
security: 'WPA WPA2' // format depending on locale for open networks in Windows
security_flags: '...' // encryption protocols (format currently depending of the OS)
mode: '...' // network mode like Infra (format currently depending of the OS)
},
...
];
*/
}
});
// Connect to a network
wifi.connect({ ssid : "10086", password : "123456"}, function(err) {
if (err) {
console.log(err);
}
console.log('Connected');
});