设备管理
更新时间:2025-04-22 14:25:13
包括设备绑定,设备解绑,设备列表
// 导入设备管理SDK头文件
#import <RTCXDeviceCenter/RTCXDeviceCenter.h>
设备绑定
总体介绍参考设备绑定开发指南。
- 设备绑定流程
- App调用prepareAddDevice方法后,当监听到RTCXDeviceAddStatus_Prepared状态时可获取bindToken (有效时长30分钟)。
- 将bindToken传递给设备(二维码、蓝牙等),设备保证网络连通情况下,通过DeviceSDK进行设备绑定
- 在RTCXDeviceAddStatus_Prepared状态下,App只需要调用startAddDevice方法实时监听绑定状态回调即可(超时自行控制,最大超时依赖bindToken的有效时长)。
注意
- startAddDevice方法需要等添加状态为RTCXDeviceAddStatus_Prepared时才可以调用,所以可以在- (void)deviceAddListener:(RTCXDeviceAddStatus)status callbackData:(nullable id)data error:(nullable NSError *)error回调状态为RTCXDeviceAddStatus_Prepared调用开始添加方法
- 设备绑定详细流程说明请点击
@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
- 通过注册监听对象,实现设备绑定过程状态回调实时监听
注意
- 需要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) {
[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:@"添加设备失败"];
}
}
设备解绑
@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:@"设置昵称失败"];
}];