# communication_netmanager_base_5 **Repository Path**: cheng-qian-a/communication_netmanager_base_5 ## Basic Information - **Project Name**: communication_netmanager_base_5 - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 753 - **Created**: 2022-08-31 - **Last Updated**: 2022-09-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Net Manager ## 简介 网络管理模块作为电话子系统可裁剪部件,主要分为连接管理、策略管理、流量管理、网络共享、VPN管理、网络共享以及以太网连接等模块;如图1:网络管理架构图; **图 1** 网络管理架构图 ![net_conn_manager_arch_zh](figures/net_conn_manager_arch_zh.png) ## 目录 ``` foundation/communication/netmanager_base/ ├─figures # 架构图 ├─frameworks # 接口实现 │ ├─js # JS接口 │ └─native # native接口 ├─interfaces # 接口定义 │ ├─innerkits # JS接口 │ └─kits # native接口 ├─sa_profile # sa定义 ├─services # IPC服务端实现 │ ├─netconnmanger # 连接管理核心代码目录 ├─test # 测试代码 │ ├─netconnmanger # 连接管理单元测试目录 └─utils # 实用工具 ``` ## 接口说明 | 类型 | 连接管理接口 | 功能说明 | | ---- | ---- | ---- | | ohos.net.connection | function getDefaultNet(callback: AsyncCallback\): void; |获取一个含有默认网络的netId的NetHandle对象,调用callback | | ohos.net.connection | function getDefaultNet(): Promise\; |获取一个含有默认网络的netId的NetHandle对象,返回Promise | | ohos.net.connection | function getAllNets(callback: AsyncCallback>): void;| 获取所有注册的网络,调用callback | | ohos.net.connection | function getAllNets(): Promise>;| 获取所有注册的网络,返回Promise | | ohos.net.connection | function getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\): void; |查询默认网络的链路信息,调用callback | | ohos.net.connection | function getConnectionProperties(netHandle: NetHandle): Promise\; |查询默认网络的链路信息,返回Promise | | ohos.net.connection | function getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\): void; |查询默认网络的能力集信息,调用callback | | ohos.net.connection | function getNetCapabilities(netHandle: NetHandle): Promise\; |查询默认网络的能力集信息,返回Promise | | ohos.net.connection | function hasDefaultNet(callback: AsyncCallback\): void; |查询是否有默认网络,调用callback | | ohos.net.connection | function hasDefaultNet(): Promise\; |查询是否有默认网络,返回Promise | | ohos.net.connection | function getAddressesByName(host: string, callback: AsyncCallback\>): void; |使用对应网络解析域名,获取所有IP,调用callback | | ohos.net.connection | function getAddressesByName(host: string): Promise\>; |使用默认网络解析域名,获取所有IP,返回Promise | | ohos.net.connection | function createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection; |返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征,timeout是超时时间,netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络 | | ohos.net.connection | function enableAirplaneMode(callback: AsyncCallback): void; | 设置网络为飞行模式,调用callback | | ohos.net.connection | function enableAirplaneMode(): Promise;|设置网络为飞行模式,返回Promise | | ohos.net.connection | function disableAirplaneMode(callback: AsyncCallback): void;| 关闭网络飞行模式,调用callback | | ohos.net.connection | function disableAirplaneMode(): Promise;| 关闭网络飞行模式,返回Promise | | ohos.net.connection | function reportNetConnected(netHandle: NetHandle, callback: AsyncCallback): void;| 报告网络处于连接状态,调用callback | | ohos.net.connection | function reportNetConnected(netHandle: NetHandle): Promise;| 报告网络处于连接状态,返回Promise | | ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback): void;| 报告网络处于断开状态,调用callback | | ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle): Promise;| 报告网络处于断开状态,返回Promise | | ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback): void; | 将TCPSocket或UDPSockett绑定到当前网络,调用callback | | ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket): Promise;| 将TCPSocket或UDPSockett绑定到当前网络,返回Promise | | ohos.net.connection.NetHandle | getAddressesByName(host: string, callback: AsyncCallback\>): void; |使用默认网络解析域名,获取所有IP,调用callback | | ohos.net.connection.NetHandle | getAddressesByName(host: string): Promise\>; |使用对应网络解析域名,获取所有IP,返回Promise | | ohos.net.connection.NetHandle | getAddressByName(host: string, callback: AsyncCallback\): void; |使用对应网络解析域名,获取一个IP,调用callbac | | ohos.net.connection.NetHandle | getAddressByName(host: string): Promise\; |使用对应网络解析域名,获取一个IP,返回Promise | | ohos.net.connection.NetConnection | on(type: 'netAvailable', callback: Callback\): void; |监听收到网络可用的事件 | | ohos.net.connection.NetConnection | on(type: 'netCapabilitiesChange', callback: Callback\<{ netHandle: NetHandle, netCap: NetCapabilities }>): void; |监听网络能力变化的事件 | | ohos.net.connection.NetConnection | on(type: 'netConnectionPropertiesChange', callback: Callback\<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void; |监听网络连接信息变化的事件 | | ohos.net.connection.NetConnection | on(type: 'netLost', callback: Callback\): void; |监听网络丢失的事件 | | ohos.net.connection.NetConnection | on(type: 'netUnavailable', callback: Callback\): void; |监听网络不可用的事件 | | ohos.net.connection.NetConnection | register(callback: AsyncCallback\): void; |注册默认网络或者createNetConnection中指定的网络的监听 | | ohos.net.connection.NetConnection | unregister(callback: AsyncCallback\): void; |注销默认网络或者createNetConnection中指定的网络的监听 | ## 连接管理使用说明 ### 创建一个NetConnection对象 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript createNetConnection() { this.debugBegin("createNetConnection", undefined); var mNetConnection = connection.createNetConnection(); if (mNetConnection) { this.callBack(undefined, mNetConnection); } else { this.callBack({ code: 1 }, undefined) } } ``` ### 创建一个蜂窝网络的NetConnection对象 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript createNetConnectionHoneycomb(){ this.debugBegin("createNetConnection", undefined); var mNetConnection = connection.createNetConnection({ netCapabilities: { bearerTypes: [connection.NetBearType.BEARER_CELLULAR] } }); if (mNetConnection) { this.callBack(undefined, mNetConnection); } else { this.callBack({ code: 1 }, undefined) } } ``` ### 注册默认网络或者createNetConnection中指定的网络的监听 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript register() { this.debugBegin("register", undefined); if (!mNetConnection) { this.$parent().showToast("请实例化NetConnection") return; } mNetConnection.register((err, data) => { this.callBack(err, data); mNetConnection.on('netAvailable', (data) => { this.log('on netAvailable:' + JSON.stringify(data)); }); mNetConnection.on('netBlockStatusChange', (data) => { this.log('on netBlockStatusChange:' + JSON.stringify(data)); }); mNetConnection.on('netCapabilitiesChange', (data) => { this.log('on netCapabilitiesChange:' + JSON.stringify(data)); }); mNetConnection.on('netConnectionPropertiesChange', (data) => { this.log('on netConnectionPropertiesChange:' + JSON.stringify(data)); }); mNetConnection.on('netLost', (data) => { this.log('on netLost:' + JSON.stringify(data)); }); mNetConnection.on('netUnavailable', (data) => { this.log('on netUnavailable:' + JSON.stringify(data)); }); }) } ``` ### 获取一个含有默认网络的netId的NetHandle对象 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript getDefaultNet() { this.debugBegin("getDefaultNet", undefined); connection.getDefaultNet((err, data) => { this.callBack(err, data); if (data) { this.netName = data.netId; this.netHandle = data; } }); } ``` ### 获取所有注册的网络 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript getAllNets() { this.debugBegin("getAllNets", undefined); connection.getAllNets((err, data) => { this.callBack(err, data); if (data) { this.netList = data; } }); } ``` ### 查询默认网络的链路信息 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript getConnectionProperties() { if (!this.checkNetHandle()) { return; } this.debugBegin("getConnectionProperties", JSON.stringify(this.netHandle)); connection.getConnectionProperties(this.netHandle, (err, data) => { this.callBack(err, data); }); } ``` ### 查询默认网络的能力集信息 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript getNetCapabilities() { if (!this.checkNetHandle()) { return; } this.debugBegin("getNetCapabilities", JSON.stringify(this.netHandle)); connection.getNetCapabilities(this.netHandle, (err, data) => { this.callBack(err, data); }); } ``` ### 查询是否有默认网络 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript hasDefaultNet() { this.debugBegin("hasDefaultNet", undefined); connection.hasDefaultNet((err, data) => { this.callBack(err, data); }); } ``` ### 设置网络为飞行模式 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript enableAirplaneMode() { this.debugBegin("enableAirplaneMode", undefined); connection.enableAirplaneMode((err, data) => { this.callBack(err, data); console.info("11111"+data); }); } ``` ### 关闭网络飞行模式 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript disableAirplaneMode() { this.debugBegin("disableAirplaneMode", undefined); connection.disableAirplaneMode((err, data) => { this.callBack(err, data); }); } ``` ### 报告网络处于连接状态 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript reportNetConnected() { if (!this.checkNetHandle()) { return; } this.debugBegin("reportNetConnected", JSON.stringify(this.netHandle)); connection.reportNetConnected(this.netHandle, (err, data) => { this.callBack(err, data); }); } ``` ### 报告网络处于断开状态 * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript reportNetDisconnected() { if (!this.checkNetHandle()) { return; } this.debugBegin("reportNetDisconnected", JSON.stringify(this.netHandle)); connection.reportNetDisconnected(this.netHandle, (err, data) => { this.callBack(err, data); }); } ``` ### 使用默认网络解析域名,获取所有IP * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript getAddressesByName() { this.debugBegin("getAddressesByName", JSON.stringify(this.host)); connection.getAddressesByName(this.host, (err, data) => { this.callBack(err, data); }); } ``` ### 将TCPSocket或UDPSockett绑定到当前网络 * 示例 ```javascript import connection from '@ohos.net.connection' import socket from "@ohos.net.socket"; ``` ```javascript netHandleBindSocket() { if (!this.checkNetHandle()) { return; } var tcp = socket.constructTCPSocketInstance(); var udp = socket.constructUDPSocketInstance(); if (this.currentSocketType == "TCPSocket") { tcp.bind({ address: this.socketAddress, port: this.socketPort, family: this.socketFamily }, err => { if (err) { console.log('bind fail'); } this.debugBegin("netHandle.bindSocket", JSON.stringify(tcp)); this.netHandle.bindSocket(tcp, (err, data) => { this.callBack(err, data); }); console.log('bind success'); }) } else { let callback = value => { console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); } udp.on('message', callback); udp.bind({ address: this.socketAddress, port: this.socketPort, family: this.socketFamily }, err => { if (err) { console.log('bind fail'); } udp.on('message', (value) => { console.log("NET_JS_TEST: on message, message:" + JSON.stringify(value)); }); this.debugBegin("netHandle.bindSocket", JSON.stringify(udp)); this.netHandle.bindSocket(udp, (err, data) => { this.callBack(err, data); }); console.log('bind success'); }) } } ``` ### 使用对应网络解析域名,获取所有IP * 示例 ```javascript import connection from '@ohos.net.connection' ``` ```javascript netHandleGetAddressesByName() { if (!this.checkNetHandle()) { return; } this.debugBegin("netHandle.getAddressesByName", JSON.stringify(this.host)); this.netHandle.getAddressesByName(this.host, (err, data) => { this.callBack(err, data); }); } ``` ## 相关仓 [网络管理子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86%E5%AD%90%E7%B3%BB%E7%BB%9F.md) **communication_netmanager_base** [communication_netmanager_ext](https://gitee.com/openharmony/communication_netmanager_ext) [communication_netstack](https://gitee.com/openharmony/communication_netstack)