2

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

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

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

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.

Himanshu
  • 2,832
  • 4
  • 23
  • 51
  • 1
    Is the service that you are scanning for being advertised by the peripheral? The set of services a peripheral advertises may be a subset of the services a peripheral offers. What is `serviceID`? – Paulw11 Jan 27 '17 at 20:45
  • serviceID is the CBUUID of the service that has been advertise by the peripheral. – Himanshu Jan 28 '17 at 04:26

1 Answers1

2

I am using like this, connect on button click event and use 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 Singh
  • 617
  • 6
  • 15