Search code examples
c#.netuwpbluetoothbluetooth-lowenergy

Create a Bluetooth Low Energy BLE GATT Server with .NET C#


I'm trying to create a C# Windows App in Visual Studio 2022 that with the click of a button creates a BLE Gatt server with a custom service with its UUID and a custom characteristic with its UUID also, that with a write without response property receives a string and displays it in a message box. The following parameters are needed:

Advertising parameters:

  • Adv interval: 150ms

  • Coded PHY - S8

  • Adv with all channels

  • Adv flag : connectable

  • Adv with Resolvable Private address changing every 15min.

  • Expected Advertisement payload: 020106110650760ab3a2c1ebae214d08ea4d64251b0fff0001010101010101b66dc981f68c

    • TLV breakdown:

      • 02 01 06 → Adv Flag of Type 0x01 with flag value is 0x06

      • 11 06 50760ab3a2c1ebae214d08ea4d64251b → 128bit Service UUID of Type 0x06

      • 0F FF 0001010101010101b66dc981f68c → Manuf. Specific data Format of Type 0xFF with payload for data deconstructed as below

        • 0001 is Manuf. ID that can be modified

        • 010101010101 is Peripheral Device ID

        • b66dc981f68c is Peripheral Device BLE address (The advertisement is going to be targeted to an specific BLE Peripheral Device)

Connection parameters:

  • Interval: 100ms
  • Coded PHY - S8

I have tried creating the Gatt Server using UWP is up and running! I connect to it using the nRF app on my phone, I select the characteristic and send the string which shows up in a label. But I can't find a way to specify Coded PHY - S8 or to customize the Expected Advertisement payload as needed, is it even possible to do it with UWP? or do I need a third-party library? I found btframework libraries, but they are not free and I don't know if it's possible with them (insights appreciated), I also found InTheHand.BluetoothLE library but there's very little documentation and I don't know is it's even possible with that one too (insights appreciated too), or probably it is actually possible using UWP.

In case some of you have experience trying to achieve something like this and can point me in the right direction on how to do this, I will deeply appreciate your help.


Solution

  • You can not control PHY and other parameters (except Connection Interval, Timeout and Latency on Windows 11, UWP API does not allow to do that also, it provides just some pre-defned values. Bluetooth Framework does).

    When you start GATT server it automatically adds its primary service UUID to the advertisement payload and starts advertising it as 128bit UUID advertisement type.

    To add manufacturer specific advertisement you must use BluetoothLEAdvertisementPublisher class. It allows to add and create custom advertisements. There are some limitations on the advertisements that are possible: https://www.btframework.com/blebeacon.htm

    UWP does not allow to change any LE advertisement parameters. Bluetooth Framework allows to change advertising interval.

    UWP does not allow to advertise something longer that standard advertismeent payload. Bluetooth Framework solves this issue and allows to advertise more than single advertisement type.

    Also you can not change any other GATT parameters: MTU, PHY, etc. Connection parameters can be changed only on Windows 11 with limitation I described above.