DotNet-BlueZ
DotNet-BlueZ copied to clipboard
GATT Server example request
Not an issue but a example request.
Would it be possible to give us an example on how to do a simple GATT server using this library? I've tried to match up some Python based D-Bus examples with what your library provides, but could not make any sense.
In the end I'd like to have some Xamarin app access data on a PI via BLE.
It shall be much appreciated.
I'm looking for the same info please. I want the RPi to be discoverable as a serial port service.
Hi, Chris.
First of all, Thank you for your library. It seems useful and great. I want to know how can I create a GATT server and advertise it using D-Bus on Raspberry Pi? I have seen multiple examples that have been written in Python but I couldn`t match those and use them in .Net core.
I recently came across this sample code, it works very well. Its a simple generic GATT server implemented for Linux in C#/.Net, using D-Bus and Tmds.Dbus.
https://github.com/phylomeno/dotnet-ble-server
Do not use the provided Nuget, just add all the DotnetBleServer code to your project and see the examples provided for guidance
I compiled it for .Net Core 3.1 and it works fine
I recently came across this sample code, it works very well. Its a simple generic GATT server implemented for Linux in C#/.Net, using D-Bus and Tmds.Dbus.
https://github.com/phylomeno/dotnet-ble-server
Do not use the provided Nuget, just add all the DotnetBleServer code to your project and see the examples provided for guidance
I compiled it for .Net Core 3.1 and it works fine
Thanks for your info. But it cannot work here. Hello Bluetooth advertisement object /org/bluez/example/advertisement0 created Unhandled exception. System.AggregateException: One or more errors occurred. (org.freedesktop.DBus.Error.UnknownMethod: Method "RegisterAdvertisement" with signature "oa{sv}" on interface "org.bluez.LEAdvertisingManager1" doesn't exist
How you pass this exception? Thanks.
Are you running it from the source code (as I mentioned above), not the Nuget library? The Nuget version (I think) is an older version than the source.
Are you running it from the source code (as I mentioned above), not the Nuget library? The Nuget version (I think) is an older version than the source.
Thanks for your reply.
I use the code from the github directly. Only reference in the example is the main project, which is a project code reference. And the only nuget package used by the main project is tmds.dbus. I've created an issue there. Would you like to check it and upload your running code? Thanks again.
https://github.com/phylomeno/dotnet-ble-server/issues/5
I have attached the code I'm using to create my GATT server. Hope it helps. Please ignore some the app specific code not removed.
I have attached the code I'm using to create my GATT server. Hope it helps. Please ignore some the app specific code not removed.
In your code, required AddCharacteristic is missing from GattServiceBuilder. Should I use WithCharacteristic instead?
After running StartServer().Wait() from your code with the changing above, messages printed on screen: "advertisement object /org/bluez/example/advertisement0 created"
That's all. Nothing detected from my LightBlue app. Didn't see "GattServer successfully started", nor error message.
Not sure what you mean by "In your code, required AddCharacteristic is missing from GattServiceBuilder"?
My code in RegisterGattApplication(..) : var gab = new GattApplicationBuilder(); var gsp = gab.AddService(gattServiceDescription); gsp.AddCharacteristic(commandCharacteristicDescription, new[] { commandDescriptorDescription });
Not sure what you mean by "In your code, required AddCharacteristic is missing from GattServiceBuilder"?
My code in RegisterGattApplication(..) : var gab = new GattApplicationBuilder(); var gsp = gab.AddService(gattServiceDescription); gsp.AddCharacteristic(commandCharacteristicDescription, new[] { commandDescriptorDescription });
AddCharacteristic is not shipped with the original code (GattServiceBuilder).
Sorry, you're so right! I completely forgot. I did add a function to GattServiceBuilder (even marked it) which simply encapsulates ||WithCharacteristic||:
// Added by HRB public void AddCharacteristic(GattCharacteristicDescription gattCharacteristicDescription, GattDescriptorDescription[] gattDescriptorDescriptions) { WithCharacteristic(gattCharacteristicDescription, gattDescriptorDescriptions); } As far as I can see I did not do any other modifications to the code.
Insofar not seeing "GattServer successfully started"... Can you maybe debug into ||RegisterGattApplication|| to see which instruction hangs?
RegisterGattApplication
It hangs at: await GetAdvertisingManager().RegisterAdvertisementAsync(((IDBusObject) advertisement).ObjectPath, new Dictionary<string, object>()); from AdvertisingManager.RegisterAdvertisement from Advertisement.RegisterAdvertisement from StartServer
Might sound like a stupid question but: Are you running it on PI 3 or 4, or other?
Might sound like a stupid question but: Are you running it on PI 3 or 4, or other?
Last test is on my computer with Ubuntu server x64.
The test before is on my RPI4 with Ubuntu server arm64.
I'm for now running it on Pi3, because I had problems with BLE on Pi4 (but its been a while and for the life of me I cannot remember why I temporarily gave up on the Pi4). Long shot but just give it a try on PI3?
Hi have you solved this problem?
Hi have you solved this problem?
Who're you asking...?
But I don't think there is anyone solved this yet.
Might sound like a stupid question but: Are you running it on PI 3 or 4, or other?
Last test is on my computer with Ubuntu server x64.
The test before is on my RPI4 with Ubuntu server arm64.
this one in the ubuntu server. I was running it on my Vmware with ubuntu installed. I was using other library - python-bluezero but got the same error as yours
org.freedesktop.DBus.Error.UnknownMethod: Method "RegisterAdvertisement" with signature "oa{sv}" on interface "org.bluez.LEAdvertisingManager1" doesn't exist
Ok!! Could be th OS'es. I've got it working it on Raspbian
I'm for now running it on Pi3, because I had problems with BLE on Pi4 (but its been a while and for the life of me I cannot remember why I temporarily gave up on the Pi4). Long shot but just give it a try on PI3?
I found the reason why I cannot use BLE on my Pi4.
I'm using Ubuntu arm64 20.04, not Pi OS. A lib installation is required:
sudo apt install pi-bluetooth
then
sudo nano /boot/firmware/usercfg.txt
add include btcfg.txt
to the end.
and sudo reboot
.
Before testing code, make sure that bluetooth device can be listed by hciconfig -a
.
I had some issues getting BLE servers to work on my Raspberry Pi as well, so I forked CirrusResearch's notification branch (big thanks to them!), and made a couple of commented examples that should hopefully help other people: https://github.com/Caffiendish/dotnet-ble-server/tree/Notifications
I've got a couple of issues to work out though, namely how "write (with response)" actually works, since my Windows machine as a client doesn't put "request" in the options. Or "command", for a "write-without-response".
If you have a client that does add options, you'll (as I understand it!) find them in the dictionary here: https://github.com/Caffiendish/dotnet-ble-server/blob/Notifications/DotnetBleServer/Gatt/BlueZModel/GattCharacteristic.cs#L29
My Windows client just does what seems to be the basic stuff, "link", "device", "mtu". I have devices that respond, but do so with notifications, so I'm kind of assuming that's how replies work.
Edit:
I forgot to mention, I'm using a Raspberry Pi Zero W, on the most current Raspberry OS, with whatever Mono version apt install
got me 😄 and compiling my executable with DotNet 4.7.2 (Mono supports Net Core 2.0, so the library works).