# @ohos.net.connection (网络连接管理)
网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。
> **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import connection from '@ohos.net.connection'
```
## connection.createNetConnection8+
createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection
返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征;timeout是超时时间(单位是毫秒);netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| netSpecifier | [NetSpecifier](#netspecifier) | 否 | 指定网络的各项特征,不指定或为undefined时关注默认网络。 |
| timeout | number | 否 | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效,undefined时默认值为0。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | 所关注的网络的句柄。 |
**示例:**
```js
// 关注默认网络, 不需要传参
let netConnection = connection.createNetConnection()
// 关注蜂窝网络,需要传入相关网络特征,timeout参数未传入说明未使用超时时间,此时timeout为0
let netConnectionCellular = connection.createNetConnection({
netCapabilities: {
bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
}
})
```
## connection.getDefaultNet8+
getDefaultNet(callback: AsyncCallback\): void
获取默认激活的数据网络,使用callback方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数。当成功获取默认激活的数据网络时,error为undefined,data为默认激活的数据网络;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet(function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## connection.getDefaultNet8+
getDefaultNet(): Promise\
获取默认激活的数据网络,使用Promise方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回默认激活的数据网络。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (data) {
console.log(JSON.stringify(data))
})
```
## connection.getDefaultNetSync9+
getDefaultNetSync(): NetHandle
使用同步方法获取默认激活的数据网络。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------- | ---------------------------------- |
| NetHandle | 以同步方式返回默认激活的数据网络。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
let netHandle = connection.getDefaultNetSync();
```
## connection.getGlobalHttpProxy10+
getGlobalHttpProxy(callback: AsyncCallback\): void
获取网络的全局代理配置信息,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[HttpProxy](#httpproxy10)> | 是 | 回调函数。当成功获取网络的全局代理配置信息时,error为undefined,data为网络的全局代理配置信息;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 202 | Non-system applications use system APIs. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getGlobalHttpProxy((error, data) => {
console.info(JSON.stringify(error));
console.info(JSON.stringify(data));
})
```
## connection.getGlobalHttpProxy10+
getGlobalHttpProxy(): Promise\;
获取网络的全局代理配置信息,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<[HttpProxy](#httpproxy10)> | 以Promise形式返回网络的全局代理配置信息。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 202 | Non-system applications use system APIs. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getGlobalHttpProxy().then((data) => {
console.info(JSON.stringify(data));
}).catch(error => {
console.info(JSON.stringify(error));
})
```
## connection.setGlobalHttpProxy10+
setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\): void
设置网络全局Http代理配置信息,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
| httpProxy | [HttpProxy](#httpproxy10) | 是 | 网络全局Http代理配置信息 |
| callback | AsyncCallback\ | 是 | 回调函数。当成功设置网络全局Http代理配置信息时,error为undefined,否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 202 | Non-system applications use system APIs. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
let exclusionStr = "192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
host: "192.168.xx.xxx",
port: 8080,
exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy, (error) => {
console.info(JSON.stringify(error));
});
```
## connection.setGlobalHttpProxy10+
setGlobalHttpProxy(httpProxy: HttpProxy): Promise\;
设置网络全局Http代理配置信息,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy10) | 是 | 网络全局Http代理配置信息。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\ | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 202 | Non-system applications use system APIs. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
let exclusionStr = "192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
host: "192.168.xx.xxx",
port: 8080,
exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy).then(() => {
console.info("success");
}).catch(error => {
console.info(JSON.stringify(error));
})
```
## connection.getDefaultHttpProxy10+
getDefaultHttpProxy(callback: AsyncCallback\): void
获取网络默认的代理配置信息。
如果设置了全局代理,则会返回全局代理配置信息。如果进程使用[setAppNet](#connectionsetappnet)绑定到指定[NetHandle](#nethandle)对应的网络,则返回[NetHandle](#nethandle)对应网络的代理配置信息。在其它情况下,将返回默认网络的代理配置信息。
使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback<[HttpProxy](#httpproxy10)> | 是 | 回调函数。当成功获取网络默认的代理配置信息时,error为undefined,data为网络默认的代理配置信息;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| -------- | -------------------------------------------- |
| 2100002 | Operation failed. Cannot connect to service. |
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultHttpProxy((error, data) => {
console.info(JSON.stringify(error));
console.info(JSON.stringify(data));
})
```
## connection.getDefaultHttpProxy10+
getDefaultHttpProxy(): Promise\;
获取网络默认的代理配置信息。
如果设置了全局代理,则会返回全局代理配置信息。如果进程使用[setAppNet](#connectionsetappnet)绑定到指定[NetHandle](#nethandle)对应的网络,则返回[NetHandle](#nethandle)对应网络的代理配置信息。在其它情况下,将返回默认网络的代理配置信息。
使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| -------------------------------- | ----------------------------------------- |
| Promise<[HttpProxy](#httpproxy10)> | 以Promise形式返回网络默认的代理配置信息。 |
**错误码:**
| 错误码ID | 错误信息 |
| -------- | -------------------------------------------- |
| 2100002 | Operation failed. Cannot connect to service. |
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultHttpProxy().then((data) => {
console.info(JSON.stringify(data));
}).catch(error => {
console.info(JSON.stringify(error));
})
```
## connection.getAppNet9+
getAppNet(callback: AsyncCallback\): void
获取App绑定的网络信息,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数。当成功获取App绑定的网络信息时,error为undefined,data为获取到App绑定的网络信息;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 401 | Parameter error.|
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getAppNet(function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## connection.getAppNet9+
getAppNet(): Promise\;
获取App绑定的网络信息,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回App绑定的网络信息。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 401 | Parameter error.|
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getAppNet().then((data) => {
console.info(JSON.stringify(data));
}).catch(error => {
console.info(JSON.stringify(error));
})
```
## connection.SetAppNet9+
setAppNet(netHandle: NetHandle, callback: AsyncCallback\): void
绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 |
| callback | AsyncCallback\ | 是 | 回调函数。当成功绑定App到指定网络时,error为undefined,否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet(function (error, netHandle) {
connection.setAppNet(netHandle, (error, data) => {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
});
})
```
## connection.SetAppNet9+
setAppNet(netHandle: NetHandle): Promise\;
绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\ | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.setAppNet(netHandle).then(() => {
console.log("success")
}).catch(error => {
console.log(JSON.stringify(error))
})
})
```
## connection.getAllNets8+
getAllNets(callback: AsyncCallback<Array<NetHandle>>): void
获取所有处于连接状态的网络列表,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | 是 | 回调函数。当成功获取所有处于连接状态的网络列表时,error为undefined,data为激活的数据网络列表;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getAllNets(function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
});
```
## connection.getAllNets8+
getAllNets(): Promise<Array<NetHandle>>
获取所有处于连接状态的网络列表,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise<Array<[NetHandle](#nethandle)>> | 以Promise形式返回激活的数据网络列表。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getAllNets().then(function (data) {
console.log(JSON.stringify(data))
});
```
## connection.getConnectionProperties8+
getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\): void
获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 |
| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是 | 回调函数。当成功获取netHandle对应的网络的连接信息时,error为undefined,data为获取的网络连接信息;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.getConnectionProperties(netHandle, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
})
```
## connection.getConnectionProperties8+
getConnectionProperties(netHandle: NetHandle): Promise\
获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------------------------------- | --------------------------------- |
| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.getConnectionProperties(netHandle).then(function (data) {
console.log(JSON.stringify(data))
})
})
```
## connection.getNetCapabilities8+
getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\): void
获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 |
| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是 | 回调函数。当成功获取netHandle对应的网络的能力信息时,error为undefined,data为获取到的网络能力信息;否则为错误对象 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.getNetCapabilities(netHandle, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
})
```
## connection.getNetCapabilities8+
getNetCapabilities(netHandle: NetHandle): Promise\
获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------------- | --------------------------------- |
| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.getNetCapabilities(netHandle).then(function (data) {
console.log(JSON.stringify(data))
})
})
```
## connection.isDefaultNetMetered9+
isDefaultNetMetered(callback: AsyncCallback\): void
检查当前网络上的数据流量使用是否被计量,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\ | 是 | 回调函数。当前网络上的数据流量使用被计量返回true。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.isDefaultNetMetered(function (error, data) {
console.log(JSON.stringify(error))
console.log('data: ' + data)
})
```
## connection.isDefaultNetMetered9+
isDefaultNetMetered(): Promise\
检查当前网络上的数据流量使用是否被计量,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| ----------------- | ----------------------------------------------- |
| Promise\ | 以Promise形式返回,当前网络上的数据流量使用被计量true。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.isDefaultNetMetered().then(function (data) {
console.log('data: ' + data)
})
```
## connection.hasDefaultNet8+
hasDefaultNet(callback: AsyncCallback\): void
检查默认数据网络是否被激活,使用callback方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\ | 是 | 回调函数。默认数据网络被激活返回true。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.hasDefaultNet(function (error, data) {
console.log(JSON.stringify(error))
console.log('data: ' + data)
})
```
## connection.hasDefaultNet8+
hasDefaultNet(): Promise\
检查默认数据网络是否被激活,使用Promise方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| ----------------- | ----------------------------------------------- |
| Promise\ | 以Promise形式返回,默认数据网络被激活返回true。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.hasDefaultNet().then(function (data) {
console.log('data: ' + data)
})
```
## connection.enableAirplaneMode8+
enableAirplaneMode(callback: AsyncCallback\): void
开启飞行模式,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\ | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.enableAirplaneMode(function (error) {
console.log(JSON.stringify(error))
})
```
## connection.enableAirplaneMode8+
enableAirplaneMode(): Promise\
开启飞行模式,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\ | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.enableAirplaneMode().then(function (error) {
console.log(JSON.stringify(error))
})
```
## connection.disableAirplaneMode8+
disableAirplaneMode(callback: AsyncCallback\): void
关闭飞行模式,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\ | 是 | 回调函数。当关闭飞行模式成功,error为undefined,否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.disableAirplaneMode(function (error) {
console.log(JSON.stringify(error))
})
```
## connection.disableAirplaneMode8+
disableAirplaneMode(): Promise\
关闭飞行模式,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\ | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.disableAirplaneMode().then(function (error) {
console.log(JSON.stringify(error))
})
```
## connection.reportNetConnected8+
reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void
向网络管理报告网络处于可用状态,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当向网络管理报告网络处于可用状态成功,error为undefined,否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.reportNetConnected(netHandle, function (error) {
console.log(JSON.stringify(error))
});
});
```
## connection.reportNetConnected8+
reportNetConnected(netHandle: NetHandle): Promise<void>
向网络管理报告网络处于可用状态,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise<void> | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.reportNetConnected(netHandle).then(function () {
console.log(`report success`)
});
});
```
## connection.reportNetDisconnected8+
reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void
向网络管理报告网络处于不可用状态,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当向网络管理报告网络处于不可用状态成功,error为undefined,否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.reportNetDisconnected(netHandle, function (error) {
console.log(JSON.stringify(error))
});
});
```
## connection.reportNetDisconnected8+
reportNetDisconnected(netHandle: NetHandle): Promise<void>
向网络管理报告网络处于不可用状态,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise<void> | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
connection.reportNetDisconnected(netHandle).then(function () {
console.log(`report success`)
});
});
```
## connection.getAddressesByName8+
getAddressesByName(host: string, callback: AsyncCallback\>): void
使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
| host | string | 是 | 需要解析的主机名。 |
| callback | AsyncCallback\> | 是 | 回调函数。当使用默认网络解析主机名成功获取所有IP地址,error为undefined,data为获取到的所有IP地址;否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
let host = "xxxx";
connection.getAddressesByName(host, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## connection.getAddressesByName8+
getAddressesByName(host: string): Promise\>
使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------------------ |
| host | string | 是 | 需要解析的主机名。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\> | 以Promise形式返回所有IP地址。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
let host = "xxxx";
connection.getAddressesByName(host).then(function (data) {
console.log(JSON.stringify(data))
})
```
## NetConnection
网络连接的句柄。
> **说明:**
> 设备从无网络到有网络会触发netAvailable事件、netCapabilitiesChange事件和netConnectionPropertiesChange事件;
> 设备从有网络到无网络状态会触发netLost事件;
> 设备从WiFi到蜂窝会触发netLost事件(WiFi丢失)之后触发 netAvaliable事件(蜂窝可用);
### register8+
register(callback: AsyncCallback\): void
订阅指定网络状态变化的通知。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\ | 是 | 回调函数。当订阅指定网络状态变化的通知成功,error为undefined,否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
| 2101008 | The same callback exists. |
| 2101022 | The number of requests exceeded the maximum. |
**示例:**
```js
netConnection.register(function (error) {
console.log(JSON.stringify(error))
})
```
### unregister8+
unregister(callback: AsyncCallback\): void
取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\ | 是 | 回调函数。当取消订阅指定网络状态变化的通知成功,error为undefined,否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied.|
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
| 2101007 | The callback is not exists. |
**示例:**
```js
netConnection.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
### on('netAvailable')8+
on(type: 'netAvailable', callback: Callback\): void
订阅网络可用事件。
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 订阅事件,固定为'netAvailable'。
netAvailable:数据网络可用事件。 |
| callback | Callback\<[NetHandle](#nethandle)> | 是 | 回调函数,返回数据网络句柄。|
**示例:**
```js
// 创建NetConnection对象
let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon.register(function (error) {
console.log(JSON.stringify(error))
})
// 订阅网络可用事件。调用register后,才能接收到此事件通知
netCon.on('netAvailable', function (data) {
console.log(JSON.stringify(data))
})
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
### on('netBlockStatusChange')8+
on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void
订阅网络阻塞状态事件,使用callback方式作为异步方法。
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type | string | 是 | 订阅事件,固定为'netBlockStatusChange'。
netBlockStatusChange:网络阻塞状态事件。 |
| callback | Callback<{ netHandle: [NetHandle](#nethandle), blocked: boolean }> | 是 | 回调函数,返回数据网络句柄(netHandle),及网络堵塞状态(blocked)。|
**示例:**
```js
// 创建NetConnection对象
let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon.register(function (error) {
console.log(JSON.stringify(error))
})
// 订阅网络阻塞状态事件。调用register后,才能接收到此事件通知
netCon.on('netBlockStatusChange', function (data) {
console.log(JSON.stringify(data))
})
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
### on('netCapabilitiesChange')8+
on(type: 'netCapabilitiesChange', callback: Callback): void
订阅网络能力变化事件。
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type | string | 是 | 订阅事件,固定为'netCapabilitiesChange'。
netCapabilitiesChange:网络能力变化事件。 |
| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo)> | 是 | 回调函数,返回数据网络句柄(netHandle)和网络的能力信息(netCap)。|
**示例:**
```js
// 创建NetConnection对象
let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon.register(function (error) {
console.log(JSON.stringify(error))
})
// 订阅网络能力变化事件。调用register后,才能接收到此事件通知
netCon.on('netCapabilitiesChange', function (data) {
console.log(JSON.stringify(data))
})
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
### on('netConnectionPropertiesChange')8+
on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties:
ConnectionProperties }>): void
订阅网络连接信息变化事件。
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type | string | 是 | 订阅事件,固定为'netConnectionPropertiesChange'。
netConnectionPropertiesChange:网络连接信息变化事件。 |
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是 | 回调函数,返回数据网络句柄(netHandle)和网络的连接信息(connectionProperties)|
**示例:**
```js
// 创建NetConnection对象
let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon.register(function (error) {
console.log(JSON.stringify(error))
})
// 订阅网络连接信息变化事件。调用register后,才能接收到此事件通知
netCon.on('netConnectionPropertiesChange', function (data) {
console.log(JSON.stringify(data))
})
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
### on('netLost')8+
on(type: 'netLost', callback: Callback\): void
订阅网络丢失事件。
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 订阅事件,固定为'netLost'。
netLost:网络严重中断或正常断开事件。 |
| callback | Callback\<[NetHandle](#nethandle)> | 是 | 回调函数,数据网络句柄(netHandle)|
**示例:**
```js
// 创建NetConnection对象
let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon.register(function (error) {
console.log(JSON.stringify(error))
})
// 订阅网络丢失事件。调用register后,才能接收到此事件通知
netCon.on('netLost', function (data) {
console.log(JSON.stringify(data))
})
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
### on('netUnavailable')8+
on(type: 'netUnavailable', callback: Callback\): void
订阅网络不可用事件。
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
| type | string | 是 | 订阅事件,固定为'netUnavailable'。
netUnavailable:网络不可用事件。 |
| callback | Callback\ | 是 | 回调函数,无返回结果。|
**示例:**
```js
// 创建NetConnection对象
let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon.register(function (error) {
console.log(JSON.stringify(error))
})
// 订阅网络不可用事件。调用register后,才能接收到此事件通知
netCon.on('netUnavailable', function (data) {
console.log(JSON.stringify(data))
})
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
console.log(JSON.stringify(error))
})
```
## NetHandle8+
数据网络的句柄。
在调用NetHandle的方法之前,需要先获取NetHandle对象。
**系统能力**:SystemCapability.Communication.NetManager.Core
### 属性
| 名称 | 类型 | 必填 | 说明 |
| ------ | ------ | --- |------------------------- |
| netId | number | 是 | 网络ID,取值为0代表没有默认网络,其余取值必须大于等于100。 |
### bindSocket9+
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\): void
将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
| callback | AsyncCallback\ | 是 | 回调函数。当TCPSocket或UDPSocket成功绑定到当前网络,error为undefined,否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle) => {
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "TCPSocket";
if (socketType == "TCPSocket") {
tcp.bind({
address: '192.168.xx.xxx', port: 8080, family: 1
}, error => {
if (error) {
console.log('bind fail');
return;
}
netHandle.bindSocket(tcp, (error, data) => {
if (error) {
console.log(JSON.stringify(error));
} else {
console.log(JSON.stringify(data));
}
})
})
} else {
let callback = value => {
console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
}
udp.on('message', callback);
udp.bind({
address: '192.168.xx.xxx', port: 8080, family: 1
}, error => {
if (error) {
console.log('bind fail');
return;
}
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(udp, (error, data) => {
if (error) {
console.log(JSON.stringify(error));
} else {
console.log(JSON.stringify(data));
}
})
})
}
})
```
### bindSocket9+
bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\;
将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
**返回值:**
| 类型 | 说明 |
| -------------- | ---------------------- |
| Promise\ | 无返回值的Promise对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle) => {
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "TCPSocket";
if (socketType == "TCPSocket") {
tcp.bind({
address: '192.168.xx.xxx', port: 8080, family: 1
}, error => {
if (error) {
console.log('bind fail');
return;
}
netHandle.bindSocket(tcp).then((data) => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
})
})
} else {
let callback = value => {
console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
}
udp.on('message', callback);
udp.bind({
address: '192.168.xx.xxx', port: 8080, family: 1
}, error => {
if (error) {
console.log('bind fail');
return;
}
udp.on('message', (data) => {
console.log(JSON.stringify(data));
})
netHandle.bindSocket(udp).then((data) => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
})
})
}
})
```
### getAddressesByName8+
getAddressesByName(host: string, callback: AsyncCallback\>): void
使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
| host | string | 是 | 需要解析的主机名。 |
| callback | AsyncCallback\> | 是 | 回调函数。当使用对应网络解析主机名成功获取所有IP地址,error为undefined,data为获取到的所有IP地址;否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
let host = "xxxx";
netHandle.getAddressesByName(host, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
})
```
### getAddressesByName8+
getAddressesByName(host: string): Promise\>
使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------------------ |
| host | string | 是 | 需要解析的主机名。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\> | 以Promise形式返回所有IP地址。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
let host = "xxxx";
netHandle.getAddressesByName(host).then(function (data) {
console.log(JSON.stringify(data))
})
})
```
### getAddressByName8+
getAddressByName(host: string, callback: AsyncCallback\): void
使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| host | string | 是 | 需要解析的主机名。 |
| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是 | 回调函数。当使用对应网络解析主机名获取第一个IP地址成功,error为undefined,data为获取的第一个IP地址;否则为错误对象。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
let host = "xxxx";
netHandle.getAddressByName(host, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
})
```
### getAddressByName8+
getAddressByName(host: string): Promise\
使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------------------ |
| host | string | 是 | 需要解析的主机名。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error. |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
let host = "xxxx";
netHandle.getAddressByName(host).then(function (data) {
console.log(JSON.stringify(data))
})
})
```
## NetCap8+
网络具体能力。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 值 | 说明 |
| ------------------------ | ---- | ---------------------- |
| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia Message Service,多媒体短信服务)发送和接收彩信。 |
| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 |
| NET_CAPABILITY_INTERNET | 12 | 表示该网络应具有访问Internet的能力,该能力由网络提供者设置。 |
| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual Private Network,虚拟专用网络)。 |
| NET_CAPABILITY_VALIDATED | 16 | 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置。 |
## NetBearType8+
网络类型。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 值 | 说明 |
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0 | 蜂窝网络。 |
| BEARER_WIFI | 1 | Wi-Fi网络。 |
| BEARER_ETHERNET | 3 | 以太网网络。 |
## HttpProxy10+
网络代理配置信息
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ------ | ------ | --- |------------------------- |
| host | string | 否 | 代理服务器主机名。 |
| port | number | 否 | 主机端口。 |
| exclusionList | Array | 否 | 不使用代理的主机名列表,主机名支持域名、IP地址以及通配符形式,详细匹配规则如下:
1、域名匹配规则:
(1)完全匹配:代理服务器主机名只要与列表中的任意一个主机名完全相同,就可以匹配。
(2)包含匹配:代理服务器主机名只要包含列表中的任意一个主机名,就可以匹配。
例如,如果在主机名列表中设置了 “ample.com”,则 “ample.com”、“www.ample.com”、“ample.com:80”都会被匹配,而 “www.example.com”、“ample.com.org”则不会被匹配。
2、IP地址匹配规则:代理服务器主机名只要与列表中的任意一个IP地址完全相同,就可以匹配。
3、域名跟IP地址可以同时添加到列表中进行匹配。
4、单个“*”是唯一有效的通配符,当列表中只有通配符时,将与所有代理服务器主机名匹配,表示禁用代理。通配符只能单独添加,不可以与其他域名、IP地址一起添加到列表中,否则通配符将不生效。
5、匹配规则不区分主机名大小写。
6、匹配主机名时,不考虑http和https等协议前缀。 |
## NetSpecifier8+
提供承载数据网络能力的实例。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| netCapabilities | [NetCapabilities](#netcapabilities) | 是 | 存储数据网络的传输能力和承载类型。 |
| bearerPrivateIdentifier | string | 否 | 网络标识符,Wi-Fi网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 |
## NetCapabilityInfo10+
提供承载数据网络能力的实例。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络句柄。 |
| netCap | [NetCapabilities](#netcapabilities) | 否 | 存储数据网络的传输能力和承载类型。 |
## NetCapabilities8+
网络的能力集。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| --------------------- | ---------------------------------- | --- | ------------------------ |
| linkUpBandwidthKbps | number | 否 | 上行(设备到网络)带宽,0表示无法评估当前网络带宽。 |
| linkDownBandwidthKbps | number | 否 | 下行(网络到设备)带宽,0表示无法评估当前网络带宽。 |
| networkCap | Array\<[NetCap](#netcap)> | 否 | 网络具体能力。 |
| bearerTypes | Array\<[NetBearType](#netbeartype)> | 是 | 网络类型。 |
## ConnectionProperties8+
网络连接信息。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ------------- | ---------------------------------- | ----|---------------- |
| interfaceName | string | 是 |网卡名称。 |
| domains | string | 是 |所属域,默认""。 |
| linkAddresses | Array\<[LinkAddress](#linkaddress)> | 是 |链路信息。 |
| routes | Array\<[RouteInfo](#routeinfo)> | 是 |路由信息。 |
| dnses | Array\<[NetAddress](#netaddress)> | 是 |网络地址,参考[NetAddress](#netaddress)。 |
| mtu | number | 是 |最大传输单元。 |
## RouteInfo8+
网络路由信息。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 |说明 |
| -------------- | --------------------------- | --- |---------------- |
| interface | string | 是 |网卡名称。 |
| destination | [LinkAddress](#linkaddress) | 是 |目的地址。 |
| gateway | [NetAddress](#netaddress) | 是 |网关地址。 |
| hasGateway | boolean | 是 |是否有网关。 |
| isDefaultRoute | boolean | 是 |是否为默认路由。 |
## LinkAddress8+
网络链路信息。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 |说明 |
| ------------ | ----------------------- |---- |-------------------- |
| address | [NetAddress](#netaddress) | 是 | 链路地址。 |
| prefixLength | number | 是 |链路地址前缀的长度。 |
## NetAddress8+
网络地址。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ------- | ------ | -- |------------------------------ |
| address | string | 是 |地址。 |
| family | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port | number | 否 |端口,取值范围\[0, 65535]。 |