核心蓝牙的 didUpdateValueFor 回调函数不触发

Posted

技术标签:

【中文标题】核心蓝牙的 didUpdateValueFor 回调函数不触发【英文标题】:didUpdateValueFor callback function of Core Bluetooth doesn't fire 【发布时间】:2020-05-12 02:24:52 【问题描述】:

我在使用 Xcode 和 Swift 的 ios 应用程序中使用 Core Bluetooth 从 Muse 2 获取数据。除了 didUpdateValueFor 回调函数不触发之外,该过程有效。 didUpdateNotificationStateFor 回调函数触发,当我打印特征时,它们显示它们正在通知。我用 Polar H10 做过同样的事情,而且效果很好。

其中一个特征具有无响应的写入属性。我要给特征写什么?

Muse 2 Headband 的制造商是否共享有关其协议的信息?

import UIKit
import CoreBluetooth

class ViewController: UIViewController 

    var workerQueue = DispatchQueue(label: "us.utili.TrialMuse.workerQueue")
    var centralManager: CBCentralManager! = nil
    var muse: CBPeripheral! = nil

    override func viewDidLoad() 
        super.viewDidLoad()

        let options: [String: Any] = [CBCentralManagerOptionShowPowerAlertKey: true]

        self.centralManager = CBCentralManager(delegate: self, queue: workerQueue, options: options)
        self.centralManager.delegate = self

    




extension ViewController: CBCentralManagerDelegate 

    func centralManagerDidUpdateState(_ central: CBCentralManager) 

        print("centralManagerDidUpdateState")

        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")
            centralManager.scanForPeripherals(withServices: nil)
        @unknown default:
            print("switch central.state @unknown default:")
        

    

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) 

        print("didDiscover")

        print("peripheral description:", peripheral.description)

        if let peripheralName = peripheral.name, peripheralName == "Muse-7EF5" 
            muse = peripheral
            muse.delegate = self
            central.stopScan()
            central.connect(muse, options: nil)
        

    

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) 

        print("didConnect")
        print("peripheral:", peripheral)

        peripheral.discoverServices(nil)

    



extension ViewController: CBPeripheralDelegate 

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) 

        print("didDiscoverServices")

        guard let services = peripheral.services else 
            return
        

        print("services:")

        for service in services 

            print("\tservice description:", service.description)

            peripheral.discoverCharacteristics(nil, for: service)

        

    

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) 

        print("didDiscoverCharacteristicsFor")

        guard let characteristics = service.characteristics else 
            return
        

        for characteristic in characteristics 

            print("characteristic description:", characteristic.description)

            if characteristic.properties.contains(.broadcast) 
                print("\tbroadcast")
            
            if characteristic.properties.contains(.read) 
                print("\tread")
            
            if characteristic.properties.contains(.writeWithoutResponse) 
                print("\twrite without response")
            
            if characteristic.properties.contains(.write) 
                print("\twrite")
            
            if characteristic.properties.contains(.notify) 
                print("\tnotify")
            
            if characteristic.properties.contains(.indicate) 
                print("\tindicate")
            
            if characteristic.properties.contains(.authenticatedSignedWrites) 
                print("\tauthenticated signed writes")
            
            if characteristic.properties.contains(.extendedProperties) 
                print("\textended properties")
            
            if characteristic.properties.contains(.notifyEncryptionRequired) 
                print("\tnotify encryption required")
            
            if characteristic.properties.contains(.indicateEncryptionRequired) 
                print("\tindicate encryption required")
            

            if characteristic.properties.contains(.notify) 
                peripheral.setNotifyValue(true, for: characteristic)
            

            if characteristic.properties.contains(.read) 
                peripheral.readValue(for: characteristic)
            

        

    

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) 

        print("didUpdateValueFor")

        if let error = error 
            print("error:", error)
        

        guard characteristic.value != nil else 
            return
        

        print("characteristic description:", characteristic.description)

    

    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) 

        print("didUpdateNotificationStateFor")

        print("characteristic description:", characteristic.description)

    


