mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Create a Device Object for Hotspot Client to avoid being disconnected by Hotspot.
Without creating a Device Object for the connection from a Hotspot Client, [Microsoft-Windows-WLAN-AutoConfig] may think it is a malign device and will disconnect it after 2 minutes, which will also causes Hotspot being deleted eventually. PiperOrigin-RevId: 466395817
This commit is contained in:
@@ -53,6 +53,8 @@ using ::winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionListener;
|
||||
using ::winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionRequest;
|
||||
using ::winrt::Windows::Devices::WiFiDirect::
|
||||
WiFiDirectConnectionRequestedEventArgs;
|
||||
using ::winrt::Windows::Devices::WiFiDirect::WiFiDirectDevice;
|
||||
using ::winrt::Windows::Devices::WiFiDirect::WiFiDirectConnectionStatus;
|
||||
|
||||
using ::winrt::Windows::Devices::WiFi::WiFiAccessStatus;
|
||||
using ::winrt::Windows::Devices::WiFi::WiFiAdapter;
|
||||
@@ -81,6 +83,8 @@ using ::winrt::Windows::Networking::Sockets::StreamSocketListener;
|
||||
using ::winrt::Windows::Networking::Sockets::
|
||||
StreamSocketListenerConnectionReceivedEventArgs;
|
||||
|
||||
// using winrt::Windows::Foundation::IInspectable;
|
||||
|
||||
// WifiHotspotSocket wraps the socket functions to read and write stream.
|
||||
// In WiFi HOTSPOT, A WifiHotspotSocket will be passed to
|
||||
// StartAcceptingConnections's call back when StreamSocketListener got connect.
|
||||
@@ -267,6 +271,7 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
|
||||
|
||||
WiFiDirectAdvertisementPublisher publisher_{nullptr};
|
||||
WiFiDirectConnectionListener listener_{nullptr};
|
||||
WiFiDirectDevice wifi_direct_device_ {nullptr};
|
||||
|
||||
fire_and_forget OnStatusChanged(
|
||||
WiFiDirectAdvertisementPublisher sender,
|
||||
|
||||
@@ -198,10 +198,11 @@ bool WifiHotspotMedium::StopWifiHotspot() {
|
||||
|
||||
if (publisher_) {
|
||||
publisher_.Stop();
|
||||
publisher_.StatusChanged(publisher_status_changed_token_);
|
||||
listener_.ConnectionRequested(connection_requested_token_);
|
||||
publisher_ = nullptr;
|
||||
publisher_.StatusChanged(publisher_status_changed_token_);
|
||||
wifi_direct_device_ = nullptr;
|
||||
listener_ = nullptr;
|
||||
publisher_ = nullptr;
|
||||
NEARBY_LOGS(INFO) << "succeeded to stop WiFi Hotspot";
|
||||
}
|
||||
|
||||
@@ -242,8 +243,9 @@ fire_and_forget WifiHotspotMedium::OnStatusChanged(
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (publisher_ != nullptr) {
|
||||
NEARBY_LOGS(ERROR) << "Windows WiFi Hotspot cleanup.";
|
||||
publisher_.StatusChanged(publisher_status_changed_token_);
|
||||
listener_.ConnectionRequested(connection_requested_token_);
|
||||
publisher_.StatusChanged(publisher_status_changed_token_);
|
||||
wifi_direct_device_ = nullptr;
|
||||
listener_ = nullptr;
|
||||
publisher_ = nullptr;
|
||||
}
|
||||
@@ -260,13 +262,22 @@ fire_and_forget WifiHotspotMedium::OnConnectionRequested(
|
||||
NEARBY_LOGS(INFO) << "Receive connection request from: "
|
||||
<< winrt::to_string(device_name);
|
||||
|
||||
DeviceInformationPairing pairing =
|
||||
connection_request.DeviceInformation().Pairing();
|
||||
if (pairing.IsPaired())
|
||||
NEARBY_LOGS(INFO) << "Paired";
|
||||
else
|
||||
NEARBY_LOGS(INFO) << "Not Paired";
|
||||
|
||||
try {
|
||||
// This is to solve b/236805122.
|
||||
// Problem: [Microsoft-Windows-WLAN-AutoConfig] issues a disconnection to
|
||||
// Hotspot Client every 2 minutes and stopped Hotspot eventually.
|
||||
// Solution: Creating a WiFiDirectDevice for Client’s connection request can
|
||||
// solve the problem. Guess when this object is created,
|
||||
// [Microsoft-Windows-WLAN-AutoConfig] will recognise it as a valid device
|
||||
// and won't kick it away.
|
||||
wifi_direct_device_ = WiFiDirectDevice::FromIdAsync(
|
||||
connection_request.DeviceInformation().Id()).get();
|
||||
NEARBY_LOGS(INFO) << "Registered the device in WLAN-AutoConfig";
|
||||
} catch (...) {
|
||||
NEARBY_LOGS(ERROR) << "Failed to registered the device in WLAN-AutoConfig";
|
||||
wifi_direct_device_ = nullptr;
|
||||
connection_request.Close();
|
||||
}
|
||||
return winrt::fire_and_forget();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user