tayoung tayoung - 7 days ago 6
C# Question

.NET with Windows RT Bluetooth LE API - cannot read or write device after restarting Windows

I've been developing an application using the WinRT API for Bluetooth Low Energy for Windows 8.1 and above. It's worked for a while, but I've run into a serious issue: the software cannot connect to the device after Windows reboots.

If I close and restart the software, it still won't pair. The only thing that works is if I unpair and re-pair with the device. However, considering the use case for our users, this is completely unacceptable. It can't be a problem with the device firmware, because it's still capable of pairing and interacting with iOS.

In debugging, whenever I try to write to the device using

txCharacteristic.WriteValueAsync(chunk, GattWriteOption.WriteWithoutResponse)
, I get "Incorrect function." However, I KNOW this is the correct function because this is exactly what the device supports, not to mention the code works fine if I unpair and re-pair. Additionally, I get this error when I try to expand the PresentationFormats property:

PresentationFormats 'this.txServiceCharacteristic.PresentationFormats' threw an exception of type 'System.AccessViolationException' System.Collections.Generic.IReadOnlyList {System.AccessViolationException}

So this is telling me that this is a problem with Windows. I am currently running 10.

I've tried updating device drivers, setting the Bluetooth service to start automatically (from the default manual "triggered" start), and nothing seems to mitigate this issue. Does anyone have any idea what could be causing this, and is there any solution?

Thanks!

EDIT: Apparently the error is fixed if I change the GattProtectionLevel from EncryptionAndAuthenticationRequired to Plain. But why would EncryptionAndAuthenticationRequired be an incorrect mode after only a Windows reboot?

Answer

As mentioned in my edit, the error is fixed if I change the GattProtectionLevel from EncryptionAndAuthenticationRequired to Plain.

Now, we were worried about this meaning there would be no encryption or authentication. However, this doesn't seem to be the case after we used a Bluetooth packet sniffer just to be sure. This is because the details for encryption and authentication are done upon pairing, not upon connection.

Per the documentation:

Plain: Uses the default protection level.

Plain does not mean None. It means "use the minimum security the device requires," or in essence, automatic based upon what information it got at pair time.