跳到主要内容

局域网设备

更新时间:2025-07-22 18:35:15

当手机与设备处于同一局域网(非公网)时,即可通过局域网搜索设备,并与设备建立局域网连接,连接成功以后可以进行设备播放(包括:直播、SD卡回放)、PTZ操作、获取SD卡回放数据等。

// 导入设备管理SDK头文件
#import <RTCXDeviceCenter/RTCXDeviceCenter.h>
@protocol RTCXWlanLinkProtocol <NSObject>
/**
开始搜索局域网设备

@param callback 响应回调
*/
- (void)startSearchWlanDeviceOnCallback:(RTCXWlanLinkCallback)callback;
/**
停止搜索局域网设备

*/
- (void)stopSearchWlanDevice;
/**
开始局域网设备连接

@param req 请求参数对象
@param callback 响应回调
*/
- (void)startConnectWithReq:(RTCXWlanLinkConnectReq *)req onCallback:(RTCXWlanLinkCallback)callback;
/**
断开局域网设备连接

*/
- (void)disConnect;
/**
请求网关的子设备列表

@param req 请求参数对象
@param callback 响应回调
*/
- (void)queryGatewaySubdeviceListWithReq:(RTCXWlanLinkSubdeviceListReq *)req onCallback:(RTCXWlanLinkCallback)callback;
@end

搜索设备

当手机与设备处于同一局域网以后,可以通过调用搜索设备接口搜索周围局域网设备,如果周围有多台设备响应,该接口会回调多次,上层可通过UI列表展示。

[self showLoadingHUD:NSLocalizedString(@"搜索中...", nil)];
[RTCXDeviceService(RTCXWlanLinkProtocol) startSearchWlanDeviceOnCallback:^(BOOL success, id _Nullable data, NSError * _Nullable error) {
[weakSelf hideLoadingView];
if (success) {
[weakSelf.tableView.hwhd_rowArray removeAllObjects];
RTCXWlanLinkDevModel *resultM = data;
APLinkListModel *model = [APLinkListModel iotyy_modelWithJSON:[resultM iotyy_modelToJSONString]];
[weakSelf.tableView.hwhd_rowArray addObjectsFromArray:@[model]];
[weakSelf.tableView reloadData];
if (!self.isConnected) {
__weak typeof(self)weakSelf = self;
RTCXWlanLinkConnectReq *req = [[RTCXWlanLinkConnectReq alloc] init];
req.proto = model.netInfo.protocol;
req.ip = model.netInfo.sigIp;
req.port = model.netInfo.sigPort;
[RTCXDeviceService(RTCXWlanLinkProtocol) startConnectWithReq:req onCallback:^(BOOL success, id _Nullable data, NSError * _Nullable error) {
weakSelf.isConnected = success;
}];
}
} else {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}
}];

停止搜索设备

当不再需要继续搜索局域网设备的时候,可通过调用该接口停止搜索设备

[RTCXDeviceService(RTCXWlanLinkProtocol) stopSearchWlanDevice];

网关设备列表

当搜索到的是网关设备(即搜索返回数据模型:RTCXWlanLinkDevInfo.isGateway = YES)时,可以获取该网关下面的所有设备列表,非网关设备不需要调用此接口。

