惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - 朝阳向日葵

AI辅助工作实践报告 Claude Code介绍 AI使用相关 报错处理 iOS IPA体积优化 iOS开发 重要通知(critical-alerts) 关于GPT工具的操作说明 React Native之React基础 React Native之JSX语法 使用TestFlight安装ios SwichBot的测试版本 React Navite环境搭建 iOS 防止charles抓包 iOS Charles抓包 转正答辩 iOS设备和模块解耦-方案对比 iOS开发 性能优化 iOS开发 调试技巧 设计模式 项目架构 iOS 常用第三方库及原理
蓝牙
朝阳向日葵 · 2025-05-21 · via 博客园 - 朝阳向日葵

在 iOS 开发中,Bluetooth(蓝牙)功能主要通过 CoreBluetooth 框架实现。CoreBluetooth 是 iOS 自带的蓝牙通信框架,常用于 BLE(Bluetooth Low Energy)设备的通信。以下是对 iOS Bluetooth 开发的详细讲解,包括基础概念、开发流程以及常见问题。


一、基础概念

1. BLE(蓝牙低功耗)

  • BLE 是蓝牙的一种协议,专注于低功耗设备间的数据通信。
  • BLE 的工作模式主要有两种:
    • Central(中心设备): 负责扫描和连接外围设备。
    • Peripheral(外围设备): 负责广播数据,供中心设备发现和连接。

2. CoreBluetooth 框架

CoreBluetooth 是 iOS 提供的蓝牙框架,支持 BLE 功能。它主要包括以下核心类:

  • CBCentralManager: 用于管理中心设备,扫描和连接外围设备。
  • CBPeripheral: 表示连接的外围设备,负责与设备进行通信。
  • CBService: 外围设备提供的服务(功能集合)。
  • CBCharacteristic: 服务中的特性,表示具体的数据或操作的接口。

二、开发流程

1. 初始化蓝牙中心设备(Central)

中心设备负责扫描和连接蓝牙外围设备。

2. 扫描外围设备

使用 CBCentralManager 的 scanForPeripherals 方法扫描附近的 BLE 设备。

// 开始扫描设备 func startScanning() { centralManager.scanForPeripherals(withServices: nil, options: nil) } // 发现设备时的回调 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { print("发现设备: \(peripheral.name ?? "未知设备")") // 停止扫描并连接设备 centralManager.stopScan() centralManager.connect(peripheral, options: nil) }

3. 连接外围设备

当扫描到设备后,可以使用 connect 方法连接设备。

4. 发现服务和特性

外围设备通过服务和特性提供功能,连接后可以扫描服务和特性以获取具体数据。

5. 与特性交互

特性是服务的具体功能接口,可以通过特性读取数据或写入数据。


三、常见问题及解决方案

1. 蓝牙权限问题

在 iOS 13+,必须在 Info.plist 文件中声明蓝牙使用权限:

<key>NSBluetoothAlwaysUsageDescription</key> <string>需要访问蓝牙以连接设备</string>

如果不声明权限,应用可能无法正常扫描设备。

2. 蓝牙状态检查

确保蓝牙状态为 .poweredOn 才能开始扫描设备,否则可能会报错。

if centralManager.state != .poweredOn { print("蓝牙未开启,无法扫描设备") }

3. 特性交互失败

特性必须支持操作(如 read 或 write),才能与其交互。

if characteristic.properties.contains(.read) { peripheral.readValue(for: characteristic) } else { print("该特性不支持读取操作") }

4. 连接断开

设备可能会意外断开连接,需要处理断开事件并尝试重新连接。

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { print("设备断开连接: \(error?.localizedDescription ?? "未知原因")") // 尝试重新连接 centralManager.connect(peripheral, options: nil) }


四、最佳实践

  1. 优化扫描方式:
    • 使用 withServices 参数指定需要扫描的设备服务 UUID,减少扫描范围。
  2. 高性能写入特性:
    • 使用 .withoutResponse 写入特性数据时,可提高写入性能,但需确保设备支持无响应写入。
  3. 维护连接状态:
    • 监控设备的连接状态,并在断开时及时重连。
  4. 安全性:
    • 使用加密特性或设备认证机制保护蓝牙通信数据。