title: Make a C# console app (.net core 3.1) to automatically connect Joycons in paring mode (InTheHand and BluetoothAPIs.dll)
body: <p>I am trying to write a program in C# that connects to the Nintendo Switch JoyCon controller (which is a Bluetooth HID device) using the InTheHand library and BluetoothAPIs.dll on Windows 11 (But should work for Win10).</p>
  <p>My goals are to:</p>
  <ol>
  <li>remove/unpair all paired Joycon in a first step</li>
  <li>list all Joycon devices in pairing mode state</li>
  <li>automatically pair them</li>
  </ol>
  <p>I'm using a mix of InTheHand library and BluetoothAPIs.dll Win32 API (Imported inside of C#) and running into issues.</p>
  <pre><code>BluetoothClient client = new BluetoothClient();
  
          // Getting connected devices and disconnecting Joycon before repairing them
          var connectedDevices = client.PairedDevices;
          foreach (BluetoothDeviceInfo d in connectedDevices)
          {
              Console.WriteLine(d.DeviceName);
              if (d.DeviceName.Contains(&quot;Joy-Con&quot;) == true)
              {
                  Console.WriteLine(d.DeviceName + &quot;-&quot; + d.Connected + &quot;-&quot; + d.Authenticated + &quot;-&quot; + d.DeviceAddress);
                  UInt32 removingResult = BlueToothStuff.Unpair(d.DeviceAddress);
                  if (removingResult == 0)
                  {
                      Console.WriteLine(&quot;SUCCESS!&quot;);
                  }
                  else
                  {
                      Console.Write(&quot;ERROR... {0}&quot;, removingResult);
                  }
              }
          }
  </code></pre>
  <p>The code for BlueToothStuff.Unpair is here:</p>
  <pre><code>static public UInt32 Unpair(UInt64 Address)
      {
          BLUETOOTH_ADDRESS Addr = new BLUETOOTH_ADDRESS();
          Addr.ullLong = Address;
          UInt32 result = BluetoothRemoveDevice(ref Addr);
          return result;
      }
  
      [StructLayout(LayoutKind.Explicit)]
      public struct BLUETOOTH_ADDRESS
      {
          [FieldOffset(0)]
          [MarshalAs(UnmanagedType.I8)]
          public UInt64 ullLong;
          [FieldOffset(0)]
          [MarshalAs(UnmanagedType.U1)]
          public Byte rgBytes_0;
          [FieldOffset(1)]
          [MarshalAs(UnmanagedType.U1)]
          public Byte rgBytes_1;
          [FieldOffset(2)]
          [MarshalAs(UnmanagedType.U1)]
          public Byte rgBytes_2;
          [FieldOffset(3)]
          [MarshalAs(UnmanagedType.U1)]
          public Byte rgBytes_3;
          [FieldOffset(4)]
          [MarshalAs(UnmanagedType.U1)]
          public Byte rgBytes_4;
          [FieldOffset(5)]
          [MarshalAs(UnmanagedType.U1)]
          public Byte rgBytes_5;
      };
      [DllImport(&quot;BluetoothAPIs.dll&quot;, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
      [return: MarshalAs(UnmanagedType.U4)]
      static extern UInt32 BluetoothRemoveDevice([param: In, Out] ref BLUETOOTH_ADDRESS pAddress);
  }
  </code></pre>
  <p>For the 2) and 3) I use InTheHand:</p>
  <pre><code>var task = Task.Run(async () =&gt; {
              while (true)
              {
                  Console.WriteLine(&quot;Looking...&quot;);
                  var devices = client.DiscoverDevices();
                  foreach (BluetoothDeviceInfo d in devices)
                  {
                      if (d.DeviceName.Contains(&quot;Joy-Con&quot;) == true)
                      {
                          client.Connect(d.DeviceAddress, BluetoothService.HumanInterfaceDevice);
  
                      }
                  }
              }
          });
  
          Console.WriteLine(&quot;\nLOOKING FOR Joycon NOW, --&gt; Press any key when all your Joycon are paired!&quot;);
          Console.ReadKey(true);
  </code></pre>
  <p>My issue is that client.Connect(d.DeviceAddress, BluetoothService.HumanInterfaceDevice); is systematically crashing:</p>
  <blockquote>
  <p>System.Net.Sockets.SocketException (0x80004005): A socket operation failed because the destination host was down
  at InTheHand.Net.Sockets.Win32Socket.ThrowOnSocketError(Int32 result, Boolean throwOnDisconnected)
  at InTheHand.Net.Sockets.Win32Socket.Connect(EndPoint remoteEP)
  at InTheHand.Net.Sockets.BluetoothClient.DoConnect(BluetoothAddress address, Guid service)
  at InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothAddress address, Guid service)
  at BTJoyConTest.Program.Main(String[] args) in Program.cs:line 36</p>
  </blockquote>
  <p>The Joycon seems connected and paired in the Bluetooth window, but if I relaunch my program, InTheHand method to list paired devices (client.PairedDevices) don't see them at all... so they can't be unpaired again.</p>
  <p>As a quick solution I would say that maybe using BluetoothAPIs.dll to pair the Joycon would work better, but I don't have any clue of the .dll function I should use here (If someone knows it will help).</p>
  <p>Other solution would be to make it work with InTheHand. Is someone else had already the issue?</p>
  <p>Thx in advance.</p>