網(wǎng)絡(luò)連接管理

2024-01-23 16:40 更新

網(wǎng)絡(luò)連接管理提供管理網(wǎng)絡(luò)一些基礎(chǔ)能力,包括獲取默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò)、獲取所有激活數(shù)據(jù)網(wǎng)絡(luò)列表、開(kāi)啟關(guān)閉飛行模式、獲取網(wǎng)絡(luò)能力信息等功能。

說(shuō)明

本模塊首批接口從API version 8開(kāi)始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import connection from '@ohos.net.connection'

connection.createNetConnection

createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection

返回一個(gè)NetConnection對(duì)象,netSpecifier指定關(guān)注的網(wǎng)絡(luò)的各項(xiàng)特征,timeout是超時(shí)時(shí)間(單位是毫秒),netSpecifier是timeout的必要條件,兩者都沒(méi)有則表示關(guān)注默認(rèn)網(wǎng)絡(luò)。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netSpecifier

NetSpecifier

指定網(wǎng)絡(luò)的各項(xiàng)特征,不指定則關(guān)注默認(rèn)網(wǎng)絡(luò)。

timeout

number

獲取netSpecifier指定的網(wǎng)絡(luò)時(shí)的超時(shí)時(shí)間,僅netSpecifier存在時(shí)生效,undefined時(shí)默認(rèn)值為0。

返回值:

類型

說(shuō)明

NetConnection

所關(guān)注的網(wǎng)絡(luò)的句柄。

示例:

  1. // 關(guān)注默認(rèn)網(wǎng)絡(luò)
  2. let netConnection = connection.createNetConnection()
  3. // 關(guān)注蜂窩網(wǎng)絡(luò)
  4. let netConnectionCellular = connection.createNetConnection({
  5. netCapabilities: {
  6. bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
  7. }
  8. })

connection.getDefaultNet

getDefaultNet(callback: AsyncCallback<NetHandle>): void

獲取默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò),使用callback方式作為異步方法??梢允褂?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >getNetCapabilities去獲取網(wǎng)絡(luò)的類型、擁有的能力等信息。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<NetHandle>

回調(diào)函數(shù)。當(dāng)成功獲取默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò)時(shí),err為undefined,data為默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò);否則為錯(cuò)誤對(duì)象

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet(function (error, data) {
  2. console.log(JSON.stringify(error))
  3. console.log(JSON.stringify(data))
  4. })

connection.getDefaultNet

getDefaultNet(): Promise<NetHandle>

獲取默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò),使用Promise方式作為異步方法??梢允褂?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >getNetCapabilities去獲取網(wǎng)絡(luò)的類型、擁有的能力等信息。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

返回值:

類型

說(shuō)明

Promise<NetHandle>

以Promise形式返回默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò)。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (data) {
  2. console.log(JSON.stringify(data))
  3. })

connection.getDefaultNetSync9+

getDefaultNetSync(): NetHandle

使用同步方法獲取默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò)??梢允褂?a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >getNetCapabilities去獲取網(wǎng)絡(luò)的類型、擁有的能力等信息。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

返回值:

類型

說(shuō)明

NetHandle

以同步方式返回默認(rèn)激活的數(shù)據(jù)網(wǎng)絡(luò)。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. let netHandle = connection.getDefaultNetSync();

connection.getAppNet9+

getAppNet(callback: AsyncCallback<NetHandle>): void

獲取App綁定的網(wǎng)絡(luò)信息,使用callback方式作為異步方法。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<NetHandle>

回調(diào)函數(shù)。當(dāng)成功獲取App綁定的網(wǎng)絡(luò)信息時(shí),err為undefined,data為獲取到App綁定的網(wǎng)絡(luò)信息;否則為錯(cuò)誤對(duì)象

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getAppNet(function(error, data) {
  2. console.log(JSON.stringify(error))
  3. console.log(JSON.stringify(data))
  4. })

connection.getAppNet9+

getAppNet(): Promise<NetHandle>;

獲取App綁定的網(wǎng)絡(luò)信息,使用Promise方式作為異步方法。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

返回值:

類型

說(shuō)明

Promise<NetHandle>

以Promise形式返回App綁定的網(wǎng)絡(luò)信息。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getAppNet().then((data) => {
  2. console.info(JSON.stringify(data));
  3. }).catch(error => {
  4. console.info(JSON.stringify(error));
  5. })

