94 lines
3.8 KiB
Rust
94 lines
3.8 KiB
Rust
use anyhow::{bail, Result};
|
|
// Context 只在 Windows 的 WiFiDirect 分支里用;非 Windows 上整段被编译掉。
|
|
#[cfg(windows)]
|
|
use anyhow::Context;
|
|
|
|
#[cfg(windows)]
|
|
use windows::{
|
|
core::HSTRING,
|
|
Devices::WiFiDirect::{
|
|
WiFiDirectAdvertisementListenStateDiscoverability, WiFiDirectAdvertisementPublisher,
|
|
WiFiDirectAdvertisementPublisherStatus,
|
|
},
|
|
Security::Credentials::PasswordCredential,
|
|
};
|
|
|
|
pub struct DirectAp {
|
|
pub ssid: String,
|
|
#[cfg(windows)]
|
|
publisher: WiFiDirectAdvertisementPublisher,
|
|
}
|
|
|
|
impl DirectAp {
|
|
pub fn start(ssid: String, passphrase: String) -> Result<Self> {
|
|
#[cfg(not(windows))]
|
|
{
|
|
let _ = (ssid, passphrase);
|
|
bail!("QFT Direct AP 仅支持 Windows")
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
{
|
|
let publisher = WiFiDirectAdvertisementPublisher::new()
|
|
.context("创建 Wi-Fi Direct publisher 失败")?;
|
|
let ad = publisher.Advertisement()
|
|
.context("读取 Wi-Fi Direct advertisement 失败")?;
|
|
// WinRT defaults this property to `None`, which still lets the
|
|
// publisher report `Started` but does not make the AP discoverable.
|
|
// Android then waits in the system Wi-Fi picker until it times out,
|
|
// exactly as if the hotspot had never been started. Intensive keeps
|
|
// the legacy SSID discoverable even when the desktop window is not
|
|
// foregrounded during the phone-side connection flow.
|
|
ad.SetListenStateDiscoverability(
|
|
WiFiDirectAdvertisementListenStateDiscoverability::Intensive,
|
|
)
|
|
.context("启用 QFT Wi-Fi Direct 可发现性失败")?;
|
|
ad.SetIsAutonomousGroupOwnerEnabled(true)
|
|
.context("启用 Autonomous Group Owner 失败")?;
|
|
let legacy = ad.LegacySettings()
|
|
.context("读取 LegacySettings 失败")?;
|
|
legacy.SetIsEnabled(true)
|
|
.context("启用 Wi-Fi Direct Legacy AP 失败")?;
|
|
legacy.SetSsid(&HSTRING::from(&ssid))
|
|
.context("设置 QFT SSID 失败")?;
|
|
let credential = PasswordCredential::new()
|
|
.context("创建 Wi-Fi Direct 密码对象失败")?;
|
|
credential.SetPassword(&HSTRING::from(&passphrase))
|
|
.context("设置 QFT 热点密码失败")?;
|
|
legacy.SetPassphrase(&credential)
|
|
.context("应用 QFT 热点密码失败")?;
|
|
|
|
publisher.Start().context("启动 QFT Wi-Fi Direct AP 失败")?;
|
|
|
|
// Publisher startup is asynchronous. Do not expose the pairing QR until
|
|
// Windows reports Started; otherwise Android can scan a network that is
|
|
// not yet discoverable.
|
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
|
loop {
|
|
let status = publisher.Status().context("读取 AP 状态失败")?;
|
|
if status == WiFiDirectAdvertisementPublisherStatus::Started {
|
|
break;
|
|
}
|
|
if status == WiFiDirectAdvertisementPublisherStatus::Aborted {
|
|
bail!("Wi-Fi Direct AP 启动被系统中止。请确认 Windows Wi-Fi 已开启,且系统移动热点未占用 Wi-Fi Direct。")
|
|
}
|
|
if std::time::Instant::now() >= deadline {
|
|
bail!("Wi-Fi Direct AP 启动超时。请确认无线网卡支持 Wi-Fi Direct,并关闭 Windows 移动热点后重试。")
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_millis(80));
|
|
}
|
|
|
|
Ok(Self { ssid, publisher })
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for DirectAp {
|
|
fn drop(&mut self) {
|
|
#[cfg(windows)]
|
|
{
|
|
let _ = self.publisher.Stop();
|
|
}
|
|
}
|
|
}
|