我对此事非常困惑,并且想知道是否有人有任何想法,因为我不确定问题是什么,我的最佳猜测是缓存。当我运行此应用程序大约95%的时间它将正确连接并访问警报特性但不会触发didUpdateValueFor。有时候,当我更改或重新安装应用程序时,它可以正常工作并触发。我知道外围值正在我在其他应用程序上测试时正确更新。我正在使用iPhone 6,并且我已多次重启。我不确定是什么导致它有时工作,因此无法调试。我能找到问题的任何想法或方法吗?
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var manager: CBCentralManager!
var peripheral: CBPeripheral!
var devices: [CBPeripheral] = []
var closestRSSI = 0
var alertCharacteristic: CBCharacteristic? = nil
let SERVICE_UUID = CBUUID(string: "06758213-258D-44B2-A577-8AE43E9DF674")
let ALERT_UUID = CBUUID(string: "B9FBC271-666B-4CA7-8F9C-F8E9C0223D20")
let BATTERY_UUID = CBUUID(string: "4D757E85-6A28-48CB-9CF5-299CB72D5AB2")
let FIRMWARE_UUID = CBUUID(string: "64CD5AF5-B6EE-4D46-A164-246BB197F5DA")
let CLIENT_CONFIG_UUID = CBUUID(string: "00002902-0000-1000-8000-00805F9B34FB")
var characteristicUUIDArray: [CBUUID] = []
@IBOutlet weak var connectButton: UIButton!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var headLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
infoLabel.text = "0"
characteristicUUIDArray = [ALERT_UUID, BATTERY_UUID, FIRMWARE_UUID]
}
@IBAction func connectButtonPressed(_ sender: Any) {
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state{
case .unauthorized:
print("Unauthorized to use BLE")
break
case .poweredOff:
print("Bluetooth is currently powered off")
break
case .poweredOn:
print("Bluetooth is ready to use")
manager.scanForPeripherals(withServices: [SERVICE_UUID], options: nil)
break
case .resetting:
print("Blueooth is resetting")
break
case .unknown:
print("Bluetooth is unknown")
break
case .unsupported:
print("Bluetooth is unsupported")
break
}
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print(error!)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print(peripheral.name ?? "Null")
if peripheral.name != nil{
if peripheral.name! == "Watch" {
self.manager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
manager.connect(peripheral, options: nil)
}
}
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("Disconnected")
manager.cancelPeripheralConnection(peripheral)
manager.scanForPeripherals(withServices: [SERVICE_UUID], options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("\nDid Connect To \(peripheral.name!)")
peripheral.discoverServices([SERVICE_UUID])
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("Did Discover Services")
for service in peripheral.services!{
let thisService = service
if thisService.uuid == SERVICE_UUID{
peripheral.discoverCharacteristics(characteristicUUIDArray, for: thisService)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print("Did Discover Characteristics")
for charateristic in service.characteristics! {
let thisCharacter = charateristic
if thisCharacter.uuid == ALERT_UUID{
print("Alert")
peripheral.setNotifyValue(true, for: thisCharacter)
}
// if thisCharacter.uuid == BATTERY_UUID{
// print("Battery")
// self.peripheral.setNotifyValue(true, for: thisCharacter)
// }
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("Did update")
if(characteristic.uuid == ALERT_UUID){
print("Did Update Value For Alert")
let content = String(data: characteristic.value!, encoding: String.Encoding.utf8)
let value = content?.components(separatedBy: ",")
let warning = Int(value![0])
infoLabel.text = String(warning!)
}
if(characteristic.uuid == BATTERY_UUID){
print("Did Update Value For Battery")
}
}
}
答案 0 :(得分:0)
所以我认为这是一个竞争条件错误。通过在延迟中包装每个特征,问题得到了解决。这是我改变的。所以我认为这是一个竞争条件错误。通过在延迟中包装每个特征,问题得到了解决。这是我改变的。我不确定为什么这会解决它,但如果有人有任何想法,我很高兴听到为什么这是
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print("Did Discover Characteristics")
for charateristic in service.characteristics! {
let thisCharacter = charateristic
if thisCharacter.uuid == ALERT_UUID{
print("Alert")
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: {
peripheral.setNotifyValue(true, for: thisCharacter)
})
}
if thisCharacter.uuid == BATTERY_UUID{
print("Battery")
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: {
peripheral.setNotifyValue(true, for: thisCharacter)
})
}
}
}