connection.SetAppNet9+

setAppNet(netHandle: NetHandle, callback: AsyncCallback<void>): void

綁定App到指定網(wǎng)絡(luò),綁定后的App只能通過(guò)指定網(wǎng)絡(luò)訪問(wèn)外網(wǎng),使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)成功綁定App到指定網(wǎng)絡(luò)時(shí),err為undefined,否則為錯(cuò)誤對(duì)象

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet(function (error, netHandle) {
  2. connection.setAppNet(netHandle, (error, data) => {
  3. console.log(JSON.stringify(error))
  4. console.log(JSON.stringify(data))
  5. });
  6. })

connection.SetAppNet9+

setAppNet(netHandle: NetHandle): Promise<void>;

綁定App到指定網(wǎng)絡(luò),綁定后的App只能通過(guò)指定網(wǎng)絡(luò)訪問(wèn)外網(wǎng),使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

返回值:

類型

說(shuō)明

Promise<void>

無(wú)返回值的Promise對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.setAppNet(netHandle).then((error, data) => {
  3. console.log(JSON.stringify(data))
  4. }).catch(error => {
  5. console.log(JSON.stringify(error))
  6. })
  7. })

connection.getAllNets

getAllNets(callback: AsyncCallback<Array<NetHandle>>): void

獲取所有處于連接狀態(tài)的網(wǎng)絡(luò)列表,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<Array<NetHandle>>

回調(diào)函數(shù)。當(dāng)成功獲取所有處于連接狀態(tài)的網(wǎng)絡(luò)列表時(shí),err為undefined,data為激活的數(shù)據(jù)網(wǎng)絡(luò)列表;否則為錯(cuò)誤對(duì)象

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getAllNets(function (error, data) {
  2. console.log(JSON.stringify(error))
  3. console.log(JSON.stringify(data))
  4. });

connection.getAllNets

getAllNets(): Promise<Array<NetHandle>>

獲取所有處于連接狀態(tài)的網(wǎng)絡(luò)列表,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

返回值:

類型

說(shuō)明

Promise<Array<NetHandle>>

以Promise形式返回激活的數(shù)據(jù)網(wǎng)絡(luò)列表。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getAllNets().then(function (data) {
  2. console.log(JSON.stringify(data))
  3. });

connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback<ConnectionProperties>): void

獲取netHandle對(duì)應(yīng)的網(wǎng)絡(luò)的連接信息,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

callback

AsyncCallback<ConnectionProperties>

回調(diào)函數(shù)。當(dāng)成功獲取netHandle對(duì)應(yīng)的網(wǎng)絡(luò)的連接信息時(shí),err為undefined,data為獲取的網(wǎng)絡(luò)連接信息;否則為錯(cuò)誤對(duì)象

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.getConnectionProperties(netHandle, function (error, data) {
  3. console.log(JSON.stringify(error))
  4. console.log(JSON.stringify(data))
  5. })
  6. })

connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle): Promise<ConnectionProperties>

獲取netHandle對(duì)應(yīng)的網(wǎng)絡(luò)的連接信息,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

返回值:

類型

說(shuō)明

Promise<ConnectionProperties>

以Promise形式返回網(wǎng)絡(luò)的連接信息。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.getConnectionProperties(netHandle).then(function (data) {
  3. console.log(JSON.stringify(data))
  4. })
  5. })

connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback<NetCapabilities>): void

獲取netHandle對(duì)應(yīng)的網(wǎng)絡(luò)的能力信息,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

callback

AsyncCallback<NetCapabilities>

回調(diào)函數(shù)。當(dāng)成功獲取netHandle對(duì)應(yīng)的網(wǎng)絡(luò)的能力信息時(shí),err為undefined,data為獲取到的網(wǎng)絡(luò)能力信息;否則為錯(cuò)誤對(duì)象

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.getNetCapabilities(netHandle, function (error, data) {
  3. console.log(JSON.stringify(error))
  4. console.log(JSON.stringify(data))
  5. })
  6. })

connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle): Promise<NetCapabilities>

獲取netHandle對(duì)應(yīng)的網(wǎng)絡(luò)的能力信息,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

返回值:

類型

說(shuō)明

