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

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?

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.

Related

Pairing bluetooth mobile device and send data to windows application using c++ in visual studio

I am still new to c++ and winsock stuff. I want to make a MFC application in c++ language with functionality of connecting to a mobile device and transfer data between the PC and mobile device. I cannot find much information on the internet regarding this topic, or maybe is because I am too new this c++ and MFC application, thus having trouble implementing these libraries. I want to start from scanning and discovering nearby Bluetooth devices in an empty c++ console application. Previously done it in c# using the 32Feet.Net library. Do suggest me on how I can perform such task by using c++. Thanks for viewing this topic.

Bluetooth API for windows not working

I have used Bluetooth API and I have written some codes. I have used functions and structures from this link- https://msdn.microsoft.com/en-us/library/windows/desktop/aa362930(v=vs.85).aspx
The code I used were simple ones, where I check whether the device is connected/remembered/authenticated.
Initially, I wrote the code in Visual Studio 2013, in Windows 7 with internal Bluetooth v2.1. The code was working fine and I was getting the output properly. (i.e, whether the device is connected,remembered,authenticated). I used this structure https://msdn.microsoft.com/en-us/library/windows/desktop/aa362924(v=vs.85).aspx
Now, I tried that same code in Windows 7 and in Windows 10 with both using an external Bluetooth dongle with version v2.0. And then, I was not getting results. For example- for a particular device, I am getting the status for remembered,authenticated- properly, but the connected status is always the wrong result.
So, I want to know whether those Bluetooth APIs( the link which I have attached) are limited to only some of the bluetooth versions / operating systems ? If so, what are they limited to ?
NOTE- I didn't post the code because, I feel this question is about compatibility issue and not some problem in the code.

Can't access USB device inside Windows Store App

OK, first I shall point that I am completely new to Windows Apps Development, which is good, since I am trying to develop a Windows Store App for PC to use a PrimeSense Scanner connected via USB. I have asked a more specific question about this here.
This time I have a more generic question, which is more related to Windows Store app development. I am using VS2013 Express and compiling for Win32.
When I compile my application for VS2012 and run it as an execcutable file, I can connect to the scanner perfectly. But I can't do the same with VS2013 and running it as a Store app.
I know the device is connected and the drivers are updated and all dlls file placed in the Widnows System 32 directory.
I have also added all Capabilities to the App Manifest and also added the following Device Capability
<m2:DeviceCapability Name="usb">
<!--OSRFX2 Device-->
<m2:Device Id="vidpid:1d27 0609">
<m2:Function Type="classId:ff * *" />
<m2:Function Type="name:vendorSpecific" />
</m2:Device>
</m2:DeviceCapability>
The vid and pid, obviously match the corresponding codes of the device.
One of the errors I recieve when trying to conenct to the scanner using OpenNI is:
Could not open to "\\?\usb#vid_1d27&pid_0609&mi_00#7&1601586a&0&0000#{c3b5f022-5a42-1980-1909-ea72095601b1}" USB Device not found
This error is quite frustrating since I know the device is connected. So I tend to think that there is some level os specificity on the Windows Store App side of the game that is not enabling my to connect to the device. As I said, I am compiling for Win32.
Is there a chance that the drivers will not work for a Windows Store App. Is there some extra stuff I should do inside the Windows Store App logic that I am not doing and that is necessary to connect the USB device? I am sorry, but I am completely new to Windows Store App development.
Thank you.
You can't do that from metro apps.
You typically create a handle to that usb device by calling CreateFile with that object mananger path as the file to 'create'. CreateFile is not allowed in metro apps - "desktop apps only" - and its 'replacement', CreateFile2, specifically doesn't allow opening object manager objects.
Furthermore, the documentation for CreateFile2 states that in metro apps this function can only open files and directories (and not things like pipes, mailslots, consoles, etc.).
See also this post on social.msdn
Unless I missed something, I don't think this is possible.

Bluetooth in C++

I am looking to communicate via RFCOMM to another Bluetooth device. I want to use C++ (VS2008) for the application. I already know of the 32feet library, but was unsure if it would work for C++. Does anyone know of a good starting point for this kind of project? Or possibly 32feet samples written in C++? I want a simple, easy to use API for Bluetooth using C++.
This question is rather old, I know.
Just wanted to update, that Qt Framework has now Qt Bluetooth for C++ that looks very promising -- http://doc.qt.io/qt-5/qtbluetooth-module.html
You can do Bluetooth programming using BT sockets into the OS Bluetooth stack. This page discusses socket usage:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa362928(v=vs.85).aspx
This page has links to download Bluetooth SDK from Microsoft:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363058(v=vs.85).aspx
Building apps using the Microsoft stack works fairly well (XP and Vista work great, trying to run the same apps under Windows 7-64bit does not work so well...)
The real advantage to 32feet.net is that the Bluetooth support on Windows is heavily Balkanized - you never know on a given machine whether it is using the Microsoft, Broadcom, Toshiba, BlueSolei, or some other stack. While these are all mostly compatible over-the-air, the APIs are completely different for each. Building with 32feet.net lets your application run on a larger subset of machines... That said, I have not tried building it into a C++ application - sorry.

Stack independent c/c++ bluetooth api?

I wanted to know if there is any stack independent C/C++ bluetooth api for windows (XP, Vista, 7, x86 and x64).
My target is to create a connection and send/receive some time critical data over bluetooth.
My researches gave me the following options and there disadvantage for this task:
Windows Sockets for BluetoothOnly works with microsoft bluetooth stack
Using the COM port (CreateFile/ReadFile/...)Slower than the socket option and the user has to connect to the device first.
Use stack dependent APIThis would require to detect the used stack and create read/write functions for every stack. Very time consuming and I don't know if it is even possible to support all stacks
Writing a KMDF driverI thought about writing a function driver to talk directly with the bluetooth HCI driver which should be stack independent. This would be very time consuming because I would have to read the whole bluetooth spec, or is there an API for the HCI commands?
I know that there is also a commercial sdk from btframework.com which supports most (or all) bluetooth stacks, but I need a freeware option.
Please tell me if there are any other options for this task.
If there aren't any, could you tell me which options you would prefer and why?
Is it okay to use Windows Sockets and ignore the other bluetooth stacks?
Thanks for your help :)
I guess one option would be to use C++/CLI to interface with my managed Bluetooth library 32feet.NET which supports most of the stacks on Windows. http://32feet.codeplex.com/
I've been successfully using this library it is called WCL but it is not free. With this you can connect through SPP without requiring to create a virtual COM port.
While their documentation is not very good, they provide some good demo code that you may want to try.
It supports the Microsoft, Toshiba, Widcomm and Bluesoleil stacks and there is a native C++ implementation (as well as a .NET one).