如何解决网络连接状态变化的公共事件返回内容为"NetType":1的问题
====================================

返回"NetType":1枚举值表示为NET\_CONN\_STATE\_IDLE网络空闲状态。

如果是监听网络变化，建议使用@ohos.net.connection的能力，请参考[@ohos.net.connection (网络连接管理)](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-net-connection)。

代码示例如下：

import { connection } from '@kit.NetworkKit';
import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';

function  listen\_network() {
  let netSpecifier: connection.NetSpecifier = {
    netCapabilities: {
      _// Assuming the current default network is WiFi, a cellular network connection needs to be created, and the network type can be specified as cellular network_
      bearerTypes: \[connection.NetBearType.BEARER\_CELLULAR, connection.NetBearType.BEARER\_WIFI\],
    },
  };
  let conn = connection.createNetConnection(netSpecifier);

  conn.register((err: BusinessError, data: void) => {
    console.warn('register Network ' + JSON.stringify(err))
  });

  _// Subscription event, network available_
  conn.on('netAvailable', ((data: connection.NetHandle) => {
    console.warn('Network available, netId is ' + data.netId);
  }));

  _// Subscription event, network available_
  conn.on('netCapabilitiesChange', ((data: connection.NetCapabilityInfo) => {
    console.warn('Network netCapabilitiesChange bearerTypes ' + data.netCap.bearerTypes);
    console.warn('Network netCapabilitiesChange networkCap ' + data.netCap.networkCap);
  }));

  _// Subscription event, network unavailable_
  conn.on('netUnavailable', ((data: void) => {
    console.warn('The network is unavailable, data is ' + JSON.stringify(data));
  }));

  _// Subscription event, network disconnection_
  conn.on('netLost', ((data: connection.NetHandle) => {
    console.warn('Network lost, netId is ' + data.netId);
  }));
}

_// After monitoring an event, it is necessary to obtain the network status through a network interface_
function sub\_network() {
  console.warn('into sub\_network')
  _// Public event monitoring code:_
  let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
    _// Subscription message exception public event_
    events: \['usual.event.CONNECTIVITY\_CHANGE'\]
  }

  _// Create subscriber callback_
  commonEventManager.createSubscriber(subscribeInfo, (err: BusinessError, subscriber: commonEventManager.CommonEventSubscriber) => {
    if (err) {
      console.warn(\`Failed to create netWorkSubscribeInfo. Code is ${err.code}, message is ${err.message}\`);
      return;
    }
    if (subscribeInfo && subscribeInfo != null) {
      _// Subscribe to public event callbacks_
      commonEventManager.subscribe(subscriber, (err: BusinessError, data: commonEventManager.CommonEventData) => {
        if (err) {
          console.warn(\`Failed to netWorkSubscribe common event. Code is ${err.code}, message is ${err.message}\`);
          return;
        }
        console.warn('NET\_CONNECTIVITY\_CHANGE：' + JSON.stringify(data.parameters));

        setTimeout(async () => {
          connection.getDefaultNet((error, data) => {
            console.log(JSON.stringify(error))
            console.log(JSON.stringify(data))
          }) }, 500);
        _// The log printed here is{'NetType':1,'moduleName':''}_
      })
    }
  })
}

@Entry
@Component
struct NetWork {
  build() {
    Row() {
      Column() {
        Button('Monitor the network').onClick(() => {
          listen\_network()
        })
        Button('Obtain the network status').onClick(() => {
          sub\_network()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}