1
votes

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

1 Answers

0
votes

A few reasons this does not work:

  • MactsAsBeacon sends out iBeacon advertisements, which are BLE Manufacturer advertisements and not not have a Bluetooth Service UUID. Do not confuse an iBeacon ProximityUUID with the Service UUID you use for scanning with CoreBluetooth Bluetooth. They are fundamentally different things.

  • CoreBluetooth will not get background callbacks unless a Service UUID is supplied. That is a restriction imposed by Apple.

  • Even in the foreground, CoreBluetooth cannot read beacon identifiers from the data of an iBeacon packet. Apple clears out this data for iBeacon packets received on iOS.

To read beacon packets on iOS you must use CoreLocation APIs. There is no other way.

Be careful to ensure you can detect transmissions from MactsAsBeacon using an off the shelf iOS beacon detector. The Mac program does not transmit on some flavors of MacOS.