CoreBluetooth repeatedly disconnecting

那年仲夏 提交于 2019-12-19 02:47:07

问题


I've looked up and down for this problem, but no one seems to have it.

I have two iPads. One is acting as a Bluetooth peripheral that is always in the foreground. Since I am more interested in the central side, I have made my central iPad background-capable for BLE-central. My workflow is as follows:

  1. Central app runs and starts scanning
  2. Peripheral advertises service (or comes within range)
  3. Central device connects to peripheral and stops scanning
  4. Peripheral stops advertisement (or leaves range)
  5. Central device disconnects and starts scanning
  6. Do steps 1-5 seamlessly whether the central is in the foreground or background

When I run my central iPad app, steps 3 and 5 repeat every 10 seconds, even though the service is being advertised the entire time. This happens in the foreground and background.

On disconnect, I get this error:

Error occurred while disconnecting peripheral: The specified device has disconnected from us.

Any ideas on why it keeps disconnecting? Does CoreBluetooth clean up the connections if no data is being transferred? Right now, my central is doing a one-off read of peripheral characteristics. It is not subscribing to updates, since the values are static. Thanks!!


回答1:


In my case the solution was to subscribe to the characteristic regardless of whether the value is static or not. It seems as though subscribing will keep the connection alive, whereas if you read one-off values the framework will try to conserve battery life and drop the connection, forcing you to manually reconnect before performing another read or write.



来源:https://stackoverflow.com/questions/19432688/corebluetooth-repeatedly-disconnecting

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