sunyoulao 2015-08-06 02:33 采纳率: 0%
浏览 1623

iOS 8 BLE readRSSI delegate call back not firing

如题。
http://www.openradar.me/18476971
问题大概是这样的:
Summary:
There seems to be an issue in the CoreBluetooth framework that us causing all readRSSI() requests to never trigger the new iOS 8 delegate peripheral(peripheral: CBPeripheral!, didReadRSSI RSSI: NSNumber!, error: NSError!)

This issue only occurs once the application has successfully connected to this device once. Upon disconnection and reconnection the application never receives this callback again.
问题的重现过程:
1. connect to BLE device.
2. attempt to read the RSSI value with the method readRSSI()
3. disconnect from BLE device.
4. reconnect to BLE device.
5. attempt to read the RSSI value with the method readRSSI()
在步骤5之后,再无法正常读取rssi值。

还请大神帮忙!

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 04:07
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题是由于CoreBluetooth框架在iOS 8中引入了一个新的特性:CBPeripheralDelegateReadRSSI. 这个特性使得在连接到蓝牙设备后,如果尝试读取RSSI值时,会触发一个回调函数。这个回调函数会在成功或失败的情况下被调用。

    然而,在iOS 8之前的版本中,没有这种特性,因此当第一次尝试读取RSSI值时,不会触发这个回调函数。这是导致你的应用出现问题的原因。

    要解决这个问题,你需要确保你在每次尝试读取RSSI值之前都调用peripheral(_:didConnect:)方法来重新连接到蓝牙设备。这样,即使你第一次尝试读取RSSI值时没有收到回调,下次再次尝试读取RSSI值时,就会接收到这个回调了。

    这是一个简单的示例:

    func startReadingRSSI() {
        // 假设这里有一个用于获取蓝牙设备的API
        let peripheral = getBLEDevice()
        
        if let peripheral = peripheral {
            peripheral.delegate = self
            
            // 重连蓝牙设备
            peripheral.connect()
            
            // 等待设备连接完成
            while !peripheral.isConnecting && !peripheral.hasError {
                Thread.sleep(forTimeInterval: 0.1)
            }
            
            // 一旦设备连接完成,就调用didConnect方法
            peripheral.delegate.peripheral(_: didConnect: nil)
            
            // 然后开始读取RSSI值
            peripheral.startReadingRSSI(fromTime: .now, interval: 1) { (value, error) in
                if let error = error {
                    print("Error reading RSSI: \(error.localizedDescription)")
                } else if let value = value {
                    print("RSSI value: \(value)")
                }
            }
        } else {
            print("Failed to get BLE device")
        }
    }
    

    在这个示例中,我们首先获取了蓝牙设备,然后设置了它的delegate为我们的实例,并且开始监听是否已经连接好了。当设备连接完成后,我们就调用了didConnect方法。然后,我们在startReadingRSSI方法中调用了一个readRSSI方法,它会在指定的时间间隔内读取RSSI值,并且当我们有数据的时候就打印出来。

    评论

报告相同问题?