提交 629a7a1e 编写于 作者: Z zhuwenchao

update docs for netmanager

Signed-off-by: Nzhuwenchao <zhuwenchao@huawei.com>
上级 fbba6a66
......@@ -174,10 +174,15 @@
- [@ohos.telephony.data (蜂窝数据)](js-apis-telephony-data.md)
- 网络管理
- [@ohos.net.connection (网络连接管理)](js-apis-net-connection.md)
- [@ohos.net.statistics (网络流量管理)](js-apis-net-statistics.md)
- [@ohos.net.policy (网络策略管理)](js-apis-net-policy.md)
- [@ohos.net.ethernet (以太网连接管理)](js-apis-net-ethernet.md)
- [@ohos.net.sharing (网络共享管理)](js-apis-net-sharing.md)
- [@ohos.net.http (数据请求)](js-apis-http.md)
- [@ohos.request (上传下载)](js-apis-request.md)
- [@ohos.net.socket (Socket连接)](js-apis-socket.md)
- [@ohos.net.webSocket (WebSocket连接)](js-apis-webSocket.md)
- [@ohos.net.tlsSocket (TLSSocket连接)](js-apis-tlsSocket.md)
- 通信与连接
- [@ohos.bluetooth (蓝牙)](js-apis-bluetooth.md)
......
......@@ -66,6 +66,7 @@ connection.getDefaultNet().then(function (netHandle) {
hasDefaultNet(callback: AsyncCallback\<boolean>): void
检查默认数据网络是否被激活,使用callback方式作为异步方法。
默认数据网络:以太网>wifi>蜂窝,当只有一个网络为连接状态时,当前连接网络为默认数据网络。
**系统能力**:SystemCapability.Communication.NetManager.Core
......@@ -89,6 +90,7 @@ connection.hasDefaultNet(function (error, has) {
hasDefaultNet(): Promise\<boolean>
检查默认数据网络是否被激活,使用Promise方式作为异步方法。
默认数据网络:以太网>wifi>蜂窝,当只有一个网络为连接状态时,当前连接网络为默认数据网络。
**系统能力**:SystemCapability.Communication.NetManager.Core
......@@ -796,6 +798,108 @@ netConnection.unregister(function (error) {
| ------ | ------ | ------------------------- |
| netId | number | 网络ID,必须大于等于100。 |
### bindSocket
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void;
将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
| callback | AsyncCallback\<void> | 是 | 回调函数 |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "xxxx";
if (socketType == "TCPSocket") {
tcp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
netHandle.bindSocket(tcp, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
} else {
udp.on('message', callback);
udp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(udp, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
});
})
}
}
```
### bindSocket
bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;
将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
**返回值:**
| 类型 | 说明 |
| -------------- | ---------------------- |
| Promise\<void> | 以Promise形式返回结果。 |
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "xxxx";
if(socketType == "TCPSocket") {
tcp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
netHandle.bindSocket(tcp).then(err, data) {
console.log(JSON.stringify(data))
})
} else {
udp.on('message', callback);
udp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(tcp).then(err, data) {
console.log(JSON.stringify(data))
});
})
}
}
```
### getAddressesByName
getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void
......@@ -1020,4 +1124,4 @@ connection.getDefaultNet().then(function (netHandle) {
| ------- | ------ | ------------------------------ |
| address | string | 地址。 |
| family | number | IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port | number | 端口,取值范围\[0, 65535]。 |
\ No newline at end of file
| port | number | 端口,取值范围\[0, 65535]。 |
# 以太网连接管理
以太网连接管理主要提供有线网络能力,提供设置有线网络的IP地址,子网掩码,网关,DNS等信息
> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import ethernet from '@ohos.net.ethernet'
```
## ethernet.setIfaceConfig
setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback\<void>): void;
设置网络接口配置信息,使用callback方式作为异步方法。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------ |
| iface | string | 是 | 网络接口名 |
| ic | [InterfaceConfiguration](#interfaceconfiguration) | 是 | 要设置的网络接口配置信息 |
| callback | AsyncCallback\<void> | 是 | 回调函数,成功无返回,失败返回对应错误码。 |
**示例:**
```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1",
gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"},
(error) => {
if (error) {
console.log("setIfaceConfig callback error = " + error);
} else {
console.log("setIfaceConfig callback ok ");
}
});
```
## ethernet.setIfaceConfig
setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>;
设置网络接口配置信息,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------------------------------------------------- | ---- | ------------------------ |
| iface | string | 是 | 接口名 |
| ic | [InterfaceConfiguration](#interfaceconfiguration) | 是 | 要设置的网络接口配置信息 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ----------------------------------------------------------- |
| Promise\<void> | 以Promise形式返回执行结果。成功无返回,失败返回对应错误码。 |
**示例:**
```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1",
gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"}).then(() => {
console.log("setIfaceConfig promiss ok ");
}).catch((error) => {
console.log("setIfaceConfig promiss error = " + error);
});
```
## ethernet.getIfaceConfig
getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>): void;
获取指定网络接口信息,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------------- | ----- | ------------ |
| iface | string | 是 | 指定网络接口 |
| callback | AsyncCallback\<[InterfaceConfiguration](#interfaceconfiguration)> | 是 | 回调函数,返回指定网络接口信息 |
**示例:**
```js
ethernet.getIfaceConfig("eth0", (error, value) => {
if (error) {
console.log("getIfaceConfig callback error = " + error);
} else {
console.log("getIfaceConfig callback mode = " + value.mode);
console.log("getIfaceConfig callback ipAddr = " + value.ipAddr);
console.log("getIfaceConfig callback routeAddr = " + value.routeAddr);
console.log("getIfaceConfig callback gateAddr = " + value.gateAddr);
console.log("getIfaceConfig callback maskAddr = " + value.maskAddr);
console.log("getIfaceConfig callback dns0Addr = " + value.dns0Addr);
console.log("getIfaceConfig callback dns1Addr = " + value.dns1Addr);
}
});
```
## ethernet.getIfaceConfig
getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>;
获取指定网络接口信息,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ------------ |
| iface | string | 是 | 指定网络接口 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ---------------------------------- |
| Promise\<[InterfaceConfiguration](#interfaceconfiguration)> | 以Promise形式返回接口信息。 |
**示例:**
```js
ethernet.getIfaceConfig("eth0").then((data) => {
console.log("getIfaceConfig promiss mode = " + data.mode);
console.log("getIfaceConfig promiss ipAddr = " + data.ipAddr);
console.log("getIfaceConfig promiss routeAddr = " + data.routeAddr);
console.log("getIfaceConfig promiss gateAddr = " + data.gateAddr);
console.log("getIfaceConfig promiss maskAddr = " + data.maskAddr);
console.log("getIfaceConfig promiss dns0Addr = " + data.dns0Addr);
console.log("getIfaceConfig promiss dns1Addr = " + data.dns1Addr);
}).catch((error) => {
console.log("getIfaceConfig promiss error = " + error);
});
```
## ethernet.isIfaceActive
isIfaceActive(iface?: string, callback: AsyncCallback\<number>): void;
判断接口是否已激活,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | -------------------------------------------------- |
| iface | string | 否 | 接口名。为空时代表查询是否存在激活接口 |
| callback | AsyncCallback\<number> | 是 | 回调函数,已激活:1,未激活:0,其他为获取失败错误码。 |
**示例:**
```js
ethernet.isIfaceActive("eth0", (error, value) => {
if (error) {
console.log("whether2Activate callback error = " + error);
} else {
console.log("whether2Activate callback = " + value);
}
});
```
## ethernet.isIfaceActive
isIfaceActive(iface?: string): Promise\<number>;
判断接口是否已激活,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | -------------------------------------- |
| iface | string | 否 | 接口名。为空时代表查询是否存在激活接口 |
**返回值:**
| 类型 | 说明 |
| ----------------| ------------------------------------------------------------------ |
| Promise\<number> | 以Promise形式返回获取结果。已激活:1,未激活:0,其他为获取失败错误码。|
**示例:**
```js
ethernet.isIfaceActive("eth0").then((data) => {
console.log("isIfaceActive promiss = " + data);
}).catch((error) => {
console.log("isIfaceActive promiss error = " + error);
});
```
## ethernet.getAllActiveIfaces
getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void;
获取活动的网络接口,使用callback方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------ | ---- | ------------------------------ |
| callback | AsyncCallback\<Array\<string>> | 是 | 回调函数,返回值为对应接口名。 |
**示例:**
```js
ethernet.getAllActiveIfaces((error, value) => {
if (error) {
console.log("getAllActiveIfaces callback error = " + error);
} else {
console.log("getAllActiveIfaces callback value.length = " + value.length);
for (let i = 0; i < value.length; i++) {
console.log("getAllActiveIfaces callback = " + value[i]);
}
}
});
```
## ethernet.getAllActiveIfaces
getAllActiveIfaces(): Promise\<Array\<string>>;
获取活动的网络接口,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.GET_NETWORK_INFO
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
**返回值:**
| 类型 | 说明 |
| ------------------------------ | ----------------------------------------------- |
| Promise\<Array\<string>> | 以Promise形式返回获取结果。返回值为对应接口名。 |
**示例:**
```js
ethernet.getAllActiveIfaces().then((data) => {
console.log("getAllActiveIfaces promiss data.length = " + data.length);
for (let i = 0; i < data.length; i++) {
console.log("getAllActiveIfaces promiss = " + data[i]);
}
}).catch((error) => {
console.log("getAllActiveIfaces promiss error = " + error);
});
```
## InterfaceConfiguration
以太网连接配置网络信息。
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。
| 参数名 | 类型 | 说明 |
| ----------------------- | ----------------------------------- | ------------------------------------------------------------ |
| mode | [IPSetMode](#ipsetmode) | 以太网连接配置模式。 |
| ipAddr | string | 以太网连接静态配置ip信息,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
| route | string | 以太网连接静态配置路由信息,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
| gateway | string | 以太网连接配置网关信息,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
| netMask | string | 以太网连接配置子网掩码,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)。 |
| dnsServers | string | 以太网连接配置dns服务地址,地址值范围0-255.0-255.0-255.0-255(DHCP模式无需配置)多地址间用“,”隔开。 |
## IPSetMode
以太网连接模式。
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetManager.Core。
| 参数名 | 值 | 说明 |
| ------------------------ | ---- | ---------------------- |
| STATIC | 0 | 以太网连接静态配置网络信息。 |
| DHCP | 1 | 以太网连接动态配置网络信息。 |
# 网络流量管理
网络流量管理提供对用户上网过程中产生的流量进行统计,支持用户按网络接口(蜂窝、Wi-Fi)和按应用查询网络流量使用情况的功能
> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import statistics from '@ohos.net.statistics'
```
## statistics.getIfaceRxBytes
getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void
获取指定网卡的接收数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| nic | string | 是 | 网卡名 |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getIfaceRxBytes(this.nic, (err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getIfaceRxBytes
getIfaceRxBytes(nic: string): Promise\<number>;
获取指定网卡的接收数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| nic | string | 是 | 网卡名 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回指定网卡的接收数据量,单位:bytes。 |
**示例:**
```js
statistics.getIfaceRxBytes(this.nic).then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getIfaceTxBytes
getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void
获取指定网卡的发送数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| nic | string | 是 | 网卡名 |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getIfaceTxBytes(this.nic, (err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getIfaceTxBytes
getIfaceTxBytes(nic: string): Promise\<number>;
获取指定网卡的发送数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| nic | string | 是 | 网卡名 |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回指定网卡的发送数据量,单位:bytes。 |
**示例:**
```js
statistics.getIfaceTxBytes(this.nic).then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getCellularRxBytes
getCellularRxBytes(callback: AsyncCallback\<number>): void;
获取蜂窝网的接收数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getCellularRxBytes((err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getCellularRxBytes
getCellularRxBytes(): Promise\<number>;
获取蜂窝网的接收数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回蜂窝网的接收数据量,单位:bytes。 |
**示例:**
```js
statistics.getCellularRxBytes().then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getCellularTxBytes
getCellularTxBytes(callback: AsyncCallback\<number>): void;
获取蜂窝网的发送数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getCellularTxBytes((err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getCellularTxBytes
getCellularTxBytes(): Promise\<number>;
获取蜂窝网的发送数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回蜂窝网的发送数据量,单位:bytes。 |
**示例:**
```js
statistics.getCellularTxBytes().then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getAllRxBytes
getAllRxBytes(callback: AsyncCallback\<number>): void;
获取所有网卡的接收数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getAllRxBytes(err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getAllRxBytes
getAllRxBytes(): Promise\<number>;
获取所有网卡的接收数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回所有网卡的接收数据量,单位:bytes。 |
**示例:**
```js
statistics.getAllRxBytes().then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getAllTxBytes
getAllTxBytes(callback: AsyncCallback\<number>): void;
获取所有网卡的发送数据,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getAllTxBytes((err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getAllTxBytes
getAllTxBytes(): Promise\<number>;
获取所有网卡的发送数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回所有网卡的发送数据量,单位:bytes。 |
**示例:**
```js
statistics.getAllTxBytes().then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getUidRxBytes
getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void;
获取指定应用的接收数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| uid | number | 是 | 应用ID |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getUidRxBytes(this.uid, (err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getUidRxBytes
getUidRxBytes(uid: number): Promise\<number>;
获取指定应用的接收数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| uid | number | 是 | 应用ID |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回指定网卡的接收数据量,单位:bytes。 |
**示例:**
```js
statistics.getUidRxBytes(this.uid).then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
## statistics.getUidTxBytes
getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void;
获取指定应用的发送数据量,使用callback方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| uid | number | 是 | 应用ID |
| callback | AsyncCallback\<number> | 是 | 回调函数,number代表数据量,单位:bytes。 |
**示例:**
```js
statistics.getUidTxBytes(this.uid, (err, data) => {
this.callBack(err, data);
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
```
## statistics.getUidTxBytes
getUidTxBytes(uid: number): Promise\<number>;
获取指定应用的发送数据量,使用Promise方式作为异步方法。
**系统能力**:SystemCapability.Communication.NetManager.Core
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------- | ---- | ---------- |
| uid | number | 是 | 应用ID |
**返回值:**
| 类型 | 说明 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | 以Promise形式返回指定网卡的发送数据量,单位:bytes。 |
**示例:**
```js
statistics.getUidTxBytes(this.uid).then((err, data) {
console.log(JSON.stringify(err))
console.log(JSON.stringify(data))
})
```
# TLSSocket
TLS Socket通信是对Socket通信的拓展。在Socket通信的基础上添加了一层安全性保护,提供了更高的安全性,分为三个子模块,包括密钥,证书,通信。
> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import socket from '@ohos.net.tlssocket'
```
## socket.constructTLSSocketInstance
constructTLSSocketInstance(): TLSSocket
创建并返回一个TLSSocket对象。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**示例:**
```js
let tlssocket = socket.constructTLSSocketInstance();
```
## tlssocket.connect
connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void
在TLSSocket上进行通信连接,并创建和初始化TLS会话,实现建立连接过程,启动与服务器的TLS/SSL握手,实现数据传输功能,使用callback方式作为异步方法。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------| ----| --------------- |
| options | [TLSConnectOptions](#tlsconnectoptions) | 是 | 连接所需要的参数。|
| callback | AsyncCallback\<void> | 是 | 回调函数,成功无返回,失败返回对应错误码。 |
**示例:**
```js
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "xxx",
port: "xxxx",
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
protocols: "TlsV1_2",
useRemoteCipherPrefer: true,
signatureAlgorithms: SHA256,
cipherSuites: AES256-SHA256,
},
};
tlssocket.connect(options, (err, data) => {
console.info(err);
console.info(data);
});
```
## tlssocket.connect
connect(options: TLSConnectOptions): Promise\<void>;
在TLSSocket上进行通信连接,并创建和初始化TLS会话,实现建立连接过程,启动与服务器的TLS/SSL握手,实现数据传输功能,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------| ----| --------------- |
| options | [TLSConnectOptions](#tlsconnectoptions) | 是 | 连接所需要的参数。|
**返回值:**
| 类型 | 说明 |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 以Promise形式返回,成功无返回,失败返回对应错误码。 |
**示例:**
```js
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "xxxx",
port: "xxxx",
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
protocols: "TlsV1_2",
useRemoteCipherPrefer: true,
signatureAlgorithms: SHA256,
cipherSuites: AES256-SHA256,
},
};
tlssocket.connect(options).then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getCertificate
getCertificate(callback: AsyncCallback\<string>): void;
在TLSSocket通信连接之后,获取本地的数字证书,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<string> | 是 | 回调函数,返回本地的证书。|
**示例:**
```js
tlssocket.getCertificate((err, data) => {
if (err) {
console.log("getCertificate callback error = " + err);
} else {
console.log("getCertificate callback = " + data);
}
});
```
## tlssocket.getCertificate
getCertificate():Promise\<string>;
在TLSSocket通信连接之后,获取本地的数字证书,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**返回值:**
| 类型 | 说明 |
| -------------- | -------------------- |
| Promise\<string> | 以Promise形式返回本地的数字证书。 |
**示例:**
```js
tlssocket.getCertificate().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getRemoteCertificate
getRemoteCertificate(callback: AsyncCallback\<string>): void;
在TLSSocket通信连接之后,获取对等方的数字证书,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<string> | 是 | 回调函数,返回对等方的证书。 |
**示例:**
```js
tlssocket.getRemoteCertificate((err, data) => {
if (err) {
console.log("getRemoteCertificate callback error = " + err);
} else {
console.log("getRemoteCertificate callback = " + data);
}
});
```
## tlssocket.getRemoteCertificate
getRemoteCertificate():Promise\<string>;
在TLSSocket通信连接之后,获取对等方的数字证书,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**返回值:**
| 类型 | 说明 |
| -------------- | -------------------- |
| Promise\<string> | 以Promise形式返回对等方的数字证书。 |
**示例:**
```js
tlssocket.getRemoteCertificate().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getProtocol
getProtocol(callback: AsyncCallback\<string>): void;
在TLSSocket通信连接之后,获取通信的协议,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<string> | 是 | 回调函数,返回通信的协议。 |
**示例:**
```js
tlssocket.getProtocol((err, data) => {
if (err) {
console.log("getProtocol callback error = " + err);
} else {
console.log("getProtocol callback = " + data);
}
});
```
## tlssocket.getProtocol
getProtocol():Promise\<string>;
在TLSSocket通信连接之后,获取通信的协议,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**返回值:**
| 类型 | 说明 |
| -------------- | -------------------- |
| Promise\<string> | 以Promise形式返回通信的协议。 |
**示例:**
```js
tlssocket.getProtocol().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getCipherSuites
getCipherSuites(callback: AsyncCallback\<Array\<string>>): void;
在TLSSocket通信连接之后,获取通信双方支持的加密套件,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<Array\<string>> | 是 | 回调函数,返回通信双方支持的加密套件。 |
**示例:**
```js
tlssocket.getCipherSuites((err, data) => {
if (err) {
console.log("getCipherSuites callback error = " + err);
} else {
console.log("getCipherSuites callback = " + data);
}
});
```
## tlssocket.getCipherSuites
getCipherSuites(): Promise\<Array\<string>>;
在TLSSocket通信连接之后,获取通信双方支持的加密套件,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**返回值:**
| 类型 | 说明 |
| ---------------------- | --------------------- |
| Promise\<Array\<string>> | 以Promise形式返回通信双方支持的加密套件。 |
**示例:**
```js
tlssocket.getCipherSuites().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getSignatureAlgorithms
getSignatureAlgorithms(callback: AsyncCallback\<Array\<string>>): void;
在TLSSocket通信连接之后,获取通信双方支持的签名算法,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<Array\<string>> | 是 | 回调函数,返回双方支持的签名算法。 |
**示例:**
```js
tlssocket.getSignatureAlgorithms((err, data) => {
if (err) {
console.log("getSignatureAlgorithms callback error = " + err);
} else {
console.log("getSignatureAlgorithms callback = " + data);
}
});
```
## tlssocket.getSignatureAlgorithms
getSignatureAlgorithms(): Promise\<Array\<string>>;
在TLSSocket通信连接之后,获取通信双方支持的签名算法,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**返回值:**
| 类型 | 说明 |
| ---------------------- | -------------------- |
| Promise\<Array\<string>> | 以Promise形式返回获取到的双方支持的签名算法。 |
**示例:**
```js
tlssocket.getSignatureAlgorithms().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.close
close(callback: AsyncCallback\<void>): void;
在TLSSocket通信连接之后,断开连接,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -----------------------------| ---- | ---------------|
| callback | AsyncCallback\<void> | 是 | 回调函数,返回TLSSocket关闭连接的结果。 |
**示例:**
```js
tlssocket.close((err) => {
if (err) {
console.log("close callback error = " + err);
} else {
console.log("close success");
}
});
```
## tlssocket.close
close(): Promise\<void>;
在TLSSocket通信连接之后,断开连接,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**返回值:**
| 类型 | 说明 |
| -------------- | -------------------- |
| Promise\<void> | 以Promise形式返回,返回TLSSocket关闭连接的结果。 |
**示例:**
```js
tlssocket.close().then(() =>
console.log("close success");
}).catch(err => {
console.error(err);
});
```
## TLSConnectOptions
TLS连接的操作。
**系统能力**:SystemCapability.Communication.NetStack
| 参数名 | 类型 | 说明 |
| -------------- | ------------------------------------- | -------------- |
| address | [NetAddress](#netaddress) | 网关地址。 |
| secureOptions | [TLSSecureOptions](#tlssecureoptions) | TLS安全相关操作。|
| ALPNProtocols | Array\<string> | ALPN协议。 |
## NetAddress
网络地址。
**系统能力**:SystemCapability.Communication.NetStack
| 参数名 | 类型 | 说明 |
| ------- | ------ | ---------------------------- |
| address | string | 地址。 |
| family | number | IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port | number | 端口,取值范围\[0, 65535]。 |
## TLSSecureOptions
TLS安全相关操作。
**系统能力**:SystemCapability.Communication.NetStack
| 参数名 | 类型 | 说明 |
| --------------------- | ---------------------- | ---------------------- |
| ca | string \| Array\<string> | ca证书。 |
| cert | string | 本地数字证书。 |
| key | string | 本地数字证书私钥。 |
| passwd | string | 密码。 |
| protocols | string | 协议名。 |
| useRemoteCipherPrefer | boolean | 优先使用对等方的密码套件。 |
| signatureAlgorithms | string | 设置签名算法。 |
| cipherSuites | string | 加密套件。 |
\ No newline at end of file
......@@ -3,7 +3,7 @@
## 简介<a name="section104mcpsimp"></a>
网络管理子系统,作为设备联网的必备组件,提供了对不同类型网络连接的统一管理,并提供了网络协议栈能力。应用可以通过调用API来获取数据网络的连接信息,查询和订阅数据网络的连接状态等,并可通过网络协议栈进行数据传输。
网络管理子系统,作为设备联网的必备组件,提供了对不同类型网络连接的统一管理、流量管理、策略管理、网络共享,并提供了网络协议栈能力。应用可以通过调用API来获取数据网络的连接信息,查询和订阅数据网络的连接状态,网络流量数据,网路策略以及网络共享等,并可通过网络协议栈进行数据传输。
下图所示为网络管理子系统架构图。各个部件主要作用如下:
......@@ -67,6 +67,26 @@ foundation/communication/
conn.unregister((err, data) => {});
```
### 网路共享<a name="section2458213210369"></a>
1. 从@ohos.net.sharing中导入sharing命名空间。
2. 设定共享类型
3. 开始共享
4. 止共享
```
// 引入包名
import sharing from '@ohos.net.sharing';
// 设定共享类型
this.sharingType = 0; // 0: WIFI 1: USB 2: BLUETOOTH
// 开始共享
sharing.startSharing(this.sharingType,(err)=>{
this.callBack(err);
})
// 停止共享
sharing.stopSharing(this.sharingType,(err)=>{
this.callBack(err);
})
```
### 发起网络请求<a name="section750135512369"></a>
......@@ -119,13 +139,10 @@ httpRequest.request(
);
```
## 相关仓<a name="section152mcpsimp"></a>
**网络管理子系统**
[communication_netmanager_base](https://gitee.com/openharmony/communication_netmanager_base/blob/master/README_zh.md)
[communication_netmanager_ext](https://gitee.com/openharmony/communication_netmanager_ext/blob/master/README_zh.md)
[communication_netstack](https://gitee.com/openharmony/communication_netstack/blob/master/README_zh.md)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册