DotNet-BlueZ icon indicating copy to clipboard operation
DotNet-BlueZ copied to clipboard

GATT Server example request

Open hbraasch opened this issue 4 years ago • 21 comments

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.

hbraasch avatar Aug 13 '20 02:08 hbraasch

I'm looking for the same info please. I want the RPi to be discoverable as a serial port service.

ramiss avatar Aug 16 '20 21:08 ramiss

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.

Parsakarami avatar Aug 17 '20 14:08 Parsakarami

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

hbraasch avatar Aug 27 '20 04:08 hbraasch

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.

scegg avatar Nov 05 '20 05:11 scegg

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.

hbraasch avatar Nov 05 '20 22:11 hbraasch

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

scegg avatar Nov 06 '20 01:11 scegg

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.

GattServerCodeExample.txt .

hbraasch avatar Nov 06 '20 01:11 hbraasch

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.

GattServerCodeExample.txt .

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.

scegg avatar Nov 06 '20 01:11 scegg

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 });

hbraasch avatar Nov 12 '20 08:11 hbraasch

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).

scegg avatar Nov 12 '20 08:11 scegg

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?

hbraasch avatar Nov 12 '20 22:11 hbraasch

RegisterGattApplication

It hangs at: await GetAdvertisingManager().RegisterAdvertisementAsync(((IDBusObject) advertisement).ObjectPath, new Dictionary<string, object>()); from AdvertisingManager.RegisterAdvertisement from Advertisement.RegisterAdvertisement from StartServer

scegg avatar Nov 13 '20 02:11 scegg

Might sound like a stupid question but: Are you running it on PI 3 or 4, or other?

hbraasch avatar Nov 14 '20 06:11 hbraasch

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.

scegg avatar Nov 14 '20 06:11 scegg

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?

hbraasch avatar Nov 14 '20 06:11 hbraasch

Hi have you solved this problem?

ArthurKun21 avatar Apr 21 '21 09:04 ArthurKun21

Hi have you solved this problem?

Who're you asking...?

But I don't think there is anyone solved this yet.

scegg avatar Apr 21 '21 09:04 scegg

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

ArthurKun21 avatar Apr 22 '21 00:04 ArthurKun21

Ok!! Could be th OS'es. I've got it working it on Raspbian

hbraasch avatar Apr 22 '21 05:04 hbraasch

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.

scegg avatar Apr 25 '21 03:04 scegg

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).

Caffiendish avatar Dec 12 '21 08:12 Caffiendish