How to scan and connect to advertising BLE devices from C++ code?

10,468

Hello Raphaël and welcome to stackoverflow. When you say "without good results" or "but no success", you should detail why (compilation error, runtime error...).

I used this piece of code: Getting BLE Beacons in C++ Windows 10 Desktop Application

It compiles under Win10, so should the third link you posted (https://github.com/urish/win-ble-cpp) as it's very similar if you look at the includes here. Just note that this is not regular win32 projects: from Visual Studio you must create a new "Windows"/"Universal" C++ project, else they won't compile.

Alternatively, if you're OK with using a 3rd party library, you could simply use QtBluetooth, recent version supports windows 10. Then you can easily access BLE features from aregular win32 project.

Share:
10,468

Related videos on Youtube

Raphaël C
Author by

Raphaël C

I love playing music.

Updated on June 04, 2022

Comments

  • Raphaël C
    Raphaël C almost 2 years

    I'm trying to search and connect to advertising Bluetooth Low Energy devices from C++ code. I would like a piece of code to use laptop's wireless chip to discover and connect to BLE devices, regardless of their GATT Services. I want in fine to use GATT properties (Services, Characteristics, Descriptors, Notification).

    Programmatically

    I have the BluetoothLEAdvertisementWatcher Class documentation from Windows API.

    I tried to make working this example but without good results.

    I also tried this example, but no success.

    Manually

    I figured out using Windows 10 Action Center, and use manual pairing to connect devices (because Windows con see and connect my device).

    Then I could use directely the Windows.Devices.Bluetooth.GenericAttributeProfile Namespace

    Is there a way to implement that automatically?

  • DiBosco
    DiBosco about 5 years
    AFAIK, and the link you provided seems to support this, Qt does not support BLE on Windows. I'd love to be proven wrong.
  • jpo38
    jpo38 about 5 years
    @DiBosco: I see it working under Win10 right now (on a laptop with BT LE available). The device has to be paired from Windows bluetooth manager dialog. Then Qt can see it and connect to it.It's not very stable, but it works. Already filled a few Qt bugs that are being fixed for the next release. Win10 apparently supports WinRT API and this is wht it works.
  • jpo38
    jpo38 about 5 years
    @DiBosco: Doc says "Despite there not being a Win32 port yet, the WinRT backend is automatically used if the win32 target platform supports the required WinRT APIs Minimal requirement is Windows 10 version 1507 with slightly improved service discovery since Windows 10 version 1607. Therefore Windows 7 and 8.x targets are excluded."
  • DiBosco
    DiBosco about 5 years
    Thanks for that. What is "Windows bluetooth manager dialog"? I'm not much of a Windows user I'm afraid. I suppose this isn't going to help with my Bluetooth scanner example that just works on Linux?
  • DiBosco
    DiBosco about 5 years
    'SOK, got it. It's paired and my scanner will pick up its available characteristics. Apparently Qt 5.13 will be able to scan and connect too. Many thanks for the advice.
  • jpo38
    jpo38 about 5 years
    I'm using Qt 5.12 and this one already has the capability. You are welcome!
  • DiBosco
    DiBosco about 5 years
    What I meant was with 5.13 you won't have to pair using Windows BT manager, you'll be able to do it with Qt (so I am told). forum.qt.io/topic/91117/bluetooth-low-energy-windows/9
  • jpo38
    jpo38 about 5 years
    @DiBosco: That's nice!