Files
player/Project_App_Chat/App_Src/calc/ble_demo.h
2025-07-05 19:47:28 +08:00

159 lines
2.6 KiB
C

#ifndef BLE_DEMO_H__
#define BLE_DEMO_H__
#include "stdint.h"
#ifndef u8
# define u8 uint8_t
#endif
#ifndef u16
# define u16 uint16_t
#endif
#ifndef u32
# define u32 uint32_t
#endif
//定义蓝牙的最大连接数
#define BLE_MAX_LINKED 5
//定义通知缓冲条数
#define BLE_MAX_NOTICE 1
//定义通知最大长度
#define BLE_NOTICE_LEN 25
/*******************************************************
通知数据定义:
------------------------------------------------------+
主体 |obj_size |obj |op|data_size|data |
------------------------------------------------------+
长度(byte)|1 |obj_size |1 |1 |data_size |
------------------------------------------------------+
********************************************************/
//定义数据缓冲区大小
#define BLE_BUFF_LEN 512
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
{
char mode; //工作模式'i'空闲;'h'主机;'s'从机
int linked; //当前连接数
ble_device device[BLE_MAX_LINKED];
uint8_t notice[BLE_MAX_NOTICE][BLE_NOTICE_LEN];
int notice_used;
uint8_t conn_mac[6]; //正在连接的蓝牙mac地址
uint8_t data_temp[30]; //数据临时存储区
}ble_struct;
typedef struct
{
uint8_t mac[6];
int8_t rssi;
char name[20];
}ble_mac_name;
//ble初始化
void ble_init(ble_struct *ble);
//处理通知消息
int ble_deal_notice(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_get_cmd_return(ble_struct *ble,uint8_t *buff,int *buff_size);
//设置设备名称
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);
#endif