1

I considered cross posting to the Super User site or similar but don't know how. As you will see this is not just a programming question.

I developed some C# code to communicate with an Onset InTemp thermometer via Bluetooth Low Energy (BLE). It works fine for a long time. I'm able to get beacons (which have the thermometer data) and also connect, get services and characteristics in case I need to get missed data or set parameters on the thermometer. However, after about 8 hours (can be as much as 24 hours) of continuously receiving beacons and connecting, getting historical data, etc., my app hangs on this line:

var gatt = await device.GetGattServicesAsync();

I put lines before and after this line and verified it's clearly hanging on this line. Again, it can be after 8 hours or 24 hours of use. It certainly chugs along just fine for quite a time. Killing and restarting the program is of no help. It hangs on the first call to:

device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

Only rebooting the PC fixes the problem. This is not surprising given the messages in the Windows Event Logs before it hangs:

The Bluetooth driver expected an HCI event with a certain size but did not receive it.

It's for this reason that I stated that this might be a SuperUser board question. No matter how badly I programmed my code :), killing the program should get BLE working again. FWIW, I did try disabling/re-enabling BLE through settings before rebooting.

I have found some links to this problem, but nothing very definitive.
I'm also working on writing a smaller, complete program to show the problem. So far, I can't get example program to fail from which I conclude that either (1) I haven't let it run long enough, or (2) I'm not fully duplicating what is going on with my full program. Perhaps I'm not putting as much pressure on the BLE drivers or I'm not listening for beacons and attempting to connect in the same ratios or with the same timing.

I should say that I see a lot of gripes about Windows implementation of the BLE Host layer and BLE drivers. Unfortunately, I'm stuck with Windows. Nordic also suggested that everything I'm reporting is a "known problem" and using their Nordic dongle will solve the problem. See for example: https://devzone.nordicsemi.com/f/nordic-q-a/65516/using-nrf52840-dongle-as-receiver-client-for-onset-thermometer.
That may well be true, but it would be a lot of work as they have libraries in C++ and Node.JS but not C#.

Any help is greatly appreciated.
Thank you!

GSerg
  • 76,472
  • 17
  • 159
  • 346
Dave
  • 8,095
  • 14
  • 56
  • 99
  • 2
    Have you tried looking at your running app using Performance Monitor (aka PerfMon). Apps that run a long time and then hang are often the result of a leak of some resource (in Windows, typically a handle of some type) – Flydog57 Sep 09 '20 at 20:59
  • Thank you @Flydog57 I haven't used PerfMon yet but I am seeing in the EventViewer messages of the type "The speed of processor 5 in group 0 is being limited by system firmware. The processor has been in this reduced performance state for 71 seconds since the last report." which I believe is due to high processor utilization? Again, I can believe I'm doing something stupid to leak memory/resources, or that the BLE layers do it by mistake, but I would have thought closing the program, waiting a while (30 minutes!) and restarting would fix that. Thanks for reading! – Dave Sep 09 '20 at 21:40
  • 1
    Based on your title, do you mean you also want to turn on/off BLE? If it is, maybe you could try to use the [Radio](https://learn.microsoft.com/en-us/uwp/api/windows.devices.radios.radio?view=winrt-19041#Remarks) Class to control the Bluetooth module. – Faywang - MSFT Sep 10 '20 at 02:01
  • Thanks @Faywang-MSFT ! I'll play with that. I'm skeptical it will work because as I mentioned, when I get into this state, nothing works except a reboot, and I've tried turning BLE off/on from settings and disabling/enabling driver, etc. before restarting the app. Good to know about Radio class in any case though! – Dave Sep 10 '20 at 13:41
  • If you need a reboot to fix this, it's likely in some service. Hunt around to see if there is some service hung up – Flydog57 Sep 10 '20 at 15:21
  • @Flydog57 YES! There is a service that in hung. Bluetooth Support Service (aka bthserv). However, after my app hangs and I close the app, I can't stop the service or restart. I stress that under normal conditions, I can do so. The only thing that fixes it is a reboot of computer. Thank you. – Dave Sep 10 '20 at 17:16
  • 1
    Well, now you can look at that service and see what it's leaking. And, you have a great thing to search for; "bthserv hangs" is way better than something like "channel changes". You might try to get Faywang (with MSFT in his name) interested again. MSFT knows a lot – Flydog57 Sep 10 '20 at 19:35
  • We are facing the same issue, @Dave Have you found any solution/workaround yet? – Thinh Tran Oct 06 '20 at 10:57
  • 1
    @Faywang-MSFT When the issue happen, we tried to manually turn bluetooth on/off but it didn't help. Furthermore one interesting thing is that on that machine, I can still connect to other devices, only cannot connect to that specific BLE device. – Thinh Tran Oct 06 '20 at 11:00
  • @ThinhTran I have not found a solution yet. I was thinking of using the Nordic BLE dongle. See: https://stackoverflow.com/questions/63819998/experience-examples-using-nordic-nrf52840-ble-with-c – Dave Oct 07 '20 at 11:20
  • And @ThinhTran by all means let me know if you find a solution. I'll do likewise! – Dave Oct 09 '20 at 00:55

0 Answers0