0

我正在开发 Homekit iOS 应用程序。我有一个问题,我有一个附件,当我使用 HomeKit 模拟器更改其电源特性值时,HMAccessory 的代表正在校准,但如果我以编程方式更改电源特性值(使用 writevalue),则不会调用委托方法. 请让我知道任何建议的想法。

Code 

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    selectedDevice.delegate = self;
}

HMAccessoryDe​​legate

- (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];
                }
            }];

        }
}

如果我不清楚,请告诉我

4

1 回答 1

1

文档说,当您以编程方式设置值时,不会调用委托方法:

由于附件启动的值更改,调用此方法。应用程序发起的编程更改不会导致调用此方法。

如果您想在写入特征值成功(或失败)后做某事,您可以在方法completionHandler:块中进行 writeValue:completionHandler:

于 2015-06-18T09:11:24.633 回答