0

When an iOS BLE peripheral is in foreground state, centralManager discovers the peripheral correctly, once it enters background state it can not be discovered anymore.

Here is how I start advertising on the peripheral device:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    if peripheral.state == .poweredOn {
        let advertisingData: [String : Any] = [
            CBAdvertisementDataServiceUUIDsKey: [uuid],
            CBAdvertisementDataLocalNameKey: "myLocalName"
        ]
        self.peripheralManager?.startAdvertising(advertisingData)
    } else {
        print("handling other states")
    }
}

Here is the central manager device

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    switch central.state {
    case .poweredOn:
        let options = [CBCentralManagerScanOptionAllowDuplicatesKey: false]
        centralManager.scanForPeripherals(withServices: [uuid], options: options)
    default: print("bluetooth failed")
    }

}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    
    print("new device: \(peripheral)")
    
}

The scanning device is in foreground while the advertising device in background, please note that when both devices are in foreground everything is ok.

Background modes are enabled both "Uses Bluetooth LE accessories" and "Act as a Bluetooth LE accessory"

According to apple documentation:

The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised.

All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them.

I think I'm explicitly scanning for the peripheral device. What am I missing inorder to get it discovered

Community
  • 1
  • 1

0 Answers0