HarmonyOS 鸿蒙Next wifi连接报错2501000?
HarmonyOS 鸿蒙Next wifi连接报错2501000?
HarmonyOS wifi连接报错2501000?
2 回复
出现错误码‘2501000’的原因之一是当前样机已连接热点,请在执行connectToCandidateConfig方法前确保样机未连接到wifi即可(可删除网络后再次尝试运行上述代码)。
查询wifi连接状态:
connectToCandidateConfig官网参考链接:
//参考demo如下:
import { wifiManager } from '@kit.ConnectivityKit';
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State message: string = ‘Hello World’;
async getLinkInfo() {
const linkInfo = await wifiManager.getLinkedInfo()
console.log(获取到的linkInfo信息为:${<span class="hljs-built_in">JSON</span>.stringify(linkInfo)}
)
<span class="hljs-keyword">if</span> (!wifiManager.isConnected()) {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">let</span> config: wifiManager.WifiDeviceConfig = {
ssid: <span class="hljs-string">'wifi名称'</span>,
preSharedKey: <span class="hljs-string">'wifi密钥'</span>,
securityType: <span class="hljs-number">3</span>
}
wifiManager.addCandidateConfig(config).then(result => {
console.log(<span class="hljs-string">'addCandidateConfig:'</span> + result)
wifiManager.connectToCandidateConfig(result);
}).catch((err: number) => {
promptAction.showToast({ message: <span class="hljs-built_in">JSON</span>.stringify(err) })
});
} <span class="hljs-keyword">catch</span> (error) {
promptAction.showToast({ message: <span class="hljs-built_in">JSON</span>.stringify(error) })
}
} <span class="hljs-keyword">else</span> {
promptAction.showToast({ message: <span class="hljs-string">'请断开wifi后再试'</span> })
}
}
build() {
Column() {
Button(‘linkInfo’).onClick(() => {
this.getLinkInfo()
promptAction.showToast({ message: ‘方法已执行’ })
})
}
.width(“100%”)
.height(“100%”)
.justifyContent(FlexAlign.Center)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>