<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>swift
//先导入 - 标签 - 宋明的博客</title><link>https://apolla.cc/tags/swift-//%E5%85%88%E5%AF%BC%E5%85%A5/</link><description>宋明的博客</description><generator>Hugo 0.170.0 &amp; FixIt v1.0.0-rc.5-mwimukpl</generator><language>zh-cn</language><lastBuildDate>Sat, 09 Mar 2024 23:21:58 +0800</lastBuildDate><atom:link href="https://apolla.cc/tags/swift-//%E5%85%88%E5%AF%BC%E5%85%A5/index.xml" rel="self" type="application/rss+xml"/><item><title>2：蓝牙常用API</title><link>https://apolla.cc/posts/bluetooth/2%E8%93%9D%E7%89%99%E5%B8%B8%E7%94%A8api/</link><pubDate>Sat, 09 Mar 2024 23:21:58 +0800</pubDate><guid>https://apolla.cc/posts/bluetooth/2%E8%93%9D%E7%89%99%E5%B8%B8%E7%94%A8api/</guid><category domain="https://apolla.cc/categories/%E8%93%9D%E7%89%99/">蓝牙</category><description>&lt;p&gt;上篇讲了蓝牙相关的基本概念，现在结合代码讲讲具体实现。首先需要创建一个CBCentralManager对象，检查蓝牙的状态，只有蓝牙为poweredOn状态，才可进行下一步连接动作。&lt;/p&gt;&#10;&lt;h3 class="heading-element" id="cbcentralmanager-初始化"&gt;&lt;span&gt;CBCentralManager 初始化&lt;/span&gt;&#10; &lt;a href="#cbcentralmanager-%e5%88%9d%e5%a7%8b%e5%8c%96" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;//先导入`CoreBluetooth` 框架&#10; var centralManager: CBCentralManager!&#10; //设置deleagte、queue、可选参数options，options可以为nil,也可设置CBCentralManagerOptionShowPowerAlertKey蓝牙提醒弹窗和CBCentralManagerOptionRestoreIdentifierKey字符串(一个唯一的标识符，用来恢复蓝牙连接的，在后台的长连接中可能用到)&#10; override init() {&#10; super.init()&#10; &#10; self.centralManager = CBCentralManager(delegate: self, queue: nil)&#10; }&#10; &#10; init(stateRestoreIdentifier: String) {&#10; super.init()&#10; &#10; self.centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: stateRestoreIdentifier])&#10; }&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="主要api的使用"&gt;&lt;span&gt;主要API的使用&lt;/span&gt;&#10; &lt;a href="#%e4%b8%bb%e8%a6%81api%e7%9a%84%e4%bd%bf%e7%94%a8" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt; // 获取已连接的设备&#10; open func retrieveConnectedPeripherals(withServices serviceUUIDs: [CBUUID]) -&amp;gt; [CBPeripheral]&#10; // 扫描设备(serviceUUIDs:需要扫描的设备serviceUUID数组，设置为nil,则扫描附近所有蓝牙设备，通常为了提高效率，填入特定的serviceUUID数组)&#10; open func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String : Any]? = nil)&#10; // 停止扫描&#10; open func stopScan()&#10; // 连接设备&#10; open func connect(_ peripheral: CBPeripheral, options: [String : Any]? = nil)&#10; // 断开连接&#10; open func cancelPeripheralConnection(_ peripheral: CBPeripheral)&lt;/code&gt;&lt;/pre&gt;&lt;h3 class="heading-element" id="cbcentralmanagerdelegate的回调"&gt;&lt;span&gt;CBCentralManagerDelegate的回调&lt;/span&gt;&#10; &lt;a href="#cbcentralmanagerdelegate%e7%9a%84%e5%9b%9e%e8%b0%83" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;// 蓝牙状态变更&#10; func centralManagerDidUpdateState(_ central: CBCentralManager) {&#10; switch central.state.rawValue {&#10; case 0: // .unknown&#10; &#10; case 1: // .resetting&#10; &#10; case 2: // .unsupported&#10; &#10; case 3: // .unauthorized&#10; &#10; case 4: // .poweredOff(蓝牙关闭)&#10; &#10; case 5: // .poweredOn(蓝牙打开)&#10; &#10; default:&#10; fatalError(&amp;#34;Unsupported BLE CentralState&amp;#34;)&#10; }&#10; }&#10;&#10;// 扫描到设备 advertisementData：广播包数据 RSSI：信号强度&#10; func centralManager(_ central: CBCentralManager,&#10; didDiscover peripheral: CBPeripheral,&#10; advertisementData: [String: Any],&#10; rssi RSSI: NSNumber) {&#10; &#10; }&#10;// 设备成功连接&#10; func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {&#10; &#10; }&#10;&#10;// 设备连接失败&#10;optional func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {&#10;&#10;}&#10;// 设备连接断开&#10; func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {&#10;&#10;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;与设备连接成功后，会生成一个CBPeripheral对象，用来操作设备数据的读写和监听等。&lt;/p&gt;</description></item></channel></rss>