react-native-ibeacon-simulator icon indicating copy to clipboard operation
react-native-ibeacon-simulator copied to clipboard

Works only when the application running the front

Open ghost opened this issue 7 years ago • 5 comments

How to make broadcasting to work even when the application running in the background?

ghost avatar Mar 21 '17 14:03 ghost

Unfortunately, no. 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.

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

williamtran29 avatar Mar 28 '17 03:03 williamtran29

@williamtran29 I think there are still ways. Check this. Android unable to detect iOS when iOS app running in the background. I think I'm missing something

ghost avatar Apr 09 '17 09:04 ghost

Core Bluetooth Background Execution Modes

If your app needs to run in background to perform certain Bluetooth-related tasks, it must declare that it supports a Core Bluetooth background execution mode in its Information property list (Info.plist) file. When your app declares this, the system wakes it up from a suspended state to allow it to handle Bluetooth-related events. This support is important for apps that interact with Bluetooth low energy devices that deliver data at regular intervals, such as a heart rate monitor.

There are two Core Bluetooth background execution modes that an app may declare—one for apps implementing the central role, and another for apps implementing the peripheral role. If your app implements both roles, it may declare that it supports both background execution modes. The Core Bluetooth background execution modes are declared by adding the UIBackgroundModes key to your Info.plist file and setting the key’s value to an array containing one of the following strings:

bluetooth-central—The app communicates with Bluetooth low energy peripherals using the Core Bluetooth framework. bluetooth-peripheral—The app shares data using the Core Bluetooth framework. Note: The property list editor in Xcode by default displays human-readable strings for many keys instead of the actual key name. To display the actual key names as they appear in the Info.plist file, Control-click any of the keys in the editor window and enable the Show Raw Keys/Values item in the contextual window. For information about how to configure the contents of your Info.plist file, see Xcode Help.

The bluetooth-central Background Execution Mode When an app that implements the central role includes the UIBackgroundModes key with the bluetooth-central value in its Info.plist file, the Core Bluetooth framework allows your app to run in the background to perform certain Bluetooth-related tasks. While your app is in the background you can still discover and connect to peripherals, and explore and interact with peripheral data. In addition, the system wakes up your app when any of the CBCentralManagerDelegate or CBPeripheralDelegate delegate methods are invoked, allowing your app to handle important central role events, such as when a connection is established or torn down, when a peripheral sends updated characteristic values, and when a central manager’s state changes.

Although you can perform many Bluetooth-related tasks while your app is in the background, keep in mind that scanning for peripherals while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is scanning for device while in the background:

The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event. If all apps that are scanning for peripherals are in the background, the interval at which your central device scans for advertising packets increases. As a result, it may take longer to discover an advertising peripheral. These changes help minimize radio usage and improve the battery life on your iOS device.

The bluetooth-peripheral Background Execution Mode To perform certain peripheral role tasks while in the background, you must include the UIBackgroundModes key with the bluetooth-peripheral value in your app’s Info.plist file. When this key-value pair is included in the app’s Info.plist file, the system wakes up your app to process read, write, and subscription events.

In addition to allowing your app to be woken up to handle read, write, and subscription requests from connected centrals, the Core Bluetooth framework allows your app to advertise while in the background state. That said, 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.

ghost avatar Apr 09 '17 09:04 ghost

Any solution to this yet? I need iOS advertising in the background state to be detectable by Android scanning in the foreground.

https://stackoverflow.com/questions/30874311/read-advertisement-data-on-android-when-an-ios-device-is-advertising

The advertisement is limited to 31 bytes so there is no room to place several 128bit UUIDS. You can try to pass [CBUUID UUIDWithString:@"16bit uuid"] to the advertising data.

Does this work?

ahimsadas avatar Nov 12 '19 09:11 ahimsadas

Did anyone find a solution for this package to make it work in the background ? Please guide me on this if you have a solution

RashVenkat6795 avatar Apr 17 '20 10:04 RashVenkat6795