ios - 未调用 BLE 外设委托(delegate)

标签 ios swift core-bluetooth

我最近从 Swift 3 升级到 Swift 4,从 iOS 10.3.3 升级到 iOS 11.1。

我正在开发一个使用 BLE 进行双向通信的应用程序。工作流程如下:

  1. 外围 - 宣传身份
  2. CENTRAL - 接收身份(处理它...)
  3. CENTRAL - 响应外围设备
  4. PERIPHERAL - 接收来自中央的响应
  5. 完成

我的代码在更新之前运行良好,但现在不行了。在第 4 步结束时,我执行以下行:

peripheral.writeValue(encryptedData!, for: characteristic, type: .withResponse)

这应该调用以下委托(delegate)方法,但它没有:

public func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
        print("Did Write")
        print("Error=\(error?.localizedDescription)")
    }

它还应该(并且正在调用)外围设备上的以下委托(delegate)方法,但它没有:

public func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
        print("did receive write request")
}

服务和特性设置如下:

let prefs = Preferences()
            let strServiceUUID = prefs.GetString(key: Preferences.PREF_IDENTITY_SERVICE_UUID, defaultVal: "")!
            let strCharacteristicUUID = prefs.GetString(key: Preferences.PREF_IDENTITY_CHARACTERISTIC_UUID, defaultVal: "")!
            print("ServiceUUID=\(strServiceUUID)")
            print("CharacteristicUUID=\(strCharacteristicUUID)")
            mServiceUUID = CBUUID(string: strServiceUUID)
            mCharacterUUID = CBUUID(string: strCharacteristicUUID)
            mCBBluetoothServices = CBMutableService(type: mServiceUUID, primary: true)
            
            //lets configure the data we want to advertise for
            var characteristics : [CBCharacteristic] = []
            
            //let strData : String = "933911"
            //let data = strData.data(using: .utf8)
            let cbProperties: CBCharacteristicProperties = [.read, .write, .notify]
            let cbPermissions: CBAttributePermissions = [.readable, .writeable]
            mIdentityObjectCharacteristic = CBMutableCharacteristic(type: mCharacterUUID,
                                                                    properties: cbProperties,
                                                                    value: nil,
                                                                    permissions: cbPermissions)
            
            
            characteristics.append(mIdentityObjectCharacteristic)
            mCBBluetoothServices.characteristics = characteristics
            mCBPeripheralManager.add(mCBBluetoothServices)

最佳答案

我不确定为什么升级操作系统和 Swift 版本会破坏您的代码,但是,在我看来您可能使用了错误的委托(delegate)方法?

尝试使用这个

func peripheral(CBPeripheral, didWriteValueFor: CBCharacteristic, error: Error?)

而不是这个

func peripheral(CBPeripheral, didWriteValueFor: CBDescriptor, error: Error?)

关于ios - 未调用 BLE 外设委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341817/

相关文章:

objective-c - 不熟悉的@interface 声明

swift - 是否可以像 C# 一样检查 Swift 中的类型?

ios - CoreBluetooth 从不调用 iPhone 5S 上的发现服务

iOS 核心蓝牙密码

swift - 在 ios 8 中使用蓝牙发送到外设的数据量

ios - EarlGrey 卡住动画并且不调用回调

ios - 从另一个 UICollectionView 调整 UICollectionViewCells 中的 UIView 大小

ios - Xamarin iOS。如何自动转到 View 的末尾

ios - 多个 View Controller 上的 SWReveal 菜单

swift 3 : String search backwards