无法通过CBCentral Manager扫描iBecon Signal的服务ID [英] Not able to scan Through CBCentral Manager with service ID for iBecon Signal

查看:236
本文介绍了无法通过CBCentral Manager扫描iBecon Signal的服务ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Working on iBecon signal using Core Bluetooth i am able to search with CBCentralManager scan optionn nil :-

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

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

But when i provide my desirable service ID i.e :-

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

它永远不会调用didDiscoverPeripheral代理方法,我也需要在后台模式下扫描外围设备并且根据Apple文档,您需要在需要在后台模式下扫描时明确提供服务ID。任何人都可以帮助我在这里做错了。

it never calls didDiscoverPeripheral Delegate method, I need to scan the peripheral in background mode too and according to apple documentation you need to provide service id explicitly whenever you need to scan in background mode. Anyone can help what i am doing wrong here.

推荐答案

我这样使用,
连接按钮点击事件
并使用 CBCentralManagerDelegate,CBPeripheralDelegate 委托

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)
        }
    }

这篇关于无法通过CBCentral Manager扫描iBecon Signal的服务ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