中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
这个wifi_hotspot.h中包含声明AP热点相关接口函数。
打开“D1_iot_wifi_ap”工程的wifi_ap.c文件,可在代码中查看实现创建Wifi热点的代码
//注册wifi事件的回调函数 g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; error = RegisterWifiEvent(&g_wifiEventHandler); if (error != WIFI_SUCCESS) { printf("RegisterWifiEvent failed, error = %d.\r\n",error); return -1; } //设置指定的热点配置 HotspotConfig config = {0}; strcpy(config.ssid, AP_SSID); strcpy(config.preSharedKey, AP_PSK); config.securityType = WIFI_SEC_TYPE_PSK; config.band = HOTSPOT_BAND_TYPE_2G; config.channelNum = 7; error = SetHotspotConfig(&config); if (error != WIFI_SUCCESS) { printf("SetHotspotConfig failed, error = %d.\r\n", error); return -1; } //启动wifi热点模式 error = EnableHotspot(); if (error != WIFI_SUCCESS) { printf("EnableHotspot failed, error = %d.\r\n", error); return -1; } //检查热点模式是否使能 if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) { printf("Wifi station is not actived.\r\n"); return -1; } //等待STA连接 g_apEnableSuccess = 0; WaitAPResult();
这个wifi_device.h中包含声明STA联网相关接口函数。
打开“D2_iot_wifi_sta_connect”工程的wifi_sta_connect.c文件,可在代码中查看实现STA联网业务代码。
g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler; g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler; error = RegisterWifiEvent(&g_wifiEventHandler); if (error != WIFI_SUCCESS) { printf("register wifi event fail!\r\n"); } else { printf("register wifi event succeed!\r\n"); } //使能WIFI if (EnableWifi() != WIFI_SUCCESS) { printf("EnableWifi failed, error = %d\n", error); return -1; } //判断WIFI是否激活 if (IsWifiActive() == 0) { printf("Wifi station is not actived.\n"); return -1; } //分配空间,保存WiFi信息 info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT); if (info == NULL) { return -1; } //轮询查找WiFi列表 do{ //重置标志位 ssid_count = 0; g_staScanSuccess = 0; //开始扫描 Scan(); //等待扫描结果 WaitSacnResult(); //获取扫描列表 GetScanInfoList(info, &size); }while(g_staScanSuccess != 1);
微信扫码分享