linux 下判断wifi连接状态的代码

查看wifi连接状态,有时候wifi断开连接了但是我们不知道,以至于不能重新连接上wifi,这时候我们需要一个判断来知道wifi时候断开了,下面这段代码就是来检测wifi连接状态的,代码非常简单

/**********************************************************************
* 函数名称: 
* 功能描述:
* 输入参数: 
* 输出参数: 无
* 返 回 值: 0为已经连接上WIFIAP节点,其他未连接 
* 其它说明: 
***********************************************************************/
int CheckWireLessConnectState()
{
    FILE* fp; 
    char buf[PIPE_BUFSIZE] = {0}; 
    char command[300] = {0};
    int ret = -1;
    char str[] = "wpa_state=COMPLETED";
    
    sprintf(command, "wpa_cli -i wlan0 status | grep -r "%s"", str);
	
	if((fp = popen(command, "r")) != NULL)
	{
		if((fgets(buf, PIPE_BUFSIZE, fp)) != NULL)
		{
		    	   //printf("CheckWireLessConnectState buf=%s
",buf);
			    if(0 == memcmp(buf, str, strlen(str)))
		            {
		                ret = 0;
		            }     
		            else
		            {
				ret = -1;
		            }
		}
		pclose(fp);
	}
    return ret;
}
经验分享 程序员 微信小程序 职场和发展