1. Bluetooth Low Energy

Tuya Bluetooth has 3 technology lines.

  • SingleBLE: Bluetooth single point device, one-to-one connection between Bluetooth device and phone;
  • TuyaMesh: Mesh released by Tuya.
  • SigMesh: Mesh released by SIG (Bluetooth Special Interest Group). In addition to the above three, there are some multi-protocol devices, such as Dual-mode Device with Wi-Fi and BLE capabilities. You can use both BLE and Wi-Fi capabilities.
Category Product Examples
SingleBLE Body fat scale, bracelet, electric toothbrush, door lock, etc.
SigMesh Light bulb, socket, sensor,etc.
TuyaMesh Light bulb, socket, sensor,etc.
Dual-mode Device Sigmesh gateways, IPC devices, and new multi-protocol Wi-Fi devices are all possible

The activation of dual-mode devices uses SingleBLE technology, which will be described in the SingleBLE chapter.

The Bluetooth part has the following functions:

1.Activate Device

  • Scan BLE devices
  • Device activation

2.Device Function

  • Check device connection status
  • Connect To BLE Device
  • Device Function
  • Unbind Device

3.Upgrade Firmware

  • Check device new version
  • OTA(Upgrade device firmware)

1.1. Prepare

Mobile System Requirements

TuyaHomesdk has been supported since Android 4.4. API>=19

Manifest Permissions

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Permissions Check

  • Before the APP uses the Bluetooth connection or scanning operation, it needs to check whether the APP positioning permission is allowed.
  • Check if the Bluetooth status is on.

1.2. Scan Device

Before developing BLE, you need to understand the basic logic of TuyaHomeSDK. You must already use TuyaHomeSdk to complete the basic operations such as initialization, login, and home creation.

1.2.1. Start Scanning

Unactivated Bluetooth devices will send Bluetooth broadcast packets to the surroundings, and the SDK will analyze the broadcast packets according to the protocol to find the surrounding Tuya Bluetooth single-point devices and dual-mode devices.

Remind again: permissions check is required before scanning, Only permissions allowable can work 1、The Phone Bluetooth is on 2、APP has positioning permission

Declaration

void startLeScan(int timeout, ScanType type, TyBleScanResponse response);

Parameters

param description
timeout Activation timeout, unit ms, 60s recommended
type ScanType.SINGLE
response calback,can not be empty

Example

TuyaHomeSdk.getBleOperator().startLeScan(60000, ScanType.SINGLE, new    TyBleScanResponse() {
            @Override
            public void onResult(ScanDeviceBean bean) {

            }
        });

Callback Description

ScanDeviceBean description

Attributes description
id Scan ID usually consists of uuid, which can uniquely distinguish the device
name Bluetooth name of the device, usually blank
providerName SingleBleProvider
data raw data
configType config_type_single:single Device;config_type_wifi:Dual-mode device
productId product id
uuid product uuid,Device unique code
mac device mac
isbind binding status
flag bit0:Whether to support 5G Wi-Fi

1.2.2. Query Device Name

After scanning to the target device, you need to query to display the name and ICON of the product configuration.

Declaration

void getActivatorDeviceInfo(String productId, String uuid, String mac, ITuyaDataCallback<ConfigProductInfoBean> callback);

Parameters

param Type description
productId String ScanDeviceBean.productId
uuid String ScanDeviceBean.uuid
mac String ScanDeviceBean.mac

Example

TuyaHomeSdk.getActivatorInstance().getConfigProductInfo(scanDeviceBean.productId, scanDeviceBean.uuid, scanDeviceBean.mac, new ITuyaDataCallback<ConfigProductInfoBean>() {
            @Override
            public void onSuccess(ConfigProductInfoBean result) {

            }

            @Override
            public void onError(String errorCode, String errorMessage) {

            }
        });

Example

ConfigProductInfoBean description

Attributes Type description
name String The product's name
icon String The product's icon

1.2.3. Stop Scanning

Stop scanning devices, such as exiting the activation page

Declaration

void stopLeScan();

Example

TuyaHomeSdk.getBleOperator().stopLeScan();

1.3. BLE Device Activation

1.3.1. Start Activation

Scanned devices configType = config_type_single represents a single BLE device. config_type_wifi is a dual-mode device.

Declaration

void startBleConfig(long homeId, String devUuid, Map<String, Object> params, ITuyaBleConfigListener configListener);

