【发布时间】:2018-03-06 15:39:31
【问题描述】:
我使用此功能扫描周围的 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。
【问题讨论】:
-
第一次尝试
...scanForPeripherals(withServices: nil,...)有效吗?第二个,不,那是你的问题?然后...scanForPeripherals(withServices: serviceUUIDs...)仅在它宣传该服务时才有效。换句话说,一个设备可以有各种服务,但它不必做广告。要知道他们宣传的是哪一个,请使用“nil”进行扫描,并在didDiscover委托方法中记录advertisementData。它应该显示它实际宣传的服务。
标签: swift ibeacon core-bluetooth