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

577 views Asked by At

I would like to interface with Programmatically Interface with the WiFi Hotspot hosted on my RPI running Windows IoT Core. I figured out host the WiFi Hotspot part, windows 10 iot raspberry pi 3 wifi hotspot but now I would like to get the list of devices connected to this network. Is this Possible?

1

There are 1 answers

2
Michael Xu On BEST ANSWER

You can use WiFiDirectConnectionListener to achieve this goal. When other devices connect to host device over the Wifi SoftAP, WiFiDirectConnectionListener will capture a connect request.

    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);
        }
    }

The name of DeviceInformation is the MAC of you device which you to connect the wifi hotspot, you can get more information by setting a breakpoint. When you drop the connection, the WiFiDirectDevice will invoke ConnectionStatusChanged event.