gpt4 book ai didi

ios - 无法使用 iBecon 信号的服务 ID 通过 CBCentral Manager 进行扫描

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:25:52 24 4
gpt4 key购买 nike

使用 Core Bluetooth 处理 iBecon 信号,我可以使用 CBCentralManager 扫描选项进行搜索 n nil :-

Shared.sharedInstance.centralManager?.scanForPeripherals(withServices: nil, options:[CBCentralManagerScanOptionAllowDuplicatesKey:true])

但是当我提供我想要的服务 ID 时,即:-

Shared.sharedInstance.centralManager?.scanForPeripherals(withServices: [serviceID], options:[CBCentralManagerScanOptionAllowDuplicatesKey:true])

它从不调用 didDiscoverPeripheral Delegate 方法,我也需要在后台模式下扫描外围设备,根据苹果文档,无论何时需要在后台模式下扫描,您都需要明确提供服务 ID。任何人都可以帮助我在这里做错什么。

最佳答案

我是这样用的,连接按钮点击事件并使用 CBCentralManagerDelegate, CBPeripheralDelegate 委托(delegate)

func connectDevice(sender:UIButton){


if peripheral != nil {
manager.cancelPeripheralConnection(peripheral)
manager = CBCentralManager(delegate: self, queue: nil)
}
}



func centralManagerDidUpdateState(central: CBCentralManager) {
if central.state == CBCentralManagerState.PoweredOn {
central.scanForPeripheralsWithServices(nil, options: nil)
} else {
self.showAlert(Messages().alert , message: "Bluetooth is not on.")
}
}



func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
let device = (advertisementData as NSDictionary).objectForKey(CBAdvertisementDataLocalNameKey) as? NSString
print(device)

if device?.containsString(BEAN_NAME) == true {
self.manager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
manager.connectPeripheral(peripheral, options: nil)
}
}

关于ios - 无法使用 iBecon 信号的服务 ID 通过 CBCentral Manager 进行扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41899838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com