介绍
Swift 编写的蓝牙 SDK 和示例
教程
步骤 1: 集成
- 使用 CocoaPods:
pod 'GengziRat'
- 将项目中的 Products 目录拖拽到您的项目中,然后下载 https://github.com/apple/swift-protobuf.git 库文件并将它添加到您的项目中。
步骤 2: UUIDs
使用 CoreBluetooth 创建 CentralManager 以与外围设备进行通信。我们设备的 UUID 信息如下
struct PROTOBUF_UUID_STR {
static let serviceUuid:String = "2E8C0001-2D91-5533-3117-59380A40AF8F"
static let notifyUuid:String = "2E8C0002-2D91-5533-3117-59380A40AF8F"
static let writeUuid:String = "2E8C0003-2D91-5533-3117-59380A40AF8F"
}
步骤 3: 构造实例
-
- 创建实例
-
- 强返回 Peripheral & CBCharacteristic
//1.
var selectedPeripheral: CBPeripheral?
var readCharacter: CBCharacteristic?
var writeCharacter: CBCharacteristic?
let protobufIns = BLEProtobuf.init()
//2.
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
...
writeCharacter = character
...
}
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
selectedPeripheral = peripheral
readCharacter = characteristic
os_log("didUpdateNotificationStateFor: %@ - %@", peripheral, characteristic)
}
步骤 4:读取设备信息
-
- 为protobufIns构建配置结构并写入外部设备数据
-
- 实现回调方法接收数据
//Cmd
let data = protobufIns.getDeviceInfo()
selectedPeripheral?.writeValue(data, for: writeCharacter!, type: CBCharacteristicWriteType.withoutResponse)
//CallBack
func bleProtobufDidRecieveDeviceInfo(deviceInfo: DeviceInfoResponse) {
print("bleProtobufDidRecieveDeviceInfo \(deviceInfo)")
}
步骤 5:同步数据
- 实时数据
//cmd
let data = protobufIns.getRealTimeData()
selectedPeripheral?.writeValue(data, for: writeCharacter!, type: CBCharacteristicWriteType.withoutResponse)
//callback
func bleProtobufDidRecieveRealTimeData(rtData: RtHealth) {
print("bleProtobufDidRecieveRealTimeData \(rtData)")
}
- 历史记录
- 1. 获取索引表数据
- 2. 获取详细历史数据
//cmd indextable
guard let data = pVC?.protobufIns.getSyncDataIndexTable(type: HisDataType.healthData) else {
return
}
let writeCharacter:CBCharacteristic = pVC!.writeCharacter!
pVC?.selectedPeripheral?.writeValue(data, for: writeCharacter, type: CBCharacteristicWriteType.withoutResponse)
//cmd history data
let data = pVC!.protobufIns.getStartSync(type: HisDataType.healthData, blocks: self.getHisBlockArray())
let writeCharacter:CBCharacteristic = pVC!.writeCharacter!
pVC?.selectedPeripheral?.writeValue(data, for: writeCharacter, type: CBCharacteristicWriteType.withoutResponse)
func getHisBlockArray() -> Array<HisBlock> {
var hisBk = HisBlock.init()
hisBk.startSeq = 0
hisBk.endSeq = 100
return [hisBk]
}
//callback indextable
func bleProtobufDidRecieveDataIndexTable(type: HisDataType, indexTable: HisIndexTable) {
print("bleProtobufDidRecieveDataIndexTable \(type) \(indexTable)")
}
//callback history data
func bleProtobufDidRecieveData(type: HisDataType, hisData: HisData) {
print("bleProtobufDidRecieveData \(type) \(hisData)")
}
步骤 6:设备配置
- 电机感觉
var mv:MotorVibrate = MotorVibrate.init()
mv.mode = MotorShakeWay.Light
mv.round = 3
let data = protobufIns.getMotorConf(vCnf: mv)
selectedPeripheral?.writeValue(data, for: writeCharacter!, type: CBCharacteristicWriteType.withoutResponse)
- 电机配置
var mc:MotorConf = MotorConf.init()
var vc:VibrateCnf = VibrateCnf.init()
vc.mode = MotorShakeWay.Light
vc.type = VibrateType.sms
vc.round = 5
mc.conf = [vc]
let mData = protobufIns.getMotorConf(motorConf: mc)
selectedPeripheral?.writeValue(mData, for: writeCharacter!, type: CBCharacteristicWriteType.withoutResponse)
待续