我想在我的区域发现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
}
答案 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
}
}