網(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ò)能力信息等功能。
本模塊首批接口從API version 8開(kāi)始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
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 | 否 | 指定網(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ō)明 |
---|---|
所關(guān)注的網(wǎng)絡(luò)的句柄。 |
示例:
- // 關(guān)注默認(rèn)網(wǎng)絡(luò)
- let netConnection = connection.createNetConnection()
- // 關(guān)注蜂窩網(wǎng)絡(luò)
- let netConnectionCellular = connection.createNetConnection({
- netCapabilities: {
- bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
- }
- })
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. |
示例:
- connection.getDefaultNet(function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
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. |
示例:
- connection.getDefaultNet().then(function (data) {
- console.log(JSON.stringify(data))
- })
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. |
示例:
- let netHandle = connection.getDefaultNetSync();
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. |
示例:
- connection.getAppNet(function(error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
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. |
示例:
- connection.getAppNet().then((data) => {
- console.info(JSON.stringify(data));
- }).catch(error => {
- console.info(JSON.stringify(error));
- })
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 | 是 | 數(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. |
示例:
- connection.getDefaultNet(function (error, netHandle) {
- connection.setAppNet(netHandle, (error, data) => {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- });
- })
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 | 是 | 數(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.setAppNet(netHandle).then((error, data) => {
- console.log(JSON.stringify(data))
- }).catch(error => {
- console.log(JSON.stringify(error))
- })
- })
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. |
示例:
- connection.getAllNets(function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- });
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. |
示例:
- connection.getAllNets().then(function (data) {
- console.log(JSON.stringify(data))
- });
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 | 是 | 數(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.getConnectionProperties(netHandle, function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
- })
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 | 是 | 數(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.getConnectionProperties(netHandle).then(function (data) {
- console.log(JSON.stringify(data))
- })
- })
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 | 是 | 數(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.getNetCapabilities(netHandle, function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
- })
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 | 是 | 數(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.getNetCapabilities(netHandle).then(function (data) {
- console.log(JSON.stringify(data))
- })
- })
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. |
示例:
- connection.isDefaultNetMetered(function (error, data) {
- console.log(JSON.stringify(error))
- console.log('data: ' + data)
- })
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. |
示例:
- connection.isDefaultNetMetered().then(function (data) {
- console.log('data: ' + data)
- })
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. |
示例:
- connection.hasDefaultNet(function (error, data) {
- console.log(JSON.stringify(error))
- console.log('data: ' + data)
- })
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. |
示例:
- connection.hasDefaultNet().then(function (data) {
- console.log('data: ' + data)
- })
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ù):
錯(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.reportNetConnected(netHandle, function (error) {
- console.log(JSON.stringify(error))
- });
- });
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ù):
返回值:
類型 | 說(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.reportNetConnected(netHandle).then(function () {
- console.log(`report success`)
- });
- });
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ù):
錯(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.reportNetDisconnected(netHandle, function (error) {
- console.log(JSON.stringify(error))
- });
- });
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ù):
返回值:
類型 | 說(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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- connection.reportNetDisconnected(netHandle).then(function () {
- console.log(`report success`)
- });
- });
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. |
示例:
- let host = "xxxx";
- connection.getAddressesByName(host, function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
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. |
示例:
- let host = "xxxx";
- connection.getAddressesByName(host).then(function (data) {
- console.log(JSON.stringify(data))
- })
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. |
示例:
- netConnection.register(function (error) {
- console.log(JSON.stringify(error))
- })
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. |
示例:
- netConnection.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
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ò)句柄。 |
示例:
- // 創(chuàng)建NetConnection對(duì)象
- let netCon = connection.createNetConnection()
- // 先使用register接口注冊(cè)訂閱事件
- netCon.register(function (error) {
- console.log(JSON.stringify(error))
- })
- // 訂閱網(wǎng)絡(luò)可用事件。調(diào)用register后,才能接收到此事件通知
- netCon.on('netAvailable', function (data) {
- console.log(JSON.stringify(data))
- })
- // 使用unregister接口取消訂閱
- netCon.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
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)。 |
示例:
- // 創(chuàng)建NetConnection對(duì)象
- let netCon = connection.createNetConnection()
- // 先使用register接口注冊(cè)訂閱事件
- netCon.register(function (error) {
- console.log(JSON.stringify(error))
- })
- // 訂閱網(wǎng)絡(luò)阻塞狀態(tài)事件。調(diào)用register后,才能接收到此事件通知
- netCon.on('netBlockStatusChange', function (data) {
- console.log(JSON.stringify(data))
- })
- // 使用unregister接口取消訂閱
- netCon.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
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)。 |
示例:
- // 創(chuàng)建NetConnection對(duì)象
- let netCon = connection.createNetConnection()
- // 先使用register接口注冊(cè)訂閱事件
- netCon.register(function (error) {
- console.log(JSON.stringify(error))
- })
- // 訂閱網(wǎng)絡(luò)能力變化事件。調(diào)用register后,才能接收到此事件通知
- netCon.on('netCapabilitiesChange', function (data) {
- console.log(JSON.stringify(data))
- })
- // 使用unregister接口取消訂閱
- netCon.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
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) |
示例:
- // 創(chuàng)建NetConnection對(duì)象
- let netCon = connection.createNetConnection()
- // 先使用register接口注冊(cè)訂閱事件
- netCon.register(function (error) {
- console.log(JSON.stringify(error))
- })
- // 訂閱網(wǎng)絡(luò)連接信息變化事件。調(diào)用register后,才能接收到此事件通知
- netCon.on('netConnectionPropertiesChange', function (data) {
- console.log(JSON.stringify(data))
- })
- // 使用unregister接口取消訂閱
- netCon.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
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) |
示例:
- // 創(chuàng)建NetConnection對(duì)象
- let netCon = connection.createNetConnection()
- // 先使用register接口注冊(cè)訂閱事件
- netCon.register(function (error) {
- console.log(JSON.stringify(error))
- })
- // 訂閱網(wǎng)絡(luò)丟失事件。調(diào)用register后,才能接收到此事件通知
- netCon.on('netLost', function (data) {
- console.log(JSON.stringify(data))
- })
- // 使用unregister接口取消訂閱
- netCon.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
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é)果。 |
示例:
- // 創(chuàng)建NetConnection對(duì)象
- let netCon = connection.createNetConnection()
- // 先使用register接口注冊(cè)訂閱事件
- netCon.register(function (error) {
- console.log(JSON.stringify(error))
- })
- // 訂閱網(wǎng)絡(luò)不可用事件。調(diào)用register后,才能接收到此事件通知
- netCon.on('netUnavailable', function (data) {
- console.log(JSON.stringify(data))
- })
- // 使用unregister接口取消訂閱
- netCon.unregister(function (error) {
- console.log(JSON.stringify(error))
- })
數(shù)據(jù)網(wǎng)絡(luò)的句柄。
在調(diào)用NetHandle的方法之前,需要先獲取NetHandle對(duì)象。
系統(tǒng)能力:SystemCapability.Communication.NetManager.Core
bindSocket(socketParam: TCPSocket | UDPSocket, callback: AsyncCallback<void>): void
將TCPSocket或UDPSocket綁定到當(dāng)前網(wǎng)絡(luò),使用callback方式作為異步方法。
系統(tǒng)能力:SystemCapability.Communication.NetManager.Core
參數(shù):
錯(cuò)誤碼:
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
401 | Parameter error. |
2100001 | Invalid parameter value. |
2100002 | Operation failed. Cannot connect to service. |
2100003 | System internal error. |
示例:
- 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');
- }
- 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');
- }
- 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));
- }
- })
- })
- }
- })
bindSocket(socketParam: TCPSocket | UDPSocket): Promise<void>;
將TCPSocket或UDPSockett綁定到當(dāng)前網(wǎng)絡(luò),使用Promise方式作為異步方法。
系統(tǒng)能力:SystemCapability.Communication.NetManager.Core
參數(shù):
返回值:
類型 | 說(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. |
示例:
- 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');
- }
- 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');
- }
- 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));
- })
- })
- }
- })
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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- let host = "xxxx";
- netHandle.getAddressesByName(host, function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
- })
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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- let host = "xxxx";
- netHandle.getAddressesByName(host).then(function (data) {
- console.log(JSON.stringify(data))
- })
- })
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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- let host = "xxxx";
- netHandle.getAddressByName(host, function (error, data) {
- console.log(JSON.stringify(error))
- console.log(JSON.stringify(data))
- })
- })
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. |
示例:
- connection.getDefaultNet().then(function (netHandle) {
- let host = "xxxx";
- netHandle.getAddressByName(host).then(function (data) {
- console.log(JSON.stringify(data))
- })
- })
網(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è)置。 |
網(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ò)。 |
提供承載數(shù)據(jù)網(wǎng)絡(luò)能力的實(shí)例。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。
名稱 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
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)。 |
網(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ò)類型。 |
網(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 | 是 | 最大傳輸單元。 |
網(wǎng)絡(luò)路由信息。
系統(tǒng)能力:以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.Communication.NetManager.Core。
名稱 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
interface | string | 是 | 網(wǎng)卡名稱。 |
destination | 是 | 目的地址。 | |
gateway | 是 | 網(wǎng)關(guān)地址。 | |
hasGateway | boolean | 是 | 是否有網(wǎng)關(guān)。 |
isDefaultRoute | boolean | 是 | 是否為默認(rèn)路由。 |
更多建議: