1.wifi状态的检测使用下面的脚本
#!/bin/bash
function poke {
while true
do
printf '
'
sleep 1
done
}
retryTimes=10
function watch {
(poke) | wpa_cli | while read -t 10 line
do
case "$line" in
*'4-Way Handshake failed'*)
echo "incorrect key"
return
;;
*'CTRL-EVENT-CONNECTED'*)
echo "connected"
return
;;
*'Could not connect to wpa_supplicant'*)
echo "Could not connect to wpa_supplicant"
return
;;
*'Failed to initiate AP scan'*)
echo "failed to init ap scan"
return
;;
esac
done
}
watch
2.应用程序中使用该脚本
char strout[MAX_PATH];
memset(strout, 0, MAX_PATH);
FILE *fp = popen("/app/watch_wifi_connect_state.sh", "r");
if(fp != NULL)
{
if(!feof(fp))
{
fgets(strout, sizeof(strout), fp);
if(strstr(strout, "incorrect key"))
{
//密码错误
qDebug() << "密码错误";
emit passwordError();
}
else if(strstr(strout, "connected"))
{
//连接成功
qDebug() << "连接成功";
system("kill -10 $(pidof udhcpc)");
}
}
}
pclose(fp);
system("kill $(pgrep -f /app/watch_wifi_connect_state.sh)");