iOS - Cancel bluetooth connection when app in background

别来无恙 提交于 2021-02-08 06:43:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!