From 328b99bd6015647778e7f10992cca52673e8ddca Mon Sep 17 00:00:00 2001 From: maosiping Date: Tue, 1 Mar 2022 21:30:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E7=AE=A1?= =?UTF-8?q?=E7=90=86net.connection=E7=9B=B8=E5=85=B3API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../reference/apis/js-apis-net-connection.md | 712 ++++++++++++++++++ 1 file changed, 712 insertions(+) create mode 100644 zh-cn/application-dev/reference/apis/js-apis-net-connection.md diff --git a/zh-cn/application-dev/reference/apis/js-apis-net-connection.md b/zh-cn/application-dev/reference/apis/js-apis-net-connection.md new file mode 100644 index 0000000000..a25539ba4f --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-net-connection.md @@ -0,0 +1,712 @@ +# 蜂窝数据 + +> **说明:** +> +>本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 + +## 导入模块 + +```typescript +import connection from '@ohos.net.connection' +``` + +## connection.getDefaultNet + +getDefaultNet(callback: AsyncCallback\): void + +获取默认网络,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| callback | AsyncCallback\<[NetHandle](#NetHandle)> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.getDefaultNet(function (error, netHandle) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(netHandle)) +}) +``` + +## connection.getDefaultNet + +getDefaultNet(): Promise\ + +获取默认网络,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\<[NetHandle](#NetHandle)> | 以Promise形式返回 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + console.log(JSON.stringify(netHandle)) +}) +``` + +## connection.hasDefaultNet + +hasDefaultNet(callback: AsyncCallback\): void + +判断是否有默认网络,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| callback | AsyncCallback\ | 是 | 回调函数 | + +**示例:** + +```javascript +connection.hasDefaultNet(function (error, has) { + console.log(JSON.stringify(error)) + console.log(has) +}) +``` + +## connection.hasDefaultNet + +hasDefaultNet(): Promise\ + +判断是否有默认网络,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\ | 以Promise形式返回 | + +**示例:** + +```javascript +connection.hasDefaultNet().then(function (has) { + console.log(has) +}) +``` + +## connection.getConnectionProperties + +getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\): void + +查询netHandle对应的网络的连接信息,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| netHandle | [NetHandle](#NetHandle) | 是 | 对应网络 | +| callback | AsyncCallback\<[ConnectionProperties](#ConnectionProperties)> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getConnectionProperties(netHandle, function (error, info) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.getConnectionProperties + +getConnectionProperties(netHandle: NetHandle): Promise\ + +查询netHandle对应的网络的连接信息,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| netHandle | [NetHandle](#NetHandle) | 是 | 对应网络 | + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\<[ConnectionProperties](#ConnectionProperties)> | 以Promise形式返回 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getConnectionProperties(netHandle).then(function (info) { + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.getNetCapabilities + +getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\): void + +查询netHandle对应的网络的能力信息,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| netHandle | [NetHandle](#NetHandle) | 是 | 对应网络 | +| callback | AsyncCallback\<[NetCapabilities](#NetCapabilities)> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getNetCapabilities(netHandle, function (error, info) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.getNetCapabilities + +getNetCapabilities(netHandle: NetHandle): Promise\ + +查询netHandle对应的网络的能力信息,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| netHandle | [NetHandle](#NetHandle) | 是 | 对应网络 | + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\<[NetCapabilities](#NetCapabilities)> | 以Promise形式返回 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getNetCapabilities(netHandle).then(function (info) { + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.getAddressesByName + +getAddressesByName(host: string, callback: AsyncCallback\>): void + +使用默认网络将host解析成IP,返回所有IP,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| host | string | 是 | 需要解析的域名 | +| callback | AsyncCallback\> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getAddressesByName(netHandle, function (error, info) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.getAddressesByName + +getAddressesByName(netHandle: NetHandle): Promise\> + +使用默认网络将host解析成IP,返回所有IP,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| host | string | 是 | 需要解析的域名 | + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\> | 以Promise形式返回 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getAddressesByName(netHandle).then(function (info) { + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.createNetConnection + +createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection + +获取一个netSpecifier指定的网络的句柄 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| netSpecifier | [NetSpecifier](#NetSpecifier) | 否 | 关注的网络的各项特征,不指定则关注默认网络 | +| timeout | number | 否 | 获取netSpecifier指定的网络时的超时时间,前提是netSpecifier存在 | + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +|[NetConnection](#NetConnection) | 所关注的网络的句柄 | + +**示例:** + +```javascript +// 关注默认网络 +let netConnection1 = connection.createNetConnection() + +// 关注蜂窝网络 +let netConnection2 = connection.createNetConnection({ + netCapabilities: { + networkCap: [0] + } +}) +``` + +## connection.NetConnection + +网络连接的句柄 + +### connection.NetConnection.on('netAvailable') + +on(type: 'netAvailable', callback: Callback\): void + +监听网络可用事件 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| type | string | 是 | 监听的事件,固定'netAvailable' | +| callback | Callback\<[NetHandle](#NetHandle)>> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().on('netAvailable', function (data) { + console.log(JSON.stringify(data)) +}) +``` + +### connection.NetConnection.on('netCapabilitiesChange') + +on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void + +监听网络能力变化事件 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| type | string | 是 | 监听的事件,固定'netCapabilitiesChange' | +| callback | Callback<{ netHandle: [NetHandle](#NetHandle), netCap: [NetCapabilities](#NetCapabilities) }> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().on('netCapabilitiesChange', function (data) { + console.log(JSON.stringify(data)) +}) +``` + +### connection.NetConnection.on('netConnectionPropertiesChange') + +on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void + +监听网络连接信息变化事件 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| type | string | 是 | 监听的事件,固定'netConnectionPropertiesChange' | +| callback | Callback<{ netHandle: [NetHandle](#NetHandle), connectionProperties: [ConnectionProperties](#ConnectionProperties) }> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().on('netConnectionPropertiesChange', function (data) { + console.log(JSON.stringify(data)) +}) +``` + +### connection.NetConnection.on('netLost') + +on(type: 'netLost', callback: Callback\): void + +监听网络丢失事件 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| type | string | 是 | 监听的事件,固定'netLost' | +| callback | Callback\<[NetHandle](#NetHandle)>> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().on('netLost', function (data) { + console.log(JSON.stringify(data)) +}) +``` + +### connection.NetConnection.on('netUnavailable') + +on(type: 'netUnavailable', callback: Callback\): void + +监听网络不可用事件 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| type | string | 是 | 监听的事件,固定'netUnavailable' | +| callback | Callback\> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().on('netUnavailable', function (data) { + console.log(JSON.stringify(data)) +}) +``` + +### connection.NetConnection.register + +register(callback: AsyncCallback\): void + +注册网络的监听 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| callback | Callback\> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().register(function (error) { + console.log(JSON.stringify(error)) +}) +``` + +### connection.NetConnection.unregister + +unregister(callback: AsyncCallback\): void + +注销网络的监听 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| callback | Callback\> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.createNetConnection().unregister(function (error) { + console.log(JSON.stringify(error)) +}) +``` + +## connection.NetHandle + +网络的句柄 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| netId | number | 对应网络的编号 | + +### connection.NetHandle.getAddressesByName + +getAddressesByName(host: string, callback: AsyncCallback\>): void + +使用对应网络将host解析成IP,返回所有IP,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| host | string | 是 | 需要解析的域名 | +| callback | AsyncCallback\> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getAddressesByName(netHandle, function (error, info) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(info)) + }) +}) +``` + +### connection.NetHandle.getAddressesByName + +getAddressesByName(netHandle: NetHandle): Promise\> + +使用对应网络将host解析成IP,返回所有IP,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| host | string | 是 | 需要解析的域名 | + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\> | 以Promise形式返回 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getAddressesByName(netHandle).then(function (info) { + console.log(JSON.stringify(info)) + }) +}) +``` + +### connection.NetHandle.getAddressByName + +getAddressByName(host: string, callback: AsyncCallback\): void + +使用对应网络将host解析成IP,返回一个IP,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| host | string | 是 | 需要解析的域名 | +| callback | AsyncCallback\<[NetAddress](#NetAddress)> | 是 | 回调函数 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getAddressByName(netHandle, function (error, info) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(info)) + }) +}) +``` + +### connection.NetHandle.getAddressByName + +getAddressByName(netHandle: NetHandle): Promise\ + +使用对应网络将host解析成IP,返回一个IP,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.GET_NETWORK_INFO + +**系统能力**:SystemCapability.Communication.NetManager.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ----- | ---- | ----- | +| host | string | 是 | 需要解析的域名 | + +**返回值:** + +| 类型 | 说明 | +| ----- | ----- | +| Promise\<[NetAddress](#NetAddress)> | 以Promise形式返回 | + +**示例:** + +```javascript +connection.getDefaultNet().then(function (netHandle) { + connection.getAddressByName(netHandle).then(function (info) { + console.log(JSON.stringify(info)) + }) +}) +``` + +## connection.NetSpecifier + +网络的特征 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| netCapabilities | [NetCapabilities](#NetCapabilities) | 网络的能力集 | +| bearerPrivateIdentifier | string | 网络标识符,WIFI网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1) | + +## connection.NetCapabilities + +网络的能力集 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| linkUpBandwidthKbps | number | 带宽上限 | +| linkDownBandwidthKbps | number | 带宽下限 | +| networkCap | Array<[NetCap](#NetCap)> | 网络具体能力 | +| bearerTypes | Array<[NetBearType](#NetBearType)> | 网络类型 | + +## connection.NetCap + +网络具体能力 + +| 变量 | 值 | 说明 | +| ------ | ----- | ----- | +| NET_CAPABILITY_INTERNET | 12 | 联网能力 | +| NET_CAPABILITY_VALIDATED | 16 | 网络可用 | + +## connection.NetBearType + +网络类型 + +| 变量 | 值 | 说明 | +| ------ | ----- | ----- | +| BEARER_CELLULAR | 0 | 蜂窝网络 | +| BEARER_WIFI | 1 | WIFI网络 | + +## connection.ConnectionProperties + +网络连接信息 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| interfaceName | string | 网卡名称 | +| domains | string | 所属域,默认"" | +| linkAddresses | Array<[LinkAddress](#LinkAddress)> | 链路信息 | +| routes | Array<[RouteInfo](#RouteInfo)> | 路由信息 | +| mtu | number | 最大传输单元 | + +## connection.LinkAddress + +网络链路信息 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| address | [NetAddress](#NetAddress) | 链路地址 | +| prefixLength | number | 地址前缀长度 | + +## connection.RouteInfo + +网络路由信息 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| interface | string | 网卡名称 | +| destination | [LinkAddress](#LinkAddress) | 目的地址 | +| gateway | [NetAddress](#NetAddress) | 网关地址 | +| hasGateway | boolean | 是否有网关 | +| isDefaultRoute | boolean | 是否为默认路由 | + +## connection.NetAddress + +地址 + +| 变量 | 类型 | 说明 | +| ----- | ----- | ----- | +| address | string | 一个IPv4地址或者IPv6地址 | +| family | number | IPv4 = 1, IPv6 = 2, 默认IPv4 | +| port | number | 端口,取值范围\[0, 65535] | -- GitLab