1.一些类的用途
对象名 | 描述 |
---|---|
HMHomeManager | 房屋空间管理 |
HMHome | 房间管理 |
HMAccessoryBrowser | 扫描设备 |
HMAccessory | 设备 |
HMService | 设备提供的服务 |
HMCharacteristic | 服务的特征 |
2.创建HMHomeManager
- (HMHomeManager *)homeManager{
if (_homeManager == nil) {
_homeManager = [HMHomeManager new];
_homeManager.delegate = self;
}
return _homeManager;
}
3.获取房间,并且获取房间的设备
self.homeManager.primaryHome.accessories
4.初始化扫描器,并扫描设备,通过代理来监听(如果是wifi设备,要在同一局域网下)
- (HMAccessoryBrowser *)broswer{
if (_broswer == nil) {
_broswer = [HMAccessoryBrowser new];
_broswer.delegate = self;
}
return _broswer;
}
#pragma mark - HMAccessoryBrowserDelegate
- (void)accessoryBrowser:(HMAccessoryBrowser *)browser didFindNewAccessory:(HMAccessory *)accessory{
//获取到新硬件
[self.dataArray addObject:accessory];
[self.tableView reloadData];
NSLog(@"发现一个新硬件");
}
- (void)accessoryBrowser:(HMAccessoryBrowser *)browser didRemoveNewAccessory:(HMAccessory *)accessory{
//移除新硬件
[self.dataArray removeObject:accessory];
[self.tableView reloadData];
NSLog(@"失去一个新硬件");
}
5.房间添加设备
[self.homeManager.primaryHome addAccessory:access completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error);
}
}];
6 . 设备获取服务,修改属性
for (HMService * service in access.services) {
//这里要根据不同类型的 特征属性 来设置不同类型的值,下面只是举个栗子
for(HMCharacteristic * character in service.characteristics)
{
//特征属性为
NSLog(@"属性类型为:%@",character.characteristicType);
NSLog(@"特征属性有:%@",character.properties);
// NSLog(@"%@",service.characteristics);
}
[character readValueWithCompletionHandler:^(NSError * _Nullable error) {
}];
[character writeValue:@(1) completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"设置成功");
}else{
NSLog(@"设置失败");
}
}];
}
demo地址:
demo地址,实现了简单的功能
效果图
-
扫描设备
匹配设备