【已解决】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

Wi-Fi 设备、Wi-Fi 低功耗设备、Wi-Fi BLE 双模设备、Ethernet设备、Ethernet+Wi-Fi设备等
wxq1123
Posts: 108
Location: 深圳市水世界有限公司

1)开发包全称:tuyaos-iot_3.8.1_bk7231n_wifi-ble-com_1.2.8.tar.gz
2)问题现象:请问下如何正确注册“tuya_ble_reg_raw_scan_adv_cb()”这个函数才能查看原始的蓝牙广播数据(非涂鸦 Beacon协议)。目前在example工程的 “example_ble_remote.c”文件里添加的,可能方法不对没有起到作用。求教正确方法。

Ubuntu 64 位-2023-11-20-10-03-29.png
微信截图_20231120100743.jpg
Last edited by wxq1123 on 2023年 Nov 27日 14:46, edited 1 time in total.
User avatar
chenyisong
Posts: 118

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

我在BK7231N上测试了这个功能是可以正常接收到数据的,测试代码如下:

Code: Select all

#include "tuya_bt.h"
VOID ble_raw_scan(TAL_BLE_ADV_REPORT_T *scan_info)
{
    TAL_PR_DEBUG("rssi:%d", scan_info->rssi);
    for (uint32_t i = 0; i < scan_info->data_len; i ++) {
        TAL_PR_DEBUG_RAW("%02x ", scan_info->p_data[i]);
    }
    TAL_PR_DEBUG_RAW("\r\n");
}
/**
* @brief 
*
* @param[in] data: data
* @param[in] len: data len
* @param[in] type: type
* @param[in] mac: device mac
* @return none
*/
STATIC VOID ble_receive_callback(UCHAR_T *data, UCHAR_T len,  UCHAR_T type, UCHAR_T* mac)
{
    INT_T i = 0;

    TAL_PR_DEBUG_RAW("ble_receive_callback: ");
    
    for (uint32_t t = 0; t < len; t ++) {
        TAL_PR_DEBUG_RAW("%02x ", data[t]);
    }
    TAL_PR_DEBUG_RAW("\r\n");

    return;
}

/**
* @brief ble scanf adv bind check callback
*
* @param[in] type: bind type for bluetooth remote controller
* @param[in] data: data
* @param[in] len: data len
* @return OPRT_OK
*/
STATIC OPERATE_RET ble_scan_adv_bind_check_callback(TUYA_BLE_BIND_TYPE type, UCHAR_T *data, UCHAR_T len)
{
    INT_T i = 0;
    
    TAL_PR_DEBUG("----------------scan adv bind check cb-----------------");
    TAL_PR_DEBUG("ble bind type : %d, data len: %d", type, len);
    for(i=0;i<len;i++) {
        TAL_PR_DEBUG("data[%d] : %d", i, data[i]);
    }

    return OPRT_OK;
}

/**
* @brief ble scan adv bind notify callback
*
* @param[in] type: bind type for bluetooth remote controller
* @return none
*/
STATIC VOID_T ble_adv_scan_bind_notify_callback(TUYA_BLE_BIND_TYPE type, int rslt)
{
    TAL_PR_DEBUG("------------------ble scan adv bind notify cb----------------");
    TAL_PR_DEBUG("ble bind type : %s", (type == TUYA_BLE_OP_BIND) ? "bind" : "unbind");
    TAL_PR_DEBUG("result : %s", (rslt == 0) ? "success" : "failed");

    return ;
}

/**
* @brief  ffc task
*
* @param[in] param:Task parameters
* @return none
*/
VOID ble_remote_init(VOID)
{
    OPERATE_RET rt = OPRT_OK;
    TUYA_BLE_SCAN_ADV_HANDLE_CBS ble_scan_cfg = {0};
    ble_scan_cfg.bind_check = ble_scan_adv_bind_check_callback;
    ble_scan_cfg.bind_notify = ble_adv_scan_bind_notify_callback;

    /* Register callback function for advertisement scanning data processing */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_cb(ble_receive_callback), __EXIT);

    /* Register callback function for advertisement scanning data processing */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_handle_cbs(&ble_scan_cfg), __EXIT);

    /* Register callback function for advertisement scanning data processing */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_raw_scan_adv_cb(ble_raw_scan), __EXIT);

    /* set bind window (s) */
    tuya_ble_set_bind_window(30);

__EXIT:
    return;
}

