最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

bluetooth lowenergy - bluez_inC (aka binc) peripheral sample don't work on a Raspberry but works on laptop - Stack Overf

programmeradmin5浏览0评论

I try ti use the recommended Bluez_in_C library (I mean recommended in some post here) I first start with the peripheral sample (see below). It works on my two Debian laptop (Debian 11 or 12) but don't work a Raspberry PI4 (raspbian 12).

2025-03-25 01:19:23:536 DEBUG [Adapter] successfully registered application
2025-03-25 01:19:23:537 ERROR [Adapter] failed to register advertisement (error 36: GDBus.Error:.bluez.Error.Failed: Failed to parse advertisement.)

What is the problem ?

#define HTS_SERVICE_UUID "00001809-0000-1000-8000-00805f9b34fb"
#define TEMPERATURE_CHAR_UUID "00002a1c-0000-1000-8000-00805f9b34fb"
#define CUD_CHAR "00002901-0000-1000-8000-00805f9b34fb"

int main(void) {
// Get a DBus connection
GDBusConnection *dbusConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);

// Setup handler for CTRL+C
if (signal(SIGINT, cleanup_handler) == SIG_ERR)
    log_error(TAG, "can't catch SIGINT");

// Setup mainloop
loop = g_main_loop_new(NULL, FALSE);

// Get the default default_adapter
default_adapter = binc_adapter_get_default(dbusConnection);

