我正在开发Homekit iOS应用程序。我有一个问题,我有一个配件,当我使用 HomeKit 模拟器更改其功率特性值时,HMAccessory 的代表正在校准,但万一如果我以编程方式更改 powr 特征值(使用 writevalue),则不会调用委托方法。请让我知道任何建议的想法。
Code
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
selectedDevice.delegate = self;
}
HMAccessoryDelegate
- (void)accessory:(HMAccessory *)accessory service:(HMService *)service didUpdateValueForCharacteristic:(HMCharacteristic *)characteristic;
{
NSLog(@"changed");
}
写入函数
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(230, 5, 51, 31)];
[cell addSubview:sw];
sw.on = YES;
[sw addTarget:self action:@selector(updateState:) forControlEvents:UIControlEventValueChanged];
-(void)updateState:(UISwitch*)sender
{
HMCharacteristic *characteristic = self.selectedService.characteristics[tag];
[characteristic enableNotification:YES completionHandler:^(NSError *error)
{
if(!error)
{
}
}];
if([characteristic.characteristicType isEqualToString:HMCharacteristicTypePowerState])
{
id val = characteristic.value;
NSString *str = [NSString stringWithFormat:@"%@",val];
if([str isEqualToString:@"0"])
{
id a = characteristic.value;
BOOL b = [a boolValue];
NSNumber *c = [NSNumber numberWithBool:!b];
AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
[characteristic writeValue:c completionHandler:^(NSError *error) {
if (error) {
UIAlertView *alertController = [[UIAlertView alloc] initWithTitle:@"Error" message:[appDel handleErrorCodes:error.code] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertController show];
return;
}
else
{
[serviceCharacteristicsTableView reloadData];
}
}];
}
}
如果我不清楚,请告诉我
文档指出,以编程方式设置值时,不会调用委托方法:
此方法由于由 配件。不会导致应用启动的程序化更改 在调用此方法中。
如果要在写入特征值成功(或失败)后执行某些操作,则可以在writeValue:completionHandler:
方法的completionHandler:
块中执行此操作。