/**
 * @brief SOC device initialization
 *
 * @param[in] none
 *
 * @return OPRT_OK on success. Others on error, please refer to "tuya_error_code.h".
 */
OPERATE_RET __soc_device_init(VOID_T)
{
    OPERATE_RET rt = OPRT_OK;

#if (defined(UUID) && defined(AUTHKEY))
    ws_db_init_mf();

    /* Set authorization information
     * Note that if you use the default authorization information of the code, there may be problems of multiple users and conflicts, 
     * so try to use all the authorizations purchased from the tuya iot platform.
     * Buying guide: https://developer.tuya.com/cn/docs/iot/lisence-management?id=Kb4qlem97idl0.
     * You can also apply for two authorization codes for free in the five-step hardware development stage of the Tuya IoT platform.
     * Authorization information can also be written through the production testing tool.
     * When the production testing function is started and the authorization is burned with the Tuya Cloud module tool, 
     * please comment out this piece of code.
     */
    WF_GW_PROD_INFO_S prod_info = {UUID, AUTHKEY};
    TUYA_CALL_ERR_RETURN(tuya_iot_set_wf_gw_prod_info(&prod_info));
#else
    // 产测初始化, 注册函数需要应用实现,其中串口驱动不需要应用提供
    MF_IMPORT_INTF_S intf = {0};

    intf.uart_init = mf_uart_init_callback;
    intf.uart_free = mf_uart_free_callback;
    intf.uart_send = mf_uart_send_callback;
    intf.uart_recv = mf_uart_recv_callback;

    intf.mf_user_product_test = mf_user_product_test_callback;
    intf.user_callback = mf_user_callback;
    intf.user_enter_mf_callback = mf_user_enter_mf_callback;

    TAL_PR_ERR("mf_init APP_BIN_NAME[%s] USER_SW_VER[%s]", APP_BIN_NAME, USER_SW_VER);
    
    TUYA_CALL_ERR_RETURN(mf_init(&intf, APP_BIN_NAME, USER_SW_VER, TRUE));
#endif

    /* Initialize TuyaOS product information */
    TY_IOT_CBS_S iot_cbs = {0};
    iot_cbs.gw_status_cb = __soc_dev_status_changed_cb;
    iot_cbs.gw_ug_cb = __soc_dev_rev_upgrade_info_cb;
    iot_cbs.gw_reset_cb = __soc_dev_restart_req_cb;
    iot_cbs.dev_obj_dp_cb = __soc_dev_obj_dp_cmd_cb;
    iot_cbs.dev_raw_dp_cb = __soc_dev_raw_dp_cmd_cb;
    iot_cbs.dev_dp_query_cb = __soc_dev_dp_query_cb;

    TUYA_CALL_ERR_RETURN(tuya_iot_wf_soc_dev_init(GWCM_OLD, WF_START_AP_FIRST, &iot_cbs, PID, USER_SW_VER));

    TUYA_CALL_ERR_RETURN(tuya_iot_reg_get_wf_nw_stat_cb(__soc_dev_net_status_cb));

    /* Network button, LED initialization */
    app_led_init(LED_PIN);
    app_key_init(KEY_PIN);
    
    hw_output_init();

    ble_remote_init();
    
    return 0;
}

以上代码在tuyaos_demo_quickstart工程中运行。
在开启绑定窗口内(可通过tuya_ble_set_bind_window() 设置)可以接收到数据,可以通过 tuya_ble_open_bind_window();再次开启。

wxq1123
Posts: 108
Location: 深圳市水世界有限公司

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

chenyisong 2023年 Nov 20日 19:32

我在BK7231N上测试了这个功能是可以正常接收到数据的,测试代码如下:

Code: Select all

#include "tuya_bt.h"
VOID ble_raw_scan(TAL_BLE_ADV_REPORT_T *scan_info)
{
    TAL_PR_DEBUG("rssi:%d", scan_info->rssi);
    for (uint32_t i = 0; i < scan_info->data_len; i ++) {
        TAL_PR_DEBUG_RAW("%02x ", scan_info->p_data[i]);
    }
    TAL_PR_DEBUG_RAW("\r\n");
}
/**
* @brief 
*
* @param[in] data: data
* @param[in] len: data len
* @param[in] type: type
* @param[in] mac: device mac
* @return none
*/
STATIC VOID ble_receive_callback(UCHAR_T *data, UCHAR_T len,  UCHAR_T type, UCHAR_T* mac)
{
    INT_T i = 0;

    TAL_PR_DEBUG_RAW("ble_receive_callback: ");
    
    for (uint32_t t = 0; t < len; t ++) {
        TAL_PR_DEBUG_RAW("%02x ", data[t]);
    }
    TAL_PR_DEBUG_RAW("\r\n");

    return;
}

