• 大小: 29.87MB
    文件类型: .gz
    金币: 1
    下载: 0 次
    发布日期: 2023-07-16
  • 语言: C/C++
  • 标签: ble  c语言  

资源简介

由于bluez并没有提供官方可用的C语言 gatt库,所以我对源码结构做了些修改,把用到的ap编译成静态库,使用前阅读readme.txt 详情参考https://blog.csdn.net/u010659887/article/details/85329276

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include “bluetooth/bluetooth.h“
#include “bluetooth/l2cap.h“
#include “bluetooth/hci.h“
#include “bluetooth/hci_lib.h“
#include “bluetooth/sdp.h“

#include “uuid.h“
#include “src/shared/mainloop.h“
#include “src/shared/util.h“
#include “src/shared/att.h“
#include “src/shared/queue.h“
#include “src/shared/gatt-db.h“
#include “src/shared/gatt-client.h“


#define COLOR_OFF “\x1B[0m“
#define COLOR_RED “\x1B[0;91m“
#define COLOR_GREEN “\x1B[0;92m“
#define COLOR_YELLOW “\x1B[0;93m“
#define COLOR_BLUE “\x1B[0;94m“
#define COLOR_MAGENTA “\x1B[0;95m“
#define COLOR_BOLDGRAY “\x1B[1;30m“
#define COLOR_BOLDWHITE “\x1B[1;37m“

#define ATT_CID 4

struct client {
int fd;
struct bt_att *att;
struct gatt_db *db;
struct bt_gatt_client *gatt;

unsigned int reliable_session_id;
};
//打印UUID
static void print_uuid(const bt_uuid_t *uuid)
{
char uuid_str[MAX_LEN_UUID_STR];
bt_uuid_t uuid128;

bt_uuid_to_uuid128(uuid &uuid128);
bt_uuid_to_string(&uuid128 uuid_str sizeof(uuid_str));

printf(“%s\n“ uuid_str);
}

static void print_prompt(void)
{
printf(COLOR_BLUE “[GATT client]“ COLOR_OFF “# “);
fflush(stdout);
}
static void print_incl(struct gatt_db_attribute *attr void *user_data)
{
struct client *cli = user_data;
uint16_t handle start end;
struct gatt_db_attribute *service;
bt_uuid_t uuid;

if (!gatt_db_attribute_get_incl_data(attr &handle &start &end))
return;

service = gatt_db_get_attribute(cli->db start);
if (!service)
return;

gatt_db_attribute_get_service_uuid(service &uuid);

printf(“\t  “ COLOR_GREEN “include“ COLOR_OFF “ - handle: “
“0x%04x - start: 0x%04x end: 0x%04x“
“uuid: “ handle start end);
print_uuid(&uuid);
}
static void print_desc(struct gatt_db_attribute *attr void *user_data)
{
printf(“\t\t  “ COLOR_MAGENTA “descr“ COLOR_OFF
“ - handle: 0x%04x uuid: “
gatt_db_attribute_get_handle(attr));
print_uuid(gatt_db_attribute_get_type(attr));
}
static void print_chrc(struct gatt_db_attribute *attr void *user_data)
{
uint16_t handle value_handle;
uint8_t properties;
uint16_t ext_prop;
bt_uuid_t uuid;

if (!gatt_db_attribute_get_char_data(attr &handle
&value_handle
&properties
&ext_prop
&uuid))
return;

printf(“\t  “ COLOR_YELLOW “charac“ COLOR_OFF
“ - start: 0x%04x value: 0x%04x “
“props: 0x%02x ext_props: 0x%04x uuid: “
handle value_handle properties ext_prop);
print_uuid(&uuid);

gatt_db_service_foreach_desc(attr print_desc NULL);
}
//打印改变的service
static void print_service(struct gatt_db_attribute *attr void *user_data)
{
struct client *cli = user_data;
uint16_t start end;
bool primary;
bt_uuid_t uuid;

if (!gatt_db

评论

共有 条评论