feat(android): 分阶段速率记账 + 连接失败时输出网络诊断
两件都是为了让"到底哪里慢/哪里断"能被看见,而不是继续靠猜。 分阶段速率 下载分传输、校验、保存三段,三者速率含义完全不同,混在一起平均出来的 数字毫无意义——这也是"传输不到 10 MB/s"这个观察难以定位的原因: 1 GB 若传输 50 s、校验 5 s、保存 15 s,看起来就成了 14 MB/s。 现在逐段记账,完成后一并列出: 接收完成:已保存到 Downloads/QFT/xxx 接收 52.3s · 19.6 MB/s 校验 6.1s · 168 MB/s 保存 14.8s · 69 MB/s 合计 73.2s · 14.0 MB/s 并行下载的进度回调来自多个 worker 线程(DownloadClient 里 pool.submit 的闭包),因此记账用 AtomicLong 与 synchronizedList,不能用普通 var。 连接失败的诊断 "未找到 QFT 接收服务"这句话无法区分:手机在别的网段?没拿到网关? 还是对端根本没开。现在报错里直接带上手机侧的实际网络状况—— 本机地址/掩码、网关、SSID——以及二维码给出的地址、探测过的网关、 子网扫描范围,并列出三条对照检查项。 新增 WifiConnector.describeNetwork()。 注意:Kotlin 在本机无法编译,以上改动仅做过结构检查,需要实测编译。
This commit is contained in:
@@ -649,7 +649,21 @@ class MainActivity : ComponentActivity() {
|
||||
runOnUiThread {
|
||||
if (attempt != pairConnectionAttempt) return@runOnUiThread
|
||||
if (base == null) {
|
||||
showPairError(pair, "Wi-Fi 已连接,但快速发现、网关探测和当前子网扫描都未找到 QFT 接收服务。请保持 Windows 接收页开启,并在 Windows 防火墙首次弹窗时允许 QFT 访问专用网络。")
|
||||
// 把手机实际所处的网络写进报错。否则"找不到"这三个字
|
||||
// 无法区分:手机在别的网段?没拿到网关?还是对端真的没开。
|
||||
val diagnosis = buildString {
|
||||
append("Wi-Fi 已连接,但快速发现、网关探测和当前子网扫描都未找到 QFT 接收服务。\n\n")
|
||||
append("手机侧:").append(wifiConnector.describeNetwork(network)).append("\n")
|
||||
append("二维码给出的地址:")
|
||||
append(pair.endpoints.joinToString("、").ifEmpty { "(无,仅靠发现)" })
|
||||
append("\n探测过的网关地址:")
|
||||
append(gateways.joinToString("、").ifEmpty { "(无)" })
|
||||
append("\n子网扫描范围:").append(subnet.size).append(" 个地址\n\n")
|
||||
append("请对照检查:① 手机与电脑是否在同一网段;")
|
||||
append("② Windows 上是否在「Android → 电脑」页并已点「启动接收」;")
|
||||
append("③ Windows 防火墙是否放行了 qft-desktop.exe(Profile 必须含当前网络类型)。")
|
||||
}
|
||||
showPairError(pair, diagnosis)
|
||||
} else {
|
||||
connectedBaseUrl = base
|
||||
if (pair.direction == TransferDirection.DOWNLOAD_TO_PHONE) {
|
||||
@@ -801,10 +815,26 @@ class MainActivity : ComponentActivity() {
|
||||
downloadRunning = true
|
||||
transferAbortReason = null
|
||||
transferExecutor.execute {
|
||||
// 「传输 / 校验 / 保存」三个阶段各自的速率含义不同,
|
||||
// 每进入一个新阶段就重新计时,否则显示出来的速率毫无意义。
|
||||
var phaseLabel = "正在接收"
|
||||
// 「传输 / 校验 / 保存」三个阶段各自的速率含义完全不同,混在一起
|
||||
// 平均出来的数字毫无意义。逐段记账,结束后一并列出——
|
||||
// 这样"到底是哪一段慢"一眼就能看出来,不必再靠猜。
|
||||
val phases = java.util.Collections.synchronizedList(
|
||||
mutableListOf<Triple<String, Long, Long>>() // 名称、毫秒、字节
|
||||
)
|
||||
var phaseLabel = "接收"
|
||||
var phaseStartedAt = android.os.SystemClock.elapsedRealtime()
|
||||
// 并行下载的进度回调来自多个 worker 线程(DownloadClient 里
|
||||
// pool.submit 的闭包),普通 var 记账会有竞态,必须用原子变量。
|
||||
val phaseFirstBytes = java.util.concurrent.atomic.AtomicLong(-1L)
|
||||
val phaseLastBytes = java.util.concurrent.atomic.AtomicLong(0L)
|
||||
|
||||
fun closePhase() {
|
||||
val millis = android.os.SystemClock.elapsedRealtime() - phaseStartedAt
|
||||
val first = phaseFirstBytes.get().coerceAtLeast(0L)
|
||||
val bytes = (phaseLastBytes.get() - first).coerceAtLeast(0L)
|
||||
if (millis > 0L && bytes > 0L) phases += Triple(phaseLabel, millis, bytes)
|
||||
}
|
||||
|
||||
try {
|
||||
val saved = DownloadClient.download(
|
||||
context = this@MainActivity,
|
||||
@@ -815,23 +845,47 @@ class MainActivity : ComponentActivity() {
|
||||
deviceName = receiverDeviceLabel,
|
||||
abortReason = { transferAbortReason },
|
||||
onPhase = { label ->
|
||||
phaseLabel = label
|
||||
closePhase()
|
||||
// 去掉文案里的修饰,只留阶段名,汇总表才对得齐
|
||||
phaseLabel = label.removePrefix("正在").removeSuffix("…")
|
||||
.substringBefore("到 ").substringBefore("文件完整性").ifBlank { label }
|
||||
phaseStartedAt = android.os.SystemClock.elapsedRealtime()
|
||||
phaseFirstBytes.set(-1L)
|
||||
phaseLastBytes.set(0L)
|
||||
runOnUiThread { status.text = label }
|
||||
},
|
||||
) { received, expected ->
|
||||
phaseFirstBytes.compareAndSet(-1L, received)
|
||||
phaseLastBytes.set(received)
|
||||
runOnUiThread {
|
||||
val fraction = if (expected <= 0) 0 else ((received * 1000L) / expected).toInt().coerceIn(0, 1000)
|
||||
val elapsed = (android.os.SystemClock.elapsedRealtime() - phaseStartedAt).coerceAtLeast(1L)
|
||||
val rate = received * 1000.0 / elapsed
|
||||
val done = (received - phaseFirstBytes.get().coerceAtLeast(0L)).coerceAtLeast(0L)
|
||||
val rate = done * 1000.0 / elapsed
|
||||
bar.progress = fraction
|
||||
status.text = "$phaseLabel ${humanBytes(received)} / ${humanBytes(expected)} · ${humanBytes(rate.toLong())}/s"
|
||||
}
|
||||
}
|
||||
closePhase()
|
||||
|
||||
val summary = buildString {
|
||||
append("接收完成:已保存到 Downloads/QFT/").append(saved.name)
|
||||
var totalMillis = 0L
|
||||
for ((label, millis, bytes) in phases) {
|
||||
totalMillis += millis
|
||||
append("\n").append(label).append(" ")
|
||||
append(humanDuration(millis / 1000.0)).append(" · ")
|
||||
append(humanBytes((bytes * 1000.0 / millis).toLong())).append("/s")
|
||||
}
|
||||
if (phases.size > 1 && totalMillis > 0L) {
|
||||
append("\n合计 ").append(humanDuration(totalMillis / 1000.0)).append(" · ")
|
||||
append(humanBytes((saved.size * 1000.0 / totalMillis).toLong())).append("/s")
|
||||
}
|
||||
}
|
||||
runOnUiThread {
|
||||
bar.progress = 1000
|
||||
status.setTextColor(SUCCESS)
|
||||
status.text = "接收完成:已保存到 Downloads/QFT/${saved.name}"
|
||||
status.text = summary
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
runOnUiThread {
|
||||
|
||||
@@ -306,6 +306,33 @@ class WifiConnector(context: Context) {
|
||||
|
||||
private fun hasIpv4(props: LinkProperties?): Boolean = props?.linkAddresses?.any { it.address is Inet4Address } == true
|
||||
|
||||
/**
|
||||
* 当前网络的自述:本机地址/掩码、网关、SSID。
|
||||
*
|
||||
* 「找不到接收服务」是最难远程排查的一类失败——光看这句话,无法区分
|
||||
* 手机在哪个网段、有没有拿到网关、还是对端真的没开。把这些直接写进
|
||||
* 报错里,用户截个图就能定位,不必再来回猜。
|
||||
*/
|
||||
fun describeNetwork(network: Network?): String {
|
||||
if (network == null) return "未绑定到任何网络"
|
||||
val props = connectivity.getLinkProperties(network)
|
||||
?: return "已连接,但系统尚未提供链路信息"
|
||||
val addresses = props.linkAddresses
|
||||
.filter { it.address is Inet4Address }
|
||||
.joinToString("、") { "${it.address.hostAddress}/${it.prefixLength}" }
|
||||
.ifEmpty { "(尚未取得 IPv4)" }
|
||||
val gateways = props.routes
|
||||
.mapNotNull { (it.gateway as? Inet4Address)?.hostAddress }
|
||||
.distinct()
|
||||
.joinToString("、")
|
||||
.ifEmpty { "(无)" }
|
||||
@Suppress("DEPRECATION", "MissingPermission")
|
||||
val ssid = try {
|
||||
normalizeSsid(wifiManager.connectionInfo?.ssid).takeIf { it.isNotBlank() && it != "<unknown ssid>" }
|
||||
} catch (_: Exception) { null } ?: "未知"
|
||||
return "本机 $addresses · 网关 $gateways · Wi-Fi $ssid"
|
||||
}
|
||||
|
||||
fun gatewayEndpoints(network: Network?, port: Int): List<String> {
|
||||
if (network == null) return emptyList()
|
||||
val props = connectivity.getLinkProperties(network) ?: return emptyList()
|
||||
|
||||
Reference in New Issue
Block a user