Bluetooth low energy on windows under unity

Hi,

I’m working on a project using Bluetooth low energy on uwp application.

I already worked with visual studio to obtain orientation of the thingy (nordic semi conductor) but i would now integrate it to unity.

the main issue is that i cannot use

using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;

when i had them, unity doesn’t want compilate.

may i create a c# dll for my functions using visual studio and after integrate it to my project or is it possible to add the “Windows.Devices.Bluetooth.dll” Library to add support to mono ?

already thanks for your reponses.

Br,
Christophe

You can use these namespaces if you wrap you code in #if ENABLE_WINMD_SUPPORT/#endif. Note that the code will only be compatible with UWP player, and will not execute in the editor.

ok, i’ll test. thank you for your quick response

I wanted to connect our VR game (Steam VR or Oculus) with our custom Bluetooth LE controller. Any ideas how to use Windows SDK not on UWP platform? Native plugin for Win made in C++? If it’s not possible I’ll try to make it as a separate UWP app that connects via Socket.

Yeah you’ll have to use C++ in a native plugin.

yes, i agree with you, we need a c++ plugin.

i take the project on
https://bitbucket.org/Unity-Technologies/windowsstoreappssamples

and i updated the two projects:

for the first one is used :

#include <windows.h>
#include <stdio.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#include <bthdef.h>
#include <bthledef.h>
#include <Bluetoothleapis.h>
#pragma comment(lib, “SetupAPI”)
#pragma comment(lib, “BluetoothApis.lib”)

in the stdafx.h file and i based the projest on :

https://social.msdn.microsoft.com/Forums/lync/en-US/bad452cb-4fc2-4a86-9b60-070b43577cc9/is-there-a-simple-example-desktop-programming-c-for-bluetooth-low-energy-devices?forum=wdk

i added the function in stdafx.cpp to verify the compilation:

void SomethingHappened(BTH_LE_GATT_EVENT_TYPE EventType, PVOID EventOutParameter, PVOID Context)

for the second project (wsa plugins) t was more difficult because winrt wasn’t unable to compilate, i found the soultion as this:

in stdafx.h

// Windows Header Files:
#pragma comment(lib, “windowsapp”)
#include “Windows.Foundation.h”
#include “Windows.Web.Syndication.h”
#include “Windows.devices.bluetooth.h”
#include “Windows.devices.bluetooth.background.h”
#include “Windows.devices.bluetooth.advertisement.h”

and in the stdafx.cpp

#include “stdafx.h”
using namespace std;
using namespace Windows;
using namespace Windows::smile:evices;
using namespace Windows::smile:evices::Bluetooth;
using namespace Windows::smile:evices::Bluetooth::Background;
using namespace Windows::smile:evices::Bluetooth::Advertisement;
using namespace Windows::smile:evices::Bluetooth::GenericAttributeProfile;

but to conclude the conpilation, you need to change the project as this:

use Windows extension runtime in the project properties Under C/C++ and General Yes(/ZW)

after you can compilate the basic dll standlone dll and the wsa with winrt plugin

it’s for the c++ part.

i’ll check for the c# dll plugin possibility a i already did the code for the wesu (st micro electronics) and the thingy from nordic.

To Tautvydas-Zilys:

don’t hesitate to comment to see if it feels right for you.

Br,
could be the basics to implement it in the next version of unity ;->

I don’t think we have plans to add bluetooth functionality to Unity at this time…

ok, no problem.

I’ll check the third case to cover my project: une a c# dll to support the Bluetooth.

in this case 3 possibility:
c++ dll for stand alone project.
c++ dll with was for Windows store.
dedicated dll compose in a version ‘fake’ for the editor with the mono framwork and one with the winrt Framework to be able to run.

but the ++ dll feels to be the best way.

Br,
Christophe.

Remark: we see Bluetooth packages for ios and android but not uwp/Windows standalone.
so i think it can be a good idea for the asset market.

Ok, the stand Alone plugin work.
I ll create a generic version of the stand alone plugin.
I m working now
on the uwp version
Already Nordic semi / stmicro compliant.
I plan to do the same thing with Philips nxp kinetics version

@titof49 Just curious when your bluetooth standalone and/or uwp asset might be available in the Unity store or elsewhere?

@titof49 Can you please update with the result? we have the same problem and curious if you can share your solution
Thanks!

I am newbie in Unity3d.I checked for a bluetooth plugin for unity and i found out that in-order to use plugin we need to know Java for android and Swift for IOS.I dont know these two languages.All I know is Csharp and unity.Is there anyway I could learn how to make a plugin.Any tutorials?

@zyonneo for iOS/android this works quite welll and we’ve been using it for many projects: Bluetooth LE for iOS, tvOS and Android | Network | Unity Asset Store

For windows currently nothing on the asset store for BLE. I may find a way to get it to work as well using the native dll method. Will post if I get it to work.

1 Like

@qoobit I got this plugin…but the problem is how to show indoor navigation using beacons…It is tough to show which direction the beacons are placed…You can get the RSSI values(range of beacon).How to show a path?I got no clue.

If you have the RSSI I think you can use triangulation principles if you have two or more devices. Triangulation (surveying) - Wikipedia
You would effectively measure out the RSSI falloff graph for each of your beacons and based on values of each you can estimate which ring they are in for each beacon. Overlapping points will determine where they roughly are in the space. In certain scenarios you will have two overlaps so you usually require an additional device in range to eliminate the possibilities.

1 Like

@qoobit Thanks will look into that…Recently saw a video about indoor navigation without using beacons.So was looking to do that as beacons are getting outdated.Currently researching how to do it without using beacons.

See also this thread Using UWP API in Standalone Build
One user suggests to use a 3rd party commercial lib Bluetooth Framework - Easy Bluetooth Support for Your Applications
They advertise they have Unity 2018 support (for .NET 4.6 backend).

Also needing BLE for the Unity Editor on Windows, I decided to write my own C++ UWP-wrapper. Those interested can take it as a starting point for theirs: adabru/BleWinrtDll: BLE for Unity 3d in Windows without UWP. (github.com).

2 Likes