当前位置: 首页 > news >正文

蜂窝数据和wifi通道切换

蜂窝数据和wifi通道切换


import { BusinessError } from '@kit.BasicServicesKit';
import { connection } from '@kit.NetworkKit';
import { data } from '@kit.TelephonyKit';
import { ToastUtil } from '@pura/harmony-utils';export class WifiConnectManager {private static instance?: WifiConnectManager;private isWifi: boolean = true;/*** Get singleton** @returns Singleton object*/public static getInstance(): WifiConnectManager {if (!WifiConnectManager.instance) {WifiConnectManager.instance = new WifiConnectManager();}return WifiConnectManager.instance;}/*** @description :移动数据是否开启*/public async isMobileDataEnabled(): Promise<boolean> {return await data.isCellularDataEnabled();}/*** @param: isWifi: true: wifi, false: 流量*/public startListenNetChange(isWifi: boolean): void {console.info('registerNetListener');console.info('registerNetListener', `connect  type : + ${isWifi ? 'wifi' : '流量'}`);WifiConnectManager.getInstance().isWifi = isWifi;let netConnectionWifi = connection.createNetConnection({netCapabilities: {bearerTypes: [connection.NetBearType.BEARER_WIFI]}});netConnectionWifi.register((error: BusinessError) => {if (error) {console.error(`register error: ${error.code}`);}});netConnectionWifi.on('netAvailable', () => {console.info('netConnectionWifi netAvailable');if (WifiConnectManager.getInstance().isWifi) {this.bindWifiWhenConnected();} else {this.bindCellularWhenConnected();}});netConnectionWifi.on('netLost', () => {console.info('Wifi netLost');if (WifiConnectManager.getInstance().isWifi) {this.bindWifiWhenConnected();} else {this.bindCellularWhenConnected();}});let netConnectionCellular = connection.createNetConnection({netCapabilities: {bearerTypes: [connection.NetBearType.BEARER_CELLULAR]}});netConnectionCellular.register((error: BusinessError) => {if (error) {console.error(`register error: ${error.message}`);}});netConnectionCellular.on('netAvailable', () => {console.info('netConnectionCellular netAvailable');if (WifiConnectManager.getInstance().isWifi) {this.bindWifiWhenConnected();} else {this.bindCellularWhenConnected();}});netConnectionCellular.on('netLost', () => {console.info('Cellular netLost');if (WifiConnectManager.getInstance().isWifi) {this.bindWifiWhenConnected();} else {this.bindCellularWhenConnected();}});}/*** @Description: 绑定WiFi*/private async bindWifiWhenConnected(): Promise<void> {await connection.setAppNet(connection.getDefaultNetSync()).then(() => {console.info('setAppNet default success');});connection.getAllNets().then((data: connection.NetHandle[]) => {data.forEach(net => {connection.getNetCapabilities(net).then((data: connection.NetCapabilities) => {if (data.bearerTypes.length > 0 && data.bearerTypes[0] === connection.NetBearType.BEARER_WIFI) {connection.setAppNet(net).then(() => {console.info('setAppNet wifi success');return;}).catch((error: Error) => {console.error(`setAppNet wifi failed, error = ${error.message}`);});}}).catch((error: Error) => {console.error(`getNetCapabilities error = ${error.message}`);});});}).catch((error: Error) => {console.error(`getAllNets error = ${error.message}`);});}/*** @Description: 绑定移动数据*/private async bindCellularWhenConnected(): Promise<void> {// 先判断移动数据是否开启const isMobileDataEnabled = await this.isMobileDataEnabled()if (!isMobileDataEnabled) {ToastUtil.showToast($r('app.string.Telephony_error'));return}await connection.setAppNet(connection.getDefaultNetSync()).then(() => {console.info('setAppNet default success');});connection.getAllNets().then((data: connection.NetHandle[]) => {data.forEach(net => {connection.getNetCapabilities(net).then((data: connection.NetCapabilities) => {if (data.bearerTypes.length > 0 && data.bearerTypes[0] === connection.NetBearType.BEARER_CELLULAR) {connection.setAppNet(net).then(() => {console.info('setAppNet cellular success');return;}).catch((error: Error) => {console.error(`setAppNet cellular failed, error = ${error.message}`);});}}).catch((error: Error) => {console.error(`getNetCapabilities error = ${error.message}`);});});}).catch((error: Error) => {console.error(`getAllNets error = ${error.message}`);});}
}
http://www.jsqmd.com/news/1025324/

相关文章:

  • 【案例】青岛精装房墙角渗水,微创工艺修复不破坏装修 - 青岛防水品牌推荐
  • 基础-二分算法 -(二分答案 | 最小化最大值 | 最大化最小值 | 第K小)-持续更新中
  • 杭州油烟机不抽油烟怎么办?简单到家自检清单与避坑指南 - 简单到家
  • 11804华夏之光永存:黄大年茶思屋榜文118期 第4题混响环境内的声场建模与控制技术
  • 合肥购宠优选 9家实体门店现场挑选更踏实 - 园友3800037
  • .NET Upgrade Assistant:从传统框架到现代平台的快速迁移指南
  • 解锁AI写专著密码!AI专著撰写工具,20万字专著快速成型!
  • 可靠的wordpress服务商
  • 猫抓浏览器插件:5分钟掌握网页视频音频嗅探下载终极指南
  • 2026实测推荐:小红书视频怎么去水印?复制链接就能解析保存的3个小程序 - 效率工具研究所
  • Python 数据分析实战:Pandas+Matplotlib 从入门到可视化报表
  • [省选联考 2020 A 卷] 作业题
  • 20251202马思钊 2025-2026-2 实验四 Python综合实践
  • 深度盘点当贝Air1S,看看这款新品耳机都有哪些配置
  • 新手在无锡买猫狗 哪家宠物门店值得信赖? - 园友3800037
  • DeepSeek-V4:低成本高精度推理如何重塑AI算力经济
  • Steam Deck控制器Windows驱动完全指南:SWICD让你的游戏体验无缝衔接
  • CoaXPress 与 CoaXPress over Fiber 技术对比 - Hello
  • 武汉黄金回收哪家靠谱?2026 本地正规机构综合排行榜 - 奢侈品回收测评
  • 2026 粘结钕铁硼厂家推荐|高精度异形磁体定制,新能源电机磁瓦生产厂商 - 商业新知
  • 《超标量处理器设计》---Cache
  • BallonTranslator:让漫画翻译变得像聊天一样简单的AI工具
  • 2026 好用的素颜霜早八通勤实测|100 人 28 天横评榜单 黄皮自然抗暗沉优选 - 速递信息
  • 杭州购宠避坑指南:4家靠谱实体门店实测推荐 - 园友3800037
  • 2026银行秋招面试技巧班深度评测:4家头部机构对比,谁能帮你突破最后一关 - 互联网科技品牌测评
  • 2026年合肥留学机构怎么选?八家优选硬核测评行业头部梯队前五强 - 速递信息
  • 2026年北京刑事辩护律师避坑指南:5位经验丰富值得推荐 - 本地品牌推荐
  • 第29章:部署与服务化——Docker、K8s 与模型网关
  • 3步彻底改造:让Windows 11轻装上阵的终极方案
  • 猫抓浏览器插件:智能化资源嗅探与自动化下载解决方案