labapart / gattlib

Library to access GATT information from BLE (Bluetooth Low Energy) devices
http://labapart.com/
436 stars 157 forks source link

Notifications not received, Bosch CISS sensor #203

Open Zumtak opened 3 years ago

Zumtak commented 3 years ago

Hi,

I'm currently using gattlib to connect to this Bosch sensor. My goal is to retrieve information using the notifications system includes in it.
Here is the datasheet which describes the GATT profile of the sensor : link

I tried to modify thenotification.c file example provided on the repository (according to the documentation) to get notified by my sensor. However, I cannot get any notifications from it.

I already saw this issue but unfortunately, it does not help.

Here is my system specifications and my version of Bluez :

Here is the notifications.c example with my modification, according to the documentation :

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib-2.0/glib.h>
#include <stdint.h>
#include <unistd.h>

#include "include/gattlib.h"

#define INERTIAL_STR_UUID "00007502-0000-1000-8000-00805f9b34fb"
#define ENVIRONMENTAL_STR_UUID "00007504-0000-1000-8000-00805f9b34fb"
#define CONFIG_STR_UUID "0000750a-0000-1000-8000-00805f9b34fb"

static GMainLoop *m_main_loop;

void notification_handler(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data) {
    int i;

    printf("Notification Handler: ");

    for (i = 0; i < data_length; i++) {
        printf("%02x ", data[i]);
    }
    printf("\n");
}

static void on_user_abort(int arg) {
    (void) arg;
    g_main_loop_quit(m_main_loop);
}

static void usage(char *argv[]) {
    printf("%s <device_address>\n", argv[0]);
}

int main(int argc, char *argv[]) {
    int ret;
    gatt_connection_t* connection;

    if (argc != 2) {
        usage(argv);
        return 1;
    }

    connection = gattlib_connect(NULL, argv[1], GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
    if (connection == NULL) {
        fprintf(stderr, "Fail to connect to the bluetooth device.\n");
        return 1;
    } else {
        puts("Successfully connected to the device");
    }
    uuid_t inertial_uuid;
    uuid_t environmental_uuid;
    uuid_t config_uuid;

    if (gattlib_string_to_uuid(INERTIAL_STR_UUID, strlen(INERTIAL_STR_UUID) + 1, &inertial_uuid) != GATTLIB_SUCCESS ||
            gattlib_string_to_uuid(ENVIRONMENTAL_STR_UUID, strlen(ENVIRONMENTAL_STR_UUID) + 1, &environmental_uuid) != GATTLIB_SUCCESS ||
            gattlib_string_to_uuid(CONFIG_STR_UUID, strlen(CONFIG_STR_UUID) + 1, &config_uuid) != GATTLIB_SUCCESS) {
        puts("Error : bad UUID given");
        return (-1);
    }

    gattlib_register_notification(connection, notification_handler, NULL);
    uint8_t data_config[8] = {0x19, 0x0f, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00};

    puts("Sending configuration");
    ret = gattlib_write_char_by_uuid(connection, &config_uuid, data_config, sizeof(data_config));
    if (ret) {
        fprintf(stderr, "Fail to write config.\n");
        goto DISCONNECT;
    }
    sleep(1);
    puts("Setting up the notifications");
    ret = gattlib_notification_start(connection, &environmental_uuid);
    if (ret) {
        fprintf(stderr, "Fail to start notification.\n");
        goto DISCONNECT;
    }
    ret = gattlib_notification_start(connection, &inertial_uuid);
    if (ret) {
        fprintf(stderr, "Fail to start notification.\n");
        goto DISCONNECT;
    }
    puts("Starting g_main_loop\nWaiting for notifications");

    // Catch CTRL-C
    signal(SIGINT, on_user_abort);

    m_main_loop = g_main_loop_new(NULL, 0);
    g_main_loop_run(m_main_loop);

    // In case we quit the main loop, clean the connection
    gattlib_notification_stop(connection, &environmental_uuid);
    gattlib_notification_stop(connection, &inertial_uuid);
    g_main_loop_unref(m_main_loop);

DISCONNECT:
    gattlib_disconnect(connection);
    puts("Done");
    return ret;
}

Quick explanations:
After connecting to the device, I am sending to it an array that described its configuration (see documentation at the top of the issue). I told it to enable all the sensors (inertial and environmental) and push the notifications every second.

I do not understand what I am doing wrong, is anyone has an idea?

Thanks in advance for your replies.

Ps: The notifications work perfectly using the pygatt lib. This library uses the gatttol binary, in order to communicate in Bluetooth.

Pps: It is the first time I have done Bluetooth so there may be errors in the code and/or some misunderstanding.