/**
* @brief ble scanf adv bind check callback
*
* @param[in] type: bind type for bluetooth remote controller
* @param[in] data: data
* @param[in] len: data len
* @return OPRT_OK
*/
STATIC OPERATE_RET ble_scan_adv_bind_check_callback(TUYA_BLE_BIND_TYPE type, UCHAR_T *data, UCHAR_T len)
{
    INT_T i = 0;
    
    TAL_PR_DEBUG("----------------scan adv bind check cb-----------------");
    TAL_PR_DEBUG("ble bind type : %d, data len: %d", type, len);
    for(i=0;i<len;i++) {
        TAL_PR_DEBUG("data[%d] : %d", i, data[i]);
    }

    return OPRT_OK;
}

/**
* @brief ble scan adv bind notify callback
*
* @param[in] type: bind type for bluetooth remote controller
* @return none
*/
STATIC VOID_T ble_adv_scan_bind_notify_callback(TUYA_BLE_BIND_TYPE type, int rslt)
{
    TAL_PR_DEBUG("------------------ble scan adv bind notify cb----------------");
    TAL_PR_DEBUG("ble bind type : %s", (type == TUYA_BLE_OP_BIND) ? "bind" : "unbind");
    TAL_PR_DEBUG("result : %s", (rslt == 0) ? "success" : "failed");

    return ;
}

/**
* @brief  ffc task
*
* @param[in] param:Task parameters
* @return none
*/
VOID ble_remote_init(VOID)
{
    OPERATE_RET rt = OPRT_OK;
    TUYA_BLE_SCAN_ADV_HANDLE_CBS ble_scan_cfg = {0};
    ble_scan_cfg.bind_check = ble_scan_adv_bind_check_callback;
    ble_scan_cfg.bind_notify = ble_adv_scan_bind_notify_callback;

    /* Register callback function for advertisement scanning data processing */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_cb(ble_receive_callback), __EXIT);

    /* Register callback function for advertisement scanning data processing */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_handle_cbs(&ble_scan_cfg), __EXIT);

    /* Register callback function for advertisement scanning data processing */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_raw_scan_adv_cb(ble_raw_scan), __EXIT);

    /* set bind window (s) */
    tuya_ble_set_bind_window(30);

__EXIT:
    return;
}

/**
 * @brief SOC device initialization
 *
 * @param[in] none
 *
 * @return OPRT_OK on success. Others on error, please refer to "tuya_error_code.h".
 */
OPERATE_RET __soc_device_init(VOID_T)
{
    OPERATE_RET rt = OPRT_OK;

#if (defined(UUID) && defined(AUTHKEY))
    ws_db_init_mf();

    /* Set authorization information
     * Note that if you use the default authorization information of the code, there may be problems of multiple users and conflicts, 
     * so try to use all the authorizations purchased from the tuya iot platform.
     * Buying guide: https://developer.tuya.com/cn/docs/iot/lisence-management?id=Kb4qlem97idl0.
     * You can also apply for two authorization codes for free in the five-step hardware development stage of the Tuya IoT platform.
     * Authorization information can also be written through the production testing tool.
     * When the production testing function is started and the authorization is burned with the Tuya Cloud module tool, 
     * please comment out this piece of code.
     */
    WF_GW_PROD_INFO_S prod_info = {UUID, AUTHKEY};
    TUYA_CALL_ERR_RETURN(tuya_iot_set_wf_gw_prod_info(&prod_info));
#else
    // 产测初始化, 注册函数需要应用实现,其中串口驱动不需要应用提供
    MF_IMPORT_INTF_S intf = {0};

    intf.uart_init = mf_uart_init_callback;
    intf.uart_free = mf_uart_free_callback;
    intf.uart_send = mf_uart_send_callback;
    intf.uart_recv = mf_uart_recv_callback;

    intf.mf_user_product_test = mf_user_product_test_callback;
    intf.user_callback = mf_user_callback;
    intf.user_enter_mf_callback = mf_user_enter_mf_callback;

    TAL_PR_ERR("mf_init APP_BIN_NAME[%s] USER_SW_VER[%s]", APP_BIN_NAME, USER_SW_VER);
    
    TUYA_CALL_ERR_RETURN(mf_init(&intf, APP_BIN_NAME, USER_SW_VER, TRUE));
#endif

    /* Initialize TuyaOS product information */
    TY_IOT_CBS_S iot_cbs = {0};
    iot_cbs.gw_status_cb = __soc_dev_status_changed_cb;
    iot_cbs.gw_ug_cb = __soc_dev_rev_upgrade_info_cb;
    iot_cbs.gw_reset_cb = __soc_dev_restart_req_cb;
    iot_cbs.dev_obj_dp_cb = __soc_dev_obj_dp_cmd_cb;
    iot_cbs.dev_raw_dp_cb = __soc_dev_raw_dp_cmd_cb;
    iot_cbs.dev_dp_query_cb = __soc_dev_dp_query_cb;

    TUYA_CALL_ERR_RETURN(tuya_iot_wf_soc_dev_init(GWCM_OLD, WF_START_AP_FIRST, &iot_cbs, PID, USER_SW_VER));

    TUYA_CALL_ERR_RETURN(tuya_iot_reg_get_wf_nw_stat_cb(__soc_dev_net_status_cb));

    /* Network button, LED initialization */
    app_led_init(LED_PIN);
    app_key_init(KEY_PIN);
    
    hw_output_init();

    ble_remote_init();
    
    return 0;
}

