swift链式调用蓝牙框架
特点:
- 链式调用;
- 函数有序,例如我们需要在扫描连接设备后,正常情况下,通过代理获取连接成功后的状态,但是现在只需要:
let blue = Blue()
blue.scan(discoverSave: { (_,p,data,_) in
if p.name == "YUNMAI-SIGNAL-CW" {
//过滤外设,返回你要搜索到的外设
return p
}
return nil
}).connect().discoverServices([CBUUID.init(string: "FFE0"),CBUUID.init(string: "FFE5")], forServices: { (_, service) -> (CBService) in
//返回你要发现特征的服务
return service
}).discoverCharacteristics(readFor: { (characteristic) -> (CBCharacteristic?) in
return nil
}, setNotify: { (characteristic) -> (CBCharacteristic?) in
if characteristic.uuid.uuidString == "FFE4" {
//返回你要监听的特征
return characteristic
}
return nil
}, writeFor: { (characteristic) -> (CBCharacteristic?) in
if characteristic.uuid.uuidString == "FFE9" {
return characteristic
}
return nil
}).stopScan().update { [weak self](_,data) in
guard let data = data else {return}
//得到监听和读取特征的value
}
let blue = Blue()
blue.scan(discoverSave: { (_,p,data,_) in
if p.name == "YUNMAI-SIGNAL-CW" {
//过滤外设,返回你要搜索到的外设
return p
}
return nil
}).connect().stopScan()
安装
CocoaPod
SwiftFFDB 可以通过Cocoapod集成到你的工程中:
$ vim Podfile
在podfile中增加下面的内容:
platform :ios, '8.0'
target 'YouApp' do
use_frameworks!
pod 'Blue'
end