我是否必须等待 Swift 中的 didReadRSSI() 函数?
Do I have to wait for the didReadRSSI() function in Swift?
我想发现我所在地区的 BLE 设备并存储它们当前的 RSSI 值。发现是有效的,但我确定,如果我的 func didDiscoverPeripheral
真的保存了......我认为我应该在离开 didDiscoverPeripheral
之前等待 didReadRSSI
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
}
我是这样解决问题的:
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
:)
我建议这样 -
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
}
}
我想发现我所在地区的 BLE 设备并存储它们当前的 RSSI 值。发现是有效的,但我确定,如果我的 func didDiscoverPeripheral
真的保存了......我认为我应该在离开 didDiscoverPeripheral
之前等待 didReadRSSI
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
}
我是这样解决问题的:
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
:)
我建议这样 -
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
}
}