1

I am working on an app where i need to have a bluetooth connection with external device and i successfully made connection with external device.

Now, in below CBPeripheralManager delegate generate error in iOS 10 and working perfectly in iOS 9.0

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {

    if serviceCharacteristics.keys.contains(service.UUID) {
        guard let characteristics = service.characteristics else { return }

        peripheral.setNotifyValue(true, forCharacteristic: characteristics[0])

}

I am getting error in the below line,

peripheral.setNotifyValue(true, forCharacteristic: characteristics[0])

It says that CBPeripheral has no member setNotifyValue. I don't get it. I did some workaround but not getting any help.

Kirti Parghi
  • 1,142
  • 3
  • 14
  • 31

2 Answers2

2

Are you using Swift 3? The method signature has changed to setNotifyValue(_:for:), i.e.:

peripheral.setNotifyValue(true, for: characteristics[0])
Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
  • Nice. Quicker on the draw. ;) – davidethell Dec 09 '16 at 13:18
  • 1
    Do you see how similar our answers are? We must have similarly wired brains. – Lars Blumberg Dec 09 '16 at 13:19
  • Earlier, i did code in swift 2.3 now when i change deployment target and try to run my app i am getting above error. I did below code in delegate method if #available(iOS 10.0, *) { peripheral.setNotifyValue(true, for: characteristics[0]) } else { peripheral.setNotifyValue(enabled:true, characteristic: characteristics[0]) } – Kirti Parghi Dec 09 '16 at 13:56
  • Your error has nothing to do with which version of iOS you're using. You need to either set your Swift version to Swift 2.3 or your convert your code to Swift 3. – Lars Blumberg Dec 09 '16 at 14:00
  • I have set use legacy swift language version to YES so it will but obviously use swift 2.3 earlier one so if it has nothing to do with iOS version then why it is not working when i change deployment target to 10.0. I am trying to figure out it. Let me know if you have any suggestion regarding it – Kirti Parghi Dec 09 '16 at 14:08
  • That sounds indeed strange! Let us know when you found the solution. – Lars Blumberg Dec 09 '16 at 16:12
0

Did you migrate to swift 3? The new syntax uses "for" instead of "forCharacteristic":

peripheral.setNotifyValue(true, for: characteristics[0])
davidethell
  • 11,708
  • 6
  • 43
  • 63