UWP WiFi-Direct 在第二次传入 StreamSocketListener 连接后断开连接

UWP WiFi-Direct disconnects after second incoming StreamSocketListener connection

目前我即将弄清楚,我在 Windows 10 IoT Core 上的 UWP 应用程序中直接使用 WiFi 的问题是什么。目标是让 StreamSocketListener 接受来自 WiFi-direct 的传入连接。这适用于我建立的第一个连接,对 StreamSocketListener 的第二个请求会破坏 WiFi-direct 会话并断开连接。作为旁注,我必须使用带有 SSID 和密码短语的旧版 WiFi-Direct 实现。这是一些示例代码

WiFi Direct 发布者

WiFiDirectAdvertisementPublisher _publisher = new WiFiDirectAdvertisementPublisher();
_publisher.StatusChanged += OnStatusChanged;

WiFiDirectConnectionListener _listener = new WiFiDirectConnectionListener();

try
{
    _listener.ConnectionRequested += OnConnectionRequested;
}
catch
{
    return;
}

_publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal;

_publisher.Advertisement.IsAutonomousGroupOwnerEnabled = true;
_publisher.Advertisement.LegacySettings.IsEnabled = true;

PasswordCredential creds = new PasswordCredential();
creds.Password = passphrase;
_publisher.Advertisement.LegacySettings.Passphrase = creds;
_publisher.Advertisement.LegacySettings.Ssid = ssid;

_publisher.Start();

if (_publisher.Status == WiFiDirectAdvertisementPublisherStatus.Started)
{
    // OK
}
else
{
    // Error
}

OnConnectionRequested 事件处理程序

WiFiDirectConnectionRequest connectionRequest = connectionEventArgs.GetConnectionRequest();

WiFiDirectDevice wfdDevice = null;

try
{
    wfdDevice = await WiFiDirectDevice.FromIdAsync(connectionRequest.DeviceInformation.Id);
}
catch
{
    //
}

wfdDevice.ConnectionStatusChanged += OnConnectionStatusChanged;

listEndpointPairs = wfdDevice.GetConnectionEndpointPairs();

StreamSocketListener

_listener = new StreamSocketListener();
await _listener.BindEndpointAsync(listEndpointPairs[0].LocalHostName, "80");
listener.ConnectionReceived += (sender, args) => ConnectionReceivedHandler(args);

正如我之前所说,它运行良好,但我无法向 StreamSocketListener 发送第二个请求,如果我这样做,WiFi 直接连接将断开连接。我错过了什么或犯了错吗?

谢谢指教

如果您在同一个端口号上设置多个连接(您使用“80”),您可能会遇到以下异常:

Only one usage of each socket address (protocol/network address/port) is normally permitted.

所以你需要为每个连接请求分配不同的端口号。

或者您可以使用空字符串代替指定的数字,系统会为您分配一个唯一的端口号。可以参考here.

我认为这已解决,因为 MS write 在 build 17110(当前测试版)的更新日志中指出以下已知问题

The IoTCore device has to be the connecting device – it will not work as the advertising device with another device initiating the connection.

看来目前没有办法做到这一点