gpt4 book ai didi

iOS 蓝牙 : CBCentralManager Unsubscribes From Updates

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

我有一台 Mac,使用 CoreBluetooth 作为 CBPeripheralManager 设置为蓝牙配件。 Mac 在一组特征 CBUUID 上做广告,一旦它有了订阅者,我就单击一个按钮,每半秒流一次 UTF-8 编码的时间戳。

我有一个 iPhone 设置为 CBCentralManager 订阅适当的特征。每次收到数据并且应用程序处于事件状态时,我都会使用解码的时间戳更新 iPhone 的 UI。 bluetooth-central 后台模式在 .plist 文件中设置。

iPhone 继续调试打印并更新时间戳的 UI 大约 25 秒,然后停止。无论应用程序位于前台还是后台,都会发生这种情况。 编辑: CBPeripheralManager 此时收到 didUnsubscribeFrom 特征 回调。我看不出有任何理由 didUnsubscribeFrom 会被调用,也不知道为什么它总是在 25 秒后。

CBPeripheralManager 继续愉快地发送其时间戳。 CBPeripheralManagerupdateData(_:for:onSubscribedCentrals:) 调用的返回值始终为 true,表明队列永远不会满。

这里涉及很多代码,显示最相关的代码。

CBCentralManager 应用程序:

 func centralManagerDidUpdateState(_ central: CBCentralManager) {
print(central.state.rawValue)
centralManager.scanForPeripherals(withServices: [timeUUID], options: nil)
print("scanning")
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)

print("connected")
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print(peripheral.name as Any)
print(advertisementData[CBAdvertisementDataServiceUUIDsKey] as! Array<CBUUID>)
self.peripheralController = peripheral

self.centralManager.connect(peripheral, options: nil)
}

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

if service.uuid.uuidString == timeUUID.uuidString {
peripheralController.setNotifyValue(true, for: service.characteristics!.first!)
peripheralController.readValue(for: service.characteristics!.first!) // EDIT: This was the offending line
}
}

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

if characteristic.uuid.uuidString == timeUUID.uuidString {
if let valueFrom = characteristic.value {
if let this = String(data: valueFrom, encoding: .utf8) {
if UIApplication.shared.applicationState == .active {
label.text = this
print("ACTIVE \(this)")
} else if UIApplication.shared.applicationState == .background {
print("BACKGROUND \(this)")
} else if UIApplication.shared.applicationState == .inactive {
print("INACTIVE \(this)")
}
}
}
}
}

CBPeripheralManager 应用程序:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
myCharacteristic = CBMutableCharacteristic(type: myServiceUUID, properties: [CBCharacteristicProperties.read, CBCharacteristicProperties.notify], value: nil, permissions: CBAttributePermissions.readable)
let myService = CBMutableService(type: myServiceUUID, primary: true)
myService.characteristics = [myCharacteristic]
bluetoothController.add(myService)
}

func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
print(characteristic.uuid)
subscriber = central
}

func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
print(characteristic)
print("unsubscribe")
}

func repeatAdvertisement() {
timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [unowned self] (timerRef) in
guard let maybeTimer = self.timer, maybeTimer.isValid else { return }
let datum = Date()
let stringFromDate = self.dateFormatter.string(from: datum)
let data = stringFromDate.data(using: .utf8)
print(data!.count)
// myCharacteristic.value = data
let myService = CBMutableService(type: self.myServiceUUID, primary: true)
myService.characteristics = [self.myCharacteristic]
let did = self.bluetoothController.updateValue(data!, for: self.myCharacteristic as! CBMutableCharacteristic, onSubscribedCentrals: [self.subscriber])
print("timed \(stringFromDate) \(did)")
}


}


func advertise() {
if timer == nil {
repeatAdvertisement()
} else {
timer?.invalidate()
timer = nil
}
}

让我知道您还需要什么。

最佳答案

好吧,看在老天爷的份上。问题在于 peripheralController.readValue(for: service.characteristics!.first!) 行,我根据一些示例代码在应用程序中添加了该行,但是,这是不必要的。

显然,对 readValue(for:) 的调用会导致某种超时。我从应用程序中编辑了该行,它很高兴地不断更新。

留下问题并添加这个答案,以防有一天有人最终遇到同样的事情。

关于iOS 蓝牙 : CBCentralManager Unsubscribes From Updates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340463/

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