kriOSDeveloper
kriOSDeveloper

Reputation: 101

iOS/Swift/CoreBluetooth Ble Notifications not working

I have a problem where I connect to a peripheral and set notify value to true correctly but I can't get the value. I am following the next steps.

1 - I connect to peripheral

2 - I discover services

3 - I discover characteristics for services

4 - I activate notifications for a specific characteristic

override func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
         if characteristic.uuid == historicCharacteristicCBUUID && characteristic.properties.contains(.notify) {
                        print("\(characteristic.uuid): properties contains .notify")
                        if hasWritenOnHistoric == false {
                            peripheral.setNotifyValue(true, for: characteristic)
                        }
                    } 
}

5 - I wait to the delegate and then write on the characteristic to tell the peripheral to start sending data

override func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
        print(characteristic)
        if error == nil {
            print("TRUE")
            if characteristic.uuid == historicCharacteristicCBUUID && characteristic.isNotifying == true {
                writeOnHistory(peripheral: peripheral, characteristic: characteristic)
            } else {
                print("no ha cambiado su estado")
            }
        }
    }

6 - Write on peripheral

func writeOnHistory(peripheral: CBPeripheral, characteristic: CBCharacteristic) {
        if characteristic.uuid == historicCharacteristicCBUUID {
            hasWritenOnHistoric = true
            var bitesArray: [UInt8] = [0x01, 0x05]

            for _ in 1...16 {
                bitesArray.append(0x00)
            }

            print(bitesArray.count)
            print(bitesArray)
            let data = Data(bytes: bitesArray)
            peripheral.writeValue(data, for: characteristic, type: .withResponse)
        }
    }

7 - This delegate is only called once and with empty data [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00](in Android is working fine getting all the data from the peripheral)

override func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
            if let data = characteristic.value {
            print(data)
            }
        }

Is there something I am missing?

Thanks in advance!

Upvotes: 1

Views: 2443

Answers (1)

serdar aylanc
serdar aylanc

Reputation: 1347

You will not observe if your value is changed unless you call didWriteValueFor.

 func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { 
        if characteristic.uuid == myCharacteristic {
            self.bluetoothManager.readValueForCharacteristic(characteristic: myCharacteristic)

        }
 }

Upvotes: 1

Related Questions