Promise<NetCapabilities>

以Promise形式返回網(wǎng)絡(luò)的能力信息。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.getNetCapabilities(netHandle).then(function (data) {
  3. console.log(JSON.stringify(data))
  4. })
  5. })

connection.isDefaultNetMetered9+

isDefaultNetMetered(callback: AsyncCallback<boolean>): void

檢查當(dāng)前網(wǎng)絡(luò)上的數(shù)據(jù)流量使用是否被計(jì)量,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。當(dāng)前網(wǎng)絡(luò)上的數(shù)據(jù)流量使用被計(jì)量返回true。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.isDefaultNetMetered(function (error, data) {
  2. console.log(JSON.stringify(error))
  3. console.log('data: ' + data)
  4. })

connection.isDefaultNetMetered9+

isDefaultNetMetered(): Promise<boolean>

檢查當(dāng)前網(wǎng)絡(luò)上的數(shù)據(jù)流量使用是否被計(jì)量,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

返回值:

類型

說(shuō)明

Promise<boolean>

以Promise形式返回,當(dāng)前網(wǎng)絡(luò)上的數(shù)據(jù)流量使用被計(jì)量true。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.isDefaultNetMetered().then(function (data) {
  2. console.log('data: ' + data)
  3. })

connection.hasDefaultNet

hasDefaultNet(callback: AsyncCallback<boolean>): void

檢查默認(rèn)數(shù)據(jù)網(wǎng)絡(luò)是否被激活,使用callback方式作為異步方法。如果有默認(rèn)數(shù)據(jù)網(wǎng)路,可以使用getDefaultNet去獲取。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<boolean>

回調(diào)函數(shù)。默認(rèn)數(shù)據(jù)網(wǎng)絡(luò)被激活返回true。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.hasDefaultNet(function (error, data) {
  2. console.log(JSON.stringify(error))
  3. console.log('data: ' + data)
  4. })

connection.hasDefaultNet

hasDefaultNet(): Promise<boolean>

檢查默認(rèn)數(shù)據(jù)網(wǎng)絡(luò)是否被激活,使用Promise方式作為異步方法。如果有默認(rèn)數(shù)據(jù)網(wǎng)路,可以使用getDefaultNet去獲取。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

返回值:

類型

說(shuō)明

Promise<boolean>

以Promise形式返回,默認(rèn)數(shù)據(jù)網(wǎng)絡(luò)被激活返回true。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.hasDefaultNet().then(function (data) {
  2. console.log('data: ' + data)
  3. })

connection.reportNetConnected

reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void

向網(wǎng)絡(luò)管理報(bào)告網(wǎng)絡(luò)處于可用狀態(tài),調(diào)用此接口說(shuō)明應(yīng)用程序認(rèn)為網(wǎng)絡(luò)的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)與網(wǎng)絡(luò)管理不一致。

使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄,參考NetHandle

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)向網(wǎng)絡(luò)管理報(bào)告網(wǎng)絡(luò)處于可用狀態(tài)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.reportNetConnected(netHandle, function (error) {
  3. console.log(JSON.stringify(error))
  4. });
  5. });

connection.reportNetConnected

reportNetConnected(netHandle: NetHandle): Promise<void>

向網(wǎng)絡(luò)管理報(bào)告網(wǎng)絡(luò)處于可用狀態(tài),調(diào)用此接口說(shuō)明應(yīng)用程序認(rèn)為網(wǎng)絡(luò)的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)與網(wǎng)絡(luò)管理不一致。

使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄,參考NetHandle

返回值:

類型

說(shuō)明

Promise<void>

無(wú)返回值的Promise對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.reportNetConnected(netHandle).then(function () {
  3. console.log(`report success`)
  4. });
  5. });

connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void

向網(wǎng)絡(luò)管理報(bào)告網(wǎng)絡(luò)處于不可用狀態(tài),調(diào)用此接口說(shuō)明應(yīng)用程序認(rèn)為網(wǎng)絡(luò)的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)與網(wǎng)絡(luò)管理不一致。

使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄,參考NetHandle。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)向網(wǎng)絡(luò)管理報(bào)告網(wǎng)絡(luò)處于不可用狀態(tài)成功,err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.reportNetDisconnected(netHandle, function (error) {
  3. console.log(JSON.stringify(error))
  4. });
  5. });

connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle): Promise<void>

向網(wǎng)絡(luò)管理報(bào)告網(wǎng)絡(luò)處于不可用狀態(tài),調(diào)用此接口說(shuō)明應(yīng)用程序認(rèn)為網(wǎng)絡(luò)的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)與網(wǎng)絡(luò)管理不一致。

使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

netHandle

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄,參考NetHandle。

返回值:

類型

說(shuō)明

Promise<void>

無(wú)返回值的Promise對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. connection.reportNetDisconnected(netHandle).then(function () {
  3. console.log(`report success`)
  4. });
  5. });

connection.getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback<Array<NetAddress>>): void

使用默認(rèn)網(wǎng)絡(luò)解析主機(jī)名以獲取所有IP地址,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

host

string

需要解析的主機(jī)名。

callback

AsyncCallback<Array<NetAddress>>

回調(diào)函數(shù)。當(dāng)使用默認(rèn)網(wǎng)絡(luò)解析主機(jī)名成功獲取所有IP地址,err為undefined,data為獲取到的所有IP地址;否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. let host = "xxxx";
  2. connection.getAddressesByName(host, function (error, data) {
  3. console.log(JSON.stringify(error))
  4. console.log(JSON.stringify(data))
  5. })

connection.getAddressesByName

getAddressesByName(host: string): Promise<Array<NetAddress>>

使用默認(rèn)網(wǎng)絡(luò)解析主機(jī)名以獲取所有IP地址,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

host

string

需要解析的主機(jī)名。

返回值:

類型

說(shuō)明

Promise<Array<NetAddress>>

以Promise形式返回所有IP地址。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. let host = "xxxx";
  2. connection.getAddressesByName(host).then(function (data) {
  3. console.log(JSON.stringify(data))
  4. })

NetConnection

網(wǎng)絡(luò)連接的句柄。

register

register(callback: AsyncCallback<void>): void

訂閱指定網(wǎng)絡(luò)狀態(tài)變化的通知。

需要權(quán)限:ohos.permission.GET_NETWORK_INFO

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)訂閱指定網(wǎng)絡(luò)狀態(tài)變化的通知成功,err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

2101008

The callback is not exists.

2101022

The number of requests exceeded the maximum.

示例:

  1. netConnection.register(function (error) {
  2. console.log(JSON.stringify(error))
  3. })

unregister

unregister(callback: AsyncCallback<void>): void

取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)取消訂閱指定網(wǎng)絡(luò)狀態(tài)變化的通知成功,err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

2101007

The same callback exists.

示例:

  1. netConnection.unregister(function (error) {
  2. console.log(JSON.stringify(error))
  3. })

on('netAvailable')

on(type: 'netAvailable', callback: Callback<NetHandle>): void

訂閱網(wǎng)絡(luò)可用事件。

模型約束:此接口調(diào)用之前需要先調(diào)用register接口,使用unregister取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

type

string

訂閱事件,固定為'netAvailable'。

netAvailable:數(shù)據(jù)網(wǎng)絡(luò)可用事件。

callback

Callback<NetHandle>

回調(diào)函數(shù),返回?cái)?shù)據(jù)網(wǎng)絡(luò)句柄。

示例:

  1. // 創(chuàng)建NetConnection對(duì)象
  2. let netCon = connection.createNetConnection()
  3. // 先使用register接口注冊(cè)訂閱事件
  4. netCon.register(function (error) {
  5. console.log(JSON.stringify(error))
  6. })
  7. // 訂閱網(wǎng)絡(luò)可用事件。調(diào)用register后,才能接收到此事件通知
  8. netCon.on('netAvailable', function (data) {
  9. console.log(JSON.stringify(data))
  10. })
  11. // 使用unregister接口取消訂閱
  12. netCon.unregister(function (error) {
  13. console.log(JSON.stringify(error))
  14. })

on('netBlockStatusChange')

on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void

訂閱網(wǎng)絡(luò)阻塞狀態(tài)事件,使用callback方式作為異步方法。

模型約束:此接口調(diào)用之前需要先調(diào)用register接口,使用unregister取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

type

string

訂閱事件,固定為'netBlockStatusChange'。

netBlockStatusChange:網(wǎng)絡(luò)阻塞狀態(tài)事件。

