# Using bluetoothapis.h

**URL:** <https://forums.unrealengine.com/t/using-bluetoothapis-h/306060>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 5, 2015, 3:28pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060 "2015-02-05T15:28:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [February 5, 2015, 3:28pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/1 "2015-02-05T15:28:17Z")

</div>

Hey everybody,

i use the bluetoothapis.h in my unreal project to recieve sensor data from my smartphone.  
I include it this way in my cpp:

#include “AllowWindowsPlatformTypes.h”  
#include “bluetoothapis.h”  
#include “HideWindowsPlatformTypes.h”

but i still get this error:

BluetoothConnector.cpp.obj : error LNK2019: Verweis auf nicht aufgeführtes externes Symbol “BluetoothFindFirstDevice” in Funktion ““public: virtual unsigned int \_\_cdecl BluetoothConnector::Run(void)” (?Run@BluetoothConnector@@UEAAIXZ)”.  
E:\Workspaces\GameDev\Binaries\Win64\UE4Editor-CaveUniverse.dll : fatal error LNK1120: 1 nicht aufgeführtes Externe

Anyone got a solution??

Greetings  
Kosren

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [February 5, 2015, 3:34pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/2 "2015-02-05T15:34:13Z")

</div>

You need to link library in build script for UBT

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

Keep in mind that UE4 use VS only for complier, it has it’s own build system called UnrealBuildTool and it ignores VS configuration

---

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [February 5, 2015, 4:23pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/3 "2015-02-05T16:23:08Z")

</div>

And the award goes to you!! Thank you verry much!

---

<div class="post-metadata">

