问题描述
我是 Corebluetooth 的新手.我想在扫描时防止重复的外围设备.有人可以帮我吗?
还有一个问题,如何在范围内连接外设?
I am new to Corebluetooth. I want to prevent duplicate peripherals when scanning. Can anyone help me?
And one more question, how to connect peripheral with in range ?
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
{
if (peripheral.name != nil) && (peripheral.name == "EXP") ||
(peripheral.name == "EXP")
{
let key = peripheral.identifier.uuidString
let data = advertisementData.description
if let previous = datas[key]
{
if (previous != data)
{
print("Different \(String(describing: peripheral.name)): \ . (data)")
}
} else
{
print("\(String(describing: peripheral.name)): \(data)");
datas[key] = data
}
peripherals.append(peripheral)
lblDeviceCount.isHidden = false
lblDeviceCount.text = "\(peripherals.count) Devices Found"
tblPeriPheral.reloadData()
}
}
推荐答案
您可以使用循环来防止重复外围设备.如果新外设的标识符等于外设中的现有外设(您正在使用的变量),则不要将其附加到外设.
You can use a loop to prevent duplicate peripherals. If new peripheral has an identifier equal to an existing peripheral in peripherals (the variable you are using) then don't append it to peripherals.
for existing in peripherals {
if existing.identifier == peripheral.identifier { return }
}
对于第二部分,您应该阅读RSSI,它会帮助您了解是否您离设备更近或更远.您可以根据范围决定是否连接.
For the second part, you should read about RSSI, it will help you know whether you're closer or far from the device. Depending on the range you can make decision whether to connect to it or not.
这篇关于如何防止corebletooth中的重复外围设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!