gpt4 book ai didi

ios - CoreBluetooth 特征值被覆盖为零

转载 作者:行者123 更新时间:2023-11-30 11:24:21 33 4
gpt4 key购买 nike

我已经创建了蓝牙代理并且蓝牙连接正确。

在我的蓝牙对象中,我调用了所需的函数:

// Characteristic being targeted
var writeCharacteristic: CBCharacteristic!

public func peripheral(_ peripheral: CBPeripheral,
didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {
return
}
print(characteristics)
for characteristic in characteristics {
switch (characteristic.uuid) {
case (serialUUID) :
print("serialPortRXCharacterUUID: \(service.uuid)")
writeCharacteristic = characteristic
default:
print("nothing")
}
}
}

检查调试器writeCharacteristic存储了正确的特征,但是当我在ViewController中调用writeToDevice时。

// View Controllers function
@IBAction func writeToDevice(_ sender: UIButton) {
bluetoothManager.writeToPeripheral(message: "Test", peripheral: peripheral)
}

在蓝牙类对象中调用 writeToDevice 函数,如下所示:

func writeToPeripheral(message: String, peripheral: CBPeripheral) {
if let data = message.data(using: String.Encoding.utf8) {
let datareturn = String(bytes: data, encoding: .utf8)
print(writeCharacteristic)
peripheral.writeValue(data, for: writeCharacteristic!, type: .withoutResponse)
}
}

在调试器之后writeCharacteristic在设备连接时从一个值开始,但是在我从另一个 View Controller 调用按钮后,writeCharacteristic似乎返回到

最佳答案

根据 @Larme 的评论,我意识到我正在创建 BLE 对象的第二个实例,因此实现单例模式将解决此问题。

关于ios - CoreBluetooth 特征值被覆盖为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50924357/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com