前言:
在实际使用中,很多设备都需要用到wifi模块,但是如何使设备智能的连接到热点上。
其中一种方法是:先把设备的wifi模块设置为ap模式,客户端先搜索到这个设备的热点,
然后连接它,再把设备真正需要连接的热点发送给设备,从而使设备可以连接到指定的热点上。
一,设置为ap模式后,如何获取其它热点
设置为ap模式,默认是无法查找到别的wifi热点的,这个需要以下操作
iwpriv ra0 set SiteSurvey
然后在通过以下命令来获取热点信息
iwpriv ra0 get_site_survey
二,编程获取wifi热点信息
对 fgets 函数有疑惑的请看:
fgets函数的理解
测试结果:
在实际使用中,很多设备都需要用到wifi模块,但是如何使设备智能的连接到热点上。
其中一种方法是:先把设备的wifi模块设置为ap模式,客户端先搜索到这个设备的热点,
然后连接它,再把设备真正需要连接的热点发送给设备,从而使设备可以连接到指定的热点上。
一,设置为ap模式后,如何获取其它热点
设置为ap模式,默认是无法查找到别的wifi热点的,这个需要以下操作
iwpriv ra0 set SiteSurvey
然后在通过以下命令来获取热点信息
iwpriv ra0 get_site_survey
二,编程获取wifi热点信息
点击(此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- typedef unsigned char HI_U8;
- typedef unsigned short HI_U16;
- typedef unsigned int HI_U32;
-
- typedef signed char HI_S8;
- typedef char * HI_PS8;
- typedef short HI_S16;
- typedef int HI_S32;
- typedef int * HI_PS32;
-
- #define MK_PROC_MAX_WIFI_AP_SIZE (10)
-
- typedef struct _HI_DEV_WIFI_SCANNING_S
- {
- HI_U32 u32Size; //结构体的大小
- HI_U32 u32ApNum; //* 第几个ap */
- HI_U32 u32Frequency; //频率
- HI_U32 u32CurrentUsed; //* 是否正在使用 1:正在使用 0:未使用 */
- HI_U32 u32SignalQuality; //* 信号质量 */
- HI_U32 u32BitRate; //* 传输速度(比特率) */
- char szEssid[32]; //* essid */
- char szProtocol[16]; //ap support protocol
- char szMacAddr[32]; //* mac addr */
- HI_U8 u8Mode; //mode 0:Auto 1:Managed 2:Ad-Hoc ...
- HI_U8 u8IsEncrypt; //*是否加密1:加密 0:不加密*/
- HI_U8 u8EncrypType; //*网络加密方式0:NONE 1:WAP 2:WAP2 3:WEP */
- HI_U8 u8AuthMode; //* 认证模式 0:NONE 1:EAP 2:PSK 3:OPEN 4:SHARED*/
- HI_U8 u8SecretKeyType; //密钥管理方式 0:none 1:AES 2:TKIP
-
- HI_U8 u8Res[7];
- }HI_DEV_WIFI_SCANNING_S,*LPHI_DEV_WIFI_SCANNING_S;
-
- static void enable_softap_scanning(void)
- {
- system("iwpriv ra0 set SiteSurvey"); //开启ap扫描功能
- sleep(2);
- }
-
- //*网络加密方式0:NONE 1:WAP 2:WAP2 3:WEP */
- // 4:other
- static int get_ap_security_type(char *buf)
- {
- int type = 0;
- if (buf == NULL) return -1;
-
- if (strstr(buf, "WPA2") != NULL)
- type = 2;
- else if (strstr(buf, "WPA") != NULL)
- type = 1;
- else if (strstr(buf, "WEP") != NULL)
- type = 3;
- else if (strstr(buf, "NONE") != NULL)
- type = 0;
- else
- type = 4;
- return type;
- }
-
- int _softap_get_scan_res(HI_DEV_WIFI_SCANNING_S *stScanInfo)
- {
- FILE *fp;
- char buf[256] = {0};
- int findFlag = 0;
- int index = 0;
- char chBuf[4] = {0}, ssidBuf[32] = {0}, macBuf[32] = {0},
- athBuf[32] = {0}, sigBuf[16] = {0}, wBuf[8] = {0},
- extBuf[8] = {0}, ntBuf[4] = {0};
-
- enable_softap_scanning();
-
- if((fp = popen("iwpriv ra0 get_site_survey", "r")) == NULL) //获取ap扫描结果
- {
- printf("open cmd: iwpriv ra0 get_site_survey fail!\n");
- return -1;
- }
-
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(!findFlag && strstr(buf, "Ch") != NULL)
- {
- findFlag = 1;
- continue;
- }
-
- if (findFlag)
- {
- if(sscanf(buf, "%s %s %s %s %s %s %s %s\n",chBuf,ssidBuf,macBuf,athBuf,\
- sigBuf,wBuf,extBuf,ntBuf) != 8)
- {
- continue;
- }
-
- if ((strcmp(ssidBuf, "") == 0) ||
- (strcmp(ssidBuf, "CMCC-AUTO") == 0) ||
- (strcmp(ssidBuf, "CMCC-WEB") == 0) ||
- (strcmp(ssidBuf, "CMCC-FREE") == 0) ||
- (strcmp(ssidBuf, "CMCC") == 0) ||
- (strcmp(macBuf, "") == 0) ||
- (strcmp(athBuf, "") == 0) ||
- (atoi(sigBuf) <= 0))
-
- {
- continue;
- }
- else
- {
- int security_type = 0;
- if ((security_type = get_ap_security_type(athBuf)) < 0)
- continue;
-
- strncpy(stScanInfo[index].szEssid,ssidBuf,32);
- strncpy(stScanInfo[index].szMacAddr,macBuf,32);
- stScanInfo[index].u32SignalQuality = atoi(sigBuf);
- stScanInfo[index].u32CurrentUsed = 0;
- stScanInfo[index].u8EncrypType = security_type;
- ++index;
-
- if (index >= MK_PROC_MAX_WIFI_AP_SIZE)
- break;
- }
- }
- }
-
- pclose(fp);
-
- return index;
- }
-
-
- static void show_ap(HI_DEV_WIFI_SCANNING_S *stScanInfo,int count)
- {
- int index = 0;
-
- printf("%-4s%-32s%-32s%-8s%-8s\n","id","ssid","essid","sig","sec");
- for (index = 0; index < count; index++)
- {
- #if 0
- printf("%*d,ssid[%*s],essid[%*s],signal[%*d],type[%*d]\n",4,index,
- 32,stScanInfo[index].szEssid,
- 32,stScanInfo[index].szMacAddr,
- 4,stScanInfo[index].u32SignalQuality,
- 4,stScanInfo[index].u8EncrypType);
- #else
-
- printf("%-4d%-32s%-32s%-8d%-8d\n",index,
- stScanInfo[index].szEssid,
- stScanInfo[index].szMacAddr,
- stScanInfo[index].u32SignalQuality,
- stScanInfo[index].u8EncrypType);
- #endif
- }
- }
-
-
- int main(int argc,char *argv[])
- {
- HI_DEV_WIFI_SCANNING_S stScanInfo[MK_PROC_MAX_WIFI_AP_SIZE];
- int ap_count = 0;
-
- ap_count = _softap_get_scan_res(stScanInfo);
- show_ap(stScanInfo,ap_count);
-
- return 0;
- }
测试结果: