1

I have to create a Windows 10 program that connects to a Polar OH1 device. The goal is that with a push of a button, we can connect to the Bluetooth LE device and write the real-time heart rate data to a csv file on the Windows 10 computer. I plan on doing this in C#, and ideally as a Windows Console Application.

However, this needs to be done without a user interface. From what I've searched, the Windows Bluetooth GATT APIs need a UI thread in order to connect to a Bluetooth LE device.

Is there a way to do what I need to do as a console application?

hackerman
  • 11
  • 4
  • You don't need UI permission to create a thread with a UI message pump. You need UI permission to interact with visible windows (or make your windows visible) – Ben Voigt Jan 17 '18 at 23:15
  • But you could just use the UWP APIs not GATT: https://stackoverflow.com/q/39470036/103167 – Ben Voigt Jan 17 '18 at 23:16
  • What do you mean by a UI message pump? – hackerman Jan 17 '18 at 23:23
  • The thing that makes a thread a "UI thread" is that it has a message queue (used by `PostMessage`) and a message loop (calling `GetMessage` or `PeekMessage` or `MsgWaitForMultipleObjects`, then `TranslateMessage` and `DispatchMessage`). In C# there are two common message loops: the WinForms `Application.Run`, and a one in WPF that serves the same purpose but integrates with `Dispatcher`. – Ben Voigt Jan 17 '18 at 23:26
  • Do you think it would be possible then to implement this in a console application? Sorry, I'm a bit of a newbie so this is my first time working with Bluetooth le and Windows 10 – hackerman Jan 17 '18 at 23:29
  • Yes, console applications can call UI message functions. Fun trick: Create a C# Windows Forms Application, and when the wizard is done, goto Project Properties and change the type to Console Application. When it runs, you'll have both the console and form. – Ben Voigt Jan 17 '18 at 23:35
  • Wait, if you use a Windows Forms Application, I thought you can't use UWP APIs? – hackerman Jan 18 '18 at 01:23
  • You're the one who mentioned the Win32 Bluetooth GATT APIs. If you can do everything with UWP, great. If you need Win32, you can have a UI thread that supports message-driven APIs without visible UI. – Ben Voigt Jan 18 '18 at 02:35
  • _"From what I've searched, the Windows Bluetooth GATT APIs need a UI thread in order to connect to a Bluetooth LE device"_ -- I don't understand that statement. What led you to that conclusion? I have used the UWP BLE API and while that's always involved a GUI program, the API itself did not require or make use of the GUI. You need to explain better why it is you think you can't just do this "the normal way". – Peter Duniho Jan 18 '18 at 04:56
  • So in the GattDeviceService class, in order to use the "FromIdAsync" method, it says that the method should be invoked from the UI thread, because if the user does not grant consent to connect with the particular Bluetooth device, the method will return null. The method seems like an important way to get heart rate information from my polar Bluetooth device. [link](https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.genericattributeprofile.gattdeviceservice). Ideally I would want to use the UWP BLE API with a console application but am not sure if its possible – hackerman Jan 18 '18 at 15:02
  • Ah...I guess I never noticed that (or maybe it the docs didn't include that note...that was a few years ago). Really dumb API design IMHO to assume there's a UI thread to use...the OS should've included an out-of-proc confirmation (similar to firewall permissions, for example). Anyway, it seems that as of a year ago, this was addressed: https://stackoverflow.com/a/36106137 – Peter Duniho Jan 19 '18 at 00:19
  • This is exactly what I needed! Thank you! – hackerman Jan 20 '18 at 16:01

0 Answers0