weliem / bluez_inc

A C library for Bluez (BLE) that hides all DBus communication. It doesn't get easier than this. This library can also be used in C++.
MIT License
84 stars 19 forks source link

How to get the example applications to work? #21

Closed ludwit closed 6 months ago

ludwit commented 7 months ago

Hi weliem,

I ran your example BLE applications on my ubuntu 22.04.3 pc which has bluetooth 5.2 hardware support and a debian 11 beagle-bone-black with a bluetooth adapter which supports bluetooth 5.0.

When I ran the peripheral executable on the bealge-bone-black, I got this error message: DEBUG [Adapter] failed to register application (error 36: GDBus.Error:org.bluez.Error.Failed: No valid service object found).

However, on the Ubuntu PC, the peripheral application works fine. I can discover and connect to the device and retrieve the characteristic data using a generic BLE discovery software with GUI (LightBlue). In this setup, the central executable running on the beagle-bone-black doesn't connect to the Ubuntu PC's BLE service and doesn't retrieve the "hello there".

So my question is whether you can name some hardware and/or software requirements for using bluz_inc as the example works on the ubuntu pc but not on the debian begle-bone-black?

Thanks in advance!

Appendix - all log for error message debian@BeagleBone:~/bluez_inc/examples/peripheral$ ./peripheral 2024-02-14 10:55:42:978 DEBUG [Adapter] finding adapters 2024-02-14 10:55:42:988 DEBUG [Adapter] found 1 adapter 2024-02-14 10:55:42:989 DEBUG [Main] using default_adapter '/org/bluez/hci0' 2024-02-14 10:55:42:993 DEBUG [Application] successfully published application 2024-02-14 10:55:42:998 DEBUG [Application] successfully published local service 00001809-0000-1000-8000-00805f9b34fb 2024-02-14 10:55:43:001 DEBUG [Application] successfully published local characteristic 00002a1c-0000-1000-8000-00805f9b34fb 2024-02-14 10:55:43:010 DEBUG [Application] successfully published local descriptor 00002901-0000-1000-8000-00805f9b34fb 2024-02-14 10:55:43:016 DEBUG [Application] set value <68656c6c6f20746865726500> to <00002901-0000-1000-8000-00805f9b34fb> 2024-02-14 10:55:43:034 DEBUG [Application] adding /org/bluez/bincapplication/service0 2024-02-14 10:55:43:038 DEBUG [Application] adding /org/bluez/bincapplication/service0/char0 2024-02-14 10:55:43:040 DEBUG [Application] adding /org/bluez/bincapplication/service0/char0/desc0 2024-02-14 10:55:43:050 DEBUG [Adapter] started advertising (E8:48:B8:C8:40:00) 2024-02-14 10:55:43:063 DEBUG [Adapter] failed to register application (error 36: GDBus.Error:org.bluez.Error.Failed: No valid service object found) 2024-02-14 11:05:43:105 DEBUG [Application] freeing application /org/bluez/bincapplication 2024-02-14 11:05:43:110 DEBUG [Application] freeing service /org/bluez/bincapplication/service0 2024-02-14 11:05:43:111 DEBUG [Application] freeing characteristic /org/bluez/bincapplication/service0/char0 2024-02-14 11:05:43:111 DEBUG [Application] freeing descriptor /org/bluez/bincapplication/service0/char0/desc0

weliem commented 7 months ago

Hi, I see it doesn't work on the Beagle. But it should. The only thing this library does is talk to the DBus...

Maybe it is a permission issue?

weliem commented 7 months ago

Can you share some code? How are you starting the advertising?

ludwit commented 7 months ago

I am using the central and peripheral code from /examples. Could it be the version of bluez? The beagle bone only had 5.55 while the ubuntu had 5.64. You tested on raspberry pie I guess. Which OS did you use?

ludwit commented 7 months ago

