核心蓝牙:在WriteValue()之后未调用didWriteValueFor()

原学程将引见焦点蓝牙:在WriteValue()以后未挪用didWriteValueFor()的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

核心蓝牙:在WriteValue()之后未调用didWriteValueFor() 教程 第1张

成绩描写

依据Apple开辟职员文档,函数didWriteValueFor()是在挪用WriteValue()函数以后挪用的。(拜见https://developer.apple.com/documentation/corebluetooth/cbperipheraldelegate/一五一88二三-peripheral)

我有1个可写的特点,我查找了https://developer.apple.com/documentation/corebluetooth/cbcharacteristicproperties/一五一九08九-write中提到的属性

如今,当我挪用WriteValue()函数时,历来出有挪用过didWriteValueFor()函数,为何?我以为它的构造与挪用didUpdateValueFor()函数的readValue()函数雷同,这对于我去说任务患上很佳。
以下是我的代码:

  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
 for characteristic in service.characteristics!{
  print(characteristic)
  if(characteristic.uuid == TX_CHARACTERISTIC){
elsa.writeValue(dataWithHexString(hex: VALID_GET_VERSION_REQUEST), for: characteristic, type: CBCharacteristicWriteType.withResponse)//calls didWriteValueFor if Type = withResponse
  }
  if(characteristic.uuid == RX_CHARACTERISTIC){
elsa.setNotifyValue(true, for: characteristic)//calls didUpdateNotificationStateFor
  }
 }
}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
 guard let data = characteristic.value else { return }
 print("
Value: (data.toHexEncodedString()) 
was written to Characteristic:
(characteristic)")
 if(error != nil){
  print("
Error while writing on Characteristic:
(characteristic). Error Message:")
  print(error as Any)
 }
}

推举谜底

如@Paulw一一所述,只要在显式读与该值以后才会写进该值。我经由过程在didWite()函数中挪用1个readValue()修复了这个成绩。

革新的处理计划:

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
 elsa.readValue(for: characteristic)
 guard let data = characteristic.value else { return }
 print("
Value: (data.toHexEncodedString()) 
was written to Characteristic:
(characteristic)")
 if(error != nil){
  print("
Error while writing on Characteristic:
(characteristic). Error Message:")
  print(error as Any)
 }
}

佳了闭于焦点蓝牙:在WriteValue()以后未挪用didWriteValueFor()的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。