Connect to a backgrounded iOS app acting as a peripheral from a macOS app - ios

I have an iOS and macOS app that make heavy use of Core Bluetooth. The core functionality of the app involves having a central device (CBCentralManager) connect to a peripheral device (CBPeripheralManager), retrieve some data, then disconnect. Either device, whether macOS or iOS can act as either a central or peripheral depending on who needs the data.
At first I thought there was an issue with my code, but after testing the app with two iOS devices, one is able to connect and extract data from the peripheral device even when the peripheral device is locked and the app is in the background. Now according to the docs concerning background advertising on iOS under "The bluetooth-peripheral Background Execution Mode" this is kind of expected:
...they can be discovered only by an iOS device that is explicitly scanning for them.
Is there any way to have a macOS app discover peripherals even when they're backgrounded? I'm sure the code I've written is correct because it's the same across iOS and macOS.
Update 1
I believe that this might be possible just unsure how to approach the issue. Using Apple's Bluetooth Explorer on macOS, I am able to find the desired device AND I'm able to see and read data for my custom service.
This finding is validated from logs from my iOS device about the read requests which are from my peripheralManager(CBPeripheralManager, didReceiveRead: CBATTRequest).

Related

How to save a bluetooth connection to settings using CoreBluetooth and Swift

I have a device I want to keep connected to my iPhone while the app I'm building is not running. Currently, I'm at the point where I connect to the bluetooth device, hop over to settings, and can see the device there (I'm using the Nordic Thingy for testing).
I know it is possible because I have a Bose app that does the same thing with my headphones: it doesn't need me to have the app open or even in the background for the headphones to work after I've connected to them via the app.
I've looked into how to persist the bluetooth connections with my team, but we've only found guides similar to this one on how to save data in the background for autoconnection when the app is open again, or guides on how to work with bluetooth devices when your app is in the background.
Is there a way to mimic Bose's behavior here?

CoreBluetooth advertising in background on iOS 10

First: I have an iPad Air 2, and an iPhone 7. For further reading we estimate that the app is active and open on iPad and in background mode on iPhone. The app is exactly the same, even with same Bluetooth Service UUIDs and same DataLocalNameKey.
I want to write an app that can advertise a bluetooth service in background and is able to discover this service (optimal in background too). As I already read I can't use apples beacon technology cause there it's only possible to get scanning/notified by beacons in near in background (I tested this, works fine) but not to advertise. So I started to use CoreBluetooth as described in the mentioned SO answer cause there it's possible to advertise in background.
My app calls didDiscoverPeripheral method in CBCentralManagerDelegate on the iPhone (app in background), so it detected the iPad. The isAdvertising property of CBPeripheralManager on iPhone is true. But didDiscoverPeripheral is not called on iPad. I'm a little bit confused. One option is my iPad is not able to detect the iPhone anymore for some reasons or my iPhone is lying and it's not advertising.
So i thought I'm just a little bit of dumb and googled for "CoreBluetooth debug apps". I found Vicinity and AltBeacon. And with both apps the behavior is the same! If you background (press home/sleep button) the advertising app, the browsing app is not able to discover it anymore. If you open the backgrounded the app it will instant discover it. Both apps mention that its possible with them to broadcast in background.
Am I doing something wrong; is this behavior expected? Did I misunderstood the framework? Can you confirm this behavior?
To confirm this, the fastest way is to install Vicinity on two devices (you have to add the NSBluetoothPeripheralUsageDescription key to Info.plist) set one device to broadcast and press the home button.
This isn't anything specific to iOS 10 -- it's always been this way on iOS. On iOS, apps simply cannot send out standard Bluetooth LE advertisements when they are in the background. They have to be in the foreground to do this.
That said, there is limited support in iOS for apps advertising GATT Services in the background. This uses a proprietary technique that only works with other iOS devices that are looking for those services. (Because it uses a non-standard proprietary scheme, the same technique won't work for beacon advertisements, for example.)
Here's how Apple describes it:
...you should be aware that advertising while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is advertising while in the background:
The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised.
All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them.
If all apps that are advertising are in the background, the frequency at which your peripheral device sends advertising packets may decrease.
Read more here: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html
it was a bug in ios 10.0.(1) and partially fixed in ios 10.1
src: https://forums.developer.apple.com/thread/51309

Constantly Open BLE Connection Between OS X and iOS

I have been exploring CoreBluetooth and BLE connections between OS X and iOS, where I've discovered an interesting problem. I have the Mac implementing CoreBluetooth central and iOS implementing CoreBluetooth peripheral. Since iOS no longer broadcasts a constant, consistent MAC address or other identifier, there is no reliable way (that I can find) to allow the Mac to connect to the iOS peripheral whenever it is in range, including when the iOS app is running in the background.
By storing the last connected peripheral, I can get OS X to constantly search for the iOS device like shown:
[central connectPeripheral:self.lastPeripheral options:nil];
Which works if self.lastPeripheral was previously connected to the OS X client. This initial connection can only be created if the iOS app is open at the time; it will not work if it is backgrounded. This in itself is not a horrible issue, but every single time the OS X app quits, this connection must be reinstated by opening the iOS app.
Is there any way to connect and reconnect (when the device moves back into range) via BLE to an iOS device when the iOS application is backgrounded, especially to persist between app launches?
BluetoothLE is still a energy consuming technology. And Apple goes into great lengths to limit its "active" usage on iOS, in situation they don't deem as correct (e.g. background). That said, there are no guaranties on for how long a iOS device in peripheral mode continues to advertise (pre-requirement for reconnection).
Did you think about switching the roles. Making the OS X machine the peripheral, and the iOS device the central? I know from experience that iOS behaves much nicer when acting as a central.

Core Bluetooth unable to discover peripheral

I am working with hardware that is in the process of being developed. The board uses a TI CC2541 as the bluetooth chip.
Prior to a test the board is discoverable by two devices and I am able to connect to the board and perform a test where I write and read characteristics (GATT). I perform the test again, everything goes fine until I reconnect to the device and attempt to discover its services. At that point, the board is offline and neither lightblue nor my own app can detect the device. I did a factory reset on one of the iOS devices (iPads) and it is still unable to recognize the board.
Should a factory reset remove all corebluetooth caching that has happened on the device, allowing iOS to re-cache and rediscover the board?
Can this possibly be a corebluetooth problem or a problem with my app?
Your problem seems to be related to the external peripheral. Factory reset will remove any cached data from the iOS device so as long as the peripheral behaves correctly, the rediscovery should happen as planned. This is all I can tell from the info you shared.
This issue seems to be related to hanging / adding characteristics on a peripheral during development.
I have had a similar issue and instead of clearing the cache on the iOS device, I change the Bluetooth address of the peripheral. This allows the discovery to work as expected.

Communicating with a Bluetooth Classic device in iOS when the app is in the background?

I'm working on iOS development for a bluetooth peripheral. We want the (already paired) device to connect when a button is pressed on it, and this will cause the app to execute some code. Is it possible for the app to detect that the device is connected, even while in the background?
Also, is it possible for the app to bring itself into the foreground when this is detected? Would I need to be a member of the MFi program to do either of these things?
You will be able to detect that the device is connected in the background using Bluetooth 2.0, however you must include the App communicates with an accessory string under Required Background Modes in the app's plist.
So yes, the first part of your question is doable. However when you say: "We want the (already paired) device to connect when a button is pressed on it", it is important to note that this functionality must be built into the device's firmware. The app can always be searching for it, but the device will not be able to communicate with the phone until it connects. Therefore, the button must wake up the device and signal that it now wants to connect.
For the third part of your question, you will not be able to bring the app into the foreground without the user opening the app.
Btw, CoreBluetooth is the Bluetooth Low Energy framework for iOS. It is completely different from Bluetooth 2.0.
as ttarules told you iphone can't connect automaticly to a paired device.
I don't know what kind of bluetooth receiver have you in your device but generally you can set up a method of "AUTOCALL" that automatically call a paired device (in this case an iPhone).
When you (your iPhone) come around the device it automatically establish a connection!
If you set all the parameters correctly in the bluetooth module you can make sure that the iPhone shows a message like this: "YourDevice would like access to YourApplication" when the connection is established.
this way helps the user to use your application and you can run the code you mentioned above when the app start.

Resources