玩嗨 OpenHarmony:自己动手做 Wi-Fi 热点
有人说OpenHarmony玩不起来,今天小编向大家介绍下如何用OpenHarmony做个Wi-Fi热点。
OpenHarmony全场景、全连接的特性,做个Wi-Fi热点并不复杂,只需要用OpenHarmony自带的WiFI API接口,加上润和Pegasus OpenHarmomy开发套件就能搞定。话不多说,上代码:
//wifi connect task
static void WifiConnectTask(void *arg)
{
(void)arg;
WifiErrorCode errCode;
WifiEvent eventListener = {
.OnWifiConnectionChanged = OnWifiConnectionChanged,
.OnWifiScanStateChanged = OnWifiScanStateChanged
};
WifiDeviceConfig apConfig = {};
int netId = -1;
osDelay(10);
errCode = RegisterWifiEvent(&eventListener);
printf("RegisterWifiEvent: %d\r\n", errCode);
// 配置要连接的热点的账号密码
strcpy(apConfig.ssid, "ABCD");
strcpy(apConfig.preSharedKey, "12345678");
apConfig.securityType = WIFI_SEC_TYPE_PSK;
while (1) {
//打开wifi
errCode = EnableWifi();
printf("EnableWifi: %d\r\n", errCode);
osDelay(11);
//添加热点配置
errCode = AddDeviceConfig(&apConfig, &netId);
printf("AddDeviceConfig: %d\r\n", errCode);
g_connected = 0;
//连接热点
errCode = ConnectTo(netId);
printf("ConnectTo(%d): %d\r\n", netId, errCode);
//等待wifi连接
while (!g_connected) {
osDelay(10);
}
printf("g_connected: %d\r\n", g_connected);
osDelay(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(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 = 60;
while (timeout--) {
osDelay(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(); //close wifi
printf("DisableWifi: %d\r\n", errCode);
osDelay(200);
}
}
代码量很少,用润和Pegasus OpenHarmomy开发套件,很快搞定。
下面为大家介绍这个套件。
Pegasus智能家居开发套件
润和满天星系列Pegasus OpenHarmomy开发套件首发于HDC 2020,是首批支持HarmonyOS 2.0的开发套件,是OpenHarmony官方推荐套件,由润和软件HiHope量身打造,已在OpenHarmomny社区和广大OpenHarmony开发者中得到广泛应用。
Demo说明
本Demo适用版本:OpenHarmony 1.0.1~3.0
OpenHarmony源码下载:
https://gitee.com/openharmony/docs/tree/master/zh-cn/release-notes
Demo源码下载:
https://gitee.com/openharmony-sig/knowledgedemotemp/tree/master/dev/teamx/demosta
附件1:OpenHarmony 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); | 高级搜索 |
附件1:OpenHarmony WiFi STA模式相关的API接口文件路径
API | 描述 |
netifapinetiffind | 按名称查找网络接口 |
netifapidhcpstart | 启动DHCP客户端 |
netifapidhcpstop | 停止DHCP客户端 |
写在最后
我们最近正带着大家玩嗨OpenHarmony。如果你有好玩的东东,欢迎投稿,让我们一起嗨起来!有点子,有想法,有Demo,立刻联系我们:
合作邮箱:zzliang@atomsource.org