gpt4 book ai didi

ios - 无法写入或读取 CBPeripheral | CBMutableCharacteristic

转载 作者:行者123 更新时间:2023-11-28 12:52:35 24 4
gpt4 key购买 nike

我想连接到蓝牙硬件,但我无法收到通知,当值更新时,读取读取,这是我的代码:

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,它可以工作,但我无法让通知工作......

最佳答案

当您充当中心时,您不会创建新的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!)
}
}
}
}
}

另外,您需要确认您的特征是否支持通知。

关于ios - 无法写入或读取 CBPeripheral | CBMutableCharacteristic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com