Performing Common Central Role Tasks
central角色的实现
central 角色需要完成的几件事情,如搜索,连接,与peripheral交互数据。
peripheral 角色同样需要完成几件事情,如,发布和广播服务,响应读,写,订阅请求
本章中,你将学习如何完成central端的功能。
1,创建central manager 对象
2,发现和连接正在广播的peripheral
3,浏览peripheral的数据
4,发送读和写请求到peripheral设备
5,订阅characteristic的值
下一章你将学习如何开发peripheral角色的功能
Starting Up a Central Manager
创建Central manager对象
myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
在这里self设置成central角色的代理。dispath queue设置为nil,意味着central事件将交由main queue处理。
创建central manager时,会触发centralManagerDidUpdateState: 代理方法。你必须实现这个代理。
Discovering Peripheral Devices That Are Advertising
搜索正在发送广播的peripheral
[myCentralManager scanForPeripheralsWithServices:nil