Bluetooth Ble外围设备断开处理程序在gattlib c库中不工作
我尝试了不同的更改,但我不知道为什么当BLE外围设备断开连接时,中央没有得到确认或处理程序没有调用 我已经修改了用于断开处理程序的gattlib discover示例。 我的代码Bluetooth Ble外围设备断开处理程序在gattlib c库中不工作,bluetooth,bluetooth-lowenergy,wireless,dbus,gattlib,Bluetooth,Bluetooth Lowenergy,Wireless,Dbus,Gattlib,我尝试了不同的更改,但我不知道为什么当BLE外围设备断开连接时,中央没有得到确认或处理程序没有调用 我已经修改了用于断开处理程序的gattlib discover示例。 我的代码 我使用的是2020年7月21日表示gattlib库的最新提交。断开连接处理程序示例: #include <assert.h> #include <glib.h> #include <signal.h> #include <stdio.h> #include <std
我使用的是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;
}
#包括
#包括
#包括
#包括
#包括
#包括“gattlib.h”
//电池电平UUID
const uuid\u t g\u电池水平\u uuid=CREATE\u UUID16(0x2A19);
静态GMainLoop*m_主循环;
用户中止时静态无效(int arg){
g_main_loop_quit(m_main_loop);
}
void disconnectHandler(void*arg)
{
printf(“断开连接处理程序中的\n”);
g_main_loop_quit(m_main_loop);
}
静态无效用法(char*argv[]){
printf(“%s\n”,argv[0]);
}
int main(int argc,char*argv[]){
int ret;
关贸总协定连接;
如果(argc!=2){
用法(argv);
返回1;
}
connection=gattlib_connect(NULL,argv[1],gattlib_connection_OPTIONS_LEGACY_DEFAULT);
if(连接==NULL){
fprintf(stderr,“无法连接到蓝牙设备。\n”);
返回1;
}
gattlib_寄存器_on_disconnect(连接,disconnectHandler,NULL);
m_main_loop=g_main_loop_new(NULL,0);
g_主循环运行(m_主循环);
//如果退出主循环,请清理连接
g_主回路(m_主回路);
断开:
gattlib_断开(连接);
卖出(“完成”);
返回ret;
}
我希望您必须运行一个主循环才能正常工作,请参阅通知示例,因为我缺少主循环。
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)
#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;
}