Sending Data via BLE between IOS Swift App and ESP32

I have an problem. I want an App that sent data to an ESP32 via BLE. I want to send Text.

The problem is that when i use the App BLE Scanner 4.0 from the App Store i can succsessfully send Text to the ESP32 but when I try to this with my on App it does not work. It does not connect proberly but when i go to settings I am already connected.

Here is the Code from the App:

import SwiftUI
import CoreBluetooth

struct ContentView: View {
    let bleManager = BLEManager()
    @State var message: String = ""
    
    var body: some View {
        VStack {
            TextField("Enter message", text: $message)
                .padding(.horizontal)
            
            Button(action: {
                bleManager.sendText(message)
            }, label: {
                Text("Send")
            })
            .padding()
        }
    }
}

class BLEManager: NSObject, CBCentralManagerDelegate {
    
    var centralManager: CBCentralManager!
    var peripheral: CBPeripheral!
    let SERVICE_UUID = CBUUID(string: "8c2a81f7-f8b8-4b31-89b4-6b5d98a822db")
    let CHARACTERISTIC_UUID = CBUUID(string: "e8bd8c82-2506-4fae-b5f2-9bbbf4ab5b0e")

    override init() {
        super.init()
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            central.scanForPeripherals(withServices: nil, options: nil)
            print("Scanning...")
        } else {
            print("Bluetooth not available.")
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        self.peripheral = peripheral
        self.peripheral.delegate = self
        central.connect(self.peripheral, options: nil)
        print("Connecting...")
    }
    
    func sendText(_ text: String) {
        guard let peripheral = peripheral,
              let characteristic = peripheral.services?.first?.characteristics?.first(where: { $0.uuid == CHARACTERISTIC_UUID })
        else {
            print("Peripheral or characteristic not found")
            return
        }
        
        print("Sending: \(text)")
        let data = text.data(using: .utf8)!
        peripheral.writeValue(data, for: characteristic, type: .withResponse)
    }
}

extension BLEManager: CBPeripheralDelegate {

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected!")
        peripheral.discoverServices([SERVICE_UUID])
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        guard let services = peripheral.services else { return }
        for service in services {
            peripheral.discoverCharacteristics(nil, for: service)
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        guard let characteristics = service.characteristics else { return }
        for characteristic in characteristics {
            if characteristic.uuid == CHARACTERISTIC_UUID {
                print("Found characteristic!")
            }
        }
    }
}

When I test it it tries to connect and after few seconds it says connected. But immediately after that that error comes up: test ble[10829:2947007] [CoreBluetooth] API MISUSE: Forcing disconnection of unused peripheral <CBPeripheral: 0x2833704e0, identifier = EB3D29CC-47B6-C344-8C9E-4426B68EBB62, name = Siuuu, mtu = 527, state = connected>. Did you forget to cancel the connection? –

Does anyone have an idea ? Thank you already...

Sending Data via BLE between IOS Swift App and ESP32
 
 
Q