Jatin Kumar
Jatin Kumar

Reputation: 1

Bluetooth disconnection issue with PicoX 1 device:Error Domain=CBErrorDomain Code=0 "Unknown error." UserInfo={NSLocalizedDescription=Unknown error

I'm encountering a recurring disconnection issue when trying to connect to a PicoX 1 device using CoreBluetooth in iOS Swift. The disconnection occurs automatically in every 5-6 second with the following error message:

Error Domain=CBErrorDomain Code=0 "Unknown error." UserInfo={NSLocalizedDescription=Unknown error.}

Heres code of pices :

@IBAction func btnScanClick( sender: Any) {
        print("scan Start")
        centralManager.scanForPeripherals(withServices: [ParticlePeripheral.testUUID],
                                          options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
        DispatchQueue.main.asyncAfter(deadline: .now() + 30.0) {
            if self.centralManager.isScanning{
                self.centralManager.stopScan()
                print("Scanning stop")
            }
            
        }
    }

  // Handles the result of the scan
    func centralManager( _ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
       
        centralManager.stopScan()
        print("Discovered \(peripheral.name ?? "")")
 self.centralManager.connect(peripheral, options: nil)
        
    }
    
    func centralManagerDidUpdateState( _ central: CBCentralManager) {
        switch central.state {
        case .unknown:
            print("central.state is .unknown")
        case .resetting:
            print("central.state is .resetting")
        case .unsupported:
            print("central.state is .unsupported")
        case .unauthorized:
            print("central.state is .unauthorized")
        case .poweredOff:
            print("central.state is .poweredOff")
        case .poweredOn:
            print("central.state is .poweredOn")
            self.btnScanClick(sender: self) // If we're powered on, start scanning
            
        @unknown default:
            fatalError()
        }
    }
    

 func centralManager(_ central: CBCentralManager,  didConnect peripheral: CBPeripheral){
        print("Connected to "+peripheral.name!)
        peripheral.delegate = self
        peripheral.discoverServices(nil)
       
        print("Connected to your Particle Board")
        peripheral.discoverServices([ParticlePeripheral.testUUID])
     
    }

 // Handles discovery event
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        guard let services = peripheral.services else { return }
        
        for service in services {
            
            print("service.characteristics :", service.characteristics as Any)
            print("service.uuid :", service.uuid)
            peripheral.discoverCharacteristics(nil, for: service)
            
        }
        
        print(error)
        
    }


// Handling discovery of characteristics
    func peripheral(_ peripheral: CBPeripheral,didDiscoverCharacteristicsFor service: CBService,error: Error?){
        if let characteristics = service.characteristics {
            for characteristic in characteristics {
                
                print("characteristic found >> : ",characteristic)
                print("characteristic.properties found >> : ",characteristic.properties)
                
                
                if characteristic.uuid == ParticlePeripheral.BATTERY_LEVEL_CHAR_UUID {
                    print("BATTERY_LEVEL_CHAR_UUID characteristic found")
                    peripheral.readValue(for: characteristic)
                    peripheral.setNotifyValue(true, for: characteristic)
                }else if characteristic.uuid == ParticlePeripheral.RX_CHARACTERISTIC_UUID{
                    print("RX_CHARACTERISTIC_UUID characteristic found")
                    peripheral.readValue(for: characteristic)
                    peripheral.setNotifyValue(true, for: characteristic)
                }else if characteristic.uuid == ParticlePeripheral.TX_CHARACTERISTIC_UUID{
                    print("TX_CHARACTERISTIC_UUID characteristic found")
                    peripheral.readValue(for: characteristic)
                    peripheral.setNotifyValue(true, for: characteristic)
                }
            }
        }
        
        print(error)
    }
    

Device is connected successfully but after connect it disconnected automatically after 5 second. i'm trying to solve the error i.e. Error Domain=CBErrorDomain Code=0 "Unknown error." UserInfo={NSLocalizedDescription=Unknown error.} but still facing the same error.

Upvotes: 0

Views: 59

Answers (0)

Related Questions