fix: keep modern Android phone links on fast bands
This commit is contained in:
@@ -12,8 +12,8 @@ android {
|
|||||||
applicationId = "com.qft.optical"
|
applicationId = "com.qft.optical"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 35
|
targetSdk = 35
|
||||||
versionCode = 27
|
versionCode = 28
|
||||||
versionName = "0.5.0-dev4"
|
versionName = "0.5.0-dev5"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
) { showReceivedFiles() })
|
) { showReceivedFiles() })
|
||||||
root.addView(space(22))
|
root.addView(space(22))
|
||||||
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
|
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
|
||||||
root.addView(text("QFT v0.5.0-dev4 · Android 7+ 到最新", 12f, MUTED))
|
root.addView(text("QFT v0.5.0-dev5 · Android 7+ 到最新", 12f, MUTED))
|
||||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -958,6 +958,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
else "正在校验 ${done + 1}/$total:$name"
|
else "正在校验 ${done + 1}/$total:$name"
|
||||||
} },
|
} },
|
||||||
onPeer = { peer -> runOnUiThread {
|
onPeer = { peer -> runOnUiThread {
|
||||||
|
phoneHotspot.markPeerAuthenticated()
|
||||||
phoneShareStatusText?.text = "$peer 已连接,正在接收队列文件…"
|
phoneShareStatusText?.text = "$peer 已连接,正在接收队列文件…"
|
||||||
} },
|
} },
|
||||||
)
|
)
|
||||||
@@ -965,6 +966,9 @@ class MainActivity : ComponentActivity() {
|
|||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
phoneHotspot.start(
|
phoneHotspot.start(
|
||||||
onStarted = { wifi -> showPhoneShareQr(server, wifi, null) },
|
onStarted = { wifi -> showPhoneShareQr(server, wifi, null) },
|
||||||
|
onStatus = { message ->
|
||||||
|
if (phoneShareServer === server) phoneShareStatusText?.text = message
|
||||||
|
},
|
||||||
onFailed = { reason ->
|
onFailed = { reason ->
|
||||||
val endpoints = PhoneShareServer.localEndpoints(server.port)
|
val endpoints = PhoneShareServer.localEndpoints(server.port)
|
||||||
if (endpoints.isEmpty()) {
|
if (endpoints.isEmpty()) {
|
||||||
@@ -1038,7 +1042,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
root.addView(phoneShareStatusText)
|
root.addView(phoneShareStatusText)
|
||||||
root.addView(text(
|
root.addView(text(
|
||||||
if (wifi != null) {
|
if (wifi != null) {
|
||||||
val link = if (wifi.ssid.startsWith("DIRECT-")) "兼容专用网络(2.4GHz / WPA2)" else "备用临时网络"
|
val link = if (wifi.ssid.startsWith("DIRECT-")) "自适应专用网络(优先高速 / 自动兼容)" else "备用临时网络"
|
||||||
"$link:${wifi.ssid}\n发送服务端口:${server.port}"
|
"$link:${wifi.ssid}\n发送服务端口:${server.port}"
|
||||||
}
|
}
|
||||||
else "连接方式:两台手机当前同一 Wi-Fi\n发送服务端口:${server.port}",
|
else "连接方式:两台手机当前同一 Wi-Fi\n发送服务端口:${server.port}",
|
||||||
|
|||||||
@@ -15,152 +15,270 @@ import java.util.concurrent.atomic.AtomicBoolean
|
|||||||
/**
|
/**
|
||||||
* Owns the phone-share access point.
|
* Owns the phone-share access point.
|
||||||
*
|
*
|
||||||
* A second LocalOnlyHotspot request cannot create a second SSID: Android shares
|
* Android only exposes one underlying local hotspot, so QFT uses one adaptive
|
||||||
* one underlying hotspot. QFT therefore prefers a Wi-Fi Direct group-owner AP,
|
* Wi-Fi Direct group instead of pretending that two independent SSIDs can run
|
||||||
* which is WPA2 and accepts legacy Wi-Fi stations. Android 10+ senders force it
|
* concurrently. Android 10+ starts on AUTO (normally 5 GHz when available). If
|
||||||
* onto 2.4 GHz so Android 7-era receivers can see the same single network.
|
* no authenticated peer appears, the exact same SSID/password is recreated on
|
||||||
* LocalOnlyHotspot remains a fallback for OEMs that disable P2P group creation.
|
* 2.4 GHz so an old receiver's already-running Wi-Fi search can join without
|
||||||
|
* rescanning the QR. LocalOnlyHotspot remains the final OEM fallback.
|
||||||
*/
|
*/
|
||||||
class PhoneHotspotController(context: Context) {
|
class PhoneHotspotController(context: Context) {
|
||||||
|
private companion object {
|
||||||
|
const val LEGACY_BAND_FALLBACK_MS = 12_000L
|
||||||
|
const val GROUP_READY_TIMEOUT_MS = 12_000L
|
||||||
|
}
|
||||||
|
|
||||||
private val appContext = context.applicationContext
|
private val appContext = context.applicationContext
|
||||||
private val wifiManager = appContext.getSystemService(WifiManager::class.java)
|
private val wifiManager = appContext.getSystemService(WifiManager::class.java)
|
||||||
private val p2pManager = appContext.getSystemService(Context.WIFI_P2P_SERVICE) as? WifiP2pManager
|
private val p2pManager = appContext.getSystemService(Context.WIFI_P2P_SERVICE) as? WifiP2pManager
|
||||||
private val handler = Handler(Looper.getMainLooper())
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
private var reservation: AutoCloseable? = null
|
private var reservation: AutoCloseable? = null
|
||||||
private var p2pChannel: WifiP2pManager.Channel? = null
|
private var p2pChannel: WifiP2pManager.Channel? = null
|
||||||
|
private var adaptiveFallback: Runnable? = null
|
||||||
private var generation = 0
|
private var generation = 0
|
||||||
|
@Volatile private var peerAuthenticated = false
|
||||||
|
|
||||||
fun start(
|
fun start(
|
||||||
onStarted: (WifiCredentials) -> Unit,
|
onStarted: (WifiCredentials) -> Unit,
|
||||||
|
onStatus: (String) -> Unit = {},
|
||||||
onFailed: (String) -> Unit,
|
onFailed: (String) -> Unit,
|
||||||
) {
|
) {
|
||||||
close()
|
close()
|
||||||
val run = ++generation
|
val run = ++generation
|
||||||
startCompatibilityGroup(
|
peerAuthenticated = false
|
||||||
run = run,
|
val finalFallbackStarted = AtomicBoolean(false)
|
||||||
onStarted = onStarted,
|
|
||||||
onFallback = { reason ->
|
fun fallBackToLocalOnly(reason: String) {
|
||||||
if (run != generation) return@startCompatibilityGroup
|
if (run != generation || !finalFallbackStarted.compareAndSet(false, true)) return
|
||||||
|
adaptiveFallback?.let(handler::removeCallbacks)
|
||||||
|
adaptiveFallback = null
|
||||||
|
removeP2pGroup {
|
||||||
|
if (run == generation) {
|
||||||
if (Build.VERSION.SDK_INT >= 26) {
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
|
onStatus("$reason;正在启动系统备用热点…")
|
||||||
startLocalOnlyHotspot(run, onStarted) { localReason ->
|
startLocalOnlyHotspot(run, onStarted) { localReason ->
|
||||||
onFailed("$reason;$localReason")
|
onFailed("$reason;$localReason")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
onFailed("$reason;Android 7 无法创建备用临时热点,将使用当前局域网")
|
onFailed("$reason;Android 7 无法创建备用临时热点,将使用当前局域网")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val manager = p2pManager ?: run {
|
||||||
|
fallBackToLocalOnly("设备没有 Wi-Fi Direct 服务")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val channel = try {
|
||||||
|
manager.initialize(appContext, Looper.getMainLooper()) {
|
||||||
|
fallBackToLocalOnly("Wi-Fi Direct 通道已断开")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
fallBackToLocalOnly("初始化专用网络失败:${e.message ?: e.javaClass.simpleName}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p2pChannel = channel
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= 29) {
|
||||||
|
val wifi = newSessionCredentials()
|
||||||
|
onStatus("正在创建高速专用网络(自动选择 5GHz/2.4GHz)…")
|
||||||
|
replaceConfiguredGroup(
|
||||||
|
run = run,
|
||||||
|
channel = channel,
|
||||||
|
wifi = wifi,
|
||||||
|
band = WifiP2pConfig.GROUP_OWNER_BAND_AUTO,
|
||||||
|
onReady = {
|
||||||
|
onStarted(wifi)
|
||||||
|
onStatus("高速专用网络已就绪;12 秒未配对将自动切换旧机兼容频段")
|
||||||
|
scheduleLegacyBandFallback(
|
||||||
|
run = run,
|
||||||
|
channel = channel,
|
||||||
|
wifi = wifi,
|
||||||
|
onStarted = onStarted,
|
||||||
|
onStatus = onStatus,
|
||||||
|
onFailed = ::fallBackToLocalOnly,
|
||||||
|
)
|
||||||
},
|
},
|
||||||
|
onFailed = ::fallBackToLocalOnly,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
onStatus("正在创建系统兼容专用网络…")
|
||||||
|
replaceLegacyGroup(
|
||||||
|
run = run,
|
||||||
|
channel = channel,
|
||||||
|
onReady = { wifi ->
|
||||||
|
onStarted(wifi)
|
||||||
|
onStatus("WPA2 兼容专用网络已就绪")
|
||||||
|
},
|
||||||
|
onFailed = ::fallBackToLocalOnly,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Called after the HTTP session token has been authenticated. */
|
||||||
|
fun markPeerAuthenticated() {
|
||||||
|
peerAuthenticated = true
|
||||||
|
adaptiveFallback?.let(handler::removeCallbacks)
|
||||||
|
adaptiveFallback = null
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(29)
|
||||||
|
private fun scheduleLegacyBandFallback(
|
||||||
|
run: Int,
|
||||||
|
channel: WifiP2pManager.Channel,
|
||||||
|
wifi: WifiCredentials,
|
||||||
|
onStarted: (WifiCredentials) -> Unit,
|
||||||
|
onStatus: (String) -> Unit,
|
||||||
|
onFailed: (String) -> Unit,
|
||||||
|
) {
|
||||||
|
val task = Runnable {
|
||||||
|
adaptiveFallback = null
|
||||||
|
if (run != generation || peerAuthenticated) return@Runnable
|
||||||
|
onStatus("尚未检测到接收端,正在用相同二维码切换到 2.4GHz 旧机兼容频段…")
|
||||||
|
replaceConfiguredGroup(
|
||||||
|
run = run,
|
||||||
|
channel = channel,
|
||||||
|
wifi = wifi,
|
||||||
|
band = WifiP2pConfig.GROUP_OWNER_BAND_2GHZ,
|
||||||
|
onReady = {
|
||||||
|
onStarted(wifi) // Same credentials: QR contents do not change.
|
||||||
|
onStatus("已切换 2.4GHz 兼容频段;旧版 Android 可继续自动搜索并连接")
|
||||||
|
},
|
||||||
|
onFailed = onFailed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
adaptiveFallback = task
|
||||||
|
handler.postDelayed(task, LEGACY_BAND_FALLBACK_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(29)
|
||||||
|
@Suppress("MissingPermission")
|
||||||
|
private fun replaceConfiguredGroup(
|
||||||
|
run: Int,
|
||||||
|
channel: WifiP2pManager.Channel,
|
||||||
|
wifi: WifiCredentials,
|
||||||
|
band: Int,
|
||||||
|
onReady: () -> Unit,
|
||||||
|
onFailed: (String) -> Unit,
|
||||||
|
) {
|
||||||
|
replaceGroup(
|
||||||
|
run = run,
|
||||||
|
channel = channel,
|
||||||
|
create = { listener ->
|
||||||
|
val config = WifiP2pConfig.Builder()
|
||||||
|
.setNetworkName(wifi.ssid)
|
||||||
|
.setPassphrase(wifi.passphrase)
|
||||||
|
.setGroupOperatingBand(band)
|
||||||
|
.build()
|
||||||
|
p2pManager?.createGroup(channel, config, listener)
|
||||||
|
?: onFailed("Wi-Fi Direct 服务不可用")
|
||||||
|
},
|
||||||
|
onReady = { actual ->
|
||||||
|
if (actual.ssid != wifi.ssid || actual.passphrase != wifi.passphrase) {
|
||||||
|
onFailed("系统创建的专用网络凭据与会话不一致")
|
||||||
|
} else {
|
||||||
|
onReady()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onFailed = onFailed,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("MissingPermission")
|
@Suppress("MissingPermission")
|
||||||
private fun startCompatibilityGroup(
|
private fun replaceLegacyGroup(
|
||||||
run: Int,
|
run: Int,
|
||||||
onStarted: (WifiCredentials) -> Unit,
|
channel: WifiP2pManager.Channel,
|
||||||
onFallback: (String) -> Unit,
|
onReady: (WifiCredentials) -> Unit,
|
||||||
|
onFailed: (String) -> Unit,
|
||||||
) {
|
) {
|
||||||
val manager = p2pManager ?: run {
|
replaceGroup(
|
||||||
onFallback("设备没有 Wi-Fi Direct 服务")
|
run = run,
|
||||||
return
|
channel = channel,
|
||||||
|
create = { listener ->
|
||||||
|
p2pManager?.createGroup(channel, listener)
|
||||||
|
?: onFailed("Wi-Fi Direct 服务不可用")
|
||||||
|
},
|
||||||
|
onReady = onReady,
|
||||||
|
onFailed = onFailed,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("MissingPermission")
|
||||||
|
private fun replaceGroup(
|
||||||
|
run: Int,
|
||||||
|
channel: WifiP2pManager.Channel,
|
||||||
|
create: (WifiP2pManager.ActionListener) -> Unit,
|
||||||
|
onReady: (WifiCredentials) -> Unit,
|
||||||
|
onFailed: (String) -> Unit,
|
||||||
|
) {
|
||||||
val terminal = AtomicBoolean(false)
|
val terminal = AtomicBoolean(false)
|
||||||
var localChannel: WifiP2pManager.Channel? = null
|
|
||||||
|
|
||||||
fun fallback(reason: String) {
|
fun fail(reason: String) {
|
||||||
if (run != generation || !terminal.compareAndSet(false, true)) return
|
if (run == generation && terminal.compareAndSet(false, true)) onFailed(reason)
|
||||||
val channel = localChannel
|
|
||||||
if (p2pChannel === channel) p2pChannel = null
|
|
||||||
if (channel != null) {
|
|
||||||
try {
|
|
||||||
manager.removeGroup(channel, object : WifiP2pManager.ActionListener {
|
|
||||||
override fun onSuccess() = onFallback(reason)
|
|
||||||
override fun onFailure(removeReason: Int) = onFallback(reason)
|
|
||||||
})
|
|
||||||
} catch (_: Exception) {
|
|
||||||
onFallback(reason)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
onFallback(reason)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val channel = try {
|
fun waitForGroup() {
|
||||||
manager.initialize(appContext, Looper.getMainLooper()) {
|
val deadline = SystemClock.elapsedRealtime() + GROUP_READY_TIMEOUT_MS
|
||||||
fallback("Wi-Fi Direct 通道已断开")
|
fun poll() {
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
onFallback("初始化兼容专用网络失败:${e.message ?: e.javaClass.simpleName}")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
localChannel = channel
|
|
||||||
p2pChannel = channel
|
|
||||||
|
|
||||||
val createListener = object : WifiP2pManager.ActionListener {
|
|
||||||
override fun onSuccess() {
|
|
||||||
val deadline = SystemClock.elapsedRealtime() + 12_000L
|
|
||||||
fun pollGroup() {
|
|
||||||
if (run != generation || terminal.get()) return
|
if (run != generation || terminal.get()) return
|
||||||
try {
|
try {
|
||||||
manager.requestGroupInfo(channel) { group ->
|
p2pManager?.requestGroupInfo(channel) { group ->
|
||||||
if (run != generation || terminal.get()) return@requestGroupInfo
|
if (run != generation || terminal.get()) return@requestGroupInfo
|
||||||
val ssid = group?.networkName.orEmpty()
|
val ssid = group?.networkName.orEmpty()
|
||||||
val passphrase = group?.passphrase.orEmpty()
|
val passphrase = group?.passphrase.orEmpty()
|
||||||
if (group?.isGroupOwner == true && ssid.isNotBlank() && passphrase.length in 8..63) {
|
if (group?.isGroupOwner == true && ssid.isNotBlank() && passphrase.length in 8..63) {
|
||||||
if (!terminal.compareAndSet(false, true)) return@requestGroupInfo
|
if (terminal.compareAndSet(false, true)) {
|
||||||
onStarted(WifiCredentials(ssid, passphrase, "WPA2"))
|
onReady(WifiCredentials(ssid, passphrase, "WPA2"))
|
||||||
|
}
|
||||||
} else if (SystemClock.elapsedRealtime() >= deadline) {
|
} else if (SystemClock.elapsedRealtime() >= deadline) {
|
||||||
fallback("创建 2.4GHz/WPA2 兼容专用网络超时")
|
fail("创建专用网络超时")
|
||||||
} else {
|
} else {
|
||||||
handler.postDelayed({ pollGroup() }, 180L)
|
handler.postDelayed({ poll() }, 180L)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} ?: fail("Wi-Fi Direct 服务不可用")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
fallback("读取兼容专用网络信息失败:${e.message ?: e.javaClass.simpleName}")
|
fail("读取专用网络信息失败:${e.message ?: e.javaClass.simpleName}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pollGroup()
|
poll()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailure(reason: Int) {
|
val listener = object : WifiP2pManager.ActionListener {
|
||||||
fallback("系统拒绝创建 2.4GHz/WPA2 兼容专用网络(错误 $reason)")
|
override fun onSuccess() = waitForGroup()
|
||||||
|
override fun onFailure(reason: Int) = fail("系统拒绝创建专用网络(错误 $reason)")
|
||||||
}
|
}
|
||||||
}
|
val startCreate = {
|
||||||
|
if (run == generation && !terminal.get()) {
|
||||||
fun createGroup() {
|
|
||||||
if (run != generation || terminal.get()) return
|
|
||||||
try {
|
try {
|
||||||
if (Build.VERSION.SDK_INT >= 29) {
|
create(listener)
|
||||||
manager.createGroup(channel, compatibilityConfig(), createListener)
|
|
||||||
} else {
|
|
||||||
// API 24–28 creates the interoperable legacy-client group
|
|
||||||
// with system-generated DIRECT-* credentials.
|
|
||||||
manager.createGroup(channel, createListener)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
fallback("启动兼容专用网络失败:${e.message ?: e.javaClass.simpleName}")
|
fail("启动专用网络失败:${e.message ?: e.javaClass.simpleName}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A stale persistent P2P group can keep old credentials or a 5 GHz
|
// Remove stale/present group before both initial creation and adaptive
|
||||||
// channel. Remove it before creating this session's 2.4 GHz group.
|
// band switching. Credentials supplied to `create` stay unchanged.
|
||||||
try {
|
try {
|
||||||
manager.removeGroup(channel, object : WifiP2pManager.ActionListener {
|
p2pManager?.removeGroup(channel, object : WifiP2pManager.ActionListener {
|
||||||
override fun onSuccess() = createGroup()
|
override fun onSuccess() = startCreate()
|
||||||
override fun onFailure(reason: Int) = createGroup()
|
override fun onFailure(reason: Int) = startCreate()
|
||||||
})
|
}) ?: fail("Wi-Fi Direct 服务不可用")
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
createGroup()
|
startCreate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(29)
|
@RequiresApi(29)
|
||||||
private fun compatibilityConfig(): WifiP2pConfig {
|
private fun newSessionCredentials(): WifiCredentials {
|
||||||
val random = ByteArray(12).also { SecureRandom().nextBytes(it) }
|
val random = ByteArray(12).also { SecureRandom().nextBytes(it) }
|
||||||
val token = random.joinToString("") { "%02x".format(it.toInt() and 0xff) }
|
val token = random.joinToString("") { "%02x".format(it.toInt() and 0xff) }
|
||||||
return WifiP2pConfig.Builder()
|
return WifiCredentials(
|
||||||
.setNetworkName("DIRECT-QF-QFT${token.take(6)}")
|
ssid = "DIRECT-QF-QFT${token.take(6)}",
|
||||||
.setPassphrase(token.take(16))
|
passphrase = token.take(16),
|
||||||
.setGroupOperatingBand(WifiP2pConfig.GROUP_OWNER_BAND_2GHZ)
|
security = "WPA2",
|
||||||
.build()
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(26)
|
@RequiresApi(26)
|
||||||
@@ -218,10 +336,34 @@ class PhoneHotspotController(context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("MissingPermission")
|
||||||
|
private fun removeP2pGroup(done: () -> Unit) {
|
||||||
|
val channel = p2pChannel
|
||||||
|
p2pChannel = null
|
||||||
|
if (channel == null) {
|
||||||
|
done()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val once = AtomicBoolean(false)
|
||||||
|
fun finish() {
|
||||||
|
if (once.compareAndSet(false, true)) done()
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
p2pManager?.removeGroup(channel, object : WifiP2pManager.ActionListener {
|
||||||
|
override fun onSuccess() = finish()
|
||||||
|
override fun onFailure(reason: Int) = finish()
|
||||||
|
}) ?: finish()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("MissingPermission")
|
@Suppress("MissingPermission")
|
||||||
fun close() {
|
fun close() {
|
||||||
generation++
|
generation++
|
||||||
handler.removeCallbacksAndMessages(null)
|
handler.removeCallbacksAndMessages(null)
|
||||||
|
adaptiveFallback = null
|
||||||
|
peerAuthenticated = false
|
||||||
try { reservation?.close() } catch (_: Exception) {}
|
try { reservation?.close() } catch (_: Exception) {}
|
||||||
reservation = null
|
reservation = null
|
||||||
val channel = p2pChannel
|
val channel = p2pChannel
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Android → Android 真机验收(v0.5.0-dev4)
|
# Android → Android 真机验收(v0.5.0-dev5)
|
||||||
|
|
||||||
## 构建约束
|
## 构建约束
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ cd android
|
|||||||
|
|
||||||
## 首轮验收
|
## 首轮验收
|
||||||
|
|
||||||
1. 两台 Android 安装同一个 `v0.5.0-dev4` APK。
|
1. 两台 Android 安装同一个 `v0.5.0-dev5` APK。
|
||||||
2. 发送手机进入“手机 → 手机”,选择至少两个文件并调整一次顺序。
|
2. 发送手机进入“手机 → 手机”,选择至少两个文件并调整一次顺序。
|
||||||
3. 点击“创建发送二维码”,允许附近 Wi-Fi/位置权限。
|
3. 点击“创建发送二维码”,允许附近 Wi-Fi/位置权限。
|
||||||
4. 接收手机进入“扫码直传”,扫描发送手机二维码并确认系统连接提示。
|
4. 接收手机进入“扫码直传”,扫描发送手机二维码并确认系统连接提示。
|
||||||
@@ -21,11 +21,12 @@ cd android
|
|||||||
|
|
||||||
## 必测网络组合
|
## 必测网络组合
|
||||||
|
|
||||||
- Android 13+ → Android 13+:`NEARBY_WIFI_DEVICES` 权限与 2.4 GHz/WPA2 Wi-Fi Direct 兼容接入点。
|
- Android 13+ → Android 13+:`NEARBY_WIFI_DEVICES` 权限;专用网络先用自动频段,支持时应协商到 5 GHz,不得固定在 2.4 GHz。
|
||||||
- Android 13+ 发送 → Android 7–9 接收:旧机应能看到 `DIRECT-*` 专用网络;连续停止并重建两次,旧机每次均使用二维码中的最新密码连接。
|
- Android 13+ 发送 → Android 7–9 接收:旧机扫码后持续搜索同一个 `DIRECT-*`;发送端 12 秒未收到认证请求时,以相同 SSID/密码切换到 2.4 GHz,旧机无需重新扫码即可连接。
|
||||||
- Android 13+ 发送 → Android 10–12 接收:新会话即使复用了热点名称,只要密码变化就必须创建新的系统配网请求。
|
- Android 13+ 发送 → Android 10–12 接收:新会话即使复用了热点名称,只要密码变化就必须创建新的系统配网请求。
|
||||||
- Wi-Fi Direct 被 OEM 禁用:发送端自动回落 LocalOnlyHotspot;纯 WPA3 且接收机不支持 SAE 时自动尝试原局域网,不应无限搜索。
|
- Wi-Fi Direct 被 OEM 禁用:发送端自动回落 LocalOnlyHotspot;纯 WPA3 且接收机不支持 SAE 时自动尝试原局域网,不应无限搜索。
|
||||||
- Android 7–9 发送 → Android 13+ 接收:至少传输一个 100 MiB 文件两次,均通过 SHA-256,不得再出现整文件重下后仍校验失败。
|
- Android 7–9 发送 → Android 13+ 接收:至少传输一个 100 MiB 文件两次,均通过 SHA-256,不得再出现整文件重下后仍校验失败。
|
||||||
|
- Android 10+ 高速链路:接收端在 12 秒内完成认证后,发送端不得切换频段或中断现有 5 GHz 连接。
|
||||||
- Android 7 发送端:验证系统生成的 `DIRECT-*` WPA2 组可被高版本接收端自动加入;P2P 创建失败时再验证同局域网回落。
|
- Android 7 发送端:验证系统生成的 `DIRECT-*` WPA2 组可被高版本接收端自动加入;P2P 创建失败时再验证同局域网回落。
|
||||||
- 手动停止发送端热点:接收端应报连接中断,而不是无限等待。
|
- 手动停止发送端热点:接收端应报连接中断,而不是无限等待。
|
||||||
- 传输中返回发送端首页:服务和临时热点应立即关闭。
|
- 传输中返回发送端首页:服务和临时热点应立即关闭。
|
||||||
|
|||||||
+7
-4
@@ -99,7 +99,7 @@ The sender returns `206 Partial Content`, the matching `Content-Range`, and the
|
|||||||
|
|
||||||
For both direct-transfer directions, the Windows listener may accept connections non-blockingly for shutdown responsiveness, but each accepted file socket is switched to blocking I/O with explicit timeouts before reading or writing request bodies. This prevents `WSAEWOULDBLOCK (10035)` from turning an in-progress parallel transfer into an HTTP 500 failure.
|
For both direct-transfer directions, the Windows listener may accept connections non-blockingly for shutdown responsiveness, but each accepted file socket is switched to blocking I/O with explicit timeouts before reading or writing request bodies. This prevents `WSAEWOULDBLOCK (10035)` from turning an in-progress parallel transfer into an HTTP 500 failure.
|
||||||
|
|
||||||
## 6. Android → Android (`QFTPHONE2`, v0.5.0-dev4)
|
## 6. Android → Android (`QFTPHONE2`, v0.5.0-dev5)
|
||||||
|
|
||||||
The sender starts an authenticated local HTTP service and encodes only session,
|
The sender starts an authenticated local HTTP service and encodes only session,
|
||||||
port, sender identity and optional dedicated-network credentials in the QR. The
|
port, sender identity and optional dedicated-network credentials in the QR. The
|
||||||
@@ -117,9 +117,12 @@ The manifest contains a bounded ordered list of `name`, `size`, `sha256` and a
|
|||||||
server-generated fixed download path. The receiver does not accept arbitrary
|
server-generated fixed download path. The receiver does not accept arbitrary
|
||||||
paths from the QR or manifest: entry `N` must use `/qft/phone/download/N`.
|
paths from the QR or manifest: entry `N` must use `/qft/phone/download/N`.
|
||||||
|
|
||||||
Android prefers a 2.4 GHz WPA2 Wi-Fi Direct group-owner AP that accepts legacy
|
Android 10+ first creates a WPA2 Wi-Fi Direct group-owner AP with automatic band
|
||||||
Wi-Fi stations. An OEM P2P failure falls back to LocalOnlyHotspot, then literal
|
selection, normally allowing a 5 GHz link. If no authenticated peer arrives in
|
||||||
private IPv4 endpoints on the current LAN. WPA2/WPA3
|
12 seconds, the sender recreates the group on 2.4 GHz with the exact same SSID
|
||||||
|
and passphrase, so an older receiver's pending scan continues without a new QR.
|
||||||
|
An OEM P2P failure falls back to LocalOnlyHotspot, then literal private IPv4
|
||||||
|
endpoints on the current LAN. WPA2/WPA3
|
||||||
credentials are distinguished in `w[2]`. The receiver reuses the existing Range
|
credentials are distinguished in `w[2]`. The receiver reuses the existing Range
|
||||||
ledger, retries and SHA-256-before-publish behavior for every file, processing
|
ledger, retries and SHA-256-before-publish behavior for every file, processing
|
||||||
items in manifest order. Phone senders use one deterministic Range worker to
|
items in manifest order. Phone senders use one deterministic Range worker to
|
||||||
|
|||||||
@@ -29,7 +29,10 @@ assert 'probeConnectedNetwork' in upload and 'workers = 48' in upload
|
|||||||
assert 'awaitGatewayEndpoints' not in main
|
assert 'awaitGatewayEndpoints' not in main
|
||||||
assert 'QFTPHONE2:' in (ROOT/'android/app/src/main/java/com/qft/optical/PairingProtocol.kt').read_text()
|
assert 'QFTPHONE2:' in (ROOT/'android/app/src/main/java/com/qft/optical/PairingProtocol.kt').read_text()
|
||||||
assert 'WifiP2pManager' in phone_hotspot and 'createGroup' in phone_hotspot
|
assert 'WifiP2pManager' in phone_hotspot and 'createGroup' in phone_hotspot
|
||||||
assert 'GROUP_OWNER_BAND_2GHZ' in phone_hotspot and 'setNetworkName("DIRECT-QF-' in phone_hotspot
|
assert 'GROUP_OWNER_BAND_AUTO' in phone_hotspot and 'GROUP_OWNER_BAND_2GHZ' in phone_hotspot
|
||||||
|
assert 'LEGACY_BAND_FALLBACK_MS = 12_000L' in phone_hotspot and 'onStarted(wifi) // Same credentials' in phone_hotspot
|
||||||
|
assert 'markPeerAuthenticated' in phone_hotspot and 'phoneHotspot.markPeerAuthenticated()' in main
|
||||||
|
assert 'setNetworkName(wifi.ssid)' in phone_hotspot and 'setPassphrase(wifi.passphrase)' in phone_hotspot
|
||||||
assert 'startLocalOnlyHotspot' in phone_hotspot and 'Build.VERSION.SDK_INT < 26' not in main
|
assert 'startLocalOnlyHotspot' in phone_hotspot and 'Build.VERSION.SDK_INT < 26' not in main
|
||||||
assert '/qft/phone/manifest' in phone_server and '/qft/phone/download/' in phone_server
|
assert '/qft/phone/manifest' in phone_server and '/qft/phone/download/' in phone_server
|
||||||
assert 'startDownloadsFromPhone' in main and 'downloadPath = item.path' in main
|
assert 'startDownloadsFromPhone' in main and 'downloadPath = item.path' in main
|
||||||
|
|||||||
Reference in New Issue
Block a user