线程1:信号SIGABRT无法识别的选择器

时间:2017-08-01 08:12:56

标签: ios swift

我目前正在使用Xcode 8创建一个蓝牙聊天应用程序,它允许两个设备通过蓝牙相互连接,并相互发送和接收消息。以下是此代码。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBPeripheralManagerDelegate, CBCentralManagerDelegate, CBPeripheralDelegate {
    let messageUUID = CBUUID(string: "053D6600-821E-46A7-AC25-43A81D948E8B")
    let inputUUID = CBUUID(string: "49A79D26-5323-4374-81EA-29B099AF85C8")
    let otherUUID = CBUUID(string: "053D6600-821E-46A7-AC25-43A81D948E87")
    var peripheralManager: CBPeripheralManager!
    var characteritic: CBMutableCharacteristic!
    var getDataPeripheralCharacteristic: CBMutableCharacteristic!
    var service: CBMutableService!
    var outputData:String = ""
    var centralManager: CBCentralManager!
    var connectingPeripheral: CBPeripheral?
    var centralWriteCharacteristic: CBCharacteristic!

    @IBOutlet weak var inputLabel: UITextField!
    @IBOutlet weak var outputLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func centralMode(sender: AnyObject) {
        centralManager = CBCentralManager(delegate: self, queue: nil)
        peripheralManager = nil
    }

    @IBAction func peripheralMode(sender: AnyObject) {
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
        centralManager = nil
    }


    func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) {
        peripheral.stopAdvertising()
        let identifier = Bundle.main.bundleIdentifier!
        let manufacturerData = identifier.data(using: String.Encoding.utf8, allowLossyConversion: false)
        let dataToBeAdvertised:[String: Any] = [
            CBAdvertisementDataLocalNameKey : "Sample peripheral",
            CBAdvertisementDataManufacturerDataKey : manufacturerData as Any,
            CBAdvertisementDataServiceUUIDsKey : [messageUUID, inputUUID],
            ]

        peripheralManager.startAdvertising(dataToBeAdvertised)
    }


    @IBAction func sendData(_ sender: UIButton) {
        if (peripheralManager != nil) {
            let passed = peripheralManager.updateValue(inputLabel.text!.data(using: String.Encoding.utf8)!, for: characteritic!, onSubscribedCentrals: nil)
            if passed == false {
                print("error couldn't send data")
            }
        }
        if (centralManager != nil) {
            if (connectingPeripheral != nil) {
                connectingPeripheral?.writeValue(inputLabel.text!.data(using: String.Encoding.utf8)!, for: centralWriteCharacteristic, type: CBCharacteristicWriteType.withResponse)
            }
        }
    }


    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        if (peripheral.state == .poweredOn) {
            characteritic = CBMutableCharacteristic(type: messageUUID, properties: CBCharacteristicProperties.notify, value: nil, permissions: CBAttributePermissions.readable)
            getDataPeripheralCharacteristic = CBMutableCharacteristic(type: inputUUID, properties:
                CBCharacteristicProperties.write, value: nil, permissions: CBAttributePermissions.writeable)
            service = CBMutableService(type: otherUUID, primary: true)
            service.characteristics = [characteritic!, getDataPeripheralCharacteristic!]
            peripheralManager!.add(service)
        }
    }


    func peripheralManager(peripheral: CBPeripheralManager, didReceiveWriteRequests requests: [CBATTRequest]) {
        for request in requests {
            if (request.characteristic.uuid.uuidString == inputUUID.uuidString) {
                outputData = String(data: request.value!, encoding: String.Encoding.utf8)!
                outputLabel.text = outputData
            }
        }
    }

    func peripheralManager(peripheral: CBPeripheralManager, didReceiveReadRequest request: CBATTRequest) {
    }




    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            centralManager.scanForPeripherals(withServices: nil, options: nil)
            //search for peripheral device
        }
    }


    //when peripheral is discovered, this method is called

    private func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        self.centralManager.stopScan();
        self.connectingPeripheral = peripheral;
        peripheral.delegate = self;
        centralManager.connect(peripheral, options: nil)
    }

    //if connection request from central is succesfull, this method is called
    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        peripheral.delegate = self;
        peripheral.discoverServices(nil)
    }

    //if central discovers services, this method is called
    private func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
        for service: CBService in peripheral.services! {
            peripheral.discoverCharacteristics(nil, for: service)
        }
    }

    private func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
        if (service.uuid.uuidString == otherUUID.uuidString) {
            guard let characteristics = service.characteristics else {
                return
            }
            for characteristic in characteristics {
                if characteristic.uuid.uuidString == messageUUID.uuidString {
                    peripheral.setNotifyValue(true, for: characteristic)
                } else if characteristic.uuid.uuidString == inputUUID.uuidString {
                    centralWriteCharacteristic = characteristic
                }
            }
        }
    }


    private func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
        if error != nil {
            print("Error reading characteristics");
        }
        if (characteristic.uuid == messageUUID) {
            outputData = String(data: characteristic.value!, encoding: String.Encoding.utf8)!
            print(outputData)
            outputLabel.text = outputData
        }
    }


}

当我单击中央模式或外围模式时产生线程1信号SIGABRT错误,但是当我单击发送数据时则不会产生错误。我在网上做了一些研究,这个错误的一般解决方案是检查按钮的连接,并确保只有一个连接,这是正确的。我已经这样做但它仍然没有修复错误。下面是这些按钮的连接图像。

central mode button

peripheral mode button

0 个答案:

没有答案
相关问题