Last Updated on : 2024-03-01 07:48:12download
This topic describes how Wi-Fi and Bluetooth combo devices are controlled through physical Bluetooth remotes.
Prerequisites for enabling Bluetooth scan
Before device initialization, the application has registered the callback for handling Bluetooth remote data.
The framework turns on Bluetooth scan after device initialization.
The device is either not in pairing mode or has already obtained the Wi-Fi SSID and password during the pairing process.
Enabling Bluetooth scan can compromise the success rate of pairing a combo device.
Timing to turn on the binding window
The Bluetooth scan is on.
When an unpaired and inactivated device is powered on and enters low power mode, the framework will automatically turn on the binding window.
After the device obtains Wi-Fi SSID and password in pairing mode, the framework will automatically turn on the binding window.
After an activated device is powered on, the framework will automatically turn on the binding window during device initialization.
The application calls the binding window API to turn it on.
Open tuya_iot_config.h
and check if the following macro is defined.
#define ENABLE_BT_REMOTE_CTRL 1
tuya_bt.h
tuya_ble_reg_app_scan_adv_cb
for receiving data from the Tuya-enabled Bluetooth remote.tuya_ble_reg_raw_scan_adv_cb
for receiving custom data.The framework will automatically turn on the binding window at the specified time mentioned above. The application can call the API to turn on the binding window when needed.
Register the callback before device initialization. Only after this callback is registered, the Bluetooth scan can be turned on. This API serves as the toggle for the Bluetooth scan feature.
This callback returns the command received from the Tuya-enabled Bluetooth remote.
//Callback function for advertisement scanning data processing
//type: 0xFF old remote
// 0x16 new remote
typedef VOID (*TUYA_BLE_APP_SCAN_HANDLE)(UCHAR_T *data, UCHAR_T len, UCHAR_T type, UCHAR_T* mac);
/**
* @brief Register callback function for advertisement scanning data processing
*
* @param[in] cb: callback function
*
* @note This API is used for registering callback function for advertisement scanning data processing
*
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tuya_ble_reg_app_scan_adv_cb(TUYA_BLE_APP_SCAN_HANDLE cb);
This callback notifies the result of binding or unbinding a Tuya-enabled Bluetooth remote.
//Callback function for validity check for bind or unbind operation
typedef OPERATE_RET(*BLE_SCAN_ADV_BIND_CHECK_CB)(TUYA_BLE_BIND_TYPE type, UCHAR_T *data, UCHAR_T len);
//Callback function for result notify for bind or unbind operation
typedef VOID_T(*BLE_SCAN_ADV_BIND_RSLT_NOTIFY_CB)(TUYA_BLE_BIND_TYPE type, int rslt);
typedef struct {
BLE_SCAN_ADV_BIND_CHECK_CB bind_check;
BLE_SCAN_ADV_BIND_RSLT_NOTIFY_CB bind_notify;
} TUYA_BLE_SCAN_ADV_HANDLE_CBS;
/**
* @brief Register callback function for advertisement scanning data processing
*
* @param[in] cbs: callback function
*
* @note This API is used for registering callback function for inform binding/unbinding state
*
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tuya_ble_reg_app_scan_adv_handle_cbs(TUYA_BLE_SCAN_ADV_HANDLE_CBS* cbs);
If the duration is less than 30 seconds, it will be treated as 30 seconds. If you set the duration to 0, the device will stay in binding mode forever.
/**
* @brief Set timeout window for binding remote controller
*
* @param[in] time_out: time out in second, 0: no window
*
* @note This API is used for setting timeout window for binding remote controller
*
* @return VOID
*/
VOID_T tuya_ble_set_bind_window(UINT_T time_out);
The framework will automatically turn on the binding window at the specified time mentioned above. The application can call this API to turn on the binding window when needed.
/**
* @brief Open window for binding remote controller
*
* @param[in] VOID
*
* @note This API is used for opening window for binding remote controller
*
* @return VOID
*/
VOID_T tuya_ble_open_bind_window(VOID_T);
If your product only supports custom remotes, besides the callback for receiving custom data, you must also register the callback for receiving data from Tuya-enabled Bluetooth remotes.
The framework will pass the advertising data it receives to the application using a callback. See the previous section for the prerequisites to enable the Bluetooth scan.
/**
* @brief Register callback function for advertisement scanning raw data processing
*
* @param[in] cb: callback function
*
* @note This API is used for registering callback function for advertisement scanning raw data processing
*
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tuya_ble_reg_raw_scan_adv_cb(TUYA_BLE_RAW_SCAN_HANDLE cb);
service_ble_remote
in TuyaOS example collection contains the complete example code.
Review the prerequisites to enable the Bluetooth scan.
ADV_IND
, SCAN_RSP
, and NONCONN_IND
advertising packets are supported.Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback