在 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)")
}
}
}
当 运行 代码时,我可以在调试器控制台中看到最后一个函数 (peripheral:didUpdateValueFor:error) 被调用用于第一个特征 (myCharacOne) ,但不是第二个(myCharacTwo)。也就是说,只有第一部分信息被传输。
希望CoreBluetooth
高手能看出我的代码有什么问题,并给予指导。
Core Bluetooth 传输队列已限制 space。如果 updateValue
returns false
则传输队列中没有足够的 space 用于更新。您的程序必须对进一步的更新进行排队,直到您调用 peripheralManagerIsReady(toUpdateSubscribers)
CBPeripheralManagerDelegate
方法。
这是我在使用 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)")
}
}
}
当 运行 代码时,我可以在调试器控制台中看到最后一个函数 (peripheral:didUpdateValueFor:error) 被调用用于第一个特征 (myCharacOne) ,但不是第二个(myCharacTwo)。也就是说,只有第一部分信息被传输。
希望CoreBluetooth
高手能看出我的代码有什么问题,并给予指导。
Core Bluetooth 传输队列已限制 space。如果 updateValue
returns false
则传输队列中没有足够的 space 用于更新。您的程序必须对进一步的更新进行排队,直到您调用 peripheralManagerIsReady(toUpdateSubscribers)
CBPeripheralManagerDelegate
方法。