Windows 上的蓝牙低功耗堆栈溢出
Bluetooth Low Energy Stack Overflow on Windows
我想编写一个程序,通过低功耗蓝牙每 10 毫秒接收一次数据。
我有很多事情要做,但我总是遇到一个问题,而且找不到来源。
这是我在 Windows 10 上用 C++Builder 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++;
}
//---------------------------------------------------------------------------
恰好在 86303 通知(调用 BluetoothLE1CharacteristicRead)之后,我得到了堆栈溢出。所以一定是哪里出了问题。
一开始我用 Visual Studio 用 C++ 编写程序,使用 Windows 驱动程序函数,但同样的事情。
我找到问题了。
这是 Win32 蓝牙 API 中的错误。
使用 Windows 更新 KB3156421,每个程序在一个通知后崩溃,Mircosoft 提供了一个解决方法。 Link
该解决方法也解决了我的上述问题。
我想编写一个程序,通过低功耗蓝牙每 10 毫秒接收一次数据。
我有很多事情要做,但我总是遇到一个问题,而且找不到来源。 这是我在 Windows 10 上用 C++Builder 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++;
}
//---------------------------------------------------------------------------
恰好在 86303 通知(调用 BluetoothLE1CharacteristicRead)之后,我得到了堆栈溢出。所以一定是哪里出了问题。 一开始我用 Visual Studio 用 C++ 编写程序,使用 Windows 驱动程序函数,但同样的事情。
我找到问题了。 这是 Win32 蓝牙 API 中的错误。 使用 Windows 更新 KB3156421,每个程序在一个通知后崩溃,Mircosoft 提供了一个解决方法。 Link
该解决方法也解决了我的上述问题。