HMAccessoryDelegates 不调用按钮操作
HMAccessoryDelegates not calling on Button action
我正在开发 Homekit iOS 应用程序。我有一个问题,我有一个附件,当我使用 HomeKit 模拟器更改其功率特性值时,HMAccessory 的代表正在调用,但如果我以编程方式更改功率特性值(使用 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];
}
}];
}
}
如有不明之处请告知
documentation 表示当您以编程方式设置值时不会调用委托方法:
This method is called as a result of a change in value initiated by
the accessory. Programmatic changes initiated by the app do not result
in this method being called.
如果你想在写入特征值成功(或失败)后做一些事情,你可以在writeValue:completionHandler:
方法的completionHandler:
块中进行。
我正在开发 Homekit iOS 应用程序。我有一个问题,我有一个附件,当我使用 HomeKit 模拟器更改其功率特性值时,HMAccessory 的代表正在调用,但如果我以编程方式更改功率特性值(使用 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];
}
}];
}
}
如有不明之处请告知
documentation 表示当您以编程方式设置值时不会调用委托方法:
This method is called as a result of a change in value initiated by the accessory. Programmatic changes initiated by the app do not result in this method being called.
如果你想在写入特征值成功(或失败)后做一些事情,你可以在writeValue:completionHandler:
方法的completionHandler:
块中进行。