鸿蒙开发板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

相关文章

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

java如何通过Kerberos认证方式连接hive

《java如何通过Kerberos认证方式连接hive》该文主要介绍了如何在数据源管理功能中适配不同数据源(如MySQL、PostgreSQL和Hive),特别是如何在SpringBoot3框架下通过... 目录Java实现Kerberos认证主要方法依赖示例续期连接hive遇到的问题分析解决方式扩展思考总

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

oracle如何连接登陆SYS账号

《oracle如何连接登陆SYS账号》在Navicat12中连接Oracle11g的SYS用户时,如果设置了新密码但连接失败,可能是因为需要以SYSDBA或SYSOPER角色连接,解决方法是确保在连接... 目录oracle连接登陆NmOtMSYS账号工具问题解决SYS用户总结oracle连接登陆SYS账号

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

SSID究竟是什么? WiFi网络名称及工作方式解析

《SSID究竟是什么?WiFi网络名称及工作方式解析》SID可以看作是无线网络的名称,类似于有线网络中的网络名称或者路由器的名称,在无线网络中,设备通过SSID来识别和连接到特定的无线网络... 当提到 Wi-Fi 网络时,就避不开「SSID」这个术语。简单来说,SSID 就是 Wi-Fi 网络的名称。比如