This software development kit (SDK) provides a solution for controlling Bluetooth Mesh groups from your app. It lets you add, delete, and control groups.
Overview
For single-device control, use the Thing Specification Language model SDK.
Dependent SDK | Overview |
This SDK provides the app-side Thing Specification Language model. |
Usage instructions
Connect to a Mesh Network
Before you can control a single device or a group of devices, you must connect the app to the Mesh network.
/// Dependent header file
#import <ALBBluetoothMesh/ALBBluetoothMesh.h>
/// Connect to the Mesh network. The iotId can be the iotId of any device under your account.
[[ALBBluetoothMesh sharedInstance] connectWithDeviceInfo:@{@"iotId": iotId}];Check the Mesh Network Connection Status
You can use the following method to check whether the app is connected to the Mesh network.
/// Dependent header file
#import <ALBBluetoothMesh/ALBBluetoothMesh.h>
//// The iotId can be the iotId of any device under your account.
[[ALBBluetoothMesh sharedInstance] isConnectWithIotId:@{@"iotId": iotId}]Configure a Multicast Address for a Device
Before you can control devices in a group, you must add a multicast address to each device. The request parameters for this method depend on the API operations related to group control. For more information, see Create a device group, Obtain the multicast address configuration for a mesh device group in advance, Obtain a list of devices in a device group, Delete device groups in batches, and Delete a device group.
/// Dependent header file
#import <ALBBluetoothMesh/ALBBluetoothMesh.h>
/// The values of the input parameters depend on the return values of the cloud APIs.
NSString * deviceKey = @""; // The deviceKey of the device to which you want to add the multicast address.
int primaryAddress = ; // The unicast address of the primary node.
int elementAddress = ; // The unicast address of the node to be configured.
int subscriptionAddress = ; // The multicast address to subscribe to.
int modelIdentifier = ; // SIG Model ID or Vendor Model ID
[[ALBBluetoothMesh sharedInstance] configModelSubscriptionAdd:deviceKey
primaryAddress:primaryAddress
elementAddress:elementAddress
subscriptionAddress:subscriptionAddress
modelIdentifier:modelIdentifier
complete:^(ALBDeviceSubscribeInfo *subInfo, NSError * _Nullable error) {
if (error) {
IMSIMSControlGroupLogDebug(@"configModelSubscriptionAdd error:%@", error);
}
if (complete) {
//// If error is nil, the multicast address was successfully written to the device.
complete(error, iotId, YES);
}
}];
Parameters
Name | Type | Required | Default | Description |
deviceKey | NSString * | Yes | The deviceKey of the node to be configured. Each device has a unique deviceKey. | |
primaryAddress | Integer | Yes | Same as elementAddress. | |
elementAddress | Integer | Yes | The unicast address of the node to be configured. | |
subscriptionAddress | Integer | Yes | The multicast address to subscribe to. | |
modelIdentifier | Integer | Yes | The SIG Model ID or Vendor Model ID. It identifies the model within the node. | |
complete | IActionListener<Boolean> | No | The callback for the configuration result. |
The cloud API returns all the preceding parameters.
Delete a Configured Multicast Address from a Device
/// The values of the input parameters depend on the return values of the cloud APIs.
NSString * deviceKey = @""; // The deviceKey of the device to be configured.
int primaryAddress = ; // The unicast address of the primary node.
int elementAddress = ; // The unicast address of the node to be configured.
int subscriptionAddress = ; // The multicast address to delete.
int modelIdentifier = ; // SIG Model ID or Vendor Model ID
[[ALBBluetoothMesh sharedInstance] configModelSubscriptionDelete:deviceKey
primaryAddress:primaryAddress
elementAddress:elementAddress
subscriptionAddress:subscriptionAddress
modelIdentifier:modelIdentifier
complete:^(ALBDeviceSubscribeInfo *subInfo, NSError * _Nullable error) {
if (error) {
IMSIMSControlGroupLogDebug(@"configModelSubscriptionDelete error:%@", error);
}
if (complete) {
complete(error, iotId, NO);
}
}];Parameters
Name | Type | Required | Default | Description |
deviceKey | NSString * | Yes | The deviceKey of the node to be configured. Each device has a unique deviceKey. | |
primaryAddress | Integer | Yes | Same as elementAddress. | |
elementAddress | Integer | Yes | The unicast address of the node to be configured. | |
subscriptionAddress | Integer | Yes | The subscribed multicast address to delete. | |
modelIdentifier | Integer | Yes | The SIG Model ID or Vendor Model ID. It identifies the model within the node. | |
deviceControlListener | IActionListener<Boolean> | No | The callback for the configuration result. |
The cloud API returns all the preceding parameters.
Group Control Calls
Group control for devices involves operations on device properties that are defined in the Thing Specification Language model. For more information about properties, see Terms.
// /// This is an example of setting the switch property for a group. A value of 1 for isOpen turns on the device, and 0 turns it off.
- (void)setControlGroupPowerProperty:(NSString *)groupId devices:NSArray<NSString *> *deviceIotIds
isOpen:(BOOL)isOpen {
IMSHomeLogDebug(@"setControlGroupPwoerProperty:%@ isOpen:%@", groupId, @(isOpen));
if (groupId) {
NSNumber *channel = @(GroupPolicyLocalPrefer);
__weak typeof(self) weakSelf = self;
NSDictionary* extra = @{@"controlGroupId":groupId,
@"QosLevel":@"Qos_CON",
@"Channel":channel,
@"iotId": deviceIotIds.firstObject ? : @"",
@"isNative": @(YES),
@"deviceIotIds": deviceIotIds};
/// powerstate is the device switch property. For the values of other properties, see the Thing Specification Language (TSL) model.
[[IMSGroupManager sharedManager] setGroupProps:@{@"items":@{@"powerstate": isOpen ? @(1) : @(0)}}
extraData:extra
responseHandler:^(IMSGroupActionsResponse * _Nullable response) {
if (response.responseError) {
IMSHomeLogVerbose(@"ims cycle HomeVC : Control Failed to set properties for the control group! groupId = %@ error = %@", controlgroup.groupModel.groupId, response.responseError.localizedDescription);
///[IMSToast showToast:response.responseError.localizedDescription withShowTime:1 inVC:[weakSelf currentViewController]];
}
// This callback only indicates whether the message was sent. It does not indicate whether the properties were set successfully. Use the delegate to get property changes.
}];
}
}Parameters
Name | Type | Required | Default | Description |
groupId | NSString * | Yes | The ID of the group to control. | |
deviceIotIds | NSArray<NSString *> * | Yes | The iotIds of all devices in the group. |
After a group control call, the device status change messages are the same as those for single-device control. For more information, see the Thing Specification Language model SDK.