0

I am trying to write a program in C# with that connects to the Nintendo Switch JoyCon controller (which is a Bluetooth HID device) using the 32feet library. I want to bypass the buggy Bluetooth pairing system in Windows 10, so this is why I am writing my own solution. Also, I am aware that there are many drivers out there for this controller, but I haven't had much luck with any of them, and I want to try to make it myself.

Here is my code so far:

var client = new BluetoothClient();
var devices = client.DiscoverDevices().ToList();
var device = devices.FirstOrDefault(d => d.DeviceName.Contains("Joy-Con (L)"));

if (device != null)
{
    Console.Out.WriteLine("Found Device!");
    Console.Out.WriteLine("    Name: " + device.DeviceName);
    Console.Out.WriteLine("    Address: " + device.DeviceAddress);
    Console.Out.WriteLine("    Authenticated: " + device.Authenticated);
    Console.Out.WriteLine("    Connected: " + device.Connected);
    Console.Out.WriteLine("    HID: " + device.InstalledServices.Contains(BluetoothService.HumanInterfaceDevice));

    try
    {
        Console.Out.WriteLine("Enabling Service...");
        device.SetServiceState(BluetoothService.HumanInterfaceDevice, true);
        Console.Out.WriteLine("Connecting...");
        client.Connect(device.DeviceAddress, BluetoothService.HumanInterfaceDevice); // Program.cs:line 36
        Console.Out.WriteLine("Connected!");
    }
    catch (Exception e)
    {
        Console.WriteLine("Failed to connect.");
        Console.Out.WriteLine(e);
    }

    var stream = client.GetStream();

// ...

    Console.ReadLine();
}

Currently my program can discover the device, but when I try to connect, I get an exception.

Here is the output from the code above:

Found Device!
    Name: Joy-Con (L)
    Address: 48A5E761AB55
    Authenticated: True
    Connected: False
    HID: True
Enabling Service...
Connecting...
Failed to connect.
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

As you can see, the device is paired (device.Authenticated) and it reports that it supports HID.

I've never used 32feet before, so I might be missing something from the code. Any help is appretiated.

Lázár Zsolt
  • 685
  • 8
  • 29
  • One of two things is happening 1) You are trying to connect to a server that is more than the strength of the Wifi 2) The is no route to the server. From cmd.exe >Ping IP to see if you have a route. Always try Ping to debug. If ping works then you do not have a server with the port listening. You can use from cmd.exe >Netstat -a at both client and server to see the status of the connection (look at port number). – jdweng Aug 17 '20 at 23:54
  • @jdweng Did you mean to comment on this question? I can't see what my question has to do with servers, wifi or networking. I am asking about bluetooth programming. – Lázár Zsolt Aug 17 '20 at 23:59
  • BNEP uses IP over Bluetooth. You are getting an IP exception (Net.Sockets). The RF may be BlueTooth but the connection to/from the BlueTooth device is over ethernet. – jdweng Aug 18 '20 at 00:52
  • @jdweng Okay, how do I get the IP address of the bluetooth device to ping? (sorry if I came off as rude in the first comment, I didn't know about BNEP) – Lázár Zsolt Aug 18 '20 at 01:12
  • You can not connect to HID on Windows using Sockets. HID BT uses L2CAP level, byt Windows supports only RFCOMM. – Mike Petrichenko Aug 18 '20 at 06:00
  • Good question. Look at user manual. Ping can use IP or device name. You can have a Bluetooth Stereo System with Speaker are Blue tooth and the Stereo having an IP connection (hardwire ethernet or WiFi). Then you can download music into the Stereo from a PC. You are failing the IP connection where you are trying to download music or send a command to play a song. Every IP device has a MAC address. Your is shown on following : https://dnschecker.org/mac-lookup.php?query=48A5E761AB55. You should be able to get IP from cmd.exe look at MAC >ARP -a – jdweng Aug 18 '20 at 06:07

0 Answers0