设备管理
更新时间:2025-08-11 20:58:07
包括设备绑定,设备解绑,设备列表
// 导入设备管理SDK头文件
#import <RTCXDeviceCenter/RTCXDeviceCenter.h>
设备绑定
1. 无线绑定(如: 二维码、蓝牙等方式)
总体介绍参考设备绑定开发指南。
- 设备绑定流程
- App调用prepareAddDevice方法后,当监听到RTCXDeviceAddStatus_Prepared状态时可获取bindToken (有效时长30分钟)。
- 将bindToken传递给设备(二维码、蓝牙等),设备保证网络连通情况下,通过DeviceSDK进行设备绑定
- 海外设备绑定时,同时还需要将regionId传递给设备
- 在RTCXDeviceAddStatus_Prepared状态下,App只需要调用startAddDevice方法实时监听绑定状态回调即可(超时自行控制,最大超时依赖bindToken的有效时长)。
注意
- startAddDevice方法需要等添加状态为RTCXDeviceAddStatus_Prepared时才可以调用,所以可以在- (void)deviceAddListener:(RTCXDeviceAddStatus)status callbackData:(nullable id)data error:(nullable NSError *)error回调状态为RTCXDeviceAddStatus_Prepared调用开始添加方法
- 已配网设备(如4G、有线设备)无需处理步骤2,即不需要处理bindToken
- 设备绑定详细流程说明请点击
@protocol RTCXDeviceAddProtocol <NSObject>
#pragma mark - 设备添加步骤 -
/**
* 准备添加设备,成功才可以进行下一步
*
* **@param addType 添加类型
*/
- (void)prepareAddDevice:(RTCXDeviceAddType)addType;
/**
* 开始添加设备,成功才可以进行下一步
*
* @param req 配网模式对应入参
*/
- (void)startAddDevice:(nullable RTCXDeviceAddReq *)req;
/**
* 停止添加设备
*
*/
- (void)stopAddDevice;
@end
- 获取设备配网bindToken
@protocol RTCXDeviceAddProtocol <NSObject>
/**
* 获取配网所需随机数(需要RTCXDeviceAddStatus_Prepared状态才能获取到)
*/
- (nullable NSString *)getBindToken
@end
- 获取设备配网regionId(海外设备配网需要)
@protocol RTCXDeviceAddProtocol <NSObject>
/**
获取当前regionId(海外设备配网需要)
*/
- (nullable NSNumber *)getRegionId;
@end
- 通过注册监听对象,实现设备绑定过程状态回调实时监听
注意
- 需要prepareAddDevice:方法之前注册监听
/**
* 注册设备添加状态回调代理对象
*
* @param delegate 代理对象
*/
- (void)registerDelegate:(id<RTCXDeviceAddDelegate>_Nullable)delegate;
// 配网过程中的状态
typedef NS_ENUM(NSUInteger, RTCXDeviceAddStatus) {
/// 就绪,可以发起添加
RTCXDeviceAddStatus_Prepared = 1,
/// 成功 回调新添加的设备信息Model
RTCXDeviceAddStatus_Success,
/// 失败 错误信息见代理回调方法deviceAddListener中返回的error
RTCXDeviceAddStatus_Failed,
};
@protocol RTCXDeviceAddDelegate <NSObject>
// 设备添加状态代理回调方法,回调过程中状态、数据、错误信息
- (void)deviceAddListener:(RTCXDeviceAddStatus)status callbackData:(nullable id)data error:(nullable NSError *)error;
@end
- 通过二维码绑定方式,生成带有 WiFi 名和密码的二维码内容
/**
* 生成二维码字符串数组
*
* @param req RTCXDeviceAddQRReq
*/
- (nullable NSArray <NSString *> *)getQRCodeData:(nullable RTCXDeviceAddQRReq *)req;
- 使用 RTCXDeviceSDK 单例宏对象调用示例如下:
// 注册绑定状态监听
[RTCXDeviceService(RTCXDeviceAddProtocol) registerDelegate:self];
// 二维码方式绑定设备
[RTCXDeviceService(RTCXDeviceAddProtocol) prepareAddDevice:RTCXDeviceAddType_QR];
// 获取二维码内容
RTCXDeviceAddQRReq *req = [[RTCXDeviceAddQRReq alloc] init];
req.ssid = ssidName;
req.wifiPwd = password;
NSArray *qrCodeStrings = [RTCXDeviceService(RTCXDeviceAddProtocol) getQRCodeData:req];
// 设备扫描二维码绑定,需要在RTCXDeviceAddStatus_Prepared状态时调用
RTCXDeviceAddQRReq *qrReq = [[RTCXDeviceAddQRReq alloc] init];
qrReq.productKey = self.productKeyTextField.text;
qrReq.deviceName = self.deviceNameTextField.text;
[RTCXDeviceService(RTCXDeviceAddProtocol) startAddDevice:qrReq];
// 结束绑定设备
[RTCXDeviceService(RTCXDeviceAddProtocol) stopAddDevice];
// 监听绑定状态
#pragma mark -- RTCXDeviceAddDelegate --
- (void)deviceAddListener:(RTCXDeviceAddStatus)status callbackData:(nullable id)data error:(nullable NSError *)error {
COMMON_PRINTF(@"device add status: %d, %@, %@",status,data,error);
if (status == RTCXDeviceAddStatus_Prepared) {
COMMON_PRINTF(@"device add prepared: %d, %@, %@",status,data,error);
NSString *bindToken = [RTCXDeviceService(RTCXDeviceAddProtocol) getBindToken];
COMMON_PRINTF(@"bindToken: %@",bindToken);
// 此状态下才可以调用startAddDevice
RTCXDeviceAddReq *req = [[RTCXDeviceAddReq alloc] init];
[RTCXDeviceService(RTCXDeviceAddProtocol) startAddDevice:req];
} else if (status == RTCXDeviceAddStatus_Success) {
// 返回绑定结果类RTCXDeviceBindResult对象,包含设备的iotId、productKey、deviceName等信息
RTCXDeviceBindResult *result = data;
[self.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:NSClassFromString(@"DeviceListViewController")]) {
[self.navigationController popToViewController:obj animated:YES];
}
}];
} else if (status == RTCXDeviceAddStatus_Failed) {
[self stopLoading];
[self showToast:@"添加设备失败"];
}
}
2. 有线绑定(如: 4G、有线设备)
- 与上面正常设备绑定流程类似,特殊之处是不需要向设备传递bindToken,而是直接调用RTCXDeviceAddWRReq有线添加的方式即可完成绑定(如:App可以扫描设备上的二维码获取productKey和deviceName,然后调用startAddDevice:方法绑定,同时监听绑定状态回调)
// App扫描设备二维码绑定
RTCXDeviceAddWRReq *wireReq = [[RTCXDeviceAddWRReq alloc] init];
wireReq.productKey = self.productKeyTextField.text;
wireReq.deviceName = self.deviceNameTextField.text;
[RTCXDeviceService(RTCXDeviceAddProtocol) startAddDevice:wireReq];
设备解绑
@protocol RTCXDeviceMgrProtocol <NSObject>
/**
* 解绑用户的设备
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)deviceUnbindWithReq:(nonnull RTCXDeviceUnbindReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
@end
使用 RTCXDeviceSDK 单例宏对象调用示例如下:
RTCXDeviceUnbindReq *req = [[RTCXDeviceUnbindReq alloc] init];
req.iotId = cameraDevice.iotId;
[RTCXDeviceService(RTCXDeviceMgrProtocol) deviceUnbindWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf showToast:@"设备解绑成功"];
[weakSelf onGetDeviceListButtonClicked:nil];
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:@"设备解绑失败"];
}];
设备列表
@protocol RTCXDeviceMgrProtocol <NSObject>
/**
* 获取设备列表
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)getDeviceListWithReq:(nonnull RTCXQueryDeviceListReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
/**
* 获取网关的子设备列表
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)getSubDeviceListWithReq:(nonnull RTCXQuerySubDeviceListReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
@end
使用 RTCXDeviceSDK 单例宏对象调用示例如下:
RTCXQueryDeviceListReq *req = [[RTCXQueryDeviceListReq alloc] init];
req.pageNo = 1;
req.pageSize = 10;
[RTCXDeviceService(RTCXDeviceMgrProtocol) getDeviceListWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf stopLoading];
weakSelf.devicelist = [NSMutableArray arrayWithArray:((RTCXDeviceInfoList *)data).data];
[weakSelf.tableView reloadData];
} onError:^(NSError * _Nullable error) {
[weakSelf stopLoading];
[weakSelf showToast:@"获取列表失败"];
}];
- 网关子设备列表调用示例
RTCXQuerySubDeviceListReq *req = [[RTCXQuerySubDeviceListReq alloc] init];
req.iotId = self.gatewayInfo.iotId;
req.pageNo = 1;
req.pageSize = 100;
[RTCXDeviceService(RTCXDeviceMgrProtocol) getSubDeviceListWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf getDeviceListSuccessHandle:data];
} onError:^(NSError * _Nullable error) {
[weakSelf getDeviceListFailHandle:error];
}];
修改设备昵称
@protocol RTCXDeviceMgrProtocol <NSObject>
/**
* 设置设备昵称
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param *onError 失败回调
*/
- (void)setDeviceNickNameWithReq:(nonnull RTCXDeviceNickdNameSetReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
@end
使用 RTCXDeviceSDK 单例宏对象调用示例如下:
RTCXDeviceNickdNameSetReq *req = [[RTCXDeviceNickdNameSetReq alloc] init];
req.iotId = self.device.iotId;
req.nickName = text;
[RTCXDeviceService(RTCXDeviceMgrProtocol) setDeviceNickNameWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
self.device.nickName = text;
[self showToast:@"设置昵称成功"];
[self queryProperties];
} onError:^(NSError * _Nullable error) {
[self showToast:@"设置昵称失败"];
}];
设备缩略图
使用物模型获取设备缩略图
@protocol RTCXThingActions <NSObject>
/**
调用物提供的服务
@param serviceIdentifier 服务的唯一标识符
@param params 调用服务的入参,请参考物的模型 tsl,形如 {"arg1":"val1", "arg2":"val2"}
@param extraData 附加控制属性
key:@"IotPerformanceId" 性能测试
key:@"callType" "async":异步, "sync"同步调用
key:@"Channel", value: ChannelPolicyCloud ChannelPolicyLocal ChannelPolicyLocalPrefer
key:@"QosLevel", value: Qos_CON Qos_NON
key:@"NeedRsp", value: value:TRUE FALSE
@param handler 结果回调函数
*/
- (void)invokeServiceV1:(NSString * _Nonnull)serviceIdentifier
params:(NSDictionary* _Nullable)params
extraData:(NSDictionary* _Nullable)extraData
responseHandler:(RTCXThingActionsResponseHandler _Nullable)handler;
@end
使用 kRTCXThingManager 单例宏对象调用示例如下:
- (NSString *)queryThumnail:(NSString *)iotId {
NSString *picUrl = [self.thumnailDic valueForKey:iotId];
// if (!picUrl) {
if ([[kRTCXThingManager buildThing:iotId] getThingProfile]) {
[[[kRTCXThingManager buildThing:iotId] getThingActions] invokeServiceV1:@"GetDevPicture" params:@{@"definition":@(1),@"picType":@"jpg"} extraData:@{} responseHandler:^(RTCXThingActionsResponse * _Nullable response) {
if (response.success) {
NSString *picUrl = [response.dataObject valueForKey:@"downloadUrl"];
[self.thumnailDic setObject:picUrl forKey:iotId];
__block NSUInteger index = 0;
[self.devicelist enumerateObjectsUsingBlock:^(RTCXDeviceInfo *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.iotId isEqualToString:iotId]) {
index = idx;
*stop = YES;
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
});
} else {
NSString *errorMsg = response.responseError.userInfo[NSLocalizedFailureReasonErrorKey];
// [self showToast:errorMsg];
}
}];
}
// }
return picUrl;
}