callback

Callback<{ netHandle: NetHandle, blocked: boolean }>

回調(diào)函數(shù),返回?cái)?shù)據(jù)網(wǎng)絡(luò)句柄(netHandle),及網(wǎng)絡(luò)堵塞狀態(tài)(blocked)。

示例:

  1. // 創(chuàng)建NetConnection對(duì)象
  2. let netCon = connection.createNetConnection()
  3. // 先使用register接口注冊(cè)訂閱事件
  4. netCon.register(function (error) {
  5. console.log(JSON.stringify(error))
  6. })
  7. // 訂閱網(wǎng)絡(luò)阻塞狀態(tài)事件。調(diào)用register后,才能接收到此事件通知
  8. netCon.on('netBlockStatusChange', function (data) {
  9. console.log(JSON.stringify(data))
  10. })
  11. // 使用unregister接口取消訂閱
  12. netCon.unregister(function (error) {
  13. console.log(JSON.stringify(error))
  14. })

on('netCapabilitiesChange')

on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void

訂閱網(wǎng)絡(luò)能力變化事件。

模型約束:此接口調(diào)用之前需要先調(diào)用register接口,使用unregister取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

type

string

訂閱事件,固定為'netCapabilitiesChange'。

netCapabilitiesChange:網(wǎng)絡(luò)能力變化事件。

callback

Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>

回調(diào)函數(shù),返回?cái)?shù)據(jù)網(wǎng)絡(luò)句柄(netHandle)和網(wǎng)絡(luò)的能力信息(netCap)。

示例:

  1. // 創(chuàng)建NetConnection對(duì)象
  2. let netCon = connection.createNetConnection()
  3. // 先使用register接口注冊(cè)訂閱事件
  4. netCon.register(function (error) {
  5. console.log(JSON.stringify(error))
  6. })
  7. // 訂閱網(wǎng)絡(luò)能力變化事件。調(diào)用register后,才能接收到此事件通知
  8. netCon.on('netCapabilitiesChange', function (data) {
  9. console.log(JSON.stringify(data))
  10. })
  11. // 使用unregister接口取消訂閱
  12. netCon.unregister(function (error) {
  13. console.log(JSON.stringify(error))
  14. })

on('netConnectionPropertiesChange')

on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void

訂閱網(wǎng)絡(luò)連接信息變化事件。

模型約束:此接口調(diào)用之前需要先調(diào)用register接口,使用unregister取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

type

string

訂閱事件,固定為'netConnectionPropertiesChange'。

netConnectionPropertiesChange:網(wǎng)絡(luò)連接信息變化事件。

callback

Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>

回調(diào)函數(shù),返回?cái)?shù)據(jù)網(wǎng)絡(luò)句柄(netHandle)和網(wǎng)絡(luò)的連接信息(connectionProperties)

示例:

  1. // 創(chuàng)建NetConnection對(duì)象
  2. let netCon = connection.createNetConnection()
  3. // 先使用register接口注冊(cè)訂閱事件
  4. netCon.register(function (error) {
  5. console.log(JSON.stringify(error))
  6. })
  7. // 訂閱網(wǎng)絡(luò)連接信息變化事件。調(diào)用register后,才能接收到此事件通知
  8. netCon.on('netConnectionPropertiesChange', function (data) {
  9. console.log(JSON.stringify(data))
  10. })
  11. // 使用unregister接口取消訂閱
  12. netCon.unregister(function (error) {
  13. console.log(JSON.stringify(error))
  14. })

on('netLost')

on(type: 'netLost', callback: Callback<NetHandle>): void

訂閱網(wǎng)絡(luò)丟失事件。

模型約束:此接口調(diào)用之前需要先調(diào)用register接口,使用unregister取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

type

string

訂閱事件,固定為'netLost'。

netLost:網(wǎng)絡(luò)嚴(yán)重中斷或正常斷開(kāi)事件。

callback

Callback<NetHandle>

回調(diào)函數(shù),數(shù)據(jù)網(wǎng)絡(luò)句柄(netHandle)

