我是否必须等待Swift中的didReadRSSI()函数?

时间:2015-09-26 01:17:37

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

我想在我的区域发现BLE设备并存储它们当前的RSSI值。发现有效,但我要注意,如果我的func didDiscoverPeripheral真的存了......我想我应该在离开didReadRSSI之前等待didDiscoverPeripheral func。但是我怎么能以一种简单的方式认识到这一点并且我的观点是对的?

    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
}

2 个答案:

答案 0 :(得分:0)

我解决了这个问题:

   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]didConnectPeripheral致电之后致电我的CBmanager.connectPeripheral:)

答案 1 :(得分:0)

我会建议这样的事情 -

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
    }
}