CoreBluetooth / L2CAP channels, not working

泄露秘密 提交于 2019-12-08 04:55:13

问题


I have recently been experimenting quite a bit with CoreBluetooth, and now I want to use L2CAP channels.

For that I have set up two iOS apps, one is the peripheral and the other one is the central.

At this point, when I run them, here is what I see in the Xcode debugging console.

On the peripheral side I get this:

peripheralManager(_:didAdd:error:)
peripheralManager(_:didPublishL2CAPChannel:error:)
PSM: 192
peripheralManagerDidStartAdvertising(_:error:)

On the central side I get that:

centralManagerDidUpdateState
centralManager(_:didDiscover:advertisementData:rssi:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-11 ....] [CoreBluetooth] WARNING: Unknown error: 431

The question is: What am I doing wrong to get this bad looking message?

And later: What shall I need to add to this code to make an actual data transfer between the central an the terminal. For example send the message: "Hello world! Happy new year."

I am using Xcode Version 10.1 with Swift 4.2.

And below is the relevant source code.

For the peripheral:

import UIKit
import CoreBluetooth

class ViewController: UIViewController,CBPeripheralManagerDelegate {
    let service_UUID = CBUUID(string:"9869B57E-2005-492B-8ED9-B5716886975F"),
    svcCharac_UUID = CBUUID(string:"BC7E600D-1AE4-4B09-BAC2-ECC0E85247E0")
    var cbPerifMngr:CBPeripheralManager!, mutaSRVC:CBMutableService!,
    svcCharac:CBMutableCharacteristic!

    override func viewDidLoad() {
        super.viewDidLoad()
        cbPerifMngr = CBPeripheralManager(delegate: self, queue: nil)
    }


    // CBPeripheralManagerDelegate protocol implementation.

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        if peripheral.state == .poweredOn {
            mutaSRVC = CBMutableService(type: service_UUID, primary: true)
            svcCharac = CBMutableCharacteristic(type: svcCharac_UUID,
                                                      properties: [.read, .notify],
                                                      value: Data(base64Encoded: "Hello!"), permissions: .readable)
            mutaSRVC.characteristics = [svcCharac]
            cbPerifMngr?.add(mutaSRVC)

            cbPerifMngr?.publishL2CAPChannel(withEncryption: false)
        }
    }


    func peripheralManager(_ peripheral: CBPeripheralManager,
                           didPublishL2CAPChannel PSM: CBL2CAPPSM,
                           error: Error?) {
        print(#function)
        if error != nil {
            print("Error in \(#function) :\n\(error!)")
            return
        }

        print("PSM: \(PSM)")
    }


    func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
        print(#function)
        if error != nil {
            print("Error in \(#function) :\n\(error!)")
            return
        }

        cbPerifMngr.startAdvertising([CBAdvertisementDataServiceUUIDsKey:[service.uuid]])
    }


    func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
        print(#function)
        if error != nil {
            print("Error in \(#function) :\n\(error!)")
            return
        }
    }
}

For the central:

import UIKit
import CoreBluetooth

class ViewController: UIViewController,CBCentralManagerDelegate,CBPeripheralDelegate {
    let service_UUID = CBUUID(string:"9869B57E-2005-492B-8ED9-B5716886975F")
    var cbCenterMngr:CBCentralManager!, cbPerifHandle:CBPeripheral!,
    cbL2CAPChan:CBL2CAPChannel!

    override func viewDidLoad() {
        super.viewDidLoad()
        cbCenterMngr = CBCentralManager(delegate: self, queue: nil)
    }


    // CBCentralManagerDelegate protocol implementation.

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print(#function)
        if central.state == .poweredOn {
            central.scanForPeripherals(withServices: [service_UUID],
                                       options: nil)
        }
    }


    func centralManager(_ central: CBCentralManager,
                        didDiscover peripheral: CBPeripheral,
                        advertisementData: [String : Any],
                        rssi RSSI: NSNumber) {
        print(#function)
        peripheral.delegate = self
        cbPerifHandle = peripheral
        central.connect(peripheral, options: nil)
    }


    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print(#function)
        peripheral.openL2CAPChannel(192)
    }


    func peripheral(_ peripheral: CBPeripheral,
                    didOpen channel: CBL2CAPChannel?,
                    error: Error?) {
        print(#function)
        if error != nil {
            print("Error in \(#function) :\n\(error!)")
            return
        }

        cbL2CAPChan = channel
    }
}

来源:https://stackoverflow.com/questions/54142326/corebluetooth-l2cap-channels-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!