问题
I have an iOS app connecting to a peripheral over BLE. I'm able to cancel the connection immediately when the app enters the background, or leave the connection active until iOS suspends the app.
Is there a programmatic way to keep the connection active for a brief period, but ensure it disconnect within a certain time - say after 60 seconds - to minimize battery impact and make the peripheral available for other iOS devices?
回答1:
First, to get the BLE connection working in the background, request for background mode by adding the following key to your info.plist: Required background modes
(array). Add App communicates using CoreBluetooth
item to this array.
Second, in the AppDelegate's applicationDidEnterBackground
fire a timer logic that calls a method which implements [CBCentralManager cancelPeripheralConnection]
来源:https://stackoverflow.com/questions/28314215/ios-cancel-bluetooth-connection-when-app-in-background