完整项目示例可参考:Apple CoreBluetooth Sample
iOS 蓝牙技术架构
核心框架:CoreBluetooth
角色模型:
(1) Central Manager(中心设备 - 手机):CBCentralManager;扫描/连接外设
(2) Peripheral(外设 - 蓝牙设备)
、CBPeripheral;提供数据服务
开发流程及相关代码
1. 配置Xcode蓝牙权限
(1) General Tab下,Frameworks,Libraries, and Embedded Content中添加 CoreBluetooth.framework
(2) Signing & Capabilities Tab下,Background Modes中,勾选Uses Bluetooth LE accessories
(3) Info Tab 下,Custom iOS Target Properties中,添加Privacy - Bluetooth Peripheral Usage Description和Privacy - Bluetooth Always Usage Description
2. 初始化CBCentralManager
centralManager = CBCentralManager(delegate: self, queue: nil)