int getWirelessStatus(char *ath, char *ssid, char *ipAddr) { int sock_fd; struct iwreq iwr; struct ifreq ifr; char buffer[32] = {0}; if (NULL == ath) { printf("ath is NULLn"); return -1; } if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { printf("socket errn"); return -1; } memset(&iwr, 0, sizeof(iwr)); memset(&ifr, 0, sizeof(ifr)); strcpy(iwr.ifr_ifrn.ifrn_name, ath); strcpy(ifr.ifr_name, ath); if (ioctl(sock_fd, SIOCGIFADDR, &ifr) < 0) { perror("SIOCGIFADDR"); } else { strcpy(ipAddr, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); printf("ipAddr:%sn", ipAddr); } iwr.u.essid.pointer = buffer; iwr.u.essid.length = sizeof(buffer); if (ioctl(sock_fd, SIOCGIWESSID, &iwr) < 0) { perror("SIOCGIWESSID"); } else { printf("ssid:%sn", (char *)iwr.u.essid.pointer); memcpy(ssid, (char *)iwr.u.essid.pointer, sizeof(buffer)); } close(sock_fd); return 0; }
获取wifi连接状态ioctl(sockfd, SIOCGIWSTATS, &iwr),SIOCGIWSTATS有的平台不支持
理论上getWirelessStatus函数返回的ssid和ipAddr都不为NULL表示wifi连接成功
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)