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

标签 ios bluetooth-lowenergy core-bluetooth cbperipheral cbcentralmanager

使用核心蓝牙处理 iBecon 信号,我可以使用 CBCentralManager 扫描选项进行搜索 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 - 无法通过 CBCentral Manager 使用 iBecon Signal 的服务 ID 进行扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41899838/

相关文章:

objective-c - UIView 在 iOS 设备上不透明,但在模拟器上是透明的

android - 我们能否像监控信标(进入区域、退出区域)一样监控 BLE 服务?

ios - iOS上的BLE技术从哪几点入手

objective-c - scanForPeripheralsWithServices :options: unable to connect when specifying services

ios - Arduino/Swift - BLE 通信 POST 和 GET

ios - Vapor 解释代码有困难,执行时出现401错误

ios - iPhone 3.0 SDK 中的 MAP 使用有哪些限制?

支持 HID 的蓝牙 USB 智能加密狗

ios - Swift 中的后台扫描 BLE

ios - 如何限制我的应用程序仅适用于 iPhone 6 和 6 Plus?