我正在使用Home Kit Accessory 模拟器,我想从HMService的实例中检索配件的公司名称。但是,当我添加断点时,我看不到任何与公司名称相关的字段(我在 HMService 和HMAccessory中都进行了搜索)。
有什么建议吗?
您可以从HMServiceTypeAccessoryInformation 服务中获取制造商的名称,服务包含特征数组,其中有HMCharacteristicTypeManufacturer特征。
您可以使用它来显示公司名称。
- (HMCharacteristic *)characteristicForAccessory:(HMAccessory *)accessoryValue{
HMAccessory *thisAccessory = accessoryValue;
HMService *service;
for (HMService *thisService in thisAccessory.services) {
if([thisService.serviceType isEqualToString:HMServiceTypeAccessoryInformation]) {
service = thisService;
}
}
HMCharacteristic *characteristic;
if (service) {
for (HMCharacteristic *charact in service.characteristics) {
if ([charact.characteristicType isEqualToString:HMCharacteristicTypeManufacturer]) {
characteristic = charact;
}
}
}
return characteristic;
}
使用 Characteristic 对象的 value 属性来获取制造商的名称。
喜欢特征值
看看 Raeid Saqur 的RSHomeKit框架:
您可以通过调用 service.accessory 来获取配件。然后使用:
+ (NSString *)getManufacturerNameForHMAccessory:(HMAccessory *)accessory;
+ (NSString *)getManufacturerNameForHMAccessory:(HMAccessory *)accessory {
if (!accessory) {
return nil;
}
HMCharacteristic *manufacturer = [HomeKitUtility getCharacteristicWithUUID:HMCharacteristicTypeManufacturer forAccessory:accessory];
if (manufacturer && manufacturer.value) {
return (NSString *)manufacturer.value;
}
return nil;
}