更新时间:2023-05-22 06:38:31
根据 TuyaSmartDeviceModel
和 IPC SDK 中的接口,可以判断设备是否为低功耗设备。
示例代码
- (BOOL)isLowPowerDevice;
如果设备即是摄像机设备又是低功耗设备,可以认为设备是低功耗门铃。
低功耗门铃由电池供电,为了节省电量,在一定时间内没有 P2P 连接会休眠。休眠后无法直接连接 P2P,您需要先唤醒设备,再连接 P2P 通道。使用 TuyaSmartDevice
类提供的接口来唤醒设备。
示例代码
- (void)awakeDeviceWithSuccess:(nullable TYSuccessHandler)success failure:(nullable TYFailureError)failure;
当唤醒休眠的低功耗设备返回 success
调用时,只是表示唤醒命令已经成功下发给门铃设备,并不代表门铃设备已启动。当门铃设备启动时,会通过设备功能点 TuyaSmartCameraWirelessAwakeDPName
上报 YES
。
示例代码
Objective C:
- (void)viewDidLoad {
[super viewDidLoad];
self.dpManager = [[TuyaSmartCameraDPManager alloc] initWithDeviceId:self.devId];
self.device = [TuyaSmartDevice deviceWithDeviceId:self.devId];
// 添加 DP 监听
[self.dpManager addObserver:self];
[self start];
}
- (void)start {
if (self.isConnected) {
[self.videoContainer addSubview:self.camera.videoView];
self.camera.videoView.frame = self.videoContainer.bounds;
[self.camera startPreview];
}else if (!self.isConnecting) {
if (self.device.deviceModel.isLowPowerDevice) {
[self.device awakeDeviceWithSuccess:nil failure:nil];
}
[self.camera connect];
self.isConnecting = YES;
}
}
Swift:
func viewDidLoad() {
super.viewDidLoad()
self.dpManager = TuyaSmartCameraDPManager(deviceId: self.devId)
self.device = TuyaSmartDevice(deviceId: self.devId)
// 添加 DP 监听
self.dpManager?.addObserver(self)
self.start()
}
func start() {
guard self.isConnected || self.isConnecting else {
if self.device?.deviceModel.isLowPowerDevice() {
self.device?.awake(success: nil, failure: nil)
}
self.camera.connect()
self.isConnecting = true
return
}
self.videoContainer.addSubView(self.camera.videoView)
self.camera.videoView.frame = self.videoContainer.bounds
self.camera.startPreview()
}
低功耗门铃有两种供电方式,插电和电池供电。通过 IPC SDK,您可以查询到设备的供电模式以及当前电量。您还可以设置一个低电量报警阈值,当电量过低时,触发一个报警通知。
示例代码
Objective C:
- (void)viewDidLoad {
if ([self.dpManager isSupportDP:TuyaSmartCameraWirelessPowerModeDPName]) {
TuyaSmartCameraPowerMode powerMode = [[self.dpManager valueForDP:TuyaSmartCameraWirelessPowerModeDPName] tysdk_toString];
if ([powerMode isEqualToString:TuyaSmartCameraPowerModePlug]) {
// 插电供电
}else if ([powerMode isEqualToString:TuyaSmartCameraPowerModeBattery]) {
// 电池供电
}
}
if ([self.dpManager isSupportDP:TuyaSmartCameraWirelessElectricityDPName]) {
NSInteger electricity = [[self.dpManager valueForDP:TuyaSmartCameraWirelessElectricityDPName] tysdk_toInt];
NSLog(@"设备当前的电量为:%@%%", @(electricity));
}
if ([self.dpManager isSupportDP:TuyaSmartCameraWirelessLowpowerDPName]) {
// 设置电量低于 20% 时,触发低电量警告
[self.dpManager setValue:@(20) forDP:TuyaSmartCameraWirelessLowpowerDPName success:^(id result) {
} failure:^(NSError *error) {
// 网络错误
}];
}
if ([self.dpManager isSupportDP:TuyaSmartCameraWirelessBatteryLockDPName]) {
// 解除电池锁,以拆卸电池
[self.dpManager setValue:@(NO) forDP:TuyaSmartCameraWirelessBatteryLockDPName success:^(id result) {
} failure:^(NSError *error) {
// 网络错误
}];
}
}
Swift:
override func viewDidLoad() {
super.viewDidLoad()
if self.dpManager.isSupportDP(.wirelessPowerModeDPName) {
let powerMode = self.dpManager.value(forDP: .wirelessPowerModeDPName) as! String
switch TuyaSmartCameraPowerMode(rawValue: powerMode) {
case .plug: break
// 插电供电
case .battery: break
// 电池供电
default: break
}
}
if self.dpManager.isSupportDP(.wirelessElectricityDPName) {
let electricity = self.dpManager.value(forDP: .wirelessElectricityDPName) as! Int
print("设备当前的电量为:", electricity)
}
if self.dpManager.isSupportDP(.wirelessLowpowerDPName) {
// 设置电量低于 20% 时,触发低电量警告
self.dpManager.setValue(20, forDP: .wirelessLowpowerDPName, success: { _ in
}) { _ in
// 网络错误
}
}
if self.dpManager.isSupportDP(.wirelessBatteryLockDPName) {
// 解除电池锁,以拆卸电池
self.dpManager.setValue(false, forDP: .wirelessBatteryLockDPName, success: { _ in
}) { _ in
// 网络错误
}
}
}
使用低功耗门铃功能时,您需要下载默认资源文件(resources.zip),解压并加入工程。也可以按默认文件名,添加自定义的音频文件。
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