我用这个功能在我身边扫描BLE,但收到太多信号。我只想使用来自信标的信号。
func centralManagerDidUpdateState(_ central: CBCentralManager) {
centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true)])
}
I had used LightBlue发现我的灯塔有5项服务:180A,FFA0,FFB0,FFF0和180F。
然后我尝试了这个代码,但没有找到这个服务的外围设备。
let serviceUUIDs = [CBUUID(string: "FFA0")]
func centralManagerDidUpdateState(_ central: CBCentralManager) {
centralManager.scanForPeripherals(withServices: serviceUUIDs, options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true)])
}
并且didDiscover方法从未调用过
func centralManager(_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String : Any],
rssi: NSNumber)
{
if peripheral.name == "MY_BEACON_NAME" {
print("peripheral: \(peripheral)")
print("rssi = \(rssi)")
}
}
我做错了什么?
P. S.我不能在这个项目中使用CoreLocation,只能使用CoreBluetooth。
根据硬件信标所宣传的信标格式,可能无法使用CoreBluetooth进行检测。 CoreBluetooth可以检测Eddystone格式和AltBeacon格式,但它无法检测到iBeacon格式。
有几点需要了解:
centralManager.scanForPeripherals(withServices: serviceUUIDs ...
时要过滤的内容。虽然Eddystone广告确实包含服务UUID,但iBeacon和AltBeacon不会,因为它们是一种不同类型的BLE广告,称为制造商广告。只有GATT广告才有服务UUID。结论:没有简单的方法可以使用CoreBluetooth仅过滤您想要的信标。您最好的办法是在CoreBluetooth上编写自己的过滤软件,然后忽略您不关心的数据包。