【问题标题】:UWP WiFi-Direct disconnects after second incoming StreamSocketListener connectionUWP WiFi-Direct 在第二次传入 StreamSocketListener 连接后断开连接
【发布时间】:2018-01-16 02:13:17
【问题描述】:

目前我正要弄清楚,我在 Windows 10 IoT Core 上的 UWP 应用程序中的 WiFi 直接问题是什么。目标是让 StreamSocketListener 接受来自 WiFi-direct 的传入连接。这适用于我创建的第一个连接,对 StreamSocketListener 的第二个请求会破坏 WiFi 直接会话并断开连接。作为旁注,我必须使用带有 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();

流套接字监听器

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

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

感谢您的建议

【问题讨论】:

  • 你的 windows iot 核心设备是什么?
  • 具有最新版本的 windows 10 iot 核心的树莓派 3。
  • 你使用板载wifi还是外置wifi适配器?
  • 我正在使用板载 wifi 适配器。

标签: c# uwp win-universal-app wifi-direct windows-10-iot-core


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢,我知道,但这不是我的问题。当我设置 WiFi Direct Publisher 并运行 StreamSocketListener (在我的情况下在端口 80 上)时,我可以连接一个设备,例如通过 WiFi Direct 选择 SSID 并输入密码的智能手机。连接建立,我可以向 StreamSocketListener 发送请求。但是当我尝试向 StreamSocketListener 发出第二个请求时,WiFi 直接连接被我树莓上的应用程序破坏了。
  • 遗憾的是没有,没有异常或其他错误。我正在用电话连接并发送一个请求,这完美地完成了。然后我发送另一个请求并且没有任何反应,如果我再查看我的“OnConnectionStatusChanged”事件处理程序,我看到“WiFiDirectConnectionStatus”断开连接。
【解决方案2】:

我认为这已经解决了,因为 MS write 在 build 17110(当前 beta)的更改日志中说以下已知问题

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

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

【讨论】:

    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 2017-07-20
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多