所以我正在关注http://www.raywenderlich.com/85900/arduino-tutorial-integrating-bluetooth-le-ios-swift
中的这个iOS App代码示例以下代码取自上面的链接,为服务注册了一个特征。您将如何为一个服务注册多个特征,以便使用Swift可以读取和写入它们?
/* Services & Characteristics UUIDs */
let BLEServiceUUID = CBUUID(string: "025A7775-49AA-42BD-BBDB-E2AE77782966")
let PositionCharUUID = CBUUID(string: "F38A2C23-BC54-40FC-BED0-60EDDA139F47")
let BLEServiceChangedStatusNotification = "kBLEServiceChangedStatusNotification"
.
.
.
.
.
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
let uuidsForBTService: [CBUUID] = [PositionCharUUID]
if (peripheral != self.peripheral) {
// Wrong Peripheral
return
}
if (error != nil) {
return
}
if ((peripheral.services == nil) || (peripheral.services.count == 0)) {
// No Services
return
}
for service in peripheral.services {
if service.UUID == BLEServiceUUID {
peripheral.discoverCharacteristics(uuidsForBTService, forService: service as CBService)
}
}
}
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
if (peripheral != self.peripheral) {
// Wrong Peripheral
return
}
if (error != nil) {
return
}
for characteristic in service.characteristics {
if characteristic.UUID == PositionCharUUID {
self.positionCharacteristic = (characteristic as CBCharacteristic)
peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
// Send notification that Bluetooth is connected and all required characteristics are discovered
self.sendBTServiceNotificationWithIsBluetoothConnected(true)
}
}
提前致谢!
答案 0 :(得分:3)
我认为它就像在for循环中添加更多if语句一样简单。像这样:
let characteristicOneUUID= CBUUID(string: "025A7775-49AA-42BD-BBDB-E2AE77782966")
let characteristicTwoUUID= CBUUID(string: "F38A2C23-BC54-40FC-BED0-60EDDA139F47")
for characteristic in service.characteristics {
//Characteristic One
if characteristic.UUID == characteristicOneUUID{
peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
}
//Characteristic Two
if characteristic.UUID == characteristicTwoUUID{
peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
}
}