- Home /
Bluetooth Support on Win and OSX
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.
Haven't found an in Unity solution. Had to fallback to developing a WinForms companion application. Unity and the companion communicate through a TCP connection. It's not an ideal work around, but I got what I needed working.
Still looking for a true answer though.
Answer by Freefly18 · May 09, 2015 at 12:08 AM
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.
The issue with doing the plugin solution is I have too much on my plate with the game's development to really devote the time necessary. That and we need support for Windows and OSX. Still possible with a plugin, but someone doing a plugin would have to integrate the Windows and OSX solutions together to a degree while the man-in-the-middle allows for an entirely different app to jump and stand in the middle for OSX as long as it sends the information in the same manner.
I hope you didn't interpret my answer as saying that your solution was bad. Both have their pros and cons and I'm probably going to go for the man-in-the-middle myself since I'm short on time. I consider the plugin solution to be a slightly better one because it makes for a more elegant solution as well as not depending on a third application.
Speaking of which, may I ask what was your way of handling the execution of the man-in-the-middle? Does the user need to run 2 software or is there some triggering mechanism?
To trigger the man-in-the-middle I use the following line of code.
System.Diagnostics.Process.Start(storedAppPath);
In this the storedAppPath is the computed path to the man-in-the-middle exe which, in my case, lives next door to the Unity game's standalone executable.
The other half of this is quitting. In my implementation, it is the man-in-the-middles responsibility to detect that the game has disconnected from the other side of the TCP. If it detect this, it closes itself.
Hey @$$anonymous$$olluskJesse ! I need to do something similar for the HoloLens except with a Bluetooth GPS receiver. Would you be able to share your source code for your man-in-the-middle exe? I have been trying to find a solution to implement this and would really like to see how you implemented yours so I can try to use the same approach for my app.
I would appreciate any help on this.
Thanks!
Not sure if he answered back, but I just pushed to GitHub what I had for my version of it, including the code for Android and the one for Windows: https://github.com/Freefly18/UnityDesktopToAndroidBluetooth/
Feel free to use the man-in-the-middle (see the readme). It translates anything from a connected Bluetooth device using a specific GUID (currently hardcoded, just search for "GUID"), to an application on the same computer, using TCP on port 13000 (once again you may want to change that).
Also feel free to use my code for the PC side (TCP server), although you'll propably have to change a lot of things in SmartphoneCommand.cs and other files with 'Smartphone' in it... all in all still a lot of work for you to do, but it should save you the hassle of having to deal with client-server stuff.