1. Bluetooth Mesh (TUYA)

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

Dual-mode's Bluetooth distribution network part, Use SingleBLE technology to network the device, Will be explained in the SingleBLE chapter.

Tips

Please familiar with TuyaHomeSdk first and develop Bluetooth Mesh. All operations of Mesh are based on the initialization of family data. A family can has multiple Meshs (recommend one family create one).

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. Basic Notion

Basic Notion Description
pcc Each mesh device corresponds to a product, and each product has its own size class label. The SDK uses pcc andtype as the size class label.
mesh node Id The node id is used to distinguish the "unique identifier" of each mesh device in the mesh network. For example, if you want to control a device, you can send the nodeId command corresponding to this device to the mesh network
mesh group local Id The local Id is used to distinguish the unique identifier of each mesh group in the mesh network.For example, if you want to control the devices in a group, you can send the localId command corresponding to this group to the mesh network.
Local Connection The networked device is connected via Bluetooth to control mesh and command operations
Gateway Connection The networked devices are connected through the gateway (the gateway needs to be with the device, and the distance cannot be too far) to control the mesh and command operations.

1.3. Management

1.3.1. Create Mesh

Declaration

void createBlueMesh(String meshName, ITuyaResultCallback<BlueMeshBean> callback);

Parameters

Field Type Describe
meshName String mesh's name(limit 16)
callback ITuyaResultCallback callback

Example

