【问题标题】:Do I have to wait for the didReadRSSI() function in Swift?我必须等待 Swift 中的 didReadRSSI() 函数吗?
【发布时间】:2015-09-26 01:17:36
【问题描述】:

我想发现我所在地区的 BLE 设备并存储它们当前的 RSSI 值。发现有效,但我确定,如果我的函数 didDiscoverPeripheral 真的保存...我认为我应该在离开 didDiscoverPeripheral 之前等待 didReadRSSI 函数。但是我怎样才能以一种简单的方式意识到这一点,我的观点是否正确?

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
    CBperipheral = [peripheral]

    peripheral.readRSSI()
}

func peripheral(peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?)
{
    //Store the peripheral specific RSSI, Name and identifier
}

【问题讨论】:

  • 您不能在didDiscoverPeripheral 中“等待”。当它发生时,您需要处理对didReadRSSI 的回调。您的didDiscoverPeripheral 的第一行也没有任何意义。我建议您创建一个由外围设备键入的 RSSI 值字典。
  • 请看我的回答:)

标签: ios swift bluetooth bluetooth-lowenergy core-bluetooth


【解决方案1】:

我会建议这样的东西-

var peripherals = Set<CBPeripheral>
var peripheralRSSIs = Dictionary<CBPeripheral,NSNumber>()


func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
    self.peripherals.insert(peripheral)
    self.peripheralRSSIs[peripheral]=RSSI
    central.connectPeripheral(peripheral,options:nil) // Connect if you want to be able to retrieve additional RSSI values
}

func centralManager(_ central: CBCentralManager,
    didConnectPeripheral peripheral: CBPeripheral)
{
    peripheral.delegate=self
    peripheral.readRSSI()
}

func peripheral(peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?)
{
    if (error != nil) {
        print(error)
    } else {
        self.peripheralRSSIs[peripheral]=RSSI
    }
}

【讨论】:

    【解决方案2】:

    我解决了这样的问题:

       func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
    {
        CBperipheral = [peripheral]
        myStrings.append(StringPair("\(peripheral.name!)", "\(peripheral.name!)", "\(RSSI)"))
        //Coose the right peripheral and connect!
    }
    

    我不需要RSSI 的最新值,只需要一些指导值。所以我希望这段代码符合IOS 8及更高版本。

    但如果你能告诉我处理我的didReadRSSI 的回调来解决类似这样的其他问题,那就太好了。

    顺便说一句。我必须写这个:CBperipheral = [peripheral] 在调用CBmanager.connectPeripheral 之后调用我的didConnectPeripheral :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多