I am developing an application that allows you to interact with people on your local network.
I have a view called ProfileView()
which has has identifiers inside of it such as that build up the profile for each person.
Essentially, what I want to do is discover people who are on this app on your local network, or who are nearby to you based on bluetooth. I do not want to use a server, as I would like this to be an application that does not require internet access to function. Also if possible, I would like a toggle to allow yourself to be discovered in the background, even if not using the app.
Any ideas how to do this? Also, is there any better way to do this instead of Bluetooth and Local Network?
Thank you
Possible code chunks needed:
- Discover nearby bluetooth users
- Discover nearby network users
- Toggle for discovery
- Toggle for background discovery (while not using app)
- Share profile (mainly just text and a profile image)
And on the Bluetooth side while this is technically possible, your success in actual use situations will vastly vary, and whether the results are acceptable or not will depend on your use case and expectations.
I will use two devices as an example, one will be advertising its existence, and the other will be scanning for the same app advertising. Once they find each other, they can connect via Bluetooth and exchange the information they need.
Apps can advertise and scan using Bluetooth LE (via the CoreBluetooth framework) in the background, and this will work fine if at least one of the apps is in the foreground.
Using Bluetooth, while you can have a successful implementation when both apps are in the foreground, the chances of this working will drop when one of them in the background, and will hardly work when both are in the background. So, unless your use case involves users having the app open scanning for other users of the app, I don't believe this will be a successful solution.]]
When a scanning app is in the background, two things change specifically. One, you will be limited on the actual information you can transfer through advertising, so 1-1 connections will be necessary. And the scanning is slowed down that will make chance encounters difficult, requiring the two devices to be nearby each other for a longer amount of time.
Understanding these are the hard limitations, there is also a set of changes in the system behavior that will make two iOS devices to be able to detect each other not practically feasible.
In general, advertising is done on a best effort basis. As Bluetooth is a shared resource, when other apps and system resources need to use Bluetooth, advertising for apps in the background will slow down, and may even stop for short periods of time. The rate of advertising is directly related to the probability and time required to be discovered by a scanning device.
On the scanning side, the scan rate also drops once the app is in the background. Scanning rate is also directly related to a timely discovery. Also, both these rates will further slow down when the phone screen goes off, and even further down after a while when the phone goes to sleep mode.
Unfortunately, all these restrictions and slowdowns end up making the discovery process very slow, to the point of becoming unusable. It could potentially take minutes, if not longer. So, if your use case is that the two devices detect each other promptly when they get close to each other, and both devices are locked, it will probably not going to work as you wish it did.
As these limitations in behavior is systemwide, and by design for power management purposes, there are unfortunately no workarounds we can offer at this time outside of trying to keep at least one side in the foreground.
Argun Tekant / DTS Engineer / Core Technologies