1. Bluetooth Mesh (SIG)

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

Contains MeshId creation, destruction

1.3.1. Create SIG Mesh

MeshId is created by the cloud. Only one Mesh can be created in a family.

Declaration

void createSigMesh(ITuyaResultCallback<SigMeshBean> callback);

Example

TuyaHomeSdk.newHomeInstance("homeIdxxxx").createSigMesh(new ITuyaResultCallback<SigMeshBean>() {
    @Override
    public void onError(String errorCode, String errorMsg) {
    }

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

1.3.2. Delete SIG Mesh

Delete SigMesh MeshId

Declaration

void removeMesh(IResultCallback callback);

Example

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

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

1.3.3. Get the List of SIG Mesh in the Family

Declaration

List<SigMeshBean> getSigMeshList();

Example

ITuyaHome mTuyaHome = TuyaHomeSdk.newHomeInstance("homeIdxxxx");
if (mTuyaHome.getHomeBean() != null){
    List<SigMeshBean> meshList = TuyaHomeSdk.getSigMeshInstance().getSigMeshList()
}

1.3.4. Get Sub-device Data

Declaration

 List<DeviceBean> getMeshSubDevList();

Example

List<DeviceBean> meshSubDevList = TuyaHomeSdk.newSigMeshDeviceInstance("meshIdxxxxx").getMeshSubDevList();

1.3.5. SIG Mesh Instance

It is recommended to destroy the current mesh and re-initialize the mesh when the family switches.

Declaration

//init mesh
void initMesh(String meshId);

Example

//init mesh
TuyaHomeSdk.getTuyaSigMeshClient().initMesh("meshIdxxxxx");

1.3.6. SIG Mesh Destory

Declaration

void destroyMesh();

Example

TuyaHomeSdk.getTuyaSigMeshClient().destroyMesh();

1.3.7. SIG Mesh Sub-device Connect and disconnect

ITuyaBlueMeshClient provides connect, disconnect, scan, stop scan.

Example

// start connect
TuyaHomeSdk.getTuyaSigMeshClient().startClient(mSigMeshBean);
// start connect with timeout
TuyaHomeSdk.getTuyaSigMeshClient().startClient(mSigMeshBean,searchTime);

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

//start scan
TuyaHomeSdk.getTuyaSigMeshClient().startSearch()

//stop scan
TuyaHomeSdk.getTuyaSigMeshClient().stopSearch();

Precautions

  • startClient(mSigMeshBean) After calling, it will continuously scan the surrounding connectable devices in the background until the connection is successful.
  • Scanning in the background always consumes resources. You can control background scanning by starting and stopping
  • if not call startClient() , startSearch() and stopSearch() is not working
  • When connected to the Mesh network, calling startSearch and stopSearch is not working.

1.4. Activation

1.4.1. Scaning SIG Mesh Devices

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() {

    }
};
// SigMesh's UUID
UUID[] MESH_PROVISIONING_UUID = {UUID.fromString("00001827-0000-1000-8000-00805f9b34fb")};
SearchBuilder searchBuilder = new SearchBuilder()
                                .setServiceUUIDs(MESH_PROVISIONING_UUID)
                .setTimeOut(100)      
                .setTuyaBlueMeshSearchListener(iTuyaBlueMeshSearchListener).build();

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

//start
mMeshSearch.startSearch();

//stop
mMeshSearch.stopSearch();

1.4.2. Active SIG Mesh Device by Bluetooth

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

Declaration


void startActivator();

void stopActivator();

Example

TuyaSigMeshActivatorBuilder tuyaSigMeshActivatorBuilder = new TuyaSigMeshActivatorBuilder()
            .setSearchDeviceBeans(mSearchDeviceBeanList)
            .setSigMeshBean(sigMeshBean) 
            .setTimeOut(timeout) 
            .setTuyaBlueMeshActivatorListener(new ITuyaBlueMeshActivatorListener() {
     @Override
     public void onSuccess(String mac, DeviceBean deviceBean) {
     }
     @Override
     public void onError(String mac, String errorCode, String errorMsg) {
     }
     @Override
     public void onFinish() {
     });

ITuyaBlueMeshActivator iTuyaBlueMeshActivator = TuyaHomeSdk.getTuyaBlueMeshConfig().newSigActivator(tuyaSigMeshActivatorBuilder);

//start
iTuyaBlueMeshActivator.startActivator();
//stop
iTuyaBlueMeshActivator.stopActivator();

Parameters

field describe
mSearchDeviceBeanList List of devices to be activated
timeout Activation timeout,default 100s.
sigMeshBean SigMeshBean

Data Model

DeviceBean See DeviceBean Data Model

errorCode See Error Code

1.4.3. Active SIG Mesh Gateway Device

See ZigBee Sub-devices Activation

1.4.4. Active SIG Mesh Device by Gateway

SigMesh gateway essentially dual-mode device

  1. Activation as a Wi-Fi device. See Wifi EZ Activation
  2. Activation as a BLE device. See Dual-mode Activation

1.4.5. Error Code

Code MSG
21002 invite failed
21004 provision failed
21006 send public key failed
21008 conform failed
210010 random conform failed
210014 send data failed
210016 composition data failed
210018 add appkey failed
210020 bind model failed
210022 publication model failed
210024 network transmit failed
210026 Cloud registration failed
210027 Device address allocation is full
210034 notify failed
20021 timeout

1.5. Device

ITuyaBlueMeshDevice provides all operations for Mesh devices.

ITuyaBlueMeshDevice  mTuyaBlueMeshDevice = TuyaHomeSdk.newSigMeshDeviceInstance(meshId);

1.5.1. Get Device Instance

Example

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

if(deviceBean.isSigMesh()){
    L.d(TAG, "This device is sigmesh device");
 }

if(deviceBean.isSigMeshWifi()){
    L.d(TAG, "This device is sigmesh wifi device");
 }

1.5.2. Local Connection and Gateway Connection

Example

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

//online status (Including local online and gateway online)
boolean online=deviceBean.getIsOnline()
//local online
boolean localOnline=deviceBean.getIsLocalOnline()
//gateway online
boolean wifiOnline=deviceBean.isCloudOnline() && gwBean.getIsOnline()

1.5.3. SIG Mesh Device And Gateway Judgment Method

Example

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

if(deviceBean.isSigMesh()){
    L.d(TAG, "This device is sigmesh device");
}

if(deviceBean.isSigMeshWifi()){
    L.d(TAG, "This device is sigmesh wifi device");
}

1.5.4. SIG Mesh Sub-devices Rename

Declaration

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

Parameters

param describe
devId Device Id
name New name
callback Callback

Example

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

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

1.5.5. Get 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, final String nodeId, final IResultCallback callback);

Parameters

param describe
pcc Device type
nodeId Device nodeId
callback Callback

Example

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

1.5.6. Remove Device

Declaration

void removeMeshSubDev(String devId, IResultCallback callback);

Parameters

param 说明
devId Device Id
pcc Device type
callback Callback

Example

mTuyaBlueMesh.removeMeshSubDev(devBean.getDevId(),devBean.getCategory(), 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. Add Group

16128 groups can be created in a mesh network. The id range when returning is 0xC000-0xFEFF . It is maintained locally.

Declaration

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

Parameters

param describe
name group name
pcc device type
localId LocalId (0xC000 - 0xFFFF)
callback Callback

Example

ITuyaBlueMeshDevice mTuyaSigMeshDevice= TuyaHomeSdk.newSigMeshDeviceInstance("meshId");

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

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

1.6.3. Add Device to Group

Declaration

void addDevice(String devId,IResultCallback callback);

Parameters

param describe
devId device Id
callback callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newSigMeshGroupInstance(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

param describe
devId Device Id
callback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newSigMeshGroupInstance(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

param describe
callback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newSigMeshGroupInstance(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

param describe
groupName new name
callback Callback

Example

ITuyaGroup mGroup = TuyaHomeSdk.newSigMeshGroupInstance(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 dps
callback Callback

Example

String dps = {"1":false};
ITuyaBlueMeshDevice mTuyaSigMeshDevice=TuyaHomeSdk.newSigMeshDeviceInstance("meshId");
mTuyaSigMeshDevice.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

Declaration

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

Parameters

param describe
localId Group No.
pcc Device type
dps dps
callback Callback

Example

String dps = {"1":false};
ITuyaBlueMeshDevice mTuyaSigMeshDevice= TuyaHomeSdk.newSigMeshDeviceInstance("meshId");
mTuyaSigMeshDevice.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

mTuyaSigMeshDevice.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.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. Mesh 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 ""