Original issue:
new BuzzHandsetFinder().FindHandsets() still found controllers even if they were detached in the meanwhile.
Background:
I had an issue with my game when no controllers were connected. So I added a "rescan for controllers" feature in order to be able to detect controllers that were connected to the PC recently. For testing purposes, I started without a controller. After attaching the controller, the newly attached controller was found. After removing the controller, I pressed the rescan button again, but the controller still appeared.
Source Code:
I looked at BuzzHandsetDevice.cs, FindBuzzHandsets(), which does not use caching. Also, HIDDevice.cs, FindDevices() does not seem to use caching. So I expected a problem somewhere in my code. I wrote the following minimal example - but its output confuses me even more.
using System;
using System.Linq;
using System.Threading;
using BuzzIO;
namespace ControllerScanTest
{
class Program
{
static void Main()
{
while (true)
{
var h = new BuzzHandsetFinder().FindHandsets();
Console.WriteLine(h.Count());
Thread.Sleep(2000);
}
}
}
}
Output:
The output is just 1 when I insert the controller. Even when I leave it connected, the next loop it returns 0. That's the opposite of what I found out in my game.
Another try:
I saw that HIDDevice is disposable. BuzzHandsetDevice inherits from HIDDevice, is it's also disposable. However, from the interface we get back from FindHandsets(), it's not possible to dispose the objects.
Adding the following code inside the loop results in exceptions:
foreach (var device in h)
{
((HIDDevice) device)?.Dispose();
}
Question:
What is the correct way to rescan for controllers?