Parameters

param description
homeId Current home's Id
devUuid ScanDeviceBean.uuid
params null,not need by SingleBLE
configListener callback

Example

TuyaHomeSdk.getBleManager().startBleConfig(123456, "abc12345678", null, new ITuyaBleConfigListener() {
            @Override
            public void onSuccess(DeviceBean bean) {

            }

            @Override
            public void onFail(String code, String msg, Object handle) {
               // code msg ,see #Error Code
            }
        });

1.3.2. Cancel Activation

Stop it during Activation.

Declaration

void stopBleConfig(String devUuid);

Parameters

param description
devUuid ScanDeviceBean.uuid

Example

TuyaHomeSdk.getBleManager().stopBleConfig("abc12345678");

1.4. Dual-mode Device Activation

1.4.1. Start Dual-mode Device Activation

Scanned devices configType = config_type_single represents a single BLE device. config_type_wifi is a dual-mode device.

Declaration

void startBleConfig(long homeId, String devUuid, Map<String, Object> params, ITuyaBleConfigListener configListener);

Parameters

param description
homeId Current home's Id
devUuid ScanDeviceBean.uuid
params Wi-Fi Network information: ssid, pwd, token
configListener callback

If not specified, The Device only support 2.4G Wi-Fi network, and some devices support 5G. Can be determined based on ScanDeviceBean.flag. token: The way to obtain the token is the same as the Wi-Fi device. Get Token

Example

 Map<String, Object> param = new HashMap<>();
  param.put("ssid", "Tuya-Wi-Fi-2.4G"); //Wi-Fi ssid
  param.put("password", "12345678"); //Wi-Fi pwd
  param.put("token", "xxxxxxxxxxxxx"); // user token
TuyaHomeSdk.getBleManager().startBleConfig(123456, "xyz12345678", param, new ITuyaBleConfigListener() {
            @Override
            public void onSuccess(DeviceBean bean) {
            }

            @Override
            public void onFail(String code, String msg, Object handle) {
            // code msg ,see #ErrorCode
            }
        });

Callback Description

DeviceBean Description See DeviceBean Data Model

1.4.2. Cancel Dual-mode Device Activation

Stop it during Activation.

Declaration

void stopBleConfig(String devUuid);

Parameters

param description
devUuid ScanDeviceBean.uuid

Example

TuyaHomeSdk.getBleManager().stopBleConfig("xyz12345678");

1.5. BLE Device Function

Summary

  1. Device status query
  2. Device connection
  3. Device sends instructions
  4. Modify the device
  5. Unbind the device

1.5.1. Device Status Query

  • The general query is the same as the Wi-Fi device (this status is a comprehensive status, if the Bluetooth device is added under the gateway, the Bluetooth device can be online in the cloud)
    TuyaHomeSdk.getDataInstance().getDeviceBean(devId).getIsOnline();
    
  • Query if the device is locally connected
    TuyaHomeSdk.getBleManager().isBleLocalOnline(devId);
    

    Usually only the local state needs to be considered. Only Bluetooth devices that join the gateway need to consider whether the cloud is online.

1.5.2. Connect Device

If the device is offline, you can call the connection method to connect the device.

【Tips】The api needs to be called in the main thread

Declaration

void addScanLinkTaskIds(String idJsonString);

Parameters

param type description
idJsonString String devId List to json

Example

 List<String> devIdList = new ArrayList<>();
 devIdList.add(devId1); //devId1
 devIdList.add(devId2); //devId2

 String ids = JSONObject.toJSONString(devIdList);
 TuyaHomeSdk.getBleManager().addScanLinkTaskIds(ids);

1.5.3. Disconnect Device

Can actively disconnect connected devices.

【Tips】The api needs to be called in the main thread

Declaration

void disconnectLinkedIds(String idJsonString);

Parameters

param type description
idJsonString String devId List to json

Example

 List<String> devIdList = new ArrayList<>();
 devIdList.add(devId1); //devId1
 devIdList.add(devId2); //devId2

 String ids = JSONObject.toJSONString(devIdList);
 TuyaHomeSdk.getBleManager().disconnectLinkedIds(ids);

1.5.4. BLE Device Function

Can monitor device status. This section is similar to Wi-Fi devices, please refer to it for more information Wi-Fi Function

Device Information Acquisition

Consistent with Wi-Fi device Device Information Acquisition

