Reputation: 25
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
Upvotes: 2
Views: 1383
Reputation: 3641
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.
Upvotes: 5
Reputation: 3808
See the official sample BluetoothLE and the server code is in Scenario3_ServerForeground. Please see its ResultCharacteristic_ReadRequestedAsync
method as a reference.
private async void ResultCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
{
// BT_Code: Process a read request.
using (args.GetDeferral())
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(async () =>
{
// Get the request information. This requires device access before an app can access the device's request.
GattReadRequest request = await args.GetRequestAsync();
if (request == null)
{
// No access allowed to the device. Application should indicate this to the user.
rootPage.NotifyUser("Access to device not allowed", NotifyType.ErrorMessage);
return;
}
var writer = new DataWriter();
writer.ByteOrder = ByteOrder.LittleEndian;
writer.WriteInt32(resultVal);
// Can get details about the request such as the size and offset,
//as well as monitor the state to see if it has been completed/cancelled externally.
// request.Offset
// request.Length
// request.State
// request.StateChanged += <Handler>
// Gatt code to handle the response
request.RespondWithValue(writer.DetachBuffer());
});
}
}
Upvotes: 0