__weak typeof(self)weakSelf = self;
RTCXWlanLinkSubdeviceListReq *req = [[RTCXWlanLinkSubdeviceListReq alloc] init];
req.productKey = self.gatewayInfo.deviceInfo.productKey;
req.deviceName = self.gatewayInfo.deviceInfo.deviceName;
[RTCXDeviceService(RTCXWlanLinkProtocol) queryGatewaySubdeviceListWithReq:req onCallback:^(BOOL success, id _Nullable data, NSError * _Nullable error) {
if (success) {
[weakSelf.tableView.hwhd_rowArray removeAllObjects];
NSArray *resultArr = data;
NSArray *subDevlist = [NSArray iotyy_modelArrayWithClass:[APLinkListModel class] json:[resultArr iotyy_modelToJSONString]];
[weakSelf.tableView.hwhd_rowArray addObjectsFromArray:subDevlist];
[weakSelf.tableView reloadData];
} else {
[weakSelf showToast:[NSString stringWithFormat:@"%ld : %@",error.code,error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}
}];

连接设备

在设备播放之前,需要先进行设备连接,只有设备连接成功以后才可以进行设备播放、PTZ操作等,建议在搜索到设备以后就提前建立设备连接。

RTCXWlanLinkDevModel *model = currentM;
if (!self.isConnected) {
__weak typeof(self)weakSelf = self;
RTCXWlanLinkConnectReq *req = [[RTCXWlanLinkConnectReq alloc] init];
req.proto = model.netInfo.protocol;
req.ip = model.netInfo.sigIp;
req.port = model.netInfo.sigPort;
[RTCXDeviceService(RTCXWlanLinkProtocol) startConnectWithReq:req onCallback:^(BOOL success, id _Nullable data, NSError * _Nullable error) {
weakSelf.isConnected = success;
}];
}

断开连接设备

当不再需要连接设备时,如:退出局域网设备列表,需要断开设备连接,释放资源。

[RTCXDeviceService(RTCXWlanLinkProtocol) disConnect];

SD卡存储录像

与通用版SD卡数据获取基本相同,请先阅读【通用版-存储卡】相关接口,区别在于构建RTCXSDStorageReq子类请求对象时,需要设置RTCXSDStorageReq.deviceType = 2,即区分是局域网设备。

设备播放

局域网设备播放接口与通用版基本相同,请先阅读【通用版-音视频播放】相关接口,只是在构建RTCXPlayInfo播放对象时,需要设置RTCXPlayInfo.deviceType = 2,即区分是局域网设备。

// 导入媒体播放SDK头文件
#import <RTCXMedia/RTCXMedia.h>

调用示例如下:

  • 直播
// 局域网设备直播
RTCXWlanLinkDevModel *model = currentM;
RTCXPlayInfo *playInfo = [[RTCXPlayInfo alloc] init];
playInfo.iotId = [NSString stringWithFormat:@"%@%@",model.deviceInfo.productKey,model.deviceInfo.deviceName];
playInfo.productKey = model.deviceInfo.productKey;
playInfo.deviceName = model.deviceInfo.deviceName;
playInfo.deviceType = 2;// 局域网设备
// 初始化播放器,传入frame和设备model
self.playerView = [[RTCXPlayerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 9.0 / 16.0) withDevice:playInfo];
self.playerView.playerController.delegate = self;// 创建player控制类并设置代理监听player状态,playeview.playerController会懒加载创建控制类
self.playerView.playerController.dataSource = self;// 设置playeview.playerController的dataSource
// 初始化player播放的视图,可以把该playerView加到任意view上
[self.view addSubview:self.playerView];
  • SD卡回放
// 局域网设备SD卡回放
RTCXPlaybackInfo *playbackInfo = [[RTCXPlaybackInfo alloc] init];
playbackInfo.startTime = [startTime timeIntervalSince1970];
playbackInfo.endTime = [startTime timeIntervalSince1970];
playbackInfo.playbackType = IOTPlaybackTypeSDCardRecord;
[self.playerView.playerController seekToTime:playbackInfo];
  • PTZ控制
// 局域网设备PTZ控制
RTCXThing *things = [kRTCXThingManager buildThing:apDevM.deviceInfo.productKey deviceName:apDevM.deviceInfo.deviceName iotId:[NSString stringWithFormat:@"%@%@",apDevM.deviceInfo.productKey,apDevM.deviceInfo.deviceName] deviceType:2];
[[things getThingActions] invokeService:@"PTZActionControl" params:@{@"ActionType":@(1),@"Step":@(10)} responseHandler:^(RTCXThingActionsResponse * _Nullable response) {
COMMON_PRINTF(response.success?@"PTZ步进转动成功":@"PTZ步进转动失败");
}];