目录

How do I obtain the IP address of the current device after it is connected to a Wi-Fi network?

Call wifiManager.getIpInfo() of the wifiManager module to obtain ipInfo and convert it into a common IP address format. Note that the ohos.permission.GET_WIFI_INFO permission is required for calling this API.

The sample code is as follows:

import { wifiManager } from '@kit.ConnectivityKit';

let ipAddress = wifiManager.getIpInfo().ipAddress;
let ip = (ipAddress >>> 24) + "." + (ipAddress >> 16 & 0xFF) + "." + (ipAddress >> 8 & 0xFF) + "." + (ipAddress & 0xFF);