主板型号
硬件主板型号:SCDAYU200,RK3568,鸿蒙开发板
固件名称/系统版本:OpenHarmony3.1
项目构建版本:API9
问题描述及复现步骤:
dayu200的开发版,购买的设备开发版系统是OpenHarmony3.1版本,调试UDP通讯,出现以下报错,
{"code":2301013,"message":"Permission denied"}
参考,这个帖子,将固件刷到了OpenHarmony4.0版本,Dayu200上运行依然是2301013报错;
成功案例:在模拟器上通讯可以实现发送和接收,在华为mate30上实现UDP通讯,可以实现发送和接收
失败案例:dayu200,OpenHarmony3.x设备,OpenHarmony4.0设备,Socket.bind均出现绑定失败的问题。
代码实现
首先保证代码没有问题,参考官方Socket链接
如下代码在模拟器和华为mate30设备上测试,均可实现UDP的收发,唯独Dayu200设备上运行报错,权限也做过配置,理论上不应该报Permission denied!
//执行UDP通讯的对象
udpSocket = socket.constructUDPSocketInstance();
//发送消息到目的ip和端口
udpSend(sendMsg:string|ArrayBuffer) {
//目的ip和端口
let remoteAddress = { address: AppInfo.UDP_Server_IP, port: AppInfo.UDP_Server_Port, family: 1 }
this.udpSocket.send({ data: sendMsg, address: remoteAddress })
.then(async () => {
})
.catch((e) => {
console.log('发送失败' + JSON.stringify(e))
})
}
//绑定本地端口
async udpReceive() {
//本地地址
let localAddress = { address: "0.0.0.0", port: AppInfo.UDP_Receive_Port, family: 1 }
//绑定服务
await this.udpSocket.bind(localAddress)
.then(() => {
console.log('bind success')
})
.catch((e) => {
console.log('bind fail ' + JSON.stringify(e))
})
//收到消息时的处理
this.udpSocket.on("message", async (value) => {
let msg = this.buf2String(value.message)
let remoteIP = value.remoteInfo.address
let remotePort = value.remoteInfo.port.toString()
//对端ip地址和端口的字符串形式
let remoteAddr = "[" + remoteIP + ":" + remotePort + "]:"
let time = await this**.getCurrentTimeString()
console.log("【接收】" + remoteAddr + msg + time)
})
console.log("【UDP服务】开启UDP接收")
}
解决办法
怀疑是固件问题,刷固件。之前从3.1刷到了4.0,继续找可用固件。 到OpenHarmony每日构建中找Dayu200对应的构建版本,一个个刷测。后续刷到了5.0以上的版本,尝试了很多个版本,会有各种各样的问题。
最后找到一个稳定可用的固件版本【OpenHarmony_4.1.5.2_dev】,才开始顺畅的写代码。