steamclock / bluejay

A simple Swift framework for building reliable Bluetooth LE apps.
MIT License
1.09k stars 98 forks source link

Receiving data when app is in background #254

Open flaviosuardi opened 3 years ago

flaviosuardi commented 3 years ago

Summary: I am facing a problem related to the execution of very long BLE listen operation that cause problems when app is in background.

After a successfully connection I start to listen a characteristic with following code:

blueJay.listen(to: uartTxCharacteristic) { [weak self] (result: ReadResult<ReceivableMultipleDataPacket>) in
                guard let self = self else {
                  return
                }

                switch result {
                case .success(let multipleDataPacket):
                    self.log("<----: \(multipleDataPacket.rawData as NSData) == \(multipleDataPacket) >>> bytesReceived: \(self.bytesReceived)")

                    [here app make business logic stuff]

                case .failure(let error):
                    self.log("error listening characteristic \(error)")
                }
            }

when I send a specific command to BLE peripheral, it starts to send me a lot of data and this operation takes several minutes because peripheral has to send me a lot of stuff. In this situation I can see data arrived in console but if I put app in background the operating system performs one of these two operations:

I suppose the problem are iOS restrictions of background operations on iOS14 but have you ever faced this problem? Do you know a way to allow app to continue receiving data in background?

I confirm that I have enabled "Uses BLE accessories" background mode.

In another app I have developed a BLE communication with a long time data transfer (about 10 minutes) but in that case BLE peripheral implements iBeacon protocol, in this app the receive data operation can be executed and completed also when app is in background. I suppose the reason is because iBeacon protocol that keep app alive with location updates.

Device, Build, OS: all iOS devices, iOS14

Thanks for your help!