KingKarl237 KingKarl237 - 26 days ago 15
C++ Question

Bluetooth Low Energy StackOverflow on Windows

I wanted to write a programm that is receiving data every 10ms over Bluetooth Low Energy. I get a lot of thinks to work, but i always have one problem and can´t find the source. Maybe you can help me.
Here is the basis of my Code written in C++Builder 10 on Windows 10.

> //---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

#define Characteristic_UUID "{6e400003-b5a3-f393-e0a9-e50e24dcca9e}"
#define Service_UUID "{6e400001-b5a3-f393-e0a9-e50e24dcca9e}"
#pragma resource "*.dfm"



TForm1 *Form1;
TBluetoothLEDevice* device;
TBluetoothGattCharacteristicList* characteristic ;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
BluetoothLE1->DiscoverDevices(100);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BluetoothLE1EndDiscoverDevices(TObject * const Sender, TBluetoothLEDeviceList * const ADeviceList)

{
device = ADeviceList->First();

BluetoothLE1->DiscoverServices(device);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BluetoothLE1ServicesDiscovered(TObject * const Sender, TBluetoothGattServiceList * const AServiceList)

{

GUID AGuid;
CLSIDFromString(TEXT(Service_UUID), &AGuid);
TBluetoothGattService* service = BluetoothLE1->GetService(device,AGuid);
//TBluetoothGattServiceList* abcd = BluetoothLE1->GetServices(device);


CLSIDFromString(TEXT(Characteristic_UUID), &AGuid);
characteristic = BluetoothLE1->GetCharacteristics(service);
while(characteristic->First()->UUID != AGuid)
{
characteristic->Delete(0);

}
if(characteristic->First()!= NULL);
BluetoothLE1->SubscribeToCharacteristic(device,characteristic->First());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BluetoothLE1CharacteristicRead(TObject * const Sender, TBluetoothGattCharacteristic * const ACharacteristic,
TBluetoothGattStatus AGattStatus)

{
static long i;

Label1->Caption = i;
i++;
}
//---------------------------------------------------------------------------


After exact 86303 Notifys (calling BluetoothLE1CharacteristicRead) i get a Stack Overflow. So there must be something wrong.
At the beginning i wrote the programm in Visual Studio in C++ with the Windows Drivers Functions, but same thing.

Answer

I have found the problem. It is a bug in the Win32 Bluetooth APIs. With Windows Update KB3156421 every program crashes after one notification and Mircosoft gives a workaround. Link

That workaround fixes my problem above too.

Comments