示例:

  1. // 創(chuàng)建NetConnection對(duì)象
  2. let netCon = connection.createNetConnection()
  3. // 先使用register接口注冊(cè)訂閱事件
  4. netCon.register(function (error) {
  5. console.log(JSON.stringify(error))
  6. })
  7. // 訂閱網(wǎng)絡(luò)丟失事件。調(diào)用register后,才能接收到此事件通知
  8. netCon.on('netLost', function (data) {
  9. console.log(JSON.stringify(data))
  10. })
  11. // 使用unregister接口取消訂閱
  12. netCon.unregister(function (error) {
  13. console.log(JSON.stringify(error))
  14. })

on('netUnavailable')

on(type: 'netUnavailable', callback: Callback<void>): void

訂閱網(wǎng)絡(luò)不可用事件。

模型約束:此接口調(diào)用之前需要先調(diào)用register接口,使用unregister取消訂閱默認(rèn)網(wǎng)絡(luò)狀態(tài)變化的通知。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

type

string

訂閱事件,固定為'netUnavailable'。

netUnavailable:網(wǎng)絡(luò)不可用事件。

callback

Callback<void>

回調(diào)函數(shù),無(wú)返回結(jié)果。

示例:

  1. // 創(chuàng)建NetConnection對(duì)象
  2. let netCon = connection.createNetConnection()
  3. // 先使用register接口注冊(cè)訂閱事件
  4. netCon.register(function (error) {
  5. console.log(JSON.stringify(error))
  6. })
  7. // 訂閱網(wǎng)絡(luò)不可用事件。調(diào)用register后,才能接收到此事件通知
  8. netCon.on('netUnavailable', function (data) {
  9. console.log(JSON.stringify(data))
  10. })
  11. // 使用unregister接口取消訂閱
  12. netCon.unregister(function (error) {
  13. console.log(JSON.stringify(error))
  14. })

NetHandle

數(shù)據(jù)網(wǎng)絡(luò)的句柄。

在調(diào)用NetHandle的方法之前,需要先獲取NetHandle對(duì)象。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

屬性

名稱

類型

必填

說(shuō)明

netId

number

網(wǎng)絡(luò)ID,取值為0代表沒(méi)有默認(rèn)網(wǎng)絡(luò),其余取值必須大于等于100。

bindSocket9+

bindSocket(socketParam: TCPSocket | UDPSocket, callback: AsyncCallback<void>): void

將TCPSocket或UDPSocket綁定到當(dāng)前網(wǎng)絡(luò),使用callback方式作為異步方法。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

socketParam

TCPSocket | UDPSocket

待綁定的TCPSocket或UDPSocket對(duì)象。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當(dāng)TCPSocket或UDPSocket成功綁定到當(dāng)前網(wǎng)絡(luò),err為undefined,否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. import socket from "@ohos.net.socket";
  2. connection.getDefaultNet().then((netHandle) => {
  3. var tcp = socket.constructTCPSocketInstance();
  4. var udp = socket.constructUDPSocketInstance();
  5. let socketType = "TCPSocket";
  6. if (socketType == "TCPSocket") {
  7. tcp.bind({
  8. address: '192.168.xx.xxx', port: 8080, family: 1
  9. }, error => {
  10. if (error) {
  11. console.log('bind fail');
  12. }
  13. netHandle.bindSocket(tcp, (error, data) => {
  14. if (error) {
  15. console.log(JSON.stringify(error));
  16. } else {
  17. console.log(JSON.stringify(data));
  18. }
  19. })
  20. })
  21. } else {
  22. let callback = value => {
  23. console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
  24. }
  25. udp.on('message', callback);
  26. udp.bind({
  27. address: '192.168.xx.xxx', port: 8080, family: 1
  28. }, error => {
  29. if (error) {
  30. console.log('bind fail');
  31. }
  32. udp.on('message', (data) => {
  33. console.log(JSON.stringify(data))
  34. });
  35. netHandle.bindSocket(udp, (error, data) => {
  36. if (error) {
  37. console.log(JSON.stringify(error));
  38. } else {
  39. console.log(JSON.stringify(data));
  40. }
  41. })
  42. })
  43. }
  44. })

bindSocket9+

bindSocket(socketParam: TCPSocket | UDPSocket): Promise<void>;

將TCPSocket或UDPSockett綁定到當(dāng)前網(wǎng)絡(luò),使用Promise方式作為異步方法。

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

socketParam

TCPSocket | UDPSocket

待綁定的TCPSocket或UDPSocket對(duì)象。

返回值:

類型

