Search code examples
c#bluetoothserveruwpgatt

C# Windows UWP Gatt Server: Request object always NULL


I have implemented a GATT Server on Windows according to the microsoft documentation site: https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server

The Server starts and I can discover the created service and characteristics with different Bluetooth LE explorers on my Android phone.

But I have a problem when a read or write request is made from the client to the GATT server on Windows (Creators Update is installed).

async void ReadCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args) {
  var deferral = args.GetDeferral();

  // Our familiar friend - DataWriter.
  var writer = new DataWriter();
  // populate writer w/ some data. 
  // ... 

  var request = await args.GetRequestAsync();
  request.RespondWithValue(writer.DetachBuffer());

  deferral.Complete();
}

The above code snippet is executed, when a read characteristic value request comes in. The request object is always NULL.

var request = await args.GetRequestAsync();

Does anyone know what I am doing wrong? Why is the request object always NULL? Is the example code on the microsoft site incomplete? Has anyone a working example of a GATT server on Windows UWP?

Thanks in advance, Christian


Solution

  • I ran into the same issue. It turns out that in order to send/receive data you need to enable Bluetooth under capabilities in the Package.appxmanifest file.

    This is pretty obvious however there's no indication on any of the tutorials, CH9 videos or documentation that I've seen that this needs to done.

    Hope this saves some people some time.

    Enable Bluetoot