簡體   English   中英

在CoreBluetooth中使用兩個特征不起作用

[英]Using two characteristics in CoreBluetooth not working

這是我在使用CoreBluetooth時面臨的一個問題。 到目前為止,我一直只使用一項服務和一項功能,並且一切正常。 但是在這里,我必須使用兩個特征,並且事情做得不太好。

發生的情況是僅傳輸第一個特征,而不傳輸第二個特征。

由於我的程序一定有錯誤,因此我嘗試在所有相關代碼的下面加上一些注釋。 如果有人認為代碼的另一部分是解決問題所必需的,請告訴我。

// Declarations relevant to the following code.
var cbPerifMngr:CBPeripheralManager!, mutaSRVC:CBMutableService!,
myCharacOne,myCharacTwo:CBMutableCharacteristic!

...................

// Code to update the CBMutableCharacteristic objects:
let strBuffOne = "abc123_my-information_1"
let strBuffTwo = "GHK678_my-information_2"
if cbPerifMngr.updateValue(Data(strBuffOne.utf8), for: myCharacOne,
                           onSubscribedCentrals: nil) {
    // All is OK.
    if cbPerifMngr.updateValue(Data(strBuffTwo.utf8), for: myCharacTwo,
                               onSubscribedCentrals: nil) {
        // All is OK.
    } else {
        print("1) For some reason, the CBPeripheralManager update was not performed in \(#function).")
    }
} else {
    print("2) For some reason, the CBPeripheralManager update was not performed in \(#function).")
}

...................

// Code for the CBPeripheralManagerDelegate protocol:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    print(#function)
    if peripheral.state == .poweredOn {
        mutaSRVC = CBMutableService(type: serviceDB_UUID, primary: true)
        myCharacOne = CBMutableCharacteristic(type: myChrcOne_UUID,
                                                    properties: [.read, .notify],
                                                    value: nil, permissions: .readable)
        myCharacTwo = CBMutableCharacteristic(type: myChrcTwo_UUID,
                                                    properties: [.read, .notify],
                                                    value: nil, permissions: .readable)
        mutaSRVC.characteristics = [myCharacOne,myCharacTwo]
        cbPerifMngr?.add(mutaSRVC)
    }
}

...................

// Code for the CBPeripheralDelegate protocol:

func peripheral(_ peripheral: CBPeripheral,
                didDiscoverCharacteristicsFor service: CBService,
                error: Error?) {
    print(#function)
    if error != nil {
        print("Error in \(#function) :\n\(error!)")
    } else {
        // Here service.characteristics? contains 2 items.
        peripheral.setNotifyValue(true,
                                  for: (service.characteristics?[0])!)
        peripheral.setNotifyValue(true,
                                  for: (service.characteristics?[1])!)
    }
}


func peripheral(_ peripheral: CBPeripheral,
                didUpdateValueFor characteristic: CBCharacteristic,
                error: Error?) {
    print(#function)
    if error != nil {
        print("Error in \(#function) :\n\(error!)")
    } else {
        if let dataStr = String(data: characteristic.value!,
                                encoding: String.Encoding.utf8) {
        print("dataStr:: \(dataStr)")
        }
    }
}

運行代碼時,我可以在調試器控制台中看到對第一個特征(myCharacOne)調用了最后一個函數( 外周設備:didUpdateValueFor:error ),而對第二個特征(myCharacTwo)則沒有調用。 這意味着僅信息的第一部分被發送。

我希望一些CoreBluetooth專家可以看到我的代碼中有什么問題,並提供一些指導。

核心藍牙傳輸隊列的空間有限。 如果updateValue返回false則傳輸隊列中沒有足夠的空間用於更新。 您的程序必須對進一步的更新排隊,直到您調用了peripheralManagerIsReady(toUpdateSubscribers) CBPeripheralManagerDelegate方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM