鸿蒙WiFi STA模式相关的API接口文件路径
foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_device.h
所使用的API接口有:
API | 接口说明 |
WifiErrorCode EnableWifi(void); | 开启STA |
WifiErrorCode DisableWifi(void); | 关闭STA |
int IsWifiActive(void); | 查询STA是否已开启 |
WifiErrorCode Scan(void); | 触发扫描 |
WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size); | 获取扫描结果 |
WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result); | 添加热点配置,成功会通过result传出netld |
WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size); | 获取本机所有热点配置 |
WifiErrorCode RemoveDevice(int networkId); | 删除热点配置 |
WifiErrorCode ConnectTo(int networkId); | 连接到热点 |
WifiErrorCode Disconnect(void); | 断开热点连接 |
WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result); | 获取当前连接热点信息 |
WifiErrorCode RegisterWifiEvent(WifiEvent* event); | 注册事件监听 |
WifiErrorCode UnRegisterWifiEvent(const WifiEvent* event); | 解除事件监听 |
WifiErrorCode GetDeviceMacAddress(unsigned char* result); | 获取Mac地址 |
WifiErrorCode AdvanceScan(WifiScanParams *params); | 高级搜索 |
Hi3861 SDK的DHCP客户端接口:
API | 描述 |
netifapi_netif_find | 按名称查找网络接口 |
netifapi_dhcp_start | 启动DHCP客户端 |
netifapi_dhcp_stop | 停止DHCP客户端 |
代码编写
修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件
# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
lite_component("demo") {
features = [
#"base_00_helloworld:base_helloworld_example",
#"base_01_led:base_led_example",
#"base_02_loopkey:base_loopkey_example",
#"base_03_irqkey:base_irqkey_example",
#"base_04_adc:base_adc_example",
#"base_05_pwm:base_pwm_example",
#"base_06_ssd1306:base_ssd1306_example",
#"kernel_01_task:kernel_task_example",
#"kernel_02_timer:kernel_timer_example",
#"kernel_03_event:kernel_event_example",
#"kernel_04_mutex:kernel_mutex_example",
#"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",
#"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",
#"kernel_07_semaphore_for_count:kernel_semaphore_for_count_example",
#"kernel_08_message_queue:kernel_message_queue_example",
#"wifi_09_hotspot:wifi_hotspot_example",
"wifi_10_sta:wifi_sta_example",
]
}
创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_10_sta文件夹
文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_10_sta\wifi_sta_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\wifi_10_sta\BUILD.gn文件
# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
static_library("wifi_sta_example") {
sources = [
"wifi_sta_example.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/kal",
"//foundation/communication/wifi_lite/interfaces/wifiservice",
]
}
/*
* 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_device.h"
#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#define STACK_SIZE 10240
#define AP_SSID "HarmonyOS"
#define AP_SKEY "1234567890"
#define IDX_0 0
#define IDX_1 1
#define IDX_2 2
#define IDX_3 3
#define IDX_4 4
#define IDX_5 5
#define DELAY_TICKS_10 (10)
#define DELAY_TICKS_50 (50)
#define DELAY_TICKS_100 (100)
#define DELAY_TICKS_200 (200)
#define COUNTER (60)
static void PrintLinkedInfo(WifiLinkedInfo* info)
{
if (!info) return;
static char macAddress[32] = {0};
unsigned char* mac = info->bssid;
// int ret = snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
int ret = snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
mac[IDX_0], mac[IDX_1], mac[IDX_2], mac[IDX_3], mac[IDX_4], mac[IDX_5]);
if (ret < 0) {
return;
}
printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}
static int g_connected = 0;
static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
if (!info) return;
printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
PrintLinkedInfo(info);
if (state == WIFI_STATE_AVALIABLE) {
g_connected = 1;
} else {
g_connected = 0;
}
}
static void OnWifiScanStateChanged(int state, int size)
{
printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
}
static void WifiConnectTask(void)
{
WifiErrorCode errCode;
WifiEvent eventListener = {
.OnWifiConnectionChanged = OnWifiConnectionChanged,
.OnWifiScanStateChanged = OnWifiScanStateChanged
};
WifiDeviceConfig apConfig = {};
int netId = -1;
osDelay(DELAY_TICKS_10);
errCode = RegisterWifiEvent(&eventListener);
printf("RegisterWifiEvent: %d\r\n", errCode);
// setup your AP params
// strcpy(apConfig.ssid, "ABCD");
// strcpy(apConfig.preSharedKey, "147258369");
strcpy_s(apConfig.ssid, WIFI_MAX_SSID_LEN, AP_SSID);
strcpy_s(apConfig.preSharedKey, WIFI_MAX_KEY_LEN, AP_SKEY);
apConfig.securityType = WIFI_SEC_TYPE_PSK;
int timeoutCounter = COUNTER * COUNTER;
while (timeoutCounter--) {
errCode = EnableWifi();
printf("EnableWifi: %d\r\n", errCode);
osDelay(DELAY_TICKS_10);
errCode = AddDeviceConfig(&apConfig, &netId);
printf("AddDeviceConfig: %d\r\n", errCode);
g_connected = 0;
errCode = ConnectTo(netId);
printf("ConnectTo(%d): %d\r\n", netId, errCode);
while (!g_connected) {
osDelay(DELAY_TICKS_10);
}
printf("g_connected: %d\r\n", g_connected);
osDelay(DELAY_TICKS_50);
// 联网业务开始
struct netif* iface = netifapi_netif_find("wlan0");
if (iface) {
err_t ret = netifapi_dhcp_start(iface);
printf("netifapi_dhcp_start: %d\r\n", ret);
osDelay(DELAY_TICKS_200); // wait DHCP server give me IP
ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
printf("netifapi_netif_common: %d\r\n", ret);
}
// 模拟一段时间的联网业务
int timeout = COUNTER;
while (timeout--) {
osDelay(DELAY_TICKS_100);
printf("after %d seconds, I'll disconnect WiFi!\n", timeout);
}
// 联网业务结束
err_t ret = netifapi_dhcp_stop(iface);
printf("netifapi_dhcp_stop: %d\r\n", ret);
Disconnect(); // disconnect with your AP
RemoveDevice(netId); // remove AP config
errCode = DisableWifi();
printf("DisableWifi: %d\r\n", errCode);
osDelay(DELAY_TICKS_200);
}
}
static void WifiConnectDemo(void)
{
osThreadAttr_t attr;
attr.name = "WifiConnectTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = STACK_SIZE;
attr.priority = osPriorityNormal;
if (osThreadNew(WifiConnectTask, NULL, &attr) == NULL) {
printf("[WifiConnectDemo] Falied to create WifiConnectTask!\n");
}
}
APP_FEATURE_INIT(WifiConnectDemo);
使用build,编译成功后,使用upload进行烧录。
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!
hilog will init.
hiview init success.
RegisterWifiEvent: 0
EnableWifi: 0
AddDeviceConfig: 0
ConnectTo(1): 0
+NOTICE:SCANFINISH
+NOTICE:CONNECTED
OnWifiConnectionChanged 80, state = 1, info =
bssid: 4A:AD:9A:1C:00:D9, rssi: 0, connState: 0, reason: 0, ssid: HarmonyOS
g_connected: 1
netifapi_dhcp_start: 0
server :
server_id : 192.168.137.1
mask : 255.255.255.0, 1
gw : 192.168.137.1
T0 : 604800
T1 : 302400
T2 : 453600
clients <1> :
mac_idx mac addr state lease tries rto
0 94c9601e15d0 192.168.137.66 10 0 1 2
netifapi_netif_common: 0
after 59 seconds, I'll disconnect WiFi!
after 58 seconds, I'll disconnect WiFi!
after 57 seconds, I'll disconnect WiFi!
after 56 seconds, I'll disconnect WiFi!
after 55 seconds, I'll disconnect WiFi!
after 54 seconds, I'll disconnect WiFi!
after 53 seconds, I'll disconnect WiFi!
after 52 seconds, I'll disconnect WiFi!
after 51 seconds, I'll disconnect WiFi!
after 50 seconds, I'll disconnect WiFi!
after 49 seconds, I'll disconnect WiFi!
after 48 seconds, I'll disconnect WiFi!
after 47 seconds, I'll disconnect WiFi!
after 46 seconds, I'll disconnect WiFi!
after 45 seconds, I'll disconnect WiFi!
after 44 seconds, I'll disconnect WiFi!
after 43 seconds, I'll disconnect WiFi!
after 42 seconds, I'll disconnect WiFi!
after 41 seconds, I'll disconnect WiFi!
after 40 seconds, I'll disconnect WiFi!
after 39 seconds, I'll disconnect WiFi!
after 38 seconds, I'll disconnect WiFi!
after 37 seconds, I'll disconnect WiFi!
after 36 seconds, I'll disconnect WiFi!
after 35 seconds, I'll disconnect WiFi!
after 34 seconds, I'll disconnect WiFi!
after 33 seconds, I'll disconnect WiFi!
after 32 seconds, I'll disconnect WiFi!
after 31 seconds, I'll disconnect WiFi!
after 30 seconds, I'll disconnect WiFi!
after 29 seconds, I'll disconnect WiFi!
after 28 seconds, I'll disconnect WiFi!
after 27 seconds, I'll disconnect WiFi!
after 26 seconds, I'll disconnect WiFi!
after 25 seconds, I'll disconnect WiFi!
after 24 seconds, I'll disconnect WiFi!
after 23 seconds, I'll disconnect WiFi!
after 22 seconds, I'll disconnect WiFi!
after 21 seconds, I'll disconnect WiFi!
after 20 seconds, I'll disconnect WiFi!
after 19 seconds, I'll disconnect WiFi!
after 18 seconds, I'll disconnect WiFi!
after 17 seconds, I'll disconnect WiFi!
after 16 seconds, I'll disconnect WiFi!
after 15 seconds, I'll disconnect WiFi!
after 14 seconds, I'll disconnect WiFi!
after 13 seconds, I'll disconnect WiFi!
after 12 seconds, I'll disconnect WiFi!
after 11 seconds, I'll disconnect WiFi!
after 10 seconds, I'll disconnect WiFi!
after 9 seconds, I'll disconnect WiFi!
after 8 seconds, I'll disconnect WiFi!
after 7 seconds, I'll disconnect WiFi!
after 6 seconds, I'll disconnect WiFi!
after 5 seconds, I'll disconnect WiFi!
after 4 seconds, I'll disconnect WiFi!
after 3 seconds, I'll disconnect WiFi!
after 2 seconds, I'll disconnect WiFi!
after 1 seconds, I'll disconnect WiFi!
after 0 seconds, I'll disconnect WiFi!
netifapi_dhcp_stop: 0
+NOTICE:DISCONNECTED