說(shuō)明

Promise<void>

無(wú)返回值的Promise對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. import socket from "@ohos.net.socket";
  2. connection.getDefaultNet().then((netHandle) => {
  3. var tcp = socket.constructTCPSocketInstance();
  4. var udp = socket.constructUDPSocketInstance();
  5. let socketType = "TCPSocket";
  6. if (socketType == "TCPSocket") {
  7. tcp.bind({
  8. address: '192.168.xx.xxx', port: 8080, family: 1
  9. }, error => {
  10. if (error) {
  11. console.log('bind fail');
  12. }
  13. netHandle.bindSocket(tcp).then((data) => {
  14. console.log(JSON.stringify(data));
  15. }).catch(error => {
  16. console.log(JSON.stringify(error));
  17. })
  18. })
  19. } else {
  20. let callback = value => {
  21. console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
  22. }
  23. udp.on('message', callback);
  24. udp.bind({
  25. address: '192.168.xx.xxx', port: 8080, family: 1
  26. }, error => {
  27. if (error) {
  28. console.log('bind fail');
  29. }
  30. udp.on('message', (data) => {
  31. console.log(JSON.stringify(data));
  32. })
  33. netHandle.bindSocket(udp).then((data) => {
  34. console.log(JSON.stringify(data));
  35. }).catch(error => {
  36. console.log(JSON.stringify(error));
  37. })
  38. })
  39. }
  40. })

getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback<Array<NetAddress>>): void

使用對(duì)應(yīng)網(wǎng)絡(luò)解析主機(jī)名以獲取所有IP地址,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

host

string

需要解析的主機(jī)名。

callback

AsyncCallback<Array<NetAddress>>

回調(diào)函數(shù)。當(dāng)使用對(duì)應(yīng)網(wǎng)絡(luò)解析主機(jī)名成功獲取所有IP地址,err為undefined,data為獲取到的所有IP地址;否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. let host = "xxxx";
  3. netHandle.getAddressesByName(host, function (error, data) {
  4. console.log(JSON.stringify(error))
  5. console.log(JSON.stringify(data))
  6. })
  7. })

getAddressesByName

getAddressesByName(host: string): Promise<Array<NetAddress>>

使用對(duì)應(yīng)網(wǎng)絡(luò)解析主機(jī)名以獲取所有IP地址,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

host

string

需要解析的主機(jī)名。

返回值:

類型

說(shuō)明

Promise<Array<NetAddress>>

以Promise形式返回所有IP地址。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. let host = "xxxx";
  3. netHandle.getAddressesByName(host).then(function (data) {
  4. console.log(JSON.stringify(data))
  5. })
  6. })

getAddressByName

getAddressByName(host: string, callback: AsyncCallback<NetAddress>): void

使用對(duì)應(yīng)網(wǎng)絡(luò)解析主機(jī)名以獲取第一個(gè)IP地址,使用callback方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

host

string

需要解析的主機(jī)名。

callback

AsyncCallback<NetAddress>

回調(diào)函數(shù)。當(dāng)使用對(duì)應(yīng)網(wǎng)絡(luò)解析主機(jī)名獲取第一個(gè)IP地址成功,err為undefined,data為獲取的第一個(gè)IP地址;否則為錯(cuò)誤對(duì)象。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. let host = "xxxx";
  3. netHandle.getAddressByName(host, function (error, data) {
  4. console.log(JSON.stringify(error))
  5. console.log(JSON.stringify(data))
  6. })
  7. })

getAddressByName

getAddressByName(host: string): Promise<NetAddress>

使用對(duì)應(yīng)網(wǎng)絡(luò)解析主機(jī)名以獲取第一個(gè)IP地址,使用Promise方式作為異步方法。

需要權(quán)限:ohos.permission.INTERNET

系統(tǒng)能力:SystemCapability.Communication.NetManager.Core

參數(shù):

參數(shù)名

類型

必填

說(shuō)明

host

string

需要解析的主機(jī)名。

返回值:

類型

說(shuō)明

Promise<NetAddress>

以Promise形式返回第一個(gè)IP地址。

錯(cuò)誤碼:

錯(cuò)誤碼ID

錯(cuò)誤信息

201

Permission denied.

401

Parameter error.

2100001

Invalid parameter value.

2100002