**Author:** ![joeycampbell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joeycampbell](https://forums.unrealengine.com/u/joeycampbell)\
**Post date:** [May 1, 2015, 11:57am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/4 "2015-05-01T11:57:53Z")

</div>

Hi Kosren - I’m working on a college project to bring in heart rate data as a game variable.  
I presume you coded a UE4 blueprint to integrate bluetooth using c++ ?  
Were there many lines of code involved and did it take long to figure out ?  
Is there any latency in reading the bluetooth data ?  
Any help or advice would be great.  
Joey

---

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [May 1, 2015, 2:00pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/5 "2015-05-01T14:00:00Z")

</div>

Hi joeycompbell, i coded a pure c++ class which read from a bluetooth socket. I used the windwos sockets. The class got round about 60 lines of codes, but many of them are just thread handling. I dont got any latency problems while reading the datas. I used a queue to process them. I used this thread tutorial as an entry point [Maintenance - Unreal Engine](https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4). After that I fight me through the mircosoft win\_socket documentation. I hope that helps you.  
Happy Coding  
Kosren

---

<div class="post-metadata">

**Author:** ![joeycampbell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joeycampbell](https://forums.unrealengine.com/u/joeycampbell)\
**Post date:** [May 4, 2015, 7:03am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/6 "2015-05-04T07:03:16Z")

</div>

Hi Kosren - would you consider packaging your bluetooth script as a plugin and selling it on the Unreal Market place ?

---

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [May 5, 2015, 4:35pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/7 "2015-05-05T16:35:21Z")

</div>

Hi joey, i will post the code of the handler in a new answer - Kosren

---

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [May 5, 2015, 4:42pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/8 "2015-05-05T16:42:05Z")

</div>

If someone need it too, here is my code!

BluetoothConnector.h

```
#pragma once

#include "CaveUniversePlayerController.h"
#include "AllowWindowsPlatformTypes.h"
#include "bluetoothapis.h"
#include <WinSock2.h>
#include <ws2bth.h>
#include "HideWindowsPlatformTypes.h"

/**
 * A thread which get data from bluetooth dongle
 */
class CAVEUNIVERSE_API BluetoothConnector : public FRunnable
{

	//Contstructor	
	public:
		BluetoothConnector(); 
		virtual ~BluetoothConnector();

	//Private attributes
	private:
		SOCKET bluetoothSocket; //The socket which listen to the bluetooth device
		SOCKET connected; //The socket which is connected to the smartphone via bluetooth
		FThreadSafeCounter stopTaskCounter; //stop the thread with this counter
		static BluetoothConnector* connector; //A static singleton to access the thread
		FRunnableThread* thread; //The running thread

	//Public methods from FRunnable
	public:
		virtual bool Init();
		virtual uint32 Run();
		virtual void Stop();

	//Public methods
	public:
		void EnsureCompletion(); //Proper shutdown
		static void joyInit(); //A static method to initialize the thread
		static void shutdownThread(); //A static method to kill the thread

};

```

BluetoothConnector.cpp

```
#include "CaveUniverse.h"
#include "BluetoothConnector.h"
#include <string>

BluetoothConnector* BluetoothConnector::connector = NULL;

BluetoothConnector::BluetoothConnector() {

	thread = FRunnableThread::Create(this, TEXT("BluetoothConnector"), 0, TPri_BelowNormal);

}

BluetoothConnector::~BluetoothConnector() {
	
	delete thread;
	thread = NULL;

}

//Initialize the thread
bool BluetoothConnector::Init() {

	return true;

}

//Run the thread
uint32 BluetoothConnector::Run() {

	//A char array to store the incoming data
	const int recvBufLen = 1024;
	char recvData[recvBufLen];

	//Data to initialize the winsocket
	WSADATA wsaData;

	//The socket which connects to the local device
	SOCKADDR_BTH socketAdress;
	bluetoothSocket = NULL;
	int error = SOCKET_ERROR;

	//initialize win socket
	error = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (error != NO_ERROR) {

		UE_LOG(LogTemp, Error, TEXT("Can not startup the win socket! Last Error:[%d]"), WSAGetLastError());
		WSACleanup();
		return 1;

	}

	//Create socket adress of the local socket to the bluetooth dongle
	bluetoothSocket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
	socketAdress.addressFamily = AF_BTH;
	socketAdress.btAddr = 0;
	socketAdress.serviceClassId = RFCOMM_PROTOCOL_UUID;
	socketAdress.port = BT_PORT_ANY;

	//Bind the socket to the local bluetooth device
	error = bind(bluetoothSocket, (SOCKADDR*)&socketAdress, sizeof(socketAdress));
	if (error == SOCKET_ERROR) {

		UE_LOG(LogTemp, Error, TEXT("Bluetooth socket can not be binded! Last Error:[%d]"), WSAGetLastError());
		closesocket(bluetoothSocket);
		WSACleanup();
		return 1;

	}

	//Listen on the bluetooth device for incoming connections
	error = listen(bluetoothSocket, 1);
	if (error == SOCKET_ERROR) {

		UE_LOG(LogTemp, Error, TEXT("Bluetooth socket listen failed! Last Error:[%d]"), WSAGetLastError());
		closesocket(bluetoothSocket);
		WSACleanup();
		return 1;

	}

	//The accepted socket to recieve data from
	UE_LOG(LogTemp, Log, TEXT("Waiting for incoming connection"));
	connected = accept(bluetoothSocket, NULL, NULL);
	UE_LOG(LogTemp, Log, TEXT("Device is connected"));

	if (connected != INVALID_SOCKET) {

		while (stopTaskCounter.GetValue() == 0) {

			//Get data from bluetooth dongle and set the rotation to the actor
			error = recv(connected, recvData, recvBufLen, 0);

			//We get data so we can use them
			if (error > 0) {

				//Do somesthing with your data

			//Connection was closed or an error appear, we shutdown the thread
			} else {

				stopTaskCounter.Increment();

			}
		} 
	}

	//Close connection to the bluetooth device
	closesocket(bluetoothSocket);
	closesocket(connected);
	WSACleanup();

	return 0;

}

//Do a joy initialize of the thread
void BluetoothConnector::joyInit() {

	if (!connector && FPlatformProcess::SupportsMultithreading()) {

		connector = new BluetoothConnector();

	}
}

//Stop the thread
void BluetoothConnector::Stop() {

	stopTaskCounter.Increment();

}

//Ensure that the thread stop
void BluetoothConnector::EnsureCompletion() {

	closesocket(bluetoothSocket);
	closesocket(connected);
	Stop();
	thread->WaitForCompletion();

}

//Shut the thread down
void BluetoothConnector::shutdownThread() {

	if(connector) {

		connector->EnsureCompletion();
		delete connector;
		connector = NULL;

	}
}
```

---

<div class="post-metadata">

**Author:** ![joeycampbell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joeycampbell](https://forums.unrealengine.com/u/joeycampbell)\
**Post date:** [May 6, 2015, 5:25am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/9 "2015-05-06T05:25:19Z")

</div>

Thanks Kosren - thats very generous of you!

---

<div class="post-metadata">

**Author:** ![anon78224193](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon78224193](https://forums.unrealengine.com/u/anon78224193)\
**Post date:** [July 12, 2016, 3:53pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/10 "2016-07-12T15:53:20Z")

</div>

Guys, I´m totally crazy with this. Iwas trying to implement this classes for read a heart rate monitor and put inside a Hud in UE4 but! I don´t know how can I do. Could you try to explain it to me like a dumb? ☹ Thanks a lot

---

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [July 13, 2016, 11:24am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/11 "2016-07-13T11:24:45Z")

</div>

Hello Emilio, can you please describe your problem in detail? Like where are your troubles with including the source code in your project.

---

<div class="post-metadata">

**Author:** ![joeycampbell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joeycampbell](https://forums.unrealengine.com/u/joeycampbell)\
**Post date:** [July 13, 2016, 11:43am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/12 "2016-07-13T11:43:55Z")

</div>

Hi I used a polar heart rate monitor connected to an arduino.  
I used the ue4duino plugin and was able to bring the bpm into the main blueprint.  
I passed this bpm variable in realtime into a HUD created in the UI design component within unreal (I forget the name of the UI designer).I tried several monitors with arduino but the polar was the most reliable but the user will need to be wirhin 2 metres of the arduino receiver. Hope this helps.

---

<div class="post-metadata">

**Author:** ![anon78224193](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon78224193](https://forums.unrealengine.com/u/anon78224193)\
**Post date:** [July 13, 2016, 11:51am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/13 "2016-07-13T11:51:10Z")

</div>

I don´t really know where the proble is and I don´t know how can i solve it because I was tryng to use your code but I´m a noob so I don´t really know what kind of component I need to create into my unreal to fix it.

---

<div class="post-metadata">

**Author:** ![anon78224193](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon78224193](https://forums.unrealengine.com/u/anon78224193)\
**Post date:** [July 13, 2016, 11:53am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/14 "2016-07-13T11:53:16Z")

</div>

Hi Joey,

Why do you need to conect it to an arduino hardw? Could you explain it to me? Probabilly I could try to fix with this method but if I do an arduino BLE reciever I think that I will have so many devices. Right?

---

<div class="post-metadata">

**Author:** ![joeycampbell](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joeycampbell](https://forums.unrealengine.com/u/joeycampbell)\
**Post date:** [July 13, 2016, 12:25pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/15 "2016-07-13T12:25:22Z")

</div>

Hi Emilio I used arduino because I did not know how to compile my own code to read directly from a smartphone or fitbit.

---

<div class="post-metadata">

**Author:** ![anon78224193](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon78224193](https://forums.unrealengine.com/u/anon78224193)\
**Post date:** [July 13, 2016, 12:50pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/16 "2016-07-13T12:50:25Z")

</div>

Oh man! I need it just for use in Windows with oculus! Could you help me? I can pay you for the bluprint!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0f0a0fe3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0f0a0fe3](https://forums.unrealengine.com/u/anonymous_user_0f0a0fe3)\
**Post date:** [September 7, 2016, 6:53am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/17 "2016-09-07T06:53:14Z")

</div>

Can not find Cad Universe PlayerController.h

---

<div class="post-metadata">

**Author:** ![Kosren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kosren](https://forums.unrealengine.com/u/Kosren)\
**Post date:** [September 7, 2016, 8:43am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/18 "2016-09-07T08:43:15Z")

</div>

The CaveUniversePlayerController comes from my Project where I used the Bluetooth connector, therefore this should be your own player controller.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_843485b4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_843485b4](https://forums.unrealengine.com/u/anonymous_user_843485b4)\
**Post date:** [June 5, 2017, 9:50am UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/19 "2017-06-05T09:50:11Z")

</div>

Hi, I copied your code to vs2015 and compiled it, then call BluetoothConnector :: joyInit () on the blueprint, but “Output Log” prints only LogTemp: Waiting for incoming connection, which has not been connected successfully. It I was running, always with my phone connected to the computer Bluetooth.

---

<div class="post-metadata">

**Author:** ![Annunaki69](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Annunaki69](https://forums.unrealengine.com/u/Annunaki69)\
**Post date:** [December 26, 2017, 1:19pm UTC](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060/20 "2017-12-26T13:19:37Z")

</div>

Hi @Kosren, I know it’s been a couple of years since this post was active but would you be able to give me any pointers on whats causing the errors in your code? I can’t seem to get it working at all.

[Next page](https://forums.unrealengine.com/t/using-bluetoothapis-h/306060.md?page=2)