这是调试窗口中的输出:

centralManagerDidUpdateState
central.state is .poweredOn
didDiscover
peripheral description: <CBPeripheral: 0x282365cc0, identifier = 151183C1-EB98-03BF-A37A-5AC3E7752F5D, name = (null), state = disconnected>
didDiscover
peripheral description: <CBPeripheral: 0x282365d60, identifier = 573D0EDD-7D27-97D6-5D03-E49C9F505847, name = Muse-7EF5, state = disconnected>
didConnect
peripheral: <CBPeripheral: 0x282365d60, identifier = 573D0EDD-7D27-97D6-5D03-E49C9F505847, name = Muse-7EF5, state = connected>
didDiscoverServices
services:
    service description: <CBService: 0x280724100, isPrimary = YES, UUID = FE8D>
didDiscoverCharacteristicsFor
characteristic description: <CBCharacteristic: 0x28367c840, UUID = 273E0001-4C4D-454D-96BE-F03BAC821358, properties = 0x14, value = (null), notifying = NO>
    write without response
    notify
characteristic description: <CBCharacteristic: 0x28367c8a0, UUID = 273E0008-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367c900, UUID = 273E0009-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367c960, UUID = 273E000A-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367c9c0, UUID = 273E000B-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367ca20, UUID = 273E0002-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367ca80, UUID = 273E0003-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cae0, UUID = 273E0004-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cb40, UUID = 273E0005-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cba0, UUID = 273E0006-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cc00, UUID = 273E0007-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cc60, UUID = 273E000C-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367ccc0, UUID = 273E000D-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cd20, UUID = 273E000E-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cd80, UUID = 273E000F-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367cde0, UUID = 273E0010-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
characteristic description: <CBCharacteristic: 0x28367ce40, UUID = 273E0011-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = NO>
    notify
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367c840, UUID = 273E0001-4C4D-454D-96BE-F03BAC821358, properties = 0x14, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367c8a0, UUID = 273E0008-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367c900, UUID = 273E0009-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367c960, UUID = 273E000A-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367c9c0, UUID = 273E000B-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367ca20, UUID = 273E0002-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367ca80, UUID = 273E0003-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cae0, UUID = 273E0004-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cb40, UUID = 273E0005-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cba0, UUID = 273E0006-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cc00, UUID = 273E0007-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cc60, UUID = 273E000C-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367ccc0, UUID = 273E000D-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cd20, UUID = 273E000E-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cd80, UUID = 273E000F-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367cde0, UUID = 273E0010-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>
didUpdateNotificationStateFor
characteristic description: <CBCharacteristic: 0x28367ce40, UUID = 273E0011-4C4D-454D-96BE-F03BAC821358, properties = 0x10, value = (null), notifying = YES>

【问题讨论】:

【参考方案1】:

我可以看到您正在检查特性.properties.contains(.notify) 两次。只需评论这些行

if characteristic.properties.contains(.notify) 
    print("\tnotify")

按照 swift 逻辑,该代码中没有错误。我也有同样的问题,令我惊讶的是,仅仅通过评论这些行,它就对我有用。但是我在 didUpdateValueFor 中的特征值为零。但在你的情况下,函数本身不会被触发。也许你可以试一试。

但代码似乎没有任何问题。这应该是缪斯的问题。

【讨论】:

以上是关于核心蓝牙的 didUpdateValueFor 回调函数不触发的主要内容,如果未能解决你的问题,请参考以下文章

蓝牙核心协议都有哪些

连接核心蓝牙或经典蓝牙设备是不是需要 CoreLocation 框架

如何使用核心蓝牙 sdk 扫描蓝牙设备?

iOS蓝牙编程指南 -- 核心蓝牙概述

蓝牙核心规格

HC-06蓝牙模块的使用