Operation failed. Cannot connect to service.

2100003

System internal error.

示例:

  1. connection.getDefaultNet().then(function (netHandle) {
  2. let host = "xxxx";
  3. netHandle.getAddressByName(host).then(function (data) {
  4. console.log(JSON.stringify(data))
  5. })
  6. })

NetCap

網(wǎng)絡(luò)具體能力。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

說(shuō)明

NET_CAPABILITY_MMS

0

表示網(wǎng)絡(luò)可以訪問(wèn)運(yùn)營(yíng)商的MMSC(Multimedia Message Service,多媒體短信服務(wù))發(fā)送和接收彩信。

NET_CAPABILITY_NOT_METERED

11

表示網(wǎng)絡(luò)流量未被計(jì)費(fèi)。

NET_CAPABILITY_INTERNET

12

表示該網(wǎng)絡(luò)應(yīng)具有訪問(wèn)Internet的能力,該能力由網(wǎng)絡(luò)提供者設(shè)置。

NET_CAPABILITY_NOT_VPN

15

表示網(wǎng)絡(luò)不使用VPN(Virtual Private Network,虛擬專用網(wǎng)絡(luò))。

NET_CAPABILITY_VALIDATED

16

表示該網(wǎng)絡(luò)訪問(wèn)Internet的能力被網(wǎng)絡(luò)管理成功驗(yàn)證,該能力由網(wǎng)絡(luò)管理模塊設(shè)置。

NetBearType

網(wǎng)絡(luò)類型。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

說(shuō)明

BEARER_CELLULAR

0

蜂窩網(wǎng)絡(luò)。

BEARER_WIFI

1

Wi-Fi網(wǎng)絡(luò)。

BEARER_ETHERNET

3

以太網(wǎng)網(wǎng)絡(luò)。

NetSpecifier

提供承載數(shù)據(jù)網(wǎng)絡(luò)能力的實(shí)例。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

類型

必填

說(shuō)明

netCapabilities

NetCapabilities

存儲(chǔ)數(shù)據(jù)網(wǎng)絡(luò)的傳輸能力和承載類型。

bearerPrivateIdentifier

string

網(wǎng)絡(luò)標(biāo)識(shí)符,Wi-Fi網(wǎng)絡(luò)的標(biāo)識(shí)符是"wifi",蜂窩網(wǎng)絡(luò)的標(biāo)識(shí)符是"slot0"(對(duì)應(yīng)SIM卡1)。

NetCapabilities

網(wǎng)絡(luò)的能力集。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

類型

必填

說(shuō)明

linkUpBandwidthKbps

number

上行(設(shè)備到網(wǎng)絡(luò))帶寬。

linkDownBandwidthKbps

number

下行(網(wǎng)絡(luò)到設(shè)備)帶寬。

networkCap

Array<NetCap>

網(wǎng)絡(luò)具體能力。

bearerTypes

Array<NetBearType>

網(wǎng)絡(luò)類型。

ConnectionProperties

網(wǎng)絡(luò)連接信息。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

類型

必填

說(shuō)明

interfaceName

string

網(wǎng)卡名稱。

domains

string

所屬域,默認(rèn)""。

linkAddresses

Array<LinkAddress>

鏈路信息。

routes

Array<RouteInfo>

路由信息。

dnses

Array<NetAddress>

網(wǎng)絡(luò)地址,參考NetAddress。

mtu

number

最大傳輸單元。

RouteInfo

網(wǎng)絡(luò)路由信息。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

類型

必填

說(shuō)明

interface

string

網(wǎng)卡名稱。

destination

LinkAddress

目的地址。

gateway

NetAddress

網(wǎng)關(guān)地址。

hasGateway

boolean

是否有網(wǎng)關(guān)。

isDefaultRoute

boolean

是否為默認(rèn)路由。

LinkAddress

網(wǎng)絡(luò)鏈路信息。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

類型

必填

說(shuō)明

address

NetAddress

鏈路地址。

prefixLength

number

鏈路地址前綴的長(zhǎng)度。

NetAddress

網(wǎng)絡(luò)地址。

系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。

名稱

類型

必填

說(shuō)明

address

string

地址。

family

number

IPv4 = 1,IPv6 = 2,默認(rèn)IPv4。

port

number

端口,取值范圍[0, 65535]。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)