1

I am writing an app that needs to write a byte value to a CBPeripheral using iOS CoreBluetooth. I am able to read values, and the write is successful and triggers the didWriteValueFor delegate like it should. Basically, it does work, BUT, it takes a long time (about 3 seconds and reports of over 10). When I test the write process in another app (nRF Connect), the write is almost instantaneous. Here's the code I am using to write to the Characteristic:

func setConfigurationValue(_ i:UInt8){

    guard peripheral != nil, configurationStateCharacteristic != nil else { return }
    var vint:UInt8 = i
    let intData = Data(bytes: &vint, count: MemoryLayout<UInt8>.size)

        isWriting = true
        peripheral?.writeValue(intData, for: configurationStateCharacteristic!, type: CBCharacteristicWriteType.withResponse)
        // Notify any views that they should show a progress overlay
        NotificationName.showProgressOverlay.post(userInfo: ["message":"Device configuring".localized], object: self)

}

Note, that peripheral is a stored reference to the CBPeripheral object, and configurationStateCharacteristic is a reference to the CBCharacteristic I am writing to. This code does work, but what is causing the peripheral BLE device to take so long in writing the data and sending a response?

Any ideas?

Lee Probert
  • 10,308
  • 8
  • 43
  • 70
  • Maybe the Time Profiler of Instruments Tools could help you find what's wrong. It maybe be related to your app slowing down (somewhere you are blocking a thread?) – Larme Aug 27 '18 at 10:45
  • The app was polling ever two seconds for a value off the board (there was no notify service), and this was causing the delay in writing and then reading the new value. There must have been some kind of cache of requests. Still not really sure what it was. – Lee Probert Sep 04 '18 at 12:11
  • @LeeProbert How did you fix the polling? I have the same issue – bogen Dec 30 '22 at 14:08
  • Sorry. This was so long ago. I honestly can't remember. – Lee Probert Jan 10 '23 at 09:47

0 Answers0