While there appears to be plenty of Bluetooth solutions for mobile platforms on the Unity Asset store, I’d like to know if anyone has any insight into supporting Bluetooth on Windows and Mac OSX platforms. Ideally, it would be best to have a solution that is itself cross platform.
I was having similar issues while looking for a way to communicate between 2 Unity applications, one on PC and one on Android. As far as I can tell, there is a lot of solutions for Mobile (notably Android Bluetooth Multiplayer), but none for PC. Even well tested Bluetooth libraries for C# (like 32feet.NET) fails on Unity3D and it took me a while to learn why. Here’s some valid C# code:
using System.Net.Sockets;
Socket s = new Socket((AddressFamily)32, SocketType.Stream, (ProtocolType)3);
32 is for a Bluetooth address and 3 is for RFCOMM protocol.
Try this in .NET 3.5 and it will work as intended. Try it in Unity with Mono 2.0 and you will get this Exception:
System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used.
This means that Mono 2.0 does not support Bluetooth Sockets and, as far as I know, you cannot do bluetooth communications using only C# in Unity3D.
I see 2 workarounds:
- Do what MolluskJesse did and implement a man-in-the-middle application made in your favorite language, that handles Bluetooth communications and talks with your Unity3D software using TCP or some other interface. This solution is good if you want to use a specific library and if you’re only fluent in C#.
- The correct solution, IMO, would be to develop a C++ plugin for your Unity3D software. The plugin would implement all the Bluetooth communication and would offer some callbacks and methods to the C# code. This of course require you to know well enough C/C++ and have a Pro version of Unity3D.
Hope this helps others.