microsoft / Windows-universal-samples

API samples for the Universal Windows Platform.
MIT License
9.46k stars 7.99k forks source link

Second WiFi Direct connection fails #1399

Open orineman opened 7 months ago

orineman commented 7 months ago

Which sample are you reporting a bug in?

WiFiDirect

Describe the bug

Second connection to a give Windows computer running the sample fails.

To Reproduce Steps to reproduce the behavior:

  1. Two computers running Windows 11 with WiFi Direct capability are required.
  2. The computers being used must not be paired - Use the Settings-Bluetooth & Devices > Devices "Remove device" option so the computers don't know about each other.
  3. Run the C# WiFi Direct sample on both computers.
  4. Use the sample to make a WiFi Direct connection.
  5. Close the other device on both computers.
  6. Attempt a second connection (this usually requires that you Stop /start advertisement on one machine and Stop/Start the watcher on the other machine or the other machine isn't found).
  7. The machine being connected to shows as "not paired", but you get this error displayed: "PairAsync failed, Status: AlreadyPaired".

Expected behavior

The connection should be successfuil.

Screenshots

image

Configuration

Additional context

The sample relies on DeviceInformationPairing.IsPaired. It is false in this case. If however, you call CreateFromIdAsync() for the device and ask for "System.Devices.Aep.IsPaired", the "System.Devices.Aep.IsPaired" property will be true. Similarly for DeviceInformationPairing.CanPair and "System.Devices.Aep.CanPair". The documentation implies DeviceInformationPairing.IsPaired is derived from "System.Devices.Aep.IsPaired", whatever that means...

It should be noted that this is a problem on the Connector side only. The Advertiser side uses an additional function, IsAepPaired() to determine if pairing is necessary.

There are various ways of making the sample work should this be an OS "feature". Don't try to pair if CanPair is false seems to work. Probably better is to call CreateFromIdAsync() for the device and use the System.Devices.Aep.IsPaired/System.Devices.Aep.CanPair properties, if present. And finally, I suppose the AlreadyPaired error could be ignored!

orineman commented 7 months ago

I suppose I should add that if we believe the System.Devices.Aep.IsPaired property, then the "Unpaired" designation in the "Discovered Devices" list is incorrect.

Whether this turns out to be an OS bug or not, the sample should take account of this problem as anyone shipping product will have to use some kind of workaround for this problem.

orineman commented 7 months ago

So, if you ask for System.Devices.Aep.IsPaired in the list of properties passed to DeviceInformation.CreateWatcher(), then the System.Devices.Aep.IsPaired property is copied to DeviceInformation.IsPaired. Similarly for CanPair.

                string deviceSelector = WiFiDirectDevice.GetDeviceSelector(Utils.GetSelectedItemTag<WiFiDirectDeviceSelectorType>(cmbDeviceSelector));
                _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector,
                    new string[] {
                        "System.Devices.WiFiDirect.InformationElements",
                        "System.Devices.Aep.CanPair",
                        "System.Devices.Aep.IsPaired"
                    }
                );

I'd suggest this modification is made to the sample. It fixes both this issue and the misreporting of the pairing status in the Discovered Devices list.

I've not found any documentation of this 'feature'.