1. Bluetooth Mesh(SIG)
1.1. Basic Notion
Bluetooth Special Interest Group use bluetooth technology began to fully support Mesh networks. Bluetooth Mesh, that is, a Bluetooth device is formed into a network. Each Bluetooth device can communicate through Bluetooth devices in the network, and transmit Bluetooth information at one end to the other end through the mesh network.
The standard Bluetooth Mesh, also called SIG Mesh, is a communication standard for the Mesh network proposed by the Bluetooth Technology Alliance. The use of Bluetooth Mesh for networking and function point updates must meet the standards of standard Bluetooth Mesh
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 | 2 bytes. 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 | 2 bytes. 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 |
The product currently follows the rules
Type | Device Type | Product Type | Sub Product Type |
---|---|---|---|
1=Tuya's standard device 2=Tuya's penetrate device |
0x01=light 0x02=switch 0x05=remote control re |
Like: 1= light 2= wc light 3= rgb light 4= |
ex. pcc(2 bytes,4 charac)
Tuya's standard device rgb light 1310 // 1 01 4 read, 1 means device type,01 means product type, 4 means sub product type
Tuya's standard device rgbwc light 1510
......
Equipment operation requires multiple steps
Because device operations, such as adding and deleting operations, and group operations, require the local Bluetooth command to be executed once and recorded in the cloud once. The operation information needs to be synchronized to the local mesh network as well.
1.2. Management
TuyaSmartBleMesh+SIGMesh.h
1.2.1. Create SIG Mesh
A family can have multiple sig meshes (it is recommended to create only one family). All operations in the sig mesh are based on the family data being initialized.
Init home See
Declaration
+ (void)createSIGMeshWithHomeId:(long long)homeId
success:(void(^)(TuyaSmartBleMeshModel *meshModel))success
failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
homeId | homeId |
success | success block |
failure | failure block |
Example
TuyaSmartHome *home = #< home instance >;
long long homeId = home.homeModel.homeId;
[TuyaSmartBleMesh createSIGMeshWithHomeId:homeId success:^(TuyaSmartBleMeshModel *meshModel) {
// success do...
} failure:^(NSError *error) {
NSLog(@"create mesh error: %@", error);
}];
1.2.2. Delete SIG Mesh
Declaration
Delete the mesh. If there are devices in the mesh group, the child devices are also removed.
- (void)removeMeshWithSuccess:(TYSuccessHandler)success failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
success | success block |
failure | failure block |
Example
self.mesh = #<TuyaSmartBleMesh instance>;
[self.mesh removeMeshWithSuccess:^{
// success do...
} failure:^(NSError *error) {
XCTFail(@"test remove mesh failure: %@", error);
}];
1.2.3. Get the List of SIG Mesh in the Family
Declaration
After initializing the home instance, you can get the mesh list of the corresponding family
- (void)getSIGMeshListWithSuccess:(void(^)(NSArray <TuyaSmartBleMeshModel *> *list))success
failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
success | success block |
failure | failure block |
Example
TuyaSmartHome *home = #<home instance>
[home getSIGMeshListWithSuccess:^(NSArray<TuyaSmartBleMeshModel *> *list) {
// success do
} failure:^(NSError *error) {
NSLog(@"get sig mesh list error: %@", error);
}];
1.2.4. Get SIG Mesh Instance
Declaration
+ (instancetype)bleMeshWithMeshId:(NSString *)meshId homeId:(long long)homeId;
Parameters
parameter | Description |
---|---|
meshId | mesh id |
homeId | home id |
Example
// The home instance (TuyaSmartHome instance) can get the sigMeshModel under the class
TuyaSmartBleMeshModel *sigMeshModel = [self getCurrentHome].sigMeshModel;
1.2.5. Connect SIG Mesh Device
Declaration
- (void)startScanWithScanType:(TuyaSmartSIGScanType)scanType
meshModel:(TuyaSmartBleMeshModel *)meshModel;
When need connect SIG Mesh device(s), only input ScanForProxyed
. More Infomation See Activation.
1.3. Activation
TuyaSmartSIGMeshManager
The distribution network refers to adding devices that are in the reset state and not connected to the sig mesh network.
The following is a list of common device reset methods
Product | reset | show |
---|---|---|
Light | Three consecutive switches | flashes quickly |
Switch | Long press 3s | flashes quickly |
1.3.1. Scaning SIG Mesh Devices
Declaration
Scan for nearby SIG-compliant Bluetooth devices
The meshModel here needs to be passed in the sigMeshModel parameter in TuyaSmartHome, not the meshModel
- (void)startScanWithScanType:(TuyaSmartSIGScanType)scanType
meshModel:(TuyaSmartBleMeshModel *)meshModel;
Parameters
parameter | Description |
---|---|
scanType | Scanning type, currently divided into unconfigured network and configured network, the scanned network will automatically enter the network |
meshModel | mesh model |
Example
[TuyaSmartSIGMeshManager sharedInstance].delegate = self;
// ScanForUnprovision, // Scan for unconfigured devices
// ScanForProxyed, // Scan for devices that have been configured
[[TuyaSmartSIGMeshManager sharedInstance] startScanWithScanType:ScanForUnprovision
meshModel:home.sigMeshModel];
Declaration
After scanning the device, you can implement the following methods in the TuyaSmartSIGMeshManagerDelegate callback to get the scanned device.
/**
Scanning to devices to be configured
*/
- (void)sigMeshManager:(TuyaSmartSIGMeshManager *)manager
didScanedDevice:(TuyaSmartSIGMeshDiscoverDeviceInfo *)device;
Parameters
parameter | Description |
---|---|
manager | mesh manager |
device | Network equipment information to be allocated |
Sub Device Bluetooth distribution network
After scanning the surrounding network devices that meet the protocol specifications, you can configure the network (s).
Networking is to add Bluetooth devices that have not joined the mesh network to the mesh network through a certain communication process.
1.3.2. Active SIG Mesh Device by Bluetooth
Declaration
- (void)startActive:(NSArray<TuyaSmartSIGMeshDiscoverDeviceInfo *> *)devList
meshModel:(TuyaSmartBleMeshModel *)meshModel;
Parameters
parameter | Description |
---|---|
devList | List of devices to be activated |
meshModel | mesh model |
delegate
Declaration
When a device is activated successfully or fails, the following methods will be called back through TuyaSmartSIGMeshManagerDelegate: Activate child device successfully callback
- (void)sigMeshManager:(TuyaSmartSIGMeshManager *)manager
didActiveSubDevice:(TuyaSmartSIGMeshDiscoverDeviceInfo *)device
devId:(NSString *)devId
error:(NSError *)error;
Parameters
parameter | Description |
---|---|
manager | mesh manager |
device | Device |
devId | dev Id |
error | active error,name or deviceId is empty |
Declaration
Activate child device failure callback
- (void)sigMeshManager:(TuyaSmartSIGMeshManager *)manager
didFailToActiveDevice:(TuyaSmartSIGMeshDiscoverDeviceInfo *)device
error:(NSError *)error;
Parameters
parameter | Description |
---|---|
manager | mesh manager |
device | Device |
error | errors |
Deactivate Device
Declaration
At any stage in the scanning device and network configuration, calling the following methods will stop the network configuration process for Bluetooth devices.
- (void)stopActiveDevice;
Mesh Connection Flag
In the process of operation, it is often judged whether the existing equipment of the mesh is connected to the network through Bluetooth to determine the method of issuing control commands and operation commands.
// mesh local connection identifier, there is a device connected via Bluetooth, this attribute is yes
BOOL isLogin = [TuyaSmartSIGMeshManager sharedInstance].isLogin;
1.3.3. Active SIG Mesh Gateway Device
SIG Mesh gateway active EZ , see EZ mode
1.3.4. Active SIG Mesh Device by Gateway
SIG Mesh gateway atcive sub device, see sub-device
1.3.5. Error code
Error Code | Description |
---|---|
701 | Connect Error |
702 | Composition response error |
703 | Composition timeout |
704 | add appkey Response Error |
705 | add appkey timeout |
706 | network transmit response error |
707 | network transmit timeout |
708 | publication model response error |
709 | publication model timeout |
710 | App bind response error |
711 | App bind timeout |
712 | IdentifySet timeout |
1.4. Device
Like the Home SDK, the device class is TuyaSmartDevice. The deviceType information in TuyaSmartDeviceModel inside can distinguish the device type.
Here the mesh device corresponds to the deviceType type TuyaSmartDeviceModelTypeSIGMeshSubDev
1.4.1. Get Device Instance
+ (instancetype)deviceWithDeviceId:(NSString *)devId;
1.4.2. Local Connection and Gateway Connection
Local Connection
The Bluetooth of the mobile phone is turned on and the mesh device is controlled by Bluetooth.
deviceModel.isOnline && deviceModel.isMeshBleOnline
Gateway Connection
The mobile phone's Bluetooth is not turned on or is far away from the device. The mesh device controls the connection through the gateway and issues commands by Wi-Fi.
deviceModel.isOnline && !deviceModel.isMeshBleOnline
1.4.3. Get Device Status
Declaration
- (void)getDeviceStatusWithDeviceModel:(TuyaSmartDeviceModel *)deviceModel;
Parameters
parameter | Description |
---|---|
deviceModel | device model |
1.4.4. Remove Device
1.5. Group
In the Bluetooth Mesh network, you can group some devices into groups and use group commands to control the devices in the group. For example, you can add all light groups to a group, and control the group's switches, colors, etc. directly. All lights in the control group have the same properties
1.5.1. Add Group
For the addition of SIG Mesh groups, in order to ensure consistent functions, it is recommended to add devices of the same category to the group
Before adding a group, you need to get the group address from the server: You can call the following of TuyaSmartBleMeshGroup
Declaration
Assign a group ID to the cloud
+ (void)getBleMeshGroupAddressFromCluondWithMeshId:(NSString *)meshId
success:(TYSuccessInt)success
failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
meshId | mesh id |
success | success block |
failure | failure block |
Declaration
The group address returned from the server needs to be added with 0x4000, and then called to get a group named by groupName under the current sigMeshModel
+ (void)createMeshGroupWithGroupName:(NSString *)groupName
meshId:(NSString *)meshId
localId:(NSString *)localId
pcc:(NSString *)pcc
success:(TYSuccessInt)success
failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
groupName | mesh group name |
meshId | meshId |
localId | 2 bytes hex string |
pcc | pcc |
success | success block , groupId |
failure | failure |
1.5.2. Add Device to Group
groupAddress = [localId inValue]
Bluetooth
If you need to add a device to the group, you need to call the following method of TuyaSmartSIGMeshManager
Declaration
- (void)addDeviceToGroupWithDevId:(NSString *)devId
groupAddress:(uint32_t)groupAddress;
Parameters
parameter | Description |
---|---|
devId | dev id |
groupAddress | group address |
TuyaSmartSIGMeshManagerDelegate
Declaration
- (void)sigMeshManager:(TuyaSmartSIGMeshManager *)manager
didHandleGroupWithGroupAddress:(nonnull NSString *)groupAddress
deviceNodeId:(nonnull NSString *)nodeId
error:(NSError *)error;
Parameters
parameter | Description |
---|---|
manager | manager |
groupAddress | group mesh address |
nodeId | device mesh address |
error | error |
Gateway
Adding sub-devices to the mesh group through the gateway can be operated through
Declaration
/**
Adding a sig mesh sub-device group through a sig mesh gateway
Need to ensure that the sub-device relationship belongs to the sig mesh gateway
*/
- (void)addSubDeviceWithSubList:(NSArray<TuyaSmartDeviceModel *> * _Nonnull )subList success:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
// TuyaSmartBleMeshGroupDelegate
@protocol TuyaSmartBleMeshGroupDelegate <NSObject>
/// Group Response of Zigbee Devices Joining Gateway
/// 1: Over the Scenario Limit 2: Subdevice Timeout 3: Setting Value Out of Range 4: Write File Error 5: Other Errors
- (void)meshGroup:(TuyaSmartBleMeshGroup *)group addResponseCode:(NSArray <NSNumber *> *)responseCode;
Parameters
parameter | Description |
---|---|
subList | Sub-devices under the gateway to be operated |
success | success block |
failure | failure block |
1.5.3. Remove Device From Group
Bluetooth
Declaration
If you need to remove a device from the group, you can use the following methods:
- (void)deleteDeviceToGroupWithDevId:(NSString *)devId groupAddress:(uint32_t)groupAddress;
Parameters
parameter | Description |
---|---|
devId | dev id |
groupAddress | group address |
TuyaSmartSIGMeshManagerDelegate
Declaration
- (void)sigMeshManager:(TuyaSmartSIGMeshManager *)manager
didHandleGroupWithGroupAddress:(nonnull NSString *)groupAddress
deviceNodeId:(nonnull NSString *)nodeId
error:(NSError *)error;
Parameters
parameter | Description |
---|---|
manager | manager |
groupAddress | group mesh address |
nodeId | device mesh address |
error | error |
Gateway
Deleting devices into the mesh group through the gateway can be operated through TuyaSmartBleMeshGroup
Declaration
/**
Adding a sig mesh sub-device group through a sig mesh gateway
Need to ensure that the sub-device relationship belongs to the sig mesh gateway
*/
- (void)removeSubDeviceWithSubList:(NSArray<TuyaSmartDeviceModel *> * _Nonnull )subList success:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
Parameters
parameter | Description |
---|---|
subList | Sub-devices under the gateway to be operated |
success | success block |
failure | failure block |
TuyaSmartBleMeshGroupDelegate
// TuyaSmartBleMeshGroupDelegate callback
@protocol TuyaSmartBleMeshGroupDelegate <NSObject>
/// Group Response of Zigbee Devices removing Gateway
/// 1: Over the Scenario Limit 2: Subdevice Timeout 3: Setting Value Out of Range 4: Write File Error 5: Other Errors
- (void)meshGroup:(TuyaSmartBleMeshGroup *)group removeResponseCode:(NSArray <NSNumber *> *)responseCode;
@end
1.5.4. Sync Group Info
If the add \ deletion succeeds or fails, you can receive the result through the proxy method, and use the group instance to change the cloud device synchronization within the group.
Add Device
Declaration
// TuyaSmartBleMeshGroup
- (void)addDeviceWithDeviceId:(NSString *)deviceId success:(TYSuccessHandler)success failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
success | success block |
failure | failure block |
Remove Device
Declaration
- (void)removeDeviceWithDeviceId:(NSString *)deviceId success:(TYSuccessHandler)success failure:(TYFailureError)failure;
Parameters
parameter | Description |
---|---|
success | success block |
failure | failure block |
1.5.5. Query Group Device
Declaration
Query devices in a group by group address
- (void)queryGroupMemberWithGroupAddress:(uint32_t)groupAddress;
Parameters
parameter | Description |
---|---|
groupAddress | group address |
Callback
Declaration
- (void)sigMeshManager:(TuyaSmartSIGMeshManager *)manager
queryDeviceModel:(TuyaSmartDeviceModel *)deviceModel
groupAddress:(uint32_t)groupAddress;
1.6. Control
DP Publish
{"(dpId)" : "(dpValue)"}
, @{@"101" : @"44"}
1.6.1. Control Command - Control Devices
Declaration
// TuyaSmartDevice
- (void)publishDps:(NSDictionary *)dps
success:(nullable TYSuccessHandler)success
failure:(nullable TYFailureError)failure;
Parameters
parameter | Description |
---|---|
dps | dp dictionary |
success | success block |
failure | failure block |
1.6.2. Control Command - Control Group
Declaration
// TuyaSmartBleMeshGroup
- (void)publishDps:(NSDictionary *)dps success:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
1.7. Firmware Upgrade
1.7.1. Query Firmware Info
Declaration
// TuyaSmartDevice
- (void)getFirmwareUpgradeInfo:(void (^)(NSArray <TuyaSmartFirmwareUpgradeModel *> *upgradeModelList))success failure:(TYFailureError)failure;
/*
type
0 updrading
1 no upgrade
2 force or remind upgrade
3 check upgrade
*/
Parameters
parameter | Description |
---|---|
success | success block |
failure | failure block |
1.7.2. Sub-Device Upgrade
Ensure Device Online
The sub-devices are upgraded through Bluetooth, so you need to determine whether the device is locally connected, and confirm the local online before performing subsequent operations.
// BOOL isBLEOnline = device.deviceModel.isMeshBleOnline;
Tell SDK Upgrade Device Node Id
[TuyaSmartSIGMeshManager sharedInstance].delegate = self;
[[TuyaSmartSIGMeshManager sharedInstance] prepareForOTAWithTargetNodeId:self.device.deviceModel.nodeId];
Wait Device Upgrade Success Callback
// TuyaSmartSIGMeshManagerDelegate
- (void)notifySIGLoginSuccess {
[TuyaSmartSIGMeshManager sharedInstance].delegate = nil;
//weakify(self);
[[TuyaSmartSIGMeshManager sharedInstance] startSendOTAPack:_otaData targetVersion:_upgradeModel.version success:^{
//strongify(self);
// update version
[self updateVersion];
} failure:^{
// log error
}];
}
Update Device Version
- (void)updateVersion {
[self.device updateDeviceVersion:_upgradeModel.version type:_upgradeModel.type success:^{
// ota success
} failure:^(NSError *error) {
// log error
}];
}
1.7.3. Mesh Gateway upgrade
See firmware upgrade