iOS: didDiscoverPeripheral not called in Background mode
Paulw11 said are right, If your app find the peripherals in the foreground. It will not call the didDiscoverPeripheral
for the same peripherals when it enters the background.
For more information about the iOS BLE Behavior in the background mode. You can check this answer
What exactly can CoreBluetooth applications do whilst in the background?
Transferring Data from Central to peripheral in Background mode in iOS
To run task in background mode, you need to follow the belowed steps.
Step 1: Declare __block UIBackgroundTaskIdentifier bgTask
as global variable.
Step 2: To add following code in applicationDidEnterBackground
.
- (void)applicationDidEnterBackground:(UIApplication *)application {
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
}
Step 3: Stop background task handler once apps come in foreground mode.
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
Bluetooth LE iOS unable to scan in background
In order to scan for new devices in the background you must specify the UUIDs of the service(s) that you are interested in. The documentation is quite clear on this.
The identifier
for a device is a value locally determined by iOS, not the device itself and will vary for different iOS devices connecting to the same peripheral. If you know the identifier for a device that you have previously discovered then you can use the identifier to connect
to the device but you can’t scan for an identifier.
It is possible for a peripheral to include data in its service advertisement and perhaps this is what your peripheral is doing. If so, then you will not be able to get readings in the background since duplicate device discovery events are not delivered when your app is in the background.
You can discover and connect to a new device in the background as long as you know the service that it is advertising but any subsequent data transfers will require the device to issue a notify on a changed characteristic. It cannot use the manufacturer data portion of the advertising frame.
Related Topics
Uisegmentedcontrol iOS 13 Clear Color
Programmatically Creating Uinavigationcontroller in iOS
How to Use Phcachingimagemanager
Obtaining Bluetooth Le Scan Response Data with iOS
Sprite Kit Sprites Loading Performance Improvement
Uilabel, Uifont and Utf-8 Triangle
Uploading Image with iOS App to Server File Size Is Too Large
Removing Lagging Latency in Drawing Uibezierpath Smooth Lines in Swift
Hide a Phone Call Completely in iOS (Jailbreak Device)
Open Uiimagepickercontroller in Landscape Mode
How to Upload Multiple Image on Firebase Using Swift
What Is the Use of Singleton Class in Objective-C
How to Detect "Clear" Notifications
Swift 3- How to Get Button in Uicollectionviewcell Work
iOS 11 - Unable to Change Navigation Bar Height
Disable Warning Dialog If Bluetooth Is Powered Off iOS
How to Switch to Speaker Output When Bluetooth Headsets Are Connected