0

I am looking to figure out which delegate methods fire reliably when connecting / disconnecting in the background when terminated. Whenever the user gets close enough to my BLE device, I want it to auto connect and then fire a quick method to exchange data with the device.

I cannot seem to get any of the delegate methods to fire reliably with this use case. I have background modes for bluetooth-central turned on, I have the proper delegates in use and the whole BT module I wrote works really well when the app is active or in the task switcher. I just need to finalize with this last use case, the app being terminated.

My original plan was to use iBeacon but this ended up being a flawed approach to wake up the application from terminated because I NEED the bluetooth pairing to remain and persist. The only way to get iBeacon background/terminated events like didEnterRegion or didExitRegion to fire, is to "forget this device" (in the iOS system BT menu) after the app went through the connecting/pairing phases with the user. So this can't work because unless the user does this for me, the app will never be able to un-pair from a device (I even tried an AT+command in the box to wipe auth/bond and disconnect).

So... Now the only plan I have left is to find a way to have the centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral and didDisconnectPeripheral methods fire whenever the iOS system reconnects. So when the device turns on or the user gets close enough to be back in range.

GoreDefex
  • 1,461
  • 2
  • 17
  • 41
  • Honestly I find that Core Bluetooth is not that reliable in the terminated state. I wrote something about it a while back: https://stackoverflow.com/questions/37493684/ios-stops-waking-up-the-app-upon-incoming-ble-connection-from-peripheral/37529843#37529843 Some of the previous issues that I had with CB have been fixed in the iOS11 betas, but unfortunately I have encountered some new serious ones instead in my own testing. My suggestion is to only use CB if it is like an extra "feature" of your app and not if it is a core part of your app/business idea. – Anton Aug 02 '17 at 13:26
  • unfortunately due to the nature of iOS I have no choice and am forced because of their policies, to use this bluetooth peripheral as the central core of my entire business. I have read your article a few times over hoping that somethings have changed since you wrote it. I wrote this article (https://stackoverflow.com/questions/45241033/obj-c-how-to-app-using-ble-connection-and-ibeacon-in-same-device) but no one has responded. It states my problems using iBeacon/BLE in same device to wake-up and respond. – GoreDefex Aug 02 '17 at 13:30
  • I have several pending bug submissions and feature enhancement requests and in iOS11 they have applied some of those and things appear to be moving in the right direction. But as I said it is not good yet. Anyhow, if you implement State Preservation & Restoration correctly then, in theory, all you need to do is to make sure that you always have a pending connection to the device. If you are developing your own hardware then you can make improvements on that side as well. For example we cancel the connection on the peripheral side in order to avoid the issue you mentioned in your post. – Anton Aug 02 '17 at 13:39
  • I tried to make a command over serial that the box receives and responds by using an AT+Command to wipe the auth/bond and disconnect all devices but iOS just reconnects at the system level and I get stuck. Also, I need to pair in order to use the charactertics so if I wiped those when the user leaves the device's area then I would need to ask the user every time to-repair... also the "Would you like to pair?" message doesn't come up at all when you connect the device from a terminated state. – GoreDefex Aug 02 '17 at 13:43
  • I have a ton of code in my app for restoration, I can post it all if you like but it seems to me the only time any of that stuff fires is when I turn on/off the iOS system level bluetooth option. If I turn it off then after already just being in the app and getting the user to press "pair", kill the app and turn back on the BT option, the `centralManagerDidUpdateState` method fires quite reliably. From here I can reconnect and send the data to the device and it works! However, it does not fire when I leave the area of the BT signal , or turn off/on the BT peripheral itself though. – GoreDefex Aug 02 '17 at 13:45
  • state restoration is only supposed to wake up an app if you happen to have a pending connection (connectPeripheral:) or a current connection to a peripheral and that peripheral either comes within range or goes out of range. I unfortunately wont have time to look at your code, but I can't seem to understand exactly what you mean. What is "the box" and how do you mean "AT+Command" ? – Anton Aug 02 '17 at 13:50
  • "The box" is a Bluefruit Feather Micro-controller with BLE and iBeacon enabled. "AT+Commands" are commands that you run in the box's boot-loaded sketch file or over the serial port. Those commands perform system level tasks such as restarting the box, changing the device name, wiping auth/bond, turning on/off BLE, etc... – GoreDefex Aug 02 '17 at 13:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150838/discussion-between-goredefex-and-anton). – GoreDefex Aug 02 '17 at 14:27

1 Answers1

0

I have finally found the answer! The reliable method is definitely iBeacon... which I thought I couldn't use because I am using a bluetooth paired connection and iBeacon isn't advertised when your device is connected. This is, "sorta" correct in the fact that you cannot detect iBeacon output from a device that is already connected to something else... what I hadn't thought of until recently is building a board with 2 Bluetooth chips in it!

What I did was use one BT chip for a dedicated iBeacon ranger to wake up the app in a more stable and reliable way and the other chip for a dedicated BT connected/paired/auth/bonded state. Now I can use my encrypted custom characteristics in the device as well as having the device wake my app back up once terminated or asleep! :)

Here is an article where I posed a similar question and answered.

GoreDefex
  • 1,461
  • 2
  • 17
  • 41