Files
player/Project/Src/MyApp/ble_demo.h
2025-07-05 19:47:28 +08:00

164 lines
3.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef BLE_DEMO_H__
#define BLE_DEMO_H__
#include "stdint.h"
//定义蓝牙的最大连接数
#define BLE_MAX_LINKED 5
//定义通知最大长度
#define BLE_NOTICE_LEN 256
//定义接收帧最大长度
#define BLE_RETURN_LINE_LEN 512
/*******************************************************
通知数据定义:
------------------------------------------------------+
主体 |obj_size |obj |op|data_size|data |
------------------------------------------------------+
长度(byte)|1 |obj_size |1 |1 |data_size |
------------------------------------------------------+
********************************************************/
//定义数据缓冲区大小
#define BLE_BUFF_LEN 512
#define BLE_UUID_TYPE_128 0x80
#define BLE_UUID_TYPE_32 0x20
#define BLE_UUID_TYPE_16 0x10
typedef struct
{
int buff_len;
int buff_used;
int read_ptr;
int save_ptr;
uint8_t buff[BLE_BUFF_LEN];
} ble_buff;
typedef struct
{
uint8_t mac[6];
uint16_t conn_handle;
int linked;
ble_buff buff_recv;
}ble_device;
typedef struct
{
uint8_t obj[4];
uint8_t obj_size;
uint8_t par[BLE_NOTICE_LEN];
int par_size;
}notice_t;
typedef struct
{
char mode; //工作模式'i'空闲;'h'主机;'s'从机
int linked; //当前连接数
ble_device device[BLE_MAX_LINKED];
notice_t notice;
uint8_t conn_mac[6]; //正在连接的蓝牙mac地址
uint8_t data_temp[BLE_BUFF_LEN]; //数据临时存储区
uint8_t return_line[BLE_RETURN_LINE_LEN]; //接收数据存储区
void (*linked_change)(uint16_t handle,uint16_t state);
void (*recv_data)(uint16_t handle,uint8_t *data,int len);
void (*user_irq)(uint8_t *data,int len);
void *ret_event;//返回事件
void *kno_event;//知晓事件
}ble_struct;
typedef struct
{
uint8_t mac[6];
int8_t rssi;
char name[20];
}ble_mac_name;
//ble初始化
void ble_init(ble_struct *ble);
//ble去初始化
void ble_deinit(ble_struct *ble);
//读取指定长度的数据
int ble_get_recv_data(ble_struct *ble,uint16_t handle,uint8_t *buff,int read_len);
//读取数据直到出现界定符
int ble_get_recv_data_by_end(ble_struct *ble,uint16_t handle,uint8_t *buff,int buff_size,uint8_t end_par);
//返回指定索引的蓝牙设备
ble_device *ble_find_device_by_index(ble_struct *ble,int index);
// 设置连接状态改变回调
int ble_set_callback_linked_change(ble_struct *ble,void (*linked_change)(uint16_t handle,uint16_t state));
// 设置接收到数据回调,如果设置了此回调函数,则接收到数据时不会保存到缓冲区
// 即通过 ble_get_recv_data | ble_get_recv_data_by_end 函数无法读取到数据
int ble_set_callback_recv_data(ble_struct *ble,void (*recv_data)(uint16_t handle,uint8_t *data,int len));
// 设置用户中断回调
int ble_set_callback_user_irq(ble_struct *ble,void (*user_irq)(uint8_t *data,int len));
//设置设备名称
int ble_set_name(ble_struct *ble,char *name);
//获取设备mac地址
int ble_get_mac(ble_struct *ble,uint8_t mac[6]);
//重启
int ble_reboot(ble_struct *ble);
//设置uuid
int ble_set_uuid(ble_struct *ble,uint8_t sub,uint8_t type,uint8_t *uuid);
//打开/关闭数据传输
int ble_set_data_transport(ble_struct *ble,uint8_t power);
//发送数据
int ble_send_data(ble_struct *ble,uint16_t handle,uint8_t *buff,int len);
//设置为从机模式
int ble_set_slave(ble_struct *ble);
//设置为主机模式
int ble_set_host(ble_struct *ble);
//连接指定的从机
int ble_connect_by_mac(ble_struct *ble,uint8_t mac[6]);
//断开指定的从机
int ble_discon_by_mac(ble_struct *ble,uint8_t mac[6]);
//扫描
int ble_scan(ble_struct *ble,ble_mac_name mac[32],int *mac_num);
//发送用户设置数据
int ble_user_cmd_set(ble_struct *ble,uint8_t *tx,int tx_len);
//发送用户获取数据rx_len获取的数据长度失败返回0
int ble_user_cmd_get(ble_struct *ble,uint8_t *tx,int tx_len,uint8_t *rx,int *rx_len);
#endif