2017-0215 最近项目需要,要做个BLE的上位机程序。开发平台Xcode 8.2.1 目标设备 iphone 和 ipad 系统 ios 7.0+,
功能: BLE 设备搜索,连接,数据写入读取,属性读取
语言: Swift 3.0
2017-0323更新,预设功能已实现,项目已传到Github上,下面贴上开发文档。
Github地址: https://github.com/DennisMao/eswiftBLE
文档资料
官方文档:
https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothOverview/CoreBluetoothOverview.html#//apple_ref/doc/uid/TP40013257-CH2-SW19
引用博文:
http://blog.csdn.net/chenli522/article/details/17114417 作者:jimoduwu
开发过程:
介绍
本文档介绍了如何使用CoreBluetooth开发蓝牙中心模式的APP,实现蓝牙BLE搜索、连接、断开、接收和发送数据等操作。外设模式在本文档不讨论。
中心模式: 即手机作为中心,蓝牙模块作为外设。手机主动发送连接、断开和数据读写请求。
外设模式: 即手机配置为一个蓝牙外设,可以被动接受和处理其他蓝牙设备的请求信息。
语言:Swift 3.0
开发平台: Xcode 7.1
调试平台: iphone6s ios 10.1
Github地址: https://github.com/DennisMao/eswiftBLE
Github上已上传了个较完整的Demo,带有中文注释,可参考
框架
主要函数
CBPeripheral 外设类 (扫描服务、属性,读写数据,设置Notify,读取信号)
属性:
- identifier: 设备的UUID信息
- name: 设备名
- delegate: 委托函数
功能操作:
搜索
- discoverServices 搜索服务,调用该函数会扫描该服务的全部信息,比如属性和描述
- discoverIncludeServices 获取指定服务,该函数可以限定搜索含有特定UUID的服务
- services 可以理解为数组,里面包含了多个服务信息
- discoverCharactistic 搜索属性,调用该函数会搜索某个服务下特定属性的全部信息,比如描述和各个值。
- discoverDescripter 搜索描述符,调用该函数会搜索某个属性下的特定的描述符
读写操作
- readValue 读取一个属性或者描述符的数值,若读取成功会在委托里响应,对应函数
func peripheral(CBPeripheral, didUpdateValueFor: CBCharacteristic, error: Error?)
- writeValue 写入一个数值到属性或者描述符,若写入成功会在委托里响应,
func peripheral(_ peripheral: CBPeripheral,
didWriteValueFor characteristic: CBCharacteristic,
error: Error?)
- readRSSI 读取当前信号值,若读取成功会返回如下响应
func peripheralDidUpdateRSSI(CBPeripheral, error: Error?)
```
设置Notify
+ setNotifyValue 设置Notify开关,调用后会在委托函数中响应,可查看是否开启/关闭成功
func peripheralDidUpdateRSSI(CBPeripheral, error: Error?)
#### CBCentralManger 中心管理器(设备扫描,连接,硬件状态)
#####功能操作:
**搜索**
+ scanForPheripherals 搜索外设&#