鸿蒙开发板Hi3861_Wifi的AP模式wifi连接_基于code-2.0-CANARY

2024-04-30 02:38

本文主要是介绍鸿蒙开发板Hi3861_Wifi的AP模式wifi连接_基于code-2.0-CANARY,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

鸿蒙开发板Hi3861_Wifi的AP模式wifi连接_基于code-2.0-CANARY

2.0支持windows编译与上传,不需要ubuntu编译
环境搭建需要有耐心:

https://www.cnblogs.com/txwtech/p/15041927.html

首先学会点亮LED的实验

https://www.cnblogs.com/txwtech/p/15139405.html
————————————————
版权声明:本文为CSDN博主「txwtech」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/txwtech/article/details/120050588
————————————————

开启热点供连接

 

 

 

wifihotspot/build.gn

# Copyright (c) 2020, HiHope Community.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.static_library("wifi_demo") {sources = [# "wifi_connecter.c",# "wifi_starter.c","wifi_hotspot_demo.c",]include_dirs = ["//utils/native/lite/include","//kernel/liteos_m/components/cmsis/2.0","//base/iot_hardware/interfaces/kits/wifiiot_lite","//foundation/communication/interfaces/kits/wifi_lite/wifiservice","//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/","//foundation/communication/wifi_lite/interfaces/wifiservice",]
}

wifihotspot/wifi_hotspot_demo.c

/** Copyright (c) 2020, HiHope Community.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** 1. Redistributions of source code must retain the above copyright notice, this*    list of conditions and the following disclaimer.** 2. Redistributions in binary form must reproduce the above copyright notice,*    this list of conditions and the following disclaimer in the documentation*    and/or other materials provided with the distribution.** 3. Neither the name of the copyright holder nor the names of its*    contributors may be used to endorse or promote products derived from*    this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include <stdio.h>
#include <string.h>
#include <unistd.h>#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_hotspot.h"
#include "lwip/netifapi.h"static volatile int g_hotspotStarted = 0;static void OnHotspotStateChanged(int state)
{printf("OnHotspotStateChanged: %d.\r\n", state);if (state == WIFI_HOTSPOT_ACTIVE) {g_hotspotStarted = 1;} else {g_hotspotStarted = 0;}
}static volatile int g_joinedStations = 0;static void PrintStationInfo(StationInfo* info)
{if (!info) return;static char macAddress[32] = {0};unsigned char* mac = info->macAddress;snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);printf(" PrintStationInfo: mac=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
}static void OnHotspotStaJoin(StationInfo* info)
{g_joinedStations++;PrintStationInfo(info);printf("+OnHotspotStaJoin: active stations = %d.\r\n", g_joinedStations);
}static void OnHotspotStaLeave(StationInfo* info)
{g_joinedStations--;PrintStationInfo(info);printf("-OnHotspotStaLeave: active stations = %d.\r\n", g_joinedStations);
}WifiEvent g_defaultWifiEventListener = {.OnHotspotStaJoin = OnHotspotStaJoin,.OnHotspotStaLeave = OnHotspotStaLeave,.OnHotspotStateChanged = OnHotspotStateChanged,
};static struct netif* g_iface = NULL;int StartHotspot(const HotspotConfig* config)
{WifiErrorCode errCode = WIFI_SUCCESS;errCode = RegisterWifiEvent(&g_defaultWifiEventListener);printf("RegisterWifiEvent: %d\r\n", errCode);errCode = SetHotspotConfig(config);printf("SetHotspotConfig: %d\r\n", errCode);g_hotspotStarted = 0;errCode = EnableHotspot();printf("EnableHotspot: %d\r\n", errCode);while (!g_hotspotStarted) {osDelay(10);}printf("g_hotspotStarted = %d.\r\n", g_hotspotStarted);g_iface = netifapi_netif_find("ap0");if (g_iface) {ip4_addr_t ipaddr;ip4_addr_t gateway;ip4_addr_t netmask;IP4_ADDR(&ipaddr,  192, 168, 1, 1);     /* input your IP for example: 192.168.1.1 */IP4_ADDR(&gateway, 192, 168, 1, 1);     /* input your gateway for example: 192.168.1.1 */IP4_ADDR(&netmask, 255, 255, 255, 0);   /* input your netmask for example: 255.255.255.0 */err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway);printf("netifapi_netif_set_addr: %d\r\n", ret);ret = netifapi_dhcps_start(g_iface, 0, 0); // 海思扩展的HDCP服务接口printf("netifapi_dhcp_start: %d\r\n", ret);}return errCode;
}void StopHotspot(void)
{if (g_iface) {err_t ret = netifapi_dhcps_stop(g_iface);  // 海思扩展的HDCP服务接口printf("netifapi_dhcps_stop: %d\r\n", ret);}WifiErrorCode errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);printf("UnRegisterWifiEvent: %d\r\n", errCode);errCode = DisableHotspot();printf("EnableHotspot: %d\r\n", errCode);
}void abcdefg(void){printf("abce---ok");
}
static void WifiHotspotTask(void *arg)
{(void)arg;WifiErrorCode errCode;HotspotConfig config = {0};abcdefg();// 准备AP的配置参数strcpy(config.ssid, "鸿蒙hi3861-AP");strcpy(config.preSharedKey, "12345678");config.securityType = WIFI_SEC_TYPE_PSK;config.band = HOTSPOT_BAND_TYPE_2G;config.channelNum = 7;osDelay(10);printf("starting AP ...\r\n");errCode = StartHotspot(&config);printf("StartHotspot: %d\r\n", errCode);int timeout = 360;while (timeout--) {printf("After %d seconds Ap will turn off!\r\n", timeout);osDelay(100);}printf("stop AP ...\r\n");StopHotspot();printf("stop AP ...\r\n");
}static void WifiHotspotDemo(void)
{osThreadAttr_t attr;attr.name = "WifiHotspotTask";attr.attr_bits = 0U;attr.cb_mem = NULL;attr.cb_size = 0U;attr.stack_mem = NULL;attr.stack_size = 10240;attr.priority = osPriorityNormal;if (osThreadNew(WifiHotspotTask, NULL, &attr) == NULL) {printf("[WifiHotspotDemo] Falied to create WifiHotspotTask!\n");}
}//APP_FEATURE_INIT(WifiHotspotTask);
SYS_RUN(WifiHotspotTask);

app/build.gn

# Copyright (c) 2020, HiHope Community.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.import("//build/lite/config/component/lite_component.gni")lite_component("app") {features = ["wifihotspot:wifi_demo",]
}

这篇关于鸿蒙开发板Hi3861_Wifi的AP模式wifi连接_基于code-2.0-CANARY的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/947724

相关文章

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma

电脑win32spl.dll文件丢失咋办? win32spl.dll丢失无法连接打印机修复技巧

《电脑win32spl.dll文件丢失咋办?win32spl.dll丢失无法连接打印机修复技巧》电脑突然提示win32spl.dll文件丢失,打印机死活连不上,今天就来给大家详细讲解一下这个问题的解... 不知道大家在使用电脑的时候是否遇到过关于win32spl.dll文件丢失的问题,win32spl.dl