if (default_adapter != NULL) {
    log_debug(TAG, "using default_adapter '%s'", binc_adapter_get_path(default_adapter));

    // Make sure the adapter is on
    binc_adapter_set_powered_state_cb(default_adapter, &on_powered_state_changed);
    if (!binc_adapter_get_powered_state(default_adapter)) {
        binc_adapter_power_on(default_adapter);
    }

    // Setup remote central connection state callback
    binc_adapter_set_remote_central_cb(default_adapter, &on_central_state_changed);

    // Setup advertisement
    GPtrArray *adv_service_uuids = g_ptr_array_new();
    g_ptr_array_add(adv_service_uuids, HTS_SERVICE_UUID);

    advertisement = binc_advertisement_create();
    binc_advertisement_set_local_name(advertisement, "BINC");
    binc_advertisement_set_interval(advertisement, 500, 500);
    binc_advertisement_set_tx_power(advertisement, 5);
    binc_advertisement_set_services(advertisement, adv_service_uuids);
    g_ptr_array_free(adv_service_uuids, TRUE);
    binc_adapter_start_advertising(default_adapter, advertisement);

    // Start application
    app = binc_create_application(default_adapter);
    binc_application_add_service(app, HTS_SERVICE_UUID);
    binc_application_add_characteristic(
            app,
            HTS_SERVICE_UUID,
            TEMPERATURE_CHAR_UUID,
            GATT_CHR_PROP_INDICATE | GATT_CHR_PROP_WRITE);
    binc_application_add_descriptor(
            app,
            HTS_SERVICE_UUID,
            TEMPERATURE_CHAR_UUID,
            CUD_CHAR,
            GATT_CHR_PROP_READ | GATT_CHR_PROP_WRITE);

    const guint8 cud[] = "hello there";
    GByteArray *cudArray = g_byte_array_sized_new(sizeof(cud));
    g_byte_array_append(cudArray, cud, sizeof(cud));
    binc_application_set_desc_value(app, HTS_SERVICE_UUID, TEMPERATURE_CHAR_UUID, CUD_CHAR, cudArray);

    binc_application_set_char_read_cb(app, &on_local_char_read);
    binc_application_set_char_write_cb(app, &on_local_char_write);
    binc_application_set_char_start_notify_cb(app, &on_local_char_start_notify);
    binc_application_set_char_stop_notify_cb(app, &on_local_char_stop_notify);
    binc_application_set_char_updated_cb(app, &on_local_char_updated);
    binc_adapter_register_application(default_adapter, app);

(this is an extract. I don't copy here all the callback and the deinit part)

I try ti use the recommended Bluez_in_C library (I mean recommended in some post here) I first start with the peripheral sample (see below). It works on my two Debian laptop (Debian 11 or 12) but don't work a Raspberry PI4 (raspbian 12).

2025-03-25 01:19:23:536 DEBUG [Adapter] successfully registered application
2025-03-25 01:19:23:537 ERROR [Adapter] failed to register advertisement (error 36: GDBus.Error:.bluez.Error.Failed: Failed to parse advertisement.)

What is the problem ?

#define HTS_SERVICE_UUID "00001809-0000-1000-8000-00805f9b34fb"
#define TEMPERATURE_CHAR_UUID "00002a1c-0000-1000-8000-00805f9b34fb"
#define CUD_CHAR "00002901-0000-1000-8000-00805f9b34fb"

int main(void) {
// Get a DBus connection
GDBusConnection *dbusConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);

// Setup handler for CTRL+C
if (signal(SIGINT, cleanup_handler) == SIG_ERR)
    log_error(TAG, "can't catch SIGINT");

// Setup mainloop
loop = g_main_loop_new(NULL, FALSE);

// Get the default default_adapter
default_adapter = binc_adapter_get_default(dbusConnection);

if (default_adapter != NULL) {
    log_debug(TAG, "using default_adapter '%s'", binc_adapter_get_path(default_adapter));

    // Make sure the adapter is on
    binc_adapter_set_powered_state_cb(default_adapter, &on_powered_state_changed);
    if (!binc_adapter_get_powered_state(default_adapter)) {
        binc_adapter_power_on(default_adapter);
    }

    // Setup remote central connection state callback
    binc_adapter_set_remote_central_cb(default_adapter, &on_central_state_changed);

    // Setup advertisement
    GPtrArray *adv_service_uuids = g_ptr_array_new();
    g_ptr_array_add(adv_service_uuids, HTS_SERVICE_UUID);

    advertisement = binc_advertisement_create();
    binc_advertisement_set_local_name(advertisement, "BINC");
    binc_advertisement_set_interval(advertisement, 500, 500);
    binc_advertisement_set_tx_power(advertisement, 5);
    binc_advertisement_set_services(advertisement, adv_service_uuids);
    g_ptr_array_free(adv_service_uuids, TRUE);
    binc_adapter_start_advertising(default_adapter, advertisement);

    // Start application
    app = binc_create_application(default_adapter);
    binc_application_add_service(app, HTS_SERVICE_UUID);
    binc_application_add_characteristic(
            app,
            HTS_SERVICE_UUID,
            TEMPERATURE_CHAR_UUID,
            GATT_CHR_PROP_INDICATE | GATT_CHR_PROP_WRITE);
    binc_application_add_descriptor(
            app,
            HTS_SERVICE_UUID,
            TEMPERATURE_CHAR_UUID,
            CUD_CHAR,
            GATT_CHR_PROP_READ | GATT_CHR_PROP_WRITE);

    const guint8 cud[] = "hello there";
    GByteArray *cudArray = g_byte_array_sized_new(sizeof(cud));
    g_byte_array_append(cudArray, cud, sizeof(cud));
    binc_application_set_desc_value(app, HTS_SERVICE_UUID, TEMPERATURE_CHAR_UUID, CUD_CHAR, cudArray);

    binc_application_set_char_read_cb(app, &on_local_char_read);
    binc_application_set_char_write_cb(app, &on_local_char_write);
    binc_application_set_char_start_notify_cb(app, &on_local_char_start_notify);
    binc_application_set_char_stop_notify_cb(app, &on_local_char_stop_notify);
    binc_application_set_char_updated_cb(app, &on_local_char_updated);
    binc_adapter_register_application(default_adapter, app);

(this is an extract. I don't copy here all the callback and the deinit part)

Share Improve this question asked Mar 25 at 0:24 MichelMichel 13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Can you also check if the adapter is up and running using hciconfig ??

before you call start_advertising , can you just stop and retry ??

binc_adapter_stop_advertising(default_adapter, advertisement);
binc_adapter_start_advertising(default_adapter, advertisement);

You are probably running an older version of Bluez on the Pi4. Try updating to the latest Bluez version.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论