For the sake of more logs: debian@BeagleBone:~/bluez_inc$ sudo dbus-monitor --system "destination='org.bluez'" "sender='org.bluez'" [sudo] password for debian: signal time=1707931243.883727 sender=org.freedesktop.DBus -> destination=:1.22 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired string ":1.22" signal time=1707931243.885098 sender=org.freedesktop.DBus -> destination=:1.22 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost string ":1.22" method call time=1707931247.717739 sender=:1.23 -> destination=org.bluez serial=2 path=/; interface=org.freedesktop.DBus.ObjectManager; member=GetManagedObjects method return time=1707931247.721053 sender=:1.11 -> destination=:1.23 serial=28 reply_serial=2 array [ dict entry( object path "/org/bluez" array [ dict entry( string "org.freedesktop.DBus.Introspectable" array [ ] ) dict entry( string "org.bluez.AgentManager1" array [ ] ) dict entry( string "org.bluez.ProfileManager1" array [ ] ) dict entry( string "org.bluez.HealthManager1" array [ ] ) ] ) dict entry( object path "/org/bluez/hci0" array [ dict entry( string "org.freedesktop.DBus.Introspectable" array [ ] ) dict entry( string "org.bluez.Adapter1" array [ dict entry( string "Address" variant string "E8:48:B8:C8:40:00" ) dict entry( string "AddressType" variant string "public" ) dict entry( string "Name" variant string "BeagleBone" ) dict entry( string "Alias" variant string "BeagleBone" ) dict entry( string "Class" variant uint32 0 ) dict entry( string "Powered" variant boolean true ) dict entry( string "Discoverable" variant boolean false ) dict entry( string "DiscoverableTimeout" variant uint32 180 ) dict entry( string "Pairable" variant boolean false ) dict entry( string "PairableTimeout" variant uint32 0 ) dict entry( string "Discovering" variant boolean false ) dict entry( string "UUIDs" variant array [ string "00001801-0000-1000-8000-00805f9b34fb" string "00001800-0000-1000-8000-00805f9b34fb" string "00001200-0000-1000-8000-00805f9b34fb" string "0000110c-0000-1000-8000-00805f9b34fb" string "0000110e-0000-1000-8000-00805f9b34fb" string "0000180a-0000-1000-8000-00805f9b34fb" ] ) dict entry( string "Modalias" variant string "usb:v1D6Bp0246d0537" ) dict entry( string "Roles" variant array [ string "central" string "peripheral" ] ) ] ) dict entry( string "org.freedesktop.DBus.Properties" array [ ] ) dict entry( string "org.bluez.GattManager1" array [ ] ) dict entry( string "org.bluez.LEAdvertisingManager1" array [ dict entry( string "ActiveInstances" variant byte 0 ) dict entry( string "SupportedInstances" variant byte 4 ) dict entry( string "SupportedIncludes" variant array [ string "tx-power" string "appearance" string "local-name" ] ) dict entry( string "SupportedSecondaryChannels" variant array [ string "1M" string "2M" string "Coded" ] ) ] ) dict entry( string "org.bluez.Media1" array [ ] ) dict entry( string "org.bluez.NetworkServer1" array [ ] ) ] ) ] method call time=1707931247.763612 sender=:1.23 -> destination=org.bluez serial=7 path=/org/bluez/hci0; interface=org.bluez.LEAdvertisingManager1; member=RegisterAdvertisement object path "/org/bluez/bincadvertisement" array [ ] method call time=1707931247.766739 sender=:1.11 -> destination=org.freedesktop.DBus serial=29 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0=':1.23'" method return time=1707931247.767814 sender=org.freedesktop.DBus -> destination=:1.11 serial=16 reply_serial=29 method call time=1707931247.771572 sender=:1.11 -> destination=org.freedesktop.DBus serial=30 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=GetNameOwner string ":1.23" method return time=1707931247.772697 sender=org.freedesktop.DBus -> destination=:1.11 serial=17 reply_serial=30 string ":1.23" method call time=1707931247.777397 sender=:1.11 -> destination=org.freedesktop.DBus serial=31 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincadvertisement',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesAdded'" method return time=1707931247.778603 sender=org.freedesktop.DBus -> destination=:1.11 serial=18 reply_serial=31 method call time=1707931247.783288 sender=:1.11 -> destination=org.freedesktop.DBus serial=32 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincadvertisement',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesRemoved'" method return time=1707931247.784444 sender=org.freedesktop.DBus -> destination=:1.11 serial=19 reply_serial=32 method call time=1707931247.788390 sender=:1.11 -> destination=org.freedesktop.DBus serial=33 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path_namespace='/org/bluez/bincadvertisement'" method return time=1707931247.790589 sender=org.freedesktop.DBus -> destination=:1.11 serial=20 reply_serial=33 method call time=1707931247.795279 sender=:1.11 -> destination=org.freedesktop.DBus serial=34 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincadvertisement',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.LEAdvertisement1'" method return time=1707931247.796406 sender=org.freedesktop.DBus -> destination=:1.11 serial=21 reply_serial=34 method call time=1707931247.800483 sender=:1.11 -> destination=org.freedesktop.DBus serial=35 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=StartServiceByName string ":1.23" uint32 0 error time=1707931247.801585 sender=org.freedesktop.DBus -> destination=:1.11 error_name=org.freedesktop.DBus.Error.ServiceUnknown reply_serial=35 string "The name :1.23 was not provided by any .service files" method call time=1707931247.805327 sender=:1.11 -> destination=:1.23 serial=36 path=/org/bluez/bincadvertisement; interface=org.freedesktop.DBus.ObjectManager; member=GetManagedObjects method call time=1707931247.808433 sender=:1.23 -> destination=org.bluez serial=8 path=/org/bluez/hci0; interface=org.bluez.GattManager1; member=RegisterApplication object path "/org/bluez/bincapplication" array [ ] method call time=1707931247.811099 sender=:1.11 -> destination=org.freedesktop.DBus serial=37 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=GetNameOwner string ":1.23" method return time=1707931247.812129 sender=org.freedesktop.DBus -> destination=:1.11 serial=23 reply_serial=37 string ":1.23" method call time=1707931247.814676 sender=:1.11 -> destination=org.freedesktop.DBus serial=38 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesAdded'" method return time=1707931247.815729 sender=org.freedesktop.DBus -> destination=:1.11 serial=24 reply_serial=38 error time=1707931247.817397 sender=:1.23 -> destination=:1.11 error_name=org.freedesktop.DBus.Error.UnknownMethod reply_serial=36 string "No such interface “org.freedesktop.DBus.ObjectManager” on object at path /org/bluez/bincadvertisement" method call time=1707931247.820811 sender=:1.11 -> destination=org.freedesktop.DBus serial=39 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesRemoved'" method return time=1707931247.821877 sender=org.freedesktop.DBus -> destination=:1.11 serial=25 reply_serial=39 method call time=1707931247.824613 sender=:1.11 -> destination=org.freedesktop.DBus serial=40 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path_namespace='/org/bluez/bincapplication'" method return time=1707931247.825644 sender=org.freedesktop.DBus -> destination=:1.11 serial=26 reply_serial=40 method call time=1707931247.832577 sender=:1.11 -> destination=:1.23 serial=41 path=/org/bluez/bincapplication; interface=org.freedesktop.DBus.ObjectManager; member=GetManagedObjects method call time=1707931247.836033 sender=:1.11 -> destination=:1.23 serial=42 path=/org/bluez/bincadvertisement; interface=org.freedesktop.DBus.Properties; member=GetAll string "org.bluez.LEAdvertisement1" method return time=1707931247.841291 sender=:1.23 -> destination=:1.11 serial=10 reply_serial=41 array [ dict entry( object path "/org/bluez/bincapplication/service0" array [ dict entry( string "org.bluez.GattService1" array [ dict entry( string "UUID" variant string "00001809-0000-1000-8000-00805f9b34fb" ) dict entry( string "Primary" variant boolean true ) dict entry( string "Characteristics" variant array [ object path "/org/bluez/bincapplication/service0/char0" ] ) ] ) ] ) dict entry( object path "/org/bluez/bincapplication/service0/char0" array [ dict entry( string "org.bluez.GattCharacteristic1" array [ dict entry( string "UUID" variant string "00002a1c-0000-1000-8000-00805f9b34fb" ) dict entry( string "Service" variant object path "/org/bluez/bincapplication/service0" ) dict entry( string "Flags" variant array [ string "indicate" string "encrypt-indicate" ] ) dict entry( string "Notifying" variant boolean false ) dict entry( string "Descriptors" variant array [ object path "/org/bluez/bincapplication/service0/char0/desc0" ] ) ] ) ] ) dict entry( object path "/org/bluez/bincapplication/service0/char0/desc0" array [ dict entry( string "org.bluez.GattDescriptor1" array [ dict entry( string "Value" variant array of bytes "hello there" + \0 ) dict entry( string "UUID" variant string "00002901-0000-1000-8000-00805f9b34fb" ) dict entry( string "Characteristic" variant object path "/org/bluez/bincapplication/service0/char0" ) dict entry( string "Flags" variant array [ string "read" string "write" ] ) ] ) ] ) ] method call time=1707931247.847773 sender=:1.11 -> destination=org.freedesktop.DBus serial=43 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication/service0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.GattService1'" method return time=1707931247.849078 sender=org.freedesktop.DBus -> destination=:1.11 serial=27 reply_serial=43 method call time=1707931247.856739 sender=:1.11 -> destination=org.freedesktop.DBus serial=44 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication/service0/char0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.GattCharacteristic1'" method return time=1707931247.858204 sender=org.freedesktop.DBus -> destination=:1.11 serial=28 reply_serial=44 method call time=1707931247.862375 sender=:1.11 -> destination=org.freedesktop.DBus serial=45 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication/service0/char0/desc0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.GattDescriptor1'" method return time=1707931247.866687 sender=org.freedesktop.DBus -> destination=:1.11 serial=29 reply_serial=45 method return time=1707931247.867543 sender=:1.23 -> destination=:1.11 serial=11 reply_serial=42 array [ dict entry( string "Type" variant string "peripheral" ) dict entry( string "LocalName" variant string "BINC" ) dict entry( string "ManufacturerData" variant array [ ] ) dict entry( string "ServiceData" variant array [ ] ) dict entry( string "ServiceUUIDs" variant array [ string "00001809-0000-1000-8000-00805f9b34fb" ] ) ] error time=1707931247.882620 sender=:1.11 -> destination=:1.23 error_name=org.bluez.Error.Failed reply_serial=8 string "No valid service object found" method call time=1707931247.886861 sender=:1.11 -> destination=org.freedesktop.DBus serial=47 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch string "type='signal',sender=':1.23',path_namespace='/org/bluez/bincapplication'" method return time=1707931247.887801 sender=org.freedesktop.DBus -> destination=:1.11 serial=30 reply_serial=47 method call time=1707931247.890570 sender=:1.11 -> destination=org.freedesktop.DBus serial=48 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication/service0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.GattService1'" method return time=1707931247.891482 sender=org.freedesktop.DBus -> destination=:1.11 serial=31 reply_serial=48 method call time=1707931247.894172 sender=:1.11 -> destination=org.freedesktop.DBus serial=49 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication/service0/char0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.GattCharacteristic1'" method return time=1707931247.895073 sender=org.freedesktop.DBus -> destination=:1.11 serial=32 reply_serial=49 method call time=1707931247.895904 sender=:1.11 -> destination=org.freedesktop.DBus serial=50 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication/service0/char0/desc0',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.bluez.GattDescriptor1'" method return time=1707931247.898804 sender=org.freedesktop.DBus -> destination=:1.11 serial=33 reply_serial=50 method call time=1707931247.899870 sender=:1.11 -> destination=org.freedesktop.DBus serial=51 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesAdded'" method return time=1707931247.900975 sender=org.freedesktop.DBus -> destination=:1.11 serial=34 reply_serial=51 method call time=1707931247.904591 sender=:1.11 -> destination=org.freedesktop.DBus serial=52 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch string "type='signal',sender=':1.23',path='/org/bluez/bincapplication',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesRemoved'" method return time=1707931247.906458 sender=org.freedesktop.DBus -> destination=:1.11 serial=35 reply_serial=52 signal time=1707931247.910750 sender=:1.11 -> destination=(null destination) serial=53 path=/org/bluez/hci0; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged string "org.bluez.LEAdvertisingManager1" array [ dict entry( string "SupportedInstances" variant byte 3 ) dict entry( string "ActiveInstances" variant byte 1 ) ] array [ ] method return time=1707931247.916677 sender=:1.11 -> destination=:1.23 serial=54 reply_serial=7

