我正在连接一个蓝牙硬件,但是我得不到通知,当一个值被更新时,我的代码如下:
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
guard peripheral == self.peripheral && error == nil else {
return
}
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.UUID == QuadrarCharacteristicUUID {
if let peripheral = self.peripheral {
self.quadrarCharacteristic = CBMutableCharacteristic(type: QuadrarCharacteristicUUID,
properties: [.Read, .Write, .Notify],
value: nil,
permissions: [.Readable, .Writeable])
self.quadrarCharacteristic?.descriptors = characteristic.descriptors
peripheral.setNotifyValue(true, forCharacteristic: self.quadrarCharacteristic!)
}
}
}
}
}当我写的时候:
func writeBytes() {
self.quadrarCharacteristic!.service.peripheral.writeValue(self.bytesToWriteArray[self.senderCounter],
forCharacteristic: self.quadrarCharacteristic!, type: CBCharacteristicWriteType.WithResponse)
}我知道,即使是写作也行不通因为
func peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)没有被称为..。但是如果我只使用
var quadrarCharacteristic: CBCharacteristic?试着写它,作为一个非CBMutableCharacteristic,它可以工作,但我不能让通知工作…
发布于 2016-03-15 07:12:06
当您充当中心时,您不需要创建新的CBMutableCharacteristic,只需使用didDiscoverCharacteristicsForService中提供给您的CBCharacteristic对象-
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
guard peripheral == self.peripheral && error == nil else {
return
}
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.UUID == QuadrarCharacteristicUUID {
self.quadrarCharacteristic=charateristic
peripheral.setNotifyValue(true, forCharacteristic: self.quadrarCharacteristic!)
}
}
}
}
}此外,您还需要确认您的特征是否支持通知。
https://stackoverflow.com/questions/35997111
复制相似问题