L2CAP通道数据传输

时间:2018-06-13 14:56:32

标签: ios bluetooth-lowenergy l2cap

我正在使用面向连接的渠道开发BLE应用程序。我使用北欧半导体nrf52作为外围设备,使用iPhone 6作为中央管理器。

我使用了Bluetooth SIG提供的预定义PSM值0x0025。 我可以成功连接到外围设备并打开L2CAP通道。

我收到以下错误:

** [CoreBluetooth]警告:未知错误:436

2018-06-08 10:03:17.532709-0400蓝牙测试[407:62057] [CoreBluetooth] **没有已知的频道匹配对等体与psm 37 ****

请告诉我如何继续以及错误代码436的含义

以下是我的代码:

   func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        //handling callback when a peripheral is discover
        print("Central Manager PowerOn State Check:\(central.state.rawValue)")
        if (peripheral.name?.contains("Nordic_OTSX") == true)
       {
            print(peripheral.name ??  "no name")
            print("advertisement Data : \(advertisementData) ")
            central.connect(peripheral, options: nil )
            myPeripheral = peripheral
       }
    }


    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
    {
        print("didConnect\(myPeripheral.debugDescription)")
        myPeripheral.delegate = self
        myPeripheral.discoverServices(nil)
    }
    //if error while making connection
    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?)
    {
        print("error:\(error.debugDescription)")
    }

    //after opening L2CAP Channel
   func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?)
    {
        print("didOpen")
        print(error.customMirror)
        print(channel!.outputStream.debugDescription)
        print(channel!.inputStream.debugDescription)
        print(channel?.outputStream.hasSpaceAvailable)
    }


    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
    {
        print("*******************************************************")

        if ((error) != nil) {
            print("Error discovering services: \(error!.localizedDescription)")
            return
        }

        guard let services = peripheral.services else {
            return
        }
        //We need to discover the all characteristic
        for service in services {

            peripheral.discoverCharacteristics(nil, for: service)
            // bleService = service
        }
        print("Discovered Services: \(services)")
    }


    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
    {
        print("*******************************************************")
        if let charcterstics = service.characteristics
        {
            print("characteristics :")
            for char in charcterstics
            {
               /* if char.uuid == buttonCharacteristicUUID
                {
                    buttonCharacteristic = char
                    enableButtonNotifications(buttonCharacteristic!)
                    readButtonValue()
                }*/
                print(char.uuid.uuidString)
            }
        }
         peripheral.openL2CAPChannel(0x0025)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

2 个答案:

答案 0 :(得分:2)

0x25 PSM适用于OTS。您需要ATT PSM,即0x1F

答案 1 :(得分:2)

我有类似的问题。我设法通过创建变量以在函数

中保存“ channel: CBL2CAPChannel?”来解决该问题
func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?)

我只保存了“ channel.outputStream”,这是我唯一需要的一个。但是,如果您不保存它,它将被关闭。