I’m having an issue with Core Bluetooth not find peripherals while scanning for specific CBUUID ,and I also want to run my application in foreground and background.but it is didDiscover all near peripheral scanning with out CBUUID "manager.scanForPeripherals(withServices: nil, options: nil)".am using "MactsAsBeacon" for brodcasting beacon simulator.but why its not working scanForPeripherals with specific CBUUID?and how it will work on background ? "I enabled bluetooth-central"is there any extra work i want to do fro background?.
var manager:CBCentralManager!
var peripheralCB:CBPeripheral!
var peripherals = [CBPeripheral]()
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
if let peripheralsObject = dict[CBCentralManagerRestoredStatePeripheralsKey] {
// 2
let peripherals = peripheralsObject as! Array<CBPeripheral>
// 3
if peripherals.count > 0 {
// 4
peripheralCB = peripherals[0]
// 5
peripheralCB?.delegate = self
}
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var consolMessages = ""
switch central.state
{
case .poweredOff:
consolMessages = "BLE is powered off"
case.poweredOn:
consolMessages = "BLE is powered on"
let serviceUUIDs = [CBUUID(string: "B0702880-A295-A8AB-F734-031A98A512DE") as AnyObject]
let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : false]
manager.scanForPeripherals(withServices: serviceUUIDs as? [CBUUID], options:dictionaryOfOptions)
// manager.scanForPeripherals(withServices: nil, options:nil)
case.resetting:
consolMessages = "BLE is resetting"
case.unauthorized:
consolMessages = "BLE is unauthorized"
case.unknown:
consolMessages = "BLE is unknown"
case.unsupported:
consolMessages = "unsupported"
}
print("\(consolMessages)")
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("peripheral \(peripheral)")
print("peripheral Name \(peripheral.name)")
if #available(iOS 9.0, *) {
peripheral.accessibilityAssistiveTechnologyFocusedIdentifiers()
} else {
// Fallback on earlier versions
}
print("peripheral Name \(peripheral.name)")
peripherals.append(peripheral)
manager.connect(peripheral, options: nil)
let AdvertsatingData = advertisementData[CBAdvertisementDataManufacturerDataKey]
print("AdvertsatingData\(AdvertsatingData)")
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("peripheral Connected")
print("peripheral didConnect \(peripheral)")
print("Connected peripheral Name \(peripheral.name)")
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
let alert = UIAlertController(title: "Alert", message: "didFailToConnect", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("peripheral Disconnectd")
print("Disconnect peripheral Name \(peripheral.name)")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if error != nil{
}
else {
print("didDiscoverCharacteristicsFor")
}
}[![This is the Beacon Simulator https://i.stack.imgur.com/diyWA.png ][1]][1]