关于 corebluetooth
面向 Apple Core Bluetooth 开发的参考技能,涵盖中央设备与外围设备角色、操作流程、API 查询及生命周期限制,帮助开发者按角色规划 BLE 操作并遵循相关实践。
主要功能
- 01中央设备工作流指南
- 02外围设备工作流指南
- 03BLE API 与符号查询
- 04后台处理与状态恢复指南
- 05角色相关的最佳实践
适用场景
- 01构建扫描并连接外围设备的应用
- 02配置应用作为外围设备广播
- 03处理蓝牙后台行为与状态恢复
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ nonameplum/ agent-skills --skill corebluetooth安装后在 Claude Code 中输入/corebluetooth确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AboutCoreBluetooth.mdBestPracticesforInteractingwithaRemotePeripheralDevice.mdBestPracticesforSettingUpYourLocalDeviceasaPeripheral.mdBluetoothStateRestorationAppReaunchRules.mdcorebluetooth.mdCoreBluetoothBackgroundProcessingforiOSApps.mdCoreBluetoothOverview.mdPerformingCommonCentralRoleTasks.mdPerformingCommonPeripheralRoleTasks.md详细介绍
Core Bluetooth
打开什么内容
- 使用
corebluetooth/AboutCoreBluetooth.md和corebluetooth/CoreBluetoothOverview.md了解概念和角色定位。 - 使用
corebluetooth/PerformingCommonCentralRoleTasks.md了解分步执行的中心角色工作流。 - 使用
corebluetooth/PerformingCommonPeripheralRoleTasks.md了解分步执行的外围设备角色工作流。 - 使用
corebluetooth/BestPracticesforInteractingwithaRemotePeripheralDevice.md和corebluetooth/BestPracticesforSettingUpYourLocalDeviceasaPeripheral.md了解常见问题和最佳实践。 - 使用
corebluetooth/CoreBluetoothBackgroundProcessingforiOSApps.md了解后台模式和生命周期限制。 - 使用
corebluetooth/BluetoothStateRestorationAppReaunchRules.md了解状态恢复时的应用重新启动规则。 - 使用
corebluetooth/corebluetooth.md快速查阅 API 和查找符号。
工作流
- 确定应用是充当中心设备、外围设备,还是两者兼任。
- 等待管理器状态变为
poweredOn后再执行 BLE 操作。 - 遵循相应角色的检查清单,确保发现和连接顺序正确。
- 首先查看相应角色的任务指南和最佳实践;准确的方法签名请查阅 API 参考。
中心设备检查清单
- 创建带有委托对象和队列的
CBCentralManager。 - 处理
centralManagerDidUpdateState(_:),并确保仅在状态为.poweredOn时扫描。 - 使用
scanForPeripherals(withServices:options:)扫描,并在找到目标后停止扫描。 - 连接设备,设置
CBPeripheral委托对象,然后发现服务和特征。 - 按需读取、写入特征,或订阅特征通知。
外围设备检查清单
- 创建带有委托对象和队列的
CBPeripheralManager。 - 等待状态变为
.poweredOn。 - 定义服务和特征,然后将其添加到管理器。
- 使用服务 UUID 和可选的本地名称开始广播。
- 响应读取和写入请求;向已订阅的中心设备发布更新。
提醒
- 保留发现的
CBPeripheral实例,避免其被释放。 - 流式传输数据时使用通知;仅当
canSendWriteWithoutResponse为 true 时才使用无响应写入。 - 仅当用例不适合使用 GATT 特征时才使用 L2CAP。
评论
支持 Markdown