TuyaHomeSdk.newHomeInstance("homeId").createBlueMesh("meshName", new ITuyaResultCallback<BlueMeshBean>() {
    @Override
    public void onError(String errorCode, String errorMsg) {
    }

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

1.3.2. Delete Mesh

Declaration

void removeMesh(IResultCallback callback);

Parameters

Field Type Describe
callback IResultCallback callback

Example

TuyaHomeSdk.newBlueMeshDeviceInstance(meshId).removeMesh(new IResultCallback() {
    @Override
    public void onError(String errorCode, String errorMsg) {
    }

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

1.3.3. Get The List Of Mesh In The Family

Declaration

List<BlueMeshBean> getMeshList();

Example

ITuyaHome mTuyaHome = TuyaHomeSdk.newHomeInstance("homeId");
if (mTuyaHome.getHomeBean() != null){
    List<BlueMeshBean> meshList = mTuyaHome.getHomeBean().getMeshList();
    BlueMeshBean meshBean= meshList.get(0);
}

1.3.4. Get Data of Mesh's Sub-devices

Declaration

List<DeviceBean> getMeshSubDevList();

Example

List<DeviceBean> meshSubDevList = TuyaHomeSdk.newBlueMeshDeviceInstance("meshId").getMeshSubDevList();

1.3.5. Mesh Instance and Destroy

recommend to destroy the Mesh and re-initialize the Mesh in the family when the family switches.

Example

//init
TuyaHomeSdk.getTuyaBlueMeshClient().initMesh(String meshId);       

//destroy
TuyaHomeSdk.getTuyaBlueMeshClient().destroyMesh();

1.3.6. Mesh Sub-devices's Connect and Disconnect

ITuyaBlueMeshClient has start connecting, disconnect, start scanning, stop scanning.

Example

//start connecting
TuyaHomeSdk.getTuyaBlueMeshClient().startClient(mBlueMeshBean);

//disconnect
TuyaHomeSdk.getTuyaBlueMeshClient().stopClient();

//start scanning
TuyaHomeSdk.getTuyaBlueMeshClient().startSearch()

//stop scanning
TuyaHomeSdk.getTuyaBlueMeshClient().stopSearch();

Precautions

  • Start connecting and will be scanning the connectable devices in the background until the connection is successful.
  • Scanning always consumes resources, can control scanning by turning on scanning and stopping scanning.
  • Use startSearch() or stopSearch() must be after calling startClient().
  • startSearch() or stopSearch() will be not working when connected to the Mesh.

1.4. Activation

1.4.1. Device Reset

Product Type Reset Operate Phenomenon
Light Switch three times in a row Flashing lights
Socket Press and hold the switch for 3s The socket indicator flashes quickly
Gateway Press and hold the switch for 3s Red and blue lights flash fast
Low-power devices Press and hold the switch for 3s Press again to display a long light to configure the network, and the network configuration must be completed during the light is on
Alarm Press and hold the switch for 3s Flashing lights

The device in reset state has a default name is out_of_mesh and a default password is 123456.

1.4.2. Scaning Mesh Device

Keep bluetooth ON and check location permissions before scanning.

Declaration

//start scanning
void startSearch();
//stop scanning
void stopSearch();

Example

ITuyaBlueMeshSearchListener iTuyaBlueMeshSearchListener=new ITuyaBlueMeshSearchListener() {
    @Override
    public void onSearched(SearchDeviceBean deviceBean) {
    }

    @Override
    public void onSearchFinish() {
    }
};

SearchBuilder searchBuilder = new SearchBuilder()
         .setMeshName("out_of_mesh")        
     .setTimeOut(100)        
     .setTuyaBlueMeshSearchListener(iTuyaBlueMeshSearchListener).build();

ITuyaBlueMeshSearch mMeshSearch = TuyaHomeSdk.getTuyaBlueMeshConfig().newTuyaBlueMeshSearch(searchBuilder);

//start
mMeshSearch.startSearch();
//stop
mMeshSearch.stopSearch();

1.4.3. Active Mesh Device By Bluetooth

Two types of sub-devices Activation, App activation and Mesh gateway activation.

Declaration

void startActivator();

void stopActivator();

Parameters

field type describe
mSearchDeviceBeans List List of devices to be activated
timeout Int Activation timeout,default 100s.
ssid String After activation, Wi-Fi's ssid.
password String After activation, Wi-Fi's password.
mMeshBean MeshBean MeshBean
homeId Long HomeId
version String Devices activation is 1.0, gateway activation is 2.2.

Example

TuyaBlueMeshActivatorBuilder tuyaBlueMeshActivatorBuilder = new TuyaBlueMeshActivatorBuilder()
                .setSearchDeviceBeans(foundDevices)
                .setVersion("1.0")
                .setBlueMeshBean(mMeshBean)
                .setTimeOut(timeOut)
                .setTuyaBlueMeshActivatorListener(new ITuyaBlueMeshActivatorListener() {
     @Override
     public void onSuccess(DeviceBean deviceBean) {
     }

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

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

ITuyaBlueMeshActivator iTuyaBlueMeshActivator = TuyaHomeSdk.getTuyaBlueMeshConfig().newActivator(tuyaBlueMeshActivatorBuilder);

iTuyaBlueMeshActivator.startActivator();

iTuyaBlueMeshActivator.stopActivator();

1.4.4. Active Mesh Gateway Device

Declaration

void startActivator();

void stopActivator();

Parameters

field type describe
mSearchDeviceBeans List List of devices to be configured
timeout Int Activation timeout,default 100s.
ssid String After activation, Wi-Fi's ssid.
password String After activation, Wi-Fi's password.
mMeshBean MeshBean MeshBean
homeId Long HomeId
version String Devices activation is 1.0, gateway activation is 2.2.

Example

TuyaBlueMeshActivatorBuilder tuyaBlueMeshActivatorBuilder = new TuyaBlueMeshActivatorBuilder()
                .setWifiSsid(mSsid)
                .setWifiPassword(mPassword)
                .setSearchDeviceBeans(foundDevices)
                .setVersion("2.2")
                .setBlueMeshBean(mMeshBean)
                .setHomeId("homeId")
                .setTuyaBlueMeshActivatorListener(new ITuyaBlueMeshActivatorListener() {

   @Override
   public void onSuccess(DeviceBean devBean) {
    }

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

   @Override
   public void onFinish() {  

   }});
ITuyaBlueMeshActivator iTuyaBlueMeshActivator = TuyaHomeSdk.getTuyaBlueMeshConfig().newWifiActivator(tuyaBlueMeshActivatorBuilder);

iTuyaBlueMeshActivator.startActivator();

iTuyaBlueMeshActivator.stopActivator();

1.4.5. Error Code

Code describe
13007 Device login failed
13004 Failed to reset device address
13005 Device address is full
13007 Ssid is empty
13011 Activation timeout

1.5. Device

1.5.1. Found Mesh Devices

Example

DeviceBean deviceBean=TuyaHomeSdk.getDataInstance().getDeviceBean(mDevId);

if(deviceBean.isBlueMesh()){
    L.d(TAG, "This device is blue mesh device");
 }

if(deviceBean.isBlueMeshWifi()){
    L.d(TAG, "This device is blue mesh wifi device");
 }

1.5.2. Rename Device

Declaration

void renameMeshSubDev(String devId, String name, IResultCallback callback);

Parameters

param type describe
devId String Device Id
name String New name
callback IResultCallback Callback

Example

mTuyaBlueMesh.renameMeshSubDev(devBean.getDevId(),"device name", new IResultCallback() {
            @Override
            public void onError(String code, String errorMsg) {
            }

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

1.5.3. Local Connection And Gateway Connection

Example

DeviceBean deviceBean=TuyaHomeSdk.getDataInstance().getDeviceBean(mDevId);

boolean online=deviceBean.getIsOnline()

boolean localOnline=deviceBean.getIsLocalOnline()

boolean wifiOnline=deviceBean.isCloudOnline() && gwBean.getIsOnline()

1.5.4. Remove Device

Declaration

void removeMeshSubDev(String devId, IResultCallback callback);

Parameters

param type describe
devId String Device Id
pcc String Device type
callback IResultCallback Callback

Example

mTuyaBlueMesh.removeMeshSubDev(devBean.getDevId(), new IResultCallback(){
  @Override
  public void onError(String code, String errorMsg) {
 }

 @Override
 public void onSuccess() {
  Toast.makeText(mContext, "sub-deivces remove success", Toast.LENGTH_LONG).show();
  }});

1.5.5. Query Mesh Device Status

Get dp data from cloud maybe not real-time data, can use it search real-time data and IMeshDevListener's onDpUpdate will be callback.

Declaration

void querySubDevStatusByLocal(String pcc, String nodeId, IResultCallback callback);

Parameters

param type describe
pcc String Device type
nodeId String Device nodeId
callback IResultCallback Callback

Example

mTuyaBlueMeshDevice.querySubDevStatusByLocal(devBean.getCategory(), devBean.getNodeId(), new IResultCallback() {
            @Override
            public void onError(String code, String errorMsg) {
            }

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

1.6. Group

ITuyaGroup provides operations for Mesh group.

1.6.1. Find Mesh Group

A Mesh group or a WiFi group can be distinguished by whether it has a MeshId.

Example

GroupBean groupBean=TuyaHomeSdk.getDataInstance().getGroupBean("groupId");
if(!TextUtils.isEmpty(groupBean.getMeshId())){    
    L.d(TAG, "This group is mesh group");
}

1.6.2. Create Group

Currently, each mesh supports a maximum of 28672 groups id in 8000 ~ EFFF (0x).

Declaration

void addGroup(String name, String pcc, String localId,IAddGroupCallback callback);

Parameters

field describe
name group name
pcc pcc
localId group address
callback callback

Example

mITuyaBlueMesh.addGroup("groupName","pcc", "8001", new IAddGroupCallback() {
   @Override
   public void onError(String errorCode, String errorMsg) {
   }   

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

1.6.3. Add Deivce to Group

Declaration

void addDevice(String devId,IResultCallback callback);

Parameters

field type describe
devId String Device Id
callback IResultCallback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newBlueMeshGroupInstance(groupId);
mGroup.addDevice("devId", new IResultCallback() {
            @Override
            public void onError(String code, String errorMsg) {
            }

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

1.6.4. Remove Device From Group

Declaration

void removeDevice(String devId,IResultCallback callback);

Parameters

field type describe
devId String Device Id
callback IResultCallback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newBlueMeshGroupInstance(groupId);
mGroup.removeDevice("devId", new IResultCallback() {
            @Override
            public void onError(String code, String errorMsg) {
            }

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

1.6.5. Disband Group

Declaration

void dismissGroup(IResultCallback callback);

Parameters

field type describe
callback IResultCallback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newBlueMeshGroupInstance(groupId);
mGroup.dismissGroup(new IResultCallback() {
            @Override
            public void onError(String code, String errorMsg) {
            }

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

1.6.6. Group Rename

Declaration

void renameGroup(String groupName,IResultCallback callback);

Parameters

field type describe
groupName String Rename
callback IResultCallback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newBlueMeshGroupInstance(groupId);
mGroup.renameGroup("group name",new IResultCallback() {
            @Override
            public void onError(String code, String errorMsg) {
            }

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

1.7. Control

ITuyaBlueMeshDevice provides operations for Mesh devices.

1.7.1. Control Command - Control Devices

Format:

{"(dpId)":"(dpValue)"}

Declaration

void publishDps(String nodeId, String pcc, String dps, IResultCallback callback);

Parameters

param describe
nodeId Sub-devices No.
pcc Devices type
dps Control command
callback Callback

Example

String dps = {"1":false};
ITuyaBlueMeshDevice mTuyaBlueMeshDevice=TuyaHomeSdk.newBlueMeshDeviceInstance("meshId");
mTuyaBlueMeshDevice.publishDps(devBean.getNodeId(), devBean.getCategory(), dps, new IResultCallback() {
            @Override
            public void onError(String s, String s1) {
            }

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

1.7.2. Control Command - Control Group

Format:

{"(dpId)":"(dpValue)"}

Declaration

void multicastDps(String localId, String pcc, String dps, IResultCallback callback)

Parameters

field describe
localId Group No.
pcc Device type
dps Control command
callback Callback

Example

String dps = {"1":false};
ITuyaBlueMeshDevice mTuyaBlueMeshDevice= TuyaHomeSdk.newBlueMeshDeviceInstance("meshId");
mTuyaBlueMeshDevice.multicastDps(groupBean.getLocalId(), devBean.getCategory(), dps, new IResultCallback() {
            @Override
            public void onError(String errorCode, String errorMsg) {
            }

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

1.7.3. Data Monitor

Mesh info( dp data、status change、device name、device remove)will real-time sync to IMeshDevListener

Example

mTuyaBlueMeshDevice.registerMeshDevListener(new IMeshDevListener() {

 @Override
 public void onDpUpdate(String nodeId, String dps,boolean isFromLocal) {
     DeviceBean deviceBean = mTuyaBlueMeshDevice.getMeshSubDevBeanByNodeId(nodeId);
 }

 @Override
 public void onStatusChanged(List<String> online, List<String> offline,String gwId) {
}

@Override
public void onNetworkStatusChanged(String devId, boolean status) {

}

@Override
public void onRawDataUpdate(byte[] bytes) {

}

@Override
public void onDevInfoUpdate(String devId) {
}

@Override
public void onRemoved(String devId) {
}});

1.7.4. Create Group

Mesh can create 28672 groups, id in [8000 ~ EFFF] (Hexadecimal) and locally maintained.

Declaration

void addGroup(String name, String pcc, String localId,IAddGroupCallback callback);

Parameters

field describe
name Group name
pcc Device size
localId Group's localId
callback Callback

Example

mITuyaBlueMesh.addGroup("group name","device size", "8001", new IAddGroupCallback() {
            @Override
            public void onError(String errorCode, String errorMsg) {
            }   

            @Override
            public void onSuccess(long groupId) {
            }

        });

1.8. Firmware Upgrade

There are two types of sub-device upgrades, one is ordinary sub-device upgrade, and the other is mesh gateway upgrade.

1.8.1. Query Firmware Info

Declaration

void requestUpgradeInfo(String devId, IRequestUpgradeInfoCallback callback);

Parameters

param 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

param 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.8.2. Sub-device Upgrade

Declaration

void startOta();

Example

private MeshUpgradeListener mListener = new MeshUpgradeListener() {
        @Override
        public void onUpgrade(int percent) {
        }

        @Override
        public void onSendSuccess() {
        }

        @Override
        public void onUpgradeSuccess() {
             mMeshOta.onDestroy();
        }

        @Override
        public void onFail(String errorCode, String errorMsg) {
             mMeshOta.onDestroy();
        }
    };
byte data[] = getFromFile(path);

TuyaBlueMeshOtaBuilder build = new TuyaBlueMeshOtaBuilder()
        .setData(data)
        .setMeshId(mDevBean.getMeshId())
        .setProductKey(mDevBean.getProductId())
        .setNodeId(mDevBean.getNodeId())
        .setDevId(mDevID)
        .setVersion("version")
        .setTuyaBlueMeshActivatorListener(mListener)
        .bulid();
ITuyaBlueMeshOta  = TuyaHomeSdk.newMeshOtaManagerInstance(build);

//start
mMeshOta.startOta();

Parameter

TuyaBlueMeshOtaBuilder

param type description
data byte[] Byte stream of the firmware to be upgraded
meshId String Device MeshId
productKey String Product Id
mNodeId String Device NodeId
devId String Device Id
version String Version

1.8.3. Sub-device Gateway Upgrade

Refer to the gateway upgrade, the following is the sample code

Example

private IOtaListener iOtaListener = new IOtaListener() {
        @Override
        public void onSuccess(int otaType) {
          }

        @Override
        public void onFailure(int otaType, String code, String error) {
        }

        @Override
        public void onProgress(int otaType, final int progress) {     
        }
    };

ITuyaOta iTuyaOta = TuyaHomeSdk.newOTAInstance(mDevID);
iTuyaOta.setOtaListener(mOtaListener);
//start
iTuyaOta.startOta();

//destroy
iTuyaOta.onDestroy();

results matching ""

    No results matching ""