wifi_device.h接口简介
-
启用Wifi STA 模式
WifiErrorCode EnableWifi(void); -
禁用Wifi STA 模式
WifiErrorCode DisableWifi(void); -
检查Wifi STA模式是否启用
int IsWifiActive(void); -
扫描热点信息
WifiErrorCode Scan(void); -
获取所有扫描到的热点列表
WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size); -
配置连接到热点信息
WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result); -
获取配置连接到热点信息
WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size); -
删除指定的热点配置信息
WifiErrorCode RemoveDevice(int networkId); -
接到指定的热点
WifiErrorCode ConnectTo(int networkId); -
断开Wifi连接
WifiErrorCode Disconnect(void); -
获取热点连接信息
WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result); -
获取设备的MAC地址
WifiErrorCode GetDeviceMacAddress(unsigned char* result);
连接到指定wifi代码
#include
#include
#include "ohos_init.h"
#include
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_adc.h"
#include "wifiiot_errno.h"
#include "wifiiot_uart.h"
#include "lwip/netifapi.h"
#include "wifi_hotspot.h"
#include "wifi_device.h"
#include "wifi_error_code.h"
#include "lwip/netif.h"
#include "lwip/ip4_addr.h"
#include "lwip/api_shell.h"
#define DEF_TIMEOUT 15
#define ONE_SECOND 1
static void WiFiInit(void);
static void WaitSacnResult(void);
static int WaitConnectResult(void);
static void OnWifiScanStateChangedHandler(int state, int size);
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
static void OnHotspotStaJoinHandler(StationInfo *info);
static void OnHotspotStateChangedHandler(int state);
static void OnHotspotStaLeaveHandler(StationInfo *info);
static int g_staScanSuccess = 0;
static int g_ConnectSuccess = 0;
static int ssid_count = 0;
WifiEvent g_wifiEventHandler = {0};
WifiErrorCode error;
#define SELECT_WLAN_PORT "wlan0"
#define SELECT_WIFI_SSID "JamesZhou"
#define SELECT_WIFI_PASSWORD "12345678"
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
static void WiFiInit(void)
{
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
error = RegisterWifiEvent(&g_wifiEventHandler);
if (error != WIFI_SUCCESS)
{
printf("register wifi event fail!\r\n");
}
else
{
printf("register wifi event succeed!\r\n");
}
}
static void OnWifiScanStateChangedHandler(int state, int size)
{
(void)state;
if(size > 0)
{
ssid_count = size;
g_staScanSuccess = 1;
}
}
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
{
(void)info;
if(state > 0)
{
g_ConnectSuccess = 1;
printf("callback function for wifi connect\r\n");
}
else
{
printf("connect error,please check password\r\n");
}
}
static void OnHotspotStateChangedHandler(int state)
{
printf("HotspotStateChanged:state is %d.\n", state);
}
static void OnHotspotStaJoinHandler(StationInfo *info)
{
(void)info;
printf("STA join AP\n");
}
static void OnHotspotStaLeaveHandler(StationInfo *info)
{
(void)info;
printf("HotspotStaLeave:info is null.\n");
}
static void WaitSacnResult(void)
{
int scanTimeout = DEF_TIMEOUT;
while(scanTimeout)
{
sleep(ONE_SECOND);
scanTimeout--;
if (g_staScanSuccess == 1)
{
printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
break;
}
}
if (scanTimeout 0)
{
sleep(1);
ConnectTimeout--;
if (g_ConnectSuccess == 1)
{
printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout));
break;
}
}
if (ConnectTimeout
关注
打赏
