恒玄SDk预留了用户接口位于app_ibrt_customif_cmd.cpp 中
发送数据的前提是进行主从配对连接, sdk给与了两种模式,IBRT_SEARCH_UI 未定义时我们可以自己定义主从蓝牙地址,IBRT_SEARCH_UI 定义时进入serch模式通过app_start_tws_serching_direactly 这个接口进行连接,这里由于sdk此功能似乎有bug尚未能实现。
首先,自己定义主从蓝牙地址,然后烧录的时候安装你设置的地址烧入即可,如例烧录时:左耳 11 11 22 33 33 51,右耳11 11 22 33 33 50 。
SDK中自定义地址的字符串
ibrt_pairing_info_t g_ibrt_pairing_info[] =
{
{{0x51, 0x33, 0x33, 0x22, 0x11, 0x11},{0x50, 0x33, 0x33, 0x22, 0x11, 0x11}
},
之后使用接口app_ibrt_if_enter_pairing_after_tws_connected(); (定义到某个按钮事件即可)
这里定义到了PWR 长按事件
void app_ibrt_normal_ui_handle_key(APP_KEY_STATUS *status, void *param)
{
if (APP_KEY_CODE_GOOGLE != status->code)
{
switch(status->event)
{
case APP_KEY_EVENT_CLICK:
TRACE(0,"first blood.");
app_tws_if_handle_click();
break;
case APP_KEY_EVENT_DOUBLECLICK:
TRACE(0,"double kill");
app_ibrt_if_enter_freeman_pairing();
break;
case APP_KEY_EVENT_LONGPRESS:
TRACE(0,"LONGPRESS kill");
app_ibrt_if_enter_pairing_after_tws_connected();
break;
}
}
日志中打印app_tws_info_t 相关信息说明主从开始分享信息了 当出现evt_type = BES_AUD_CONNECTED 标志主从之间用来传输指令的 L2cap 通道建立成功
主从连接完毕后就可以和手机配对了,但我们的目的好像不是这个…
接下来就剩主从发送数据
sdk预留了相关接口app_ibrt_customif_cmd.cpp 中
static const app_tws_cmd_instance_t g_ibrt_custom_cmd_handler_table[]=
{
{
APP_IBRT_CUSTOM_CMD_TEST1, "TWS_CMD_TEST1",//发送的
app_ibrt_customif_test1_cmd_send,
app_ibrt_customif_test1_cmd_send_handler, 0,
app_ibrt_custom_cmd_rsp_timeout_handler_null, app_ibrt_custom_cmd_rsp_handler_null
},
{
APP_IBRT_CUSTOM_CMD_TEST2, "TWS_CMD_TEST2",
app_ibrt_customif_test2_cmd_send,
app_ibrt_customif_test2_cmd_send_handler, RSP_TIMEOUT_DEFAULT,
app_ibrt_customif_test2_cmd_send_rsp_timeout_handler, app_ibrt_customif_test2_cmd_send_rsp_handler
},
};
按顺序的参数说明
uint32_t cmdcode;
const char *log_cmd_code_str;
app_tws_cmd_send_handler_t tws_cmd_send;
app_tws_cmd_receivd_handler_t cmdhandler; /**< command handler function */
uint32_t timeout_ms;
app_tws_rsp_timeout_handle_t app_tws_rsp_timeout_handle;
app_tws_rsp_handle_t app_tws_rsp_handle;
app_tws_cmd_tx_done_handler_t app_tws_cmd_tx_done_handler;
按顺序的参数说明 我们只需要关注
app_ibrt_customif_cmd_test 发送数据
app_ibrt_customif_test1_cmd_send_handler 接收数据
了解结构后使用起来就很方便了
直接app_ibrt_customif_cmd_test (str)写在按键事件中即可。或者直接用库函数tws_ctrl_send_cmd()第一个参数是需要添加的枚举值
void app_ibrt_ui_test_key_io_event(APP_KEY_STATUS *status, void *param)
{
TRACE(3,"%s %d,%d",__func__, status->code, status->event);
uint8_t senddata[]={"close"};
switch(status->event)
{
case APP_KEY_EVENT_CLICK:
if (status->code== APP_KEY_CODE_FN1)
{
TRACE(0,"FN1 onepress");
app_ibrt_customif_cmd_test(senddata);//发送数据
tws_ctrl_send_cmd(0x8101, (uint8_t *)senddata, sizeof(senddata));//库函数发送数据
// app_ibrt_if_event_entry(IBRT_OPEN_BOX_EVENT);
//app_tws_set_side(EAR_SIDE_RIGHT);//改为左声道
//app_voice_report(APP_STATUS_INDICATION_TEST,0);
}
发完肯定还要接收
app_ibrt_customif_test1_cmd_send_handler 完成了这项工作,p_buff就是你收到的数据,你当然也可以对它加一个if(p_buff==“xxxx”)让它关机,或做其他事情,到此整个主从通讯就完成了。
参考文章有https://blog.csdn.net/zhanghuaishu0/article/details/108684583?spm=1001.2014.3001.5501
static void app_ibrt_customif_test1_cmd_send_handler(uint16_t rsp_seq, uint8_t *p_buff, uint16_t length)//接受句柄
{
TRACE(2,"%s %s", __func__,p_buff);
app_shutdown();//关机
}