您当前的位置: 首页 >  HarmonyOS

仙剑情缘

暂无认证

  • 0浏览

    0关注

    333博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Bearpi开发板HarmonyOS之WiFi STA联网

仙剑情缘 发布时间:2022-05-29 19:02:15 ,浏览量:0

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             
关注
打赏
1658017818
查看更多评论
1.0220s