<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>蓝牙 - 宋明的博客</title><link>https://apolla.cc/posts/bluetooth/</link><description>宋明的博客</description><generator>Hugo 0.170.0 &amp; FixIt v1.0.0-rc.5-mwimukpl</generator><language>zh-cn</language><lastBuildDate>Sun, 10 Mar 2024 09:47:41 +0800</lastBuildDate><atom:link href="https://apolla.cc/posts/bluetooth/index.xml" rel="self" type="application/rss+xml"/><item><title>4：readValue、setNotifyValue、writeValue的含义和使用</title><link>https://apolla.cc/posts/bluetooth/4readvaluesetnotifyvaluewritevalue%E7%9A%84%E5%90%AB%E4%B9%89%E5%92%8C%E4%BD%BF%E7%94%A8/</link><pubDate>Sun, 10 Mar 2024 09:47:41 +0800</pubDate><guid>https://apolla.cc/posts/bluetooth/4readvaluesetnotifyvaluewritevalue%E7%9A%84%E5%90%AB%E4%B9%89%E5%92%8C%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/%E8%93%9D%E7%89%99/">蓝牙</category><description>&lt;p&gt;我们使用nRF Connect(一个调试蓝牙的第三方工具)先连接一个设备，可以看到设备相关信息，如下：&lt;/p&gt;&#10;&lt;p&gt;&lt;img loading="lazy" src='https://apolla.cc/images/284a273964c0ab86feba0789b80ad98c.png' alt="image.png"&gt;&#10;中标红色的就是特征值，代表相关服务的特性。特征值有三种，分别是readValue(读取)、setNotifyValue(订阅)、writeValue(写入)。 readValue说明是可读的，回调方法为&lt;/p&gt;</description></item><item><title>1 ：蓝牙连接流程</title><link>https://apolla.cc/posts/bluetooth/1-%E8%93%9D%E7%89%99%E8%BF%9E%E6%8E%A5%E6%B5%81%E7%A8%8B/</link><pubDate>Sat, 09 Mar 2024 23:21:58 +0800</pubDate><guid>https://apolla.cc/posts/bluetooth/1-%E8%93%9D%E7%89%99%E8%BF%9E%E6%8E%A5%E6%B5%81%E7%A8%8B/</guid><category domain="https://apolla.cc/categories/%E8%93%9D%E7%89%99/">蓝牙</category><description>&lt;p&gt;iOS系统中总共提供了4个框架用于实现蓝牙连接，分别如下：&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;1.GameKit.framework(用法简单) 只能用于iOS设备之间的同个应用内连接,多用于游戏(如:棋牌类),从iOS7开始过期&#10;2.MultipeerConnectivity.framework(代替GameKit.framework) 只能用于iOS设备之间的连接,从iOS7开始引入,主要用于非联网状态下,通过wifi或者蓝牙进行文件共享(仅限于沙盒的文件),多用于附近无网聊天&#10;3.ExternalAccessory.framework(MFi) 可用于第三方蓝牙设备交互,但是蓝牙设备必须经过苹果MFi认证&#10;4.CoreBluetooth.framework（常用 Apple推行蓝牙的核心)可用于第三方蓝牙设备交互,必须要支持蓝牙4.0。硬件至少是4s,系统至少是iOS6。蓝牙4.0以低功耗著称,一般也叫BLE（Bluetooth Low Energy）&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><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><item><title>3：Mac地址的获取及写入指令的处理</title><link>https://apolla.cc/posts/bluetooth/3mac%E5%9C%B0%E5%9D%80%E7%9A%84%E8%8E%B7%E5%8F%96%E5%8F%8A%E5%86%99%E5%85%A5%E6%8C%87%E4%BB%A4%E7%9A%84%E5%A4%84%E7%90%86/</link><pubDate>Sat, 09 Mar 2024 23:21:58 +0800</pubDate><guid>https://apolla.cc/posts/bluetooth/3mac%E5%9C%B0%E5%9D%80%E7%9A%84%E8%8E%B7%E5%8F%96%E5%8F%8A%E5%86%99%E5%85%A5%E6%8C%87%E4%BB%A4%E7%9A%84%E5%A4%84%E7%90%86/</guid><category domain="https://apolla.cc/categories/%E8%93%9D%E7%89%99/">蓝牙</category><description>&lt;h2 class="heading-element" id="mac地址的获取"&gt;&lt;span&gt;Mac地址的获取&lt;/span&gt;&#10; &lt;a href="#mac%e5%9c%b0%e5%9d%80%e7%9a%84%e8%8e%b7%e5%8f%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;/h2&gt;&lt;p&gt;iOS7之后，苹果不支持获取Mac地址，只能用UUID来标识设备。若想获得Mac地址，可以跟硬件工程师沟通，增加一个指令获取，也可以让硬件工程师把Mac地址写到广播包里，在扫描的时候获取Mac地址，如下所示：&lt;/p&gt;</description></item><item><title>5：蓝牙固件升级</title><link>https://apolla.cc/posts/bluetooth/5%E8%93%9D%E7%89%99%E5%9B%BA%E4%BB%B6%E5%8D%87%E7%BA%A7/</link><pubDate>Sat, 09 Mar 2024 23:21:58 +0800</pubDate><guid>https://apolla.cc/posts/bluetooth/5%E8%93%9D%E7%89%99%E5%9B%BA%E4%BB%B6%E5%8D%87%E7%BA%A7/</guid><category domain="https://apolla.cc/categories/%E8%93%9D%E7%89%99/">蓝牙</category><description>&lt;h1 class="heading-element" id="一为何要升级"&gt;&lt;span&gt;一.为何要升级&lt;/span&gt;&#10; &lt;a href="#%e4%b8%80%e4%b8%ba%e4%bd%95%e8%a6%81%e5%8d%87%e7%ba%a7" 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;/h1&gt;&lt;blockquote&gt;&#10;&lt;p&gt;蓝牙固件升级是使用手机给固件进行更新，以达到修复bug的目的，完善功能的作用。升级的大概流程是：&#10;1.当蓝牙设备的固件需要升级时，由嵌入式开发人员提供新的固件，由服务器管理人员将固件放到服务器上。&#10;2.用户打开手机 APP 的时候会检测到服务器有更新，请求更新蓝牙设备固件，确认更新后，手机会从服务器下载固件。&#10;3.下载完毕后，APP 会读取固件内容，并根据升级协议将内容传到蓝牙设备里，完成升级。&lt;/p&gt;&#10;&lt;/blockquote&gt;</description></item><item><title>6：SwiftyBluetooth的使用</title><link>https://apolla.cc/posts/bluetooth/6swiftybluetooth%E7%9A%84%E4%BD%BF%E7%94%A8/</link><pubDate>Sat, 09 Mar 2024 23:21:58 +0800</pubDate><guid>https://apolla.cc/posts/bluetooth/6swiftybluetooth%E7%9A%84%E4%BD%BF%E7%94%A8/</guid><category domain="https://apolla.cc/categories/%E8%93%9D%E7%89%99/">蓝牙</category><description>&lt;h1 class="heading-element" id="简介"&gt;&lt;span&gt;简介&lt;/span&gt;&#10; &lt;a href="#%e7%ae%80%e4%bb%8b" 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;/h1&gt;&lt;p&gt;&lt;code&gt;SwiftyBluetooth&lt;/code&gt;是&lt;code&gt;Swift&lt;/code&gt;中封装&lt;code&gt;CoreBluetooth&lt;/code&gt;框架的一个开源库&lt;a href="https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fjordanebelanger%2FSwiftyBluetooth" target="_blank" rel="external nofollow noopener noreferrer"&gt;SwiftyBluetooth&lt;i class="fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;,个人觉得比较好用，是对&lt;code&gt;CoreBluetooth&lt;/code&gt;的封装，可自行查看代码，下面来讲讲它的使用。&lt;/p&gt;</description></item></channel></rss>