HarmonyOS wifi连接报错2501000

应用有个需求是一键连接wifi,使用了系统提供的import { wifiManager } from '@kit.ConnectivityKit’来实现,但是不能连接wifi,不知道是哪里出了错误。

代码如下:

wifiLink(wifi:HotelWifiModel)
{
  try {
    let config: wifiManager.WifiDeviceConfig = {
      ssid: wifi.ssid!,
      preSharedKey: wifi.password!,
      securityType: wifi.securityType ?? 0
    }
    wifiManager.addCandidateConfig(config).then(result => {
      console.log('addCandidateConfig:' + result)
      wifiManager.connectToCandidateConfig(result);
    }).catch((err: number) => {
      promptAction.showToast({ message: JSON.stringify(err) })

    });
  } catch (error) {
    promptAction.showToast({ message: JSON.stringify(error) })

  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

可以走到输出'addCandidateConfig:' + result这一步的,但是addCandidateConfig并不能使手机连接wifi并报错2501000。wifi的数据是根据wifiManager.getScanInfoList()和我们后台给的wifi名称和密码组合的,可以保证正确性。权限上ohos.permission.GET_WIFI_INFO、ohos.permission.LOCATION、ohos.permission.SET_WIFI_INFO都在module.json5中写好了。当前连接的wifi也并非传入的wifi,且已经尝试过的方法重新执行关闭及打开WIFI开关的操作和重启设备依然无效。

HarmonyOS
2024-12-23 17:52:02
3175浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

出现错误码‘2501000’的原因之一是当前样机已连接热点,请在执行connectToCandidateConfig方法前确保样机未连接到wifi即可(可删除网络后再次尝试运行上述代码)。

查询wifi连接状态:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-wifimanager-V5#wifimanagerisconnected9

connectToCandidateConfig官网参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-wifimanager-V5#wifimanagerconnecttocandidateconfig9

//参考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信息为:${JSON.stringify(linkInfo)}`)


    if (!wifiManager.isConnected()) {
      try {
        let config: wifiManager.WifiDeviceConfig = {
          ssid: 'wifi名称',
          preSharedKey: 'wifi密钥',
          securityType: 3
        }
        wifiManager.addCandidateConfig(config).then(result => {
          console.log('addCandidateConfig:' + result)
          wifiManager.connectToCandidateConfig(result);
        }).catch((err: number) => {
          promptAction.showToast({ message: JSON.stringify(err) })

        });
      } catch (error) {
        promptAction.showToast({ message: JSON.stringify(error) })

      }
    } else {
      promptAction.showToast({ message: '请断开wifi后再试' })
    }
  }

  build() {
    Column() {
      Button('linkInfo').onClick(() => {
        this.getLinkInfo()
        promptAction.showToast({ message: '方法已执行' })
      })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
分享
微博
QQ
微信
回复
2024-12-23 20:54:49


相关问题
wifi连接候选网络报错{code : 2501000 }
846浏览 • 1回复 待解决
HarmonyOS SPP蓝牙连接报错
618浏览 • 1回复 待解决
HarmonyOS socket tcp连接报错
591浏览 • 1回复 待解决
蓝牙连接报错该如何处理?
1029浏览 • 1回复 待解决
Socket连接报错,错误码88
2231浏览 • 1回复 待解决
HarmonyOS wifi连接demo
555浏览 • 1回复 待解决
HarmonyOS 连接WIFI的问题
971浏览 • 1回复 待解决
润和轻量系统mqtt连接报错误码2
2453浏览 • 0回复 待解决
HarmonyOS 自动连接wifi的功能
687浏览 • 1回复 待解决
HarmonyOS如何通过API连接指定WIFI
1112浏览 • 1回复 待解决
怎么解决mysql远程连接报10038的错误?
2725浏览 • 1回复 待解决
如何获取已经连接wifi密码
1263浏览 • 1回复 待解决
有办法连接隐藏SSID的wifi
1210浏览 • 1回复 待解决
提问
该提问已有2人参与 ,帮助了6人