以编程方式获取连接到 Windows IoT Core 机器上托管的 WiFi 热点的设备列表,RPI

Programatically get list of devices connected to WiFi Hotspot hosted on a Windows IoT Core machine, RPI

我想以编程方式与我的 RPI 运行 Windows IoT Core 上托管的 WiFi 热点进行交互。我想出了托管 WiFi 热点部分, 但现在我想获取连接到该网络的设备列表。这可能吗?

您可以使用 WiFiDirectConnectionListener 来实现这个目标。当其他设备通过 Wifi SoftAP 连接到主机设备时,WiFiDirectConnectionListener 将捕获连接请求。

    private Dictionary<string,DeviceInformation> connectionDeviceList = new Dictionary<string, DeviceInformation>();

    WiFiDirectConnectionListener wifiAPListener = new WiFiDirectConnectionListener();
    wifiAPListener.ConnectionRequested += WifiAPListener_ConnectionRequested;

    private void WifiAPListener_ConnectionRequested(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs args)
    {
        try
        {
            var request = args.GetConnectionRequest();
            var devInfo = request.DeviceInformation;

            var wfdDevice = await WiFiDirectDevice.FromIdAsync(devInfo.Id);
            wfdDevice.ConnectionStatusChanged += WfdDevice_ConnectionStatusChanged;

            if (!connectionDeviceList.ContainsKey(devInfo.Id))
            {
                connectionDeviceList.Add(devInfo.Id, devInfo);
            }
        }
        catch(Exception ex)
        {
            Debug.Write(ex.StackTrace);
        }           
    }

    private void WfdDevice_ConnectionStatusChanged(WiFiDirectDevice sender, object args)
    {
        try
        {
            if (sender.ConnectionStatus == WiFiDirectConnectionStatus.Disconnected)
            {
                if (connectionDeviceList.ContainsKey(sender.DeviceId))
                {
                    connectionDeviceList.Remove(sender.DeviceId);
                }
            }
        }
        catch(Exception ex)
        {
            Debug.Write(ex.StackTrace);
        }
    }

DeviceInformation的名称是您要连接wifi热点的设备MAC,您可以通过设置断点来获取更多信息。当您断开连接时,WiFiDirectDevice 将调用 ConnectionStatusChanged 事件。