Custom Beacon Remote

Last Updated on : 2024-02-23 09:29:31download

Bluetooth mesh devices can receive control commands from both a Bluetooth mesh remote and a regular Bluetooth LE advertising packet. This makes it possible to control Bluetooth mesh devices via a Bluetooth LE beacon remote.

The Bluetooth LE beacon remote sends Bluetooth LE beacon packets that can be received, decrypted, and responded to by mesh devices.

Data structure

TAL_BLE_ADDR_T

typedef struct {
    TAL_BLE_ADDR_TYPE_E     type;                       /**< Mac Address Type, Refer to @ TAL_BLE_ADDR_TYPE_E */
    UCHAR_T                 addr[6];                    /**< GAP Address, Address size, 6 bytes */
} TAL_BLE_ADDR_T;
  • type: The type of MAC address, which can be public address or random address.

  • addr: The MAC address.

TAL_BLE_ADV_DATA_TYPE_E

 typedef enum {
    TAL_BLE_ADV_DATA,                                   /**< Adv Data - Only */
    TAL_BLE_RSP_DATA,                                   /**< Scan Response Data - Only */
    TAL_BLE_ADV_RSP_DATA,                               /**< Adv Data + Scan Response Data */
} TAL_BLE_ADV_DATA_TYPE_E;

Type of advertising data:

  • TAL_BLE_ADV_DATA: Advertising data.

  • TAL_BLE_RSP_DATA: Scan response data.

TAL_BLE_ADV_REPORT_T

typedef struct {
    TAL_BLE_ADDR_T          peer_addr;                  /**< After scan Adv, we can get peer mac and mac type */
    TAL_BLE_ADV_DATA_TYPE_E adv_type;                   /**< Point the advertising type, refer to @ TAL_BLE_ADV_DATA_TYPE_E */

    CHAR_T                  rssi;                       /**< After scan Adv, we can get advertising's rssi */
    UCHAR_T                 *p_data;                    /**< After scan Adv, we can get advertising's data*/
    UCHAR_T                 data_len;                   /**< advertising data length */
} TAL_BLE_ADV_REPORT_T;
  • peer_addr: The source address of advertising data.

  • adv_type: The type of advertising data.

  • rssi: The signal strength.

  • p_data: The pointer to the data.

  • data_len: The length of the data.

TAL_BLE_EVT_PARAMS_T

typedef struct {
    TAL_BLE_EVT_TYPE_E              type;

    union {
        UCHAR_T                     init;               /**< Show init states */
        TAL_BLE_CONNECT_EVT_T       connect;            /**< Receive connect callback, This value can be used with TAL_BLE_EVT_PERIPHERAL_CONNECT and TAL_BLE_EVT_CENTRAL_CONNECT_DISCOVERY */
        TAL_BLE_DISCONNECT_EVT_T    disconnect;         /**< Receive disconnect callback */
        TAL_BLE_ADV_REPORT_T        adv_report;         /**< Receive Adv and Respond report */
        TAL_BLE_CONN_PARAM_EVT_T    conn_param;         /**< We will update connect parameters. This value can be used with TAL_BLE_EVT_CONN_PARAM_REQ and TAL_BLE_EVT_CONN_PARAM_UPDATE */
        TAL_BLE_EXCHANGE_MTU_EVT_T  exchange_mtu;       /**< This value can be used with TAL_BLE_EVT_MTU_REQUEST and TAL_BLE_EVT_MTU_RSP */
        TAL_BLE_CONN_RSSI_EVT_T     link_rssi;          /**< Peer device RSSI value */
        TAL_BLE_NOTIFY_TX_EVT_T     notify_result;      /**< [Bluetooth LE Peripheral] This value can be used with TAL_BLE_EVT_NOTIFY_TX after Bluetooth LE Peripheral send a notification to peer. */
        TAL_BLE_DATA_REPORT_T       write_report;       /**< This value can be used with TAL_BLE_EVT_WRITE_REQ */
        TAL_BLE_DATA_REPORT_T       data_report;        /**< This value can be used with TAL_BLE_EVT_NOTIFY_RX */
        TAL_BLE_DATA_REPORT_T       data_read;          /**< After we do read attr in central mode, we will get the callback from Bluetooth Kernel */
    }ble_event;
} TAL_BLE_EVT_PARAMS_T;
  • type: The type of Bluetooth LE event.

  • connect: The parameter of connect event.

  • disconnect: The parameter of disconnect event.

  • adv_report: The advertising data.

  • write_report: The parameter of write event.

API description

Register Bluetooth LE data callback

typedef VOID(*TAL_BLE_EVT_FUNC_CB)(TAL_BLE_EVT_PARAMS_T *p_event);
OPERATE_RET tal_mesh_ble_recv_cb_init(TAL_BLE_EVT_FUNC_CB ble_event);

In the Bluetooth LE data callback, you can receive Bluetooth LE connection, disconnection, advertising, and GATT (Tuya-specific Bluetooth LE service) data. See the demo for the callback implementation.

Enable scan

OPERATE_RET tkl_ble_gap_scan_start(TKL_BLE_GAP_SCAN_PARAMS_T CONST *p_scan_params);

Bluetooth LE scan is enabled by default with the preset parameter. Therefore, the scan parameter specified with this API will be ignored.

Disable scan

OPERATE_RET tkl_ble_gap_scan_stop(VOID);

How-to

The following example shows the Bluetooth LE data reception function and initialization process.

VOID app_ble_data_recv(TAL_BLE_EVT_PARAMS_T *p_event)
{
    switch (p_event->type) {
        case TAL_BLE_EVT_ADV_REPORT:
            // adv_data_process(p_event);
            tal_rssi_test_ble_adv_recv(p_event->ble_event.adv_report.p_data, p_event->ble_event.adv_report.data_len,
                                            p_event->ble_event.adv_report.peer_addr.addr, p_event->ble_event.adv_report.rssi);
        break;
        default:
        break;
    }
}
OPERATE_RET tuya_init_third(VOID_T)
{
    // other init process

    tal_mesh_ble_recv_cb_init(app_ble_data_recv); // callback register
    tkl_ble_gap_scan_start(NULL); // start ble scan
    return OPRT_OK;
}

In the demo, the advertising scan data will be passed into the RSSI testing component API for an RSSI test with Tuya’s production tool. If you do not use Tuya’s production tool, you can delete the call to the RSSI testing API.

Things to note

Enable Bluetooth LE scan

Advertising scan is enabled by default. Bluetooth mesh packets are a type of Bluetooth LE advertising. Therefore, Bluetooth mesh devices will filter the data in the scan reception callback to receive and process only the mesh data. After Bluetooth LE advertising reception is enabled, the SDK can send Bluetooth LE advertising data to the application via the registered callback.

Scan filter for TLSR825x

The filter rule for scan data varies by chip platform. Devices built with TLSR825x will filter out all connectable advertising packets by default. To receive non-connectable advertising packets, there is no need to call the scan enablement API. However, this API is required to receive connectable advertising packets. If you are well-versed with the TLSR825x vendor SDK, you can alter the filter rule to filter out unnecessary data to optimize the message queue resource.