静态IP地址是固定的,不会随着网络连接的断开而变化。因此,使用静态IP地址可以有效避免由于IP地址变化引起的网络连接中断问题。针对OpenHarmony应用开发,以太网设置静态IP,本文提供OpenHarmony设置静态IP的开发demo,已适配API9和API10,详见资源附件。
一、案例展示
1、运行条件
(1)rk3568 开发板
(2)系统:OpenHarmony4.0release
2、下载并运行开发案例
(1)下载开发案例,使用Deveco Studio(建议使用4.0.0.600版本)打开运行编译、下载应用到rk3568开发板。
效果如下图所示,可以显示网络状态及其网络自动获取配置信息
(2)点击 “点击设置”,跳出弹窗进行静态IP或自动获取设置选项
(3)、点击 “静态配置”,弹出设置静态的弹窗,进行静态IP设置,如设置静态IP为:192.168.2.78,并提交完成设置
(4)、断电或者重启开发板,开发板仍然保持静态IP的设置。
二、开发流程
1、需要申请的权限
(1)查询以太网状态,需要权限:ohos.permission.GET_NETWORK_INFO
(2)http请求,需要权限:ohos.permission.INTERNET
(3)设置网络接口配置信息,需要权限:ohos.permission.CONNECTIVITY_INTERNAL
2、开发步骤:
(1)使用http申请来验证网络状态是否可以上网,
import http from '@ohos.net.http'
httpCheck() {
let promise = this.httpRequest.request(this.web, {
method: http.RequestMethod.GET,
header: new Header('application/json'),
connectTimeout: 800,
readTimeout: 800,
});
promise.then((data: http.HttpResponse) => {
this.textColor = Color.Green
this.textNetState = '可以上网'
}).catch((err: Error) => {
this.textColor = Color.Red
this.textNetState = '不可上网'
console.error(TAG, 'error:' + JSON.stringify(err));
});
}
(2)引入 @ohos.net.ethernet 接口,
调用ethernet.getIfaceConfig方法获取指定网络接口信息;调用ethernet.isIfaceActive方法判断接口是否已激活;
ethernet.isIfaceActive("eth0").then((data) => {
this.eth0.ethState = data
if (data === 1) {
this.eth0.ethState = 1
ethernet.getIfaceConfig(this.eth0.ethName).then((data: ethernet.InterfaceConfiguration) => {
this.eth0.ethMode = data.mode
this.eth0.ethNetMask = data.netMask
this.eth0.ethIpAddr = data.ipAddr
this.eth0.ethRoute = data.route
this.eth0.ethGateway = data.gateway
let index: number = data.dnsServers.indexOf(",")
this.eth0.dnsServers = data.dnsServers.substr(0, index);
this.eth0Config.netMask = data.netMask
this.eth0Config.ipAddr = data.ipAddr
this.eth0Config.route = data.route
this.eth0Config.gateway = data.gateway
if (data.mode === 1) {
this.eth0Config.dnsServers = this.eth0.dnsServers
}
}).catch((error: BusinessError) => {
console.log(TAG, "getIfaceConfig promise error = " + JSON.stringify(error));
});
} else {
this.eth0.ethState = 0
this.eth0.ethMode = -1
this.eth0.dnsServers = ''
this.eth0.ethIpAddr = ''
this.eth0.ethRoute = ''
this.eth0.ethGateway = ''
this.eth0.ethNetMask = ''
}
console.log(TAG, "isIfaceActive promise = " + JSON.stringify(data));
}).catch((error: Error) => {
this.eth0.ethState = 0
console.log(TAG, "isIfaceActive promise error = " + JSON.stringify(error));
});
调用ethernet.setIfaceConfig方法设置网络接口配置信息;
let config: ethernet.InterfaceConfiguration = {
mode: 1,
ipAddr: "",
route: "",
gateway: "",
netMask: "",
dnsServers: ""
};
let setConfigPromise = ethernet.setIfaceConfig(this.netName, config);
setConfigPromise.then(() => {
promptAction.showToast({
message: this.netName + " 动态连接配置成功"
})
console.log(TAG, "setIfaceConfig promise ok");
}).catch((error: BusinessError) => {
promptAction.showToast({
message: this.netName + " 动态连接配置失败"
})
console.error(TAG, "setIfaceConfig promise error = " + JSON.stringify(error));
});
具体开发逻辑,见工程代码。
链接参考:https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.mda
说明:厦门贝启科技官方论坛同步发贴,更多OpenHarmony开发及其BQ3568开发板欢迎关注厦门贝启科技有限公司-Bearkey-官网