0

I want to scan for Bluetooth peripheral in the background and make an API Call with found peripherals. I am doing this.

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    let theViewController = loginVC()
    APIManager.sharedInstance.pushtracerData(vc: theViewController, user_id: USERID.sharedInstance.getUserID(), SSID: peripheral.name ?? "", MAC: peripheral.identifier.uuidString, RSSI: "\(RSSI)", TST: "", tracer_or_mobile: "2") {
        
    }
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
        let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey:NSNumber(value: false)]
        centralManager?.scanForPeripherals(withServices: nil, options: options)
    }
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  
    self.centralManager = CBCentralManager.init(delegate: self, queue: DispatchQueue.main)
    completionHandler(.newData)
    
}

But in the background, this method is not called. Please help me out

yash rathod
  • 53
  • 10

1 Answers1

0
    centralManager?.scanForPeripherals(withServices: nil, options: options)

From the docs:

Your app can scan for Bluetooth devices in the background by specifying the bluetooth-central background mode. To do this, your app must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter.

It's recommended in all cases that you scan for specific services and not pass nil here, but in the background it's required.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • from where i can get serviceUUID? – yash rathod Jul 24 '20 at 14:00
  • From the documentation of the device you're scanning for. They should tell you what services they advertise. Alternately, you can gather the information directly by inspecting the advertising packets of the devices you want to discover. – Rob Napier Jul 24 '20 at 14:30
  • but did discover method is not calling in the background. How should I get advertising packets? – yash rathod Jul 24 '20 at 14:54
  • By scanning your test device in the foreground (or more generally with a scanner like LightBlue). What type of device are you searching for? – Rob Napier Jul 24 '20 at 15:09
  • I am searching for mobile devices – yash rathod Jul 24 '20 at 15:18
  • There is no way in iOS to search for arbitrary other phones nearby. What is your goal with the results? – Rob Napier Jul 24 '20 at 15:24
  • i have to pass uuid to server with API call – yash rathod Jul 24 '20 at 19:08
  • I don't know what that means. What uuid? What server? What API? Why do you want (the uuid? what uuid?) of other devices (that doesn't mean anything by itself; devices can have many, many different UUIDs associated with them) to pass to a server? What is the server doing with these? – Rob Napier Jul 24 '20 at 19:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218561/discussion-between-yash-rathod-and-rob-napier). – yash rathod Jul 25 '20 at 06:26