checkConnection method

Future<NetworkConnectionInfo> checkConnection({
  1. bool includeNativeDetails = true,
  2. bool probeReachability = false,
  3. Duration probeTimeout = const Duration(seconds: 3),
})

采集一次网络连接快照 / Captures one network connection snapshot.

includeNativeDetailstrue 时会调用原生通道获取 SSID、网关、MAC 等 信息(失败时静默降级)/ When includeNativeDetails is true an extra platform-channel call enriches the snapshot with SSID, gateway and MAC (silently degrades on failure). probeReachabilitytrue 时会额外执行主动连通性探测 / probeReachability additionally performs an active reachability probe.

Implementation

Future<NetworkConnectionInfo> checkConnection({
  bool includeNativeDetails = true,
  bool probeReachability = false,
  Duration probeTimeout = const Duration(seconds: 3),
}) async {
  final transports = await _safeTransports();
  final types = transports
      .map(NetworkType.fromRaw)
      .where((type) => type != NetworkType.none)
      .toList(growable: false);

  Map<String, Object?>? native;
  if (includeNativeDetails) {
    native = await _safeNativeDetails();
  }

  final fallback = await _dartInterfaceSnapshot();

  final ipAddress =
      _asString(native?['ipAddress']) ??
      fallback.ipAddress ??
      _asString(native?['ipv6Address']) ??
      fallback.ipv6Address;

  final info = NetworkConnectionInfo(
    isConnected: types.isNotEmpty || ipAddress != null,
    type: _resolveType(types),
    ssid: _asString(native?['ssid']),
    signalStrength: (native?['signalStrength'] as num?)?.toInt(),
    ipAddress: _asString(native?['ipAddress']) ?? fallback.ipAddress,
    ipv6Address: _asString(native?['ipv6Address']) ?? fallback.ipv6Address,
    gateway: _asString(native?['gateway']),
    macAddress: _asString(native?['macAddress']),
    isVpn: native?['isVpn'] == true || types.contains(NetworkType.vpn),
    timestamp: DateTime.now(),
  );

  if (!probeReachability) return info;
  return info.copyWith(isReachable: await _safeProbe(probeTimeout));
}