想了解更多內容,請訪問:ide
https://harmonyos.51cto.com/#bkwzspa
【編譯生成wifi可執行文件】 請參考如下示例代碼編譯可執行程序wpa_supplicant:orm
#include <dlfcn.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
pthread_t g_wpaThread;
char* g_wpaArg[20] = {0};
int g_wpaArgc = 0;
static void* ThreadMain()
{
printf("[WpaSample]init wpa_supplicant.\n");
void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL);
if (handleLibWpa == NULL) {
printf("[WpaSample]dlopen libwpa failed.\n");
return NULL;
}
int (*func)(int, char **) = NULL;
func = dlsym(handleLibWpa, "wpa_main");
if (func == NULL) {
dlclose(handleLibWpa);
printf("[WpaSample]dlsym wpa_main failed.\n");
return NULL;
}
int ret = func(g_wpaArgc, g_wpaArg);
printf("[WpaSample]run wpa_main failed, ret:%d.\n", ret);
for (int i = 0; i < g_wpaArgc; i++) {
printf("[WpaSample]arg %d:%s.\n", i, g_wpaArg[i]);
}
if (dlclose(handleLibWpa) != 0) {
printf("[WpaSample]dlclose libwpa failed.\n");
return NULL;
}
return NULL;
}
int main(int argc, char *argv[])
{
g_wpaArgc = argc;
for (int i = 0; i < g_wpaArgc; i++) {
g_wpaArg[i] = argv[i];
}
int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL);
if (ret != 0) {
printf("[WpaSample]create thread failed error:%s.\n", strerror(ret));
return 1;
}
pthread_join(g_wpaThread, NULL);
return 0;
}ip
【修改station模式的配置】 將如下配置寫到配置文件wpa_supplicant.confci
country=GB
ctrl_interface=udp
network={
#要鏈接的SSID
ssid="example"
#若是不須要加密就寫key_mgmt=NONE
#key_mgmt=NONE
#若是須要加密就寫這行密碼
psk="12345678"
}get
【修改ap模式的配置】 將如下配置寫到配置文件hostapd.confstring
interface=wlan0
driver=hdf wifi
ctrl_interface=udp
ssid=testap
hw_mode=g
channel=1
ignore_broadcast_ssid=0
#下面是wpa2-psk類型加密的配置
#若是不須要加密能夠刪除
auth_algs=1
wpa=2
wpa_passphrase=12345678
rsn_pairwise=CCMP
it
【使用方法】io
將wpa_supplicant、wpa_supplicant.conf、hostapd.conf拷貝到單板中(sdcard目錄)
啓動sta的命令:
./sdcard/wpa_supplicant -i wlan0 -c /sdcard/wpa_supplicant.conf
啓動ap的命令:
./sdcard/hostapd -i wlan0 /sdcard/hostapd.conf
【WIFI設備檢測】 進入OHOS界面後,輸入ifconfig,檢查wlan0網口是否加載正常,加載成功標識以下:
OHOS # ifconfig
wlan0 ip:0.0.0.0 netmask:0.0.0.0 gateway:0.0.0.0
HWaddr 0a:11:31:aa:7e:1a MTU:1500 Stop Link UP
lo ip:127.0.0.1 netmask:255.0.0.0 gateway:127.0.0.1
ip6: ::1/64
HWaddr 00 MTU:16436 Running Link UP