以上代码在tuyaos_demo_quickstart工程中运行。
在开启绑定窗口内(可通过tuya_ble_set_bind_window() 设置)可以接收到数据,可以通过 tuya_ble_open_bind_window();再次开启。

我按您的要求把代码复制过去,也添加了open_bind函数,可是还是看不到数据呢,手机用蓝牙工具APP能看到。您看下是哪里出的问题(补充下,遥控的数据目前只在广播的38信道发送数据)

Ubuntu 64 位-2023-11-21-10-57-23.png
log.jpg
phone.jpg
bamy
Posts: 43

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

1)tuya_ble_set_bind_window(0)后再open,可以永久打开时间窗
2)用NRF查看下是否能扫到你的beacon包,是否符合标准

wxq1123
Posts: 108
Location: 深圳市水世界有限公司

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

bamy 2023年 Nov 21日 12:02

1)tuya_ble_set_bind_window(0)后再open,可以永久打开时间窗
2)用NRF查看下是否能扫到你的beacon包,是否符合标准

应该符合什么样的标准呢?以前老的tuyaos用“esp_hal_ble_gap_set_event_cb()”注册回调后能能在“case ESP_GAP_BLE_SCAN_RESULT_EVT:”里面收到数据的。遥控数据的NRF接收如下图

det.jpg
wxq1123
Posts: 108
Location: 深圳市水世界有限公司

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

bamy 2023年 Nov 21日 12:02

1)tuya_ble_set_bind_window(0)后再open,可以永久打开时间窗
2)用NRF查看下是否能扫到你的beacon包,是否符合标准

或者有其他接口能接收蓝牙广播数据吗?

hearge
Posts: 39

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

广播的类型可以改下么?不太确定是不是因为这个类型导致的,改成BrEdrNotSupported类型试下。

Attachments
企业微信截图_17005547854307.png
wxq1123
Posts: 108
Location: 深圳市水世界有限公司

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

hearge 2023年 Nov 21日 16:22

广播的类型可以改下么?不太确定是不是因为这个类型导致的,改成BrEdrNotSupported类型试下。

改了 ,还是不行

111.jpg
22.jpg
hearge
Posts: 39

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

需要把GeneralDiscoverable 这个加上。目前除这三种的广播类型ADV_IND,SCAN_RSP,NONCONN_IND 的类型之外,其他类型都会过滤掉。

wxq1123
Posts: 108
Location: 深圳市水世界有限公司

Re: 【求助】【TuyaOs】请教下如何扫描蓝牙遥控原始数据

hearge 2023年 Nov 22日 14:34

需要把GeneralDiscoverable 这个加上。目前除这三种的广播类型ADV_IND,SCAN_RSP,NONCONN_IND 的类型之外,其他类型都会过滤掉。

“广播类型ADV_IND,SCAN_RSP,NONCONN_IND ”是指截图中 目前“Legacy”这个类型吗

Post Reply