ludwit commented 7 months ago

and ebian@BeagleBone:~/bluez_inc$ tail /var/log/syslog Feb 14 17:19:52 BeagleBone bluetoothd[1068]: sap-server: Operation not permitted (1) Feb 14 17:19:53 BeagleBone systemd[1]: Started Load/Save RF Kill Switch Status. Feb 14 17:19:53 BeagleBone systemd[1213]: Reached target Bluetooth. Feb 14 17:19:53 BeagleBone systemd[1]: Reached target Bluetooth. Feb 14 17:19:58 BeagleBone systemd[1]: systemd-rfkill.service: Succeeded. Feb 14 17:20:43 BeagleBone dbus-daemon[610]: [system] Connection :1.22 (uid=0 pid=31852 comm="dbus-monitor --system destination='org.bluez' send") became a monitor. Feb 14 17:20:47 BeagleBone bluetoothd[1068]: src/gatt-database.c:parse_chrc_flags() Invalid characteristic flag: encrypt-indicate Feb 14 17:20:47 BeagleBone bluetoothd[1068]: src/gatt-database.c:chrc_create() Failed to parse characteristic properties Feb 14 17:20:47 BeagleBone bluetoothd[1068]: src/gatt-database.c:client_ready_cb() No valid external GATT objects found Feb 14 17:20:51 BeagleBone dbus-daemon[610]: [system] Monitoring connection :1.22 closed.

weliem commented 7 months ago

Try removing the ENCRYPT_INDICATE flag. That was added in later versions of Bluez and in the log you see it crashes on it.....

ludwit commented 7 months ago

I removed the GATT_CHR_PROP_ENCRYPT_INDICATE flag in peripheral.c. Now the peripheral application works on the bealge bone with bluez 5.55 and bluetooth 4.0! Thanks for that!

I also have a raspberry pi running bluez 5.66 (the peripheral application works out of the box). Unfortunately, although I have the peripheral application working with beagel bone, ubuntu pc and raspberry pi, the central application is not connecting or retrieving data on any of these machines, although I can find the services via LightBlue. Do you have any idea why this might be happening? What was your setup like when you tested it?

weliem commented 7 months ago

The standard central app scans for a thermometer with a name prefix of "Taidoc". If you remove that the central will probably find the peripheral example.

ludwit commented 6 months ago

Works now thank you a lot!

ludwit commented 6 months ago

TLDR: