当前位置: 首页 > 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.zskr.cn/news/1537189.html

相关文章:

  • 基础-二分算法 -(二分答案 | 最小化最大值 | 最大化最小值 | 第K小)-持续更新中
  • 11804华夏之光永存:黄大年茶思屋榜文118期 第4题混响环境内的声场建模与控制技术
  • 合肥购宠优选 9家实体门店现场挑选更踏实 - 园友3800037
  • .NET Upgrade Assistant:从传统框架到现代平台的快速迁移指南
  • 可靠的wordpress服务商
  • [省选联考 2020 A 卷] 作业题
  • 《超标量处理器设计》---Cache
  • CoaXPress 与 CoaXPress over Fiber 技术对比 - Hello
  • 武汉黄金回收哪家靠谱?2026 本地正规机构综合排行榜 - 奢侈品回收测评
  • 合肥买猫狗靠谱推荐:萌宠宠园 宠物售卖,十年老牌资质齐全 - 园友3800037
  • 2026年北京刑事辩护律师避坑指南:5位经验丰富值得推荐 - 本地品牌推荐
  • 2026年合肥留学机构怎么选?八家优选硬核测评行业头部梯队前五强 - 速递信息
  • 从零到爆款:3分钟让AI帮你搞定专业短视频创作
  • 第29章:部署与服务化——Docker、K8s 与模型网关
  • 3步彻底改造:让Windows 11轻装上阵的终极方案
  • 横岗眼镜城有保障店铺核心维度解读 配镜前实用参考指南 - 速递信息
  • 使用MC工具同步MinIO
  • 苏州靠谱宠物店实地测评,买猫买狗优选门店参考 - 园友3800037
  • 智能体RAG客服系统4——middleware部分开发与agent主体开发
  • 2026年保洁外包选型指南:代表性服务商深度解析,优质保洁外包推荐 - 速递信息
  • 国内二维过渡金属碳化物MXene品牌综合实力排行 - 奔跑123
  • 从零实现一个 Web 搜索 MCP 插件
  • 2026最新救命贴:Turnitin英文初稿降ai率实操,这套降aigc保姆级教程千万别错过 - 殷念写论文
  • 肖有米开发:推三返一模式系统开发推三返一现成小程序开发
  • 如何快速掌握ViGEmBus虚拟手柄驱动:Windows游戏控制器兼容性终极解决方案
  • 2026成都卖劳力士首选!5 家实体店深度测评禹竞名奢汇 - 禹竞
  • 2026热门潜水表回收行情解析,南京劳力士无附件手表回收实测 - 奢侈品回收评测
  • BepInEx:Unity游戏插件框架的技术革新与多运行时生态构建
  • 【科普】城阳区新房收房,防水验收重点查这几个位置 - 青岛防水品牌推荐
  • 【Spring Boot + MyBatis|第9篇】使用 AOP 实现接口操作日志记录