Initial Device Control

Consistent with Wi-Fi device Initial Device Control

Register Device Listener

Consistent with Wi-Fi device Register Device Listener

Unregister Device Listener

Consistent with Wi-Fi device Unregister Device Listener

Function Points of Device

Consistent with Wi-Fi device Function Points of Device

Device Control Method

Unlike Wi-Fi devices, it does not have the LAN delivery function. Only some delivery functions are supported.

Declaration

void publishDps(String dps, IResultCallback callback);

Parameters

param type description
dps String data points
callback IResultCallback callback

Example

Assuming that the device function point of the light is 101, the control code of the light is as follows:

mDevice.publishDps("{\"101\": true}", new IResultCallback() {
    @Override
    public void onError(String code, String error) {
    }

    @Override
    public void onSuccess() {
    }
});
}

Rename Device

Consistent with Wi-Fi device Rename Device

Remove Device

Consistent with Wi-Fi device Remove Device

The unbinding of Bluetooth devices is consistent with the Wi-Fi device. TuyaHomeSdk.newDeviceInstance("devId").removeDevice() or resetFactory() But in addition to BLE cloud removal. If the device is locally connected at this time, the device will be automatically removed. If the device is offline at this time, only the cloud will be removed.

Recycling Device Resources

Consistent with Wi-Fi device Recycling Device Resources

1.6. BLE OTA

OTA process

  • Checking firmware information
  • Upgrade OTA

1.6.1. Checking Firmware Information

Declaration

void requestUpgradeInfo(String devId, IRequestUpgradeInfoCallback callback);

Parameters

Parameters type description
devId String devId
callback IRequestUpgradeInfoCallback callback

Example

TuyaHomeSdk.getMeshInstance().requestUpgradeInfo(mDevID, new IRequestUpgradeInfoCallback() {
    @Override
    public void onSuccess(ArrayList<BLEUpgradeBean> bleUpgradeBeans) {

    }

    @Override
    public void onError(String errorCode, String errorMsg) {
    }
});

BLEUpgradeBean data model

Parameters type description
upgradeStatus int 0: No new version 1: New version available 2: In upgrade
version String Latest version
currentVersion String Current version
timeout int timeout, Unit s
upgradeType int 0:Reminds to upgrade 2: Forced upgrade 3: Detect upgrade
type int 0: Wi-Fi 1: BLE 2: GPRS 3: Zigbee 9: MCU
typeDesc String Upgrade description
lastUpgradeTime long Last upgrade time
url String New firmware download url, type =1 or 9 has value
fileSize long New firmware size
md5 String New firmware MD5 value

1.6.2. OTA Upgrade

You need to download the new firmware to the local, in the previous step, and then perform OTA upgrade.

Declaration

void startBleOta(String uuid, int type, String version, String binPackagePath, OnBleUpgradeListener listener);

Parameters

Parameters type description
uuid String deviceBean.getUuid()
type int 0: means BLEUpgradeBean.type = 1; 1: meansBLEUpgradeBean.type = 9
version String New firmware version
binPackagePath String Local firmware path
listener OnBleUpgradeListener callback
TuyaHomeSdk.getBleManager().startBleOta(uuid, type, version, binPackagePath, new  OnBleUpgradeListener() {
        @Override
        public void onUpgrade(int percent) {

        }

        @Override
        public void onSuccess() {

        }

        @Override
        public void onFail(String errorCode, String errorMsg) {

        }
    });

1.7. Error Code

code description
1 Format of the packet received by the device is incorrect
2 The device cannot find the router
3 Wi-Fi password error
4 Device cannot connect to router
5 Device DHCP failed
6 The device fails to connect to the cloud
100 User cancels activation
101 Bluetooth connection error
102 Bluetooth service error found
103 Failed to open Bluetooth communication channel
104 Bluetooth failed to get device information
105 Bluetooth pairing failed
106 Activation timeout
107 Wi-Fi information transmission failed
108 Token is invalid
109 Failed to get Bluetooth encryption key
110 Device does not exist
111 Device cloud registration failed
112 Device cloud activation failed
113 Cloud device has been bound
114 Active disconnect
115 Failed to get device information in the cloud
116 The device is being networked by other methods at this time
117 OTA upgrade failed
118 OTA upgrade timeout
119 Wi-Fi parameter verification failed

results matching ""

    No results matching ""