簡體   English   中英

Ble 外設斷開連接處理程序在 gattlib c 庫中不起作用

[英]Ble peripheral disconnection handler is not working in gattlib c library

我嘗試了不同的更改,但我不知道為什么當 BLE 外圍設備斷開連接時,中央沒有得到確認或處理程序沒有調用。

我已經修改了斷開處理程序的 gattlib 發現示例。 我的代碼

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "gattlib.h"

void disconnectHandler(void *arg)
{
        printf("in disconnection handler \n");
}
int main(int argc, char *argv[])
{
        gatt_connection_t* connection;
        gattlib_primary_service_t* services;
        gattlib_characteristic_t* characteristics;
        int services_count, characteristics_count;
        char uuid_str[MAX_LEN_UUID_STR + 1];
        int ret, i;

        if (argc != 2) {
                printf("%s <device_address>\n", argv[0]);
                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;
        }
        if(gattlib_has_valid_handler(disconnectHandler) == 1)
        {
                printf("handler is valid\n");
                gattlib_register_on_disconnect(connection, disconnectHandler, NULL);
        }
        else
        {
                printf("handler is not valid\n");
        }
        ret = gattlib_discover_primary(connection, &services, &services_count);
        if (ret != GATTLIB_SUCCESS) {
                fprintf(stderr, "Fail to discover primary services.\n");
                return 1;
        }

        for (i = 0; i < services_count; i++) {
                gattlib_uuid_to_string(&services[i].uuid, uuid_str, sizeof(uuid_str));

                printf("service[%d] start_handle:%02x end_handle:%02x uuid:%s\n", i,
                                services[i].attr_handle_start, services[i].attr_handle_end,
                                uuid_str);
        }
        free(services);

        ret = gattlib_discover_char(connection, &characteristics, &characteristics_count);
        if (ret != GATTLIB_SUCCESS) {
                fprintf(stderr, "Fail to discover characteristics.\n");
                return 1;
        }
        for (i = 0; i < characteristics_count; i++) {
                gattlib_uuid_to_string(&characteristics[i].uuid, uuid_str, sizeof(uuid_str));

                printf("characteristic[%d] properties:%02x value_handle:%04x uuid:%s\n", i,
                                characteristics[i].properties, characteristics[i].value_handle,
                                uuid_str);
        }
        free(characteristics);
        for(int i=0; i<100; i++)
        {
                sleep(1);
                printf("In loop\n");
        }

        gattlib_disconnect(connection);
        return 0;
}

在 100 秒睡眠部分,我正在斷開外圍設備,但沒有收到 disconnectionHandler 調用。 我是否在 gettlib 庫中犯了任何基本錯誤或錯誤? 我用 btmon 日志檢查斷開連接打印,但它沒有豐富 gattlib 庫或我的應用程序。

btmod日志:

        Reason: Connection Timeout (0x08)
@ MGMT Event: Device Disconnected (0x000c) plen 8         {0x0002} [hci0] 327.987207
        LE Address: 30:AE:A4:F5:FF:1E (Espressif Inc.)
        Reason: Connection timeout (0x01)
@ MGMT Event: Device Disconnected (0x000c) plen 8         {0x0001} [hci0] 327.987207
        LE Address: 30:AE:A4:F5:FF:1E (Espressif Inc.)
        Reason: Connection timeout (0x01)

我使用 2020 年 7 月 21 日表示 gattlib 庫的最新提交。

斷開處理程序示例:

#include <assert.h>
#include <glib.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

#include "gattlib.h"

// Battery Level UUID
const uuid_t g_battery_level_uuid = CREATE_UUID16(0x2A19);

static GMainLoop *m_main_loop;

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

void disconnectHandler(void *arg)
{
        printf("in disconnection handler \n");
        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;
        }
        gattlib_register_on_disconnect(connection, disconnectHandler, NULL);
        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
        g_main_loop_unref(m_main_loop);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM