Bonjour detection

Hello,


I am working on a project and my goal is to detect if a smartphone is or not connected to the local WiFi network. I first wanted to use Bonjour to do so, but I recently realized that I was not able to see any iDevice while scanning with several Bonjour tools (dns-sd command in terminal, and Bonjour discovery program).


No matter if the iDevice are alive or in sleep mode, I just can not find them. Apparently Apple is moving back on it's strategy, and for privacy reasons is removing Bonjour stuff from the OS core.


I would like :

1) To be sure I have understood everything right, and if it is realy impossible to discover iDevices with bonjour ?

2) To know any other way to discover an iDevice and more generaly a Smartphone connected on the local WiFi network.

3) To know how to ping a iDevice when it is in sleep mode, all my ping requests fail 30 seconds after the iDevice enters in sleep mode.


Thank you.

Apparently Apple is moving back on it's strategy, and for privacy reasons is removing Bonjour stuff from the OS core.

That's a very odd way to put things. Bonjour has always been about advertising services, not devices. iOS has no services to advertise by default, and thus it does not appear to Bonjour. This is expected behaviour.

OTOH, if you register a Bonjour service from within an app that you run on the device, it shows up in Bonjour just fine.

Remember that the goal of Bonjour is to help users connect to services, not to provide a network management protocol.

1) To be sure I have understood everything right, and if it is realy impossible to discover iDevices with bonjour ?

Correct.

2) To know any other way to discover an iDevice and more generaly a Smartphone connected on the local WiFi network.

In general there's no good way to do this. You can ping every IP address but...

3) To know how to ping a iDevice when it is in sleep mode, all my ping requests fail 30 seconds after the iDevice enters in sleep mode.

To start, be aware that there's a difference between "screen lock" (what most users call sleep) and "device sleep" (when the main CPU actually shuts down, as it does on a MacBook when you close the lid). Locking the screen does not necessarily sleep the device, as the CPU may remain awake to handle various tasks (for example, music playback). The following discussion uses the terminology defined in this paragraph.

The situation with device sleep depends on whether the device has working WWAN or not. If the device has WWAN then it will disassociate from Wi-Fi on device sleep. That's because critical system services (like push notifications) are run over WWAN and thus the device can save power, by disassociating from Wi-Fi, without losing any functionality.

If the device has no working WWAN (either no WWAN or WWAN is disabled for some reason) then it stays associated with Wi-Fi but the main CPU has to program the Wi-Fi chip to tell it what events need to wake up main CPU. I'm not sure whether this programming allows for pings to wake the main CPU, but I expect not.

IMO the best way to find all the devices associated with the Wi-Fi is to ask the access point.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you eskimo for your clear reponse.

My mind was a bit blurred yesterday, but now with your explanations about Bonjour, the iPhone behavior makes sense.

In that case, do you think if I register a Bonjour service through an App, will the service be still viewable from an other device after the app's closed ? And what after the app is totally killed ?


You explanations about wifi in "screen off" mode is clear. I have no sim card in my test device and the only connection is made with WiFi. The ping is not available in sleep mode.



EDIT : So after several hours, I have made a prototype app that just advertise a fake Bonjour service. It advertise well while the app is running on foreground, but if the app goes to the background or if it is killed the advertisement stops.

I would like it to continue , is this possible ?

To continue on my investigations, I think that using Bonjour would be quite impossible in my case because I want to know when people are home or not (with their approval of course, and they will be aware of that).

But if the phone advertises only while awake (so, when the user is doing something or if a push notification arrives and turns on the CPU and WiFI chip for a few seconds), it means that using Bonjour would not be reliable at all. The system would think that the user left home every 30 seconds when the phone goes into sleep and stops Bonjour services.


I was thinking using other things, and I thaught about pinging and/or using other ports that would maybe stay open in sleep mode. I understand that iOS stays in permanent connection with the push notification servers, so I thaught it would maybe keep some other ports pen as well ?


Where can I find a complete listing of used ports by iOS ? With details about witch port is used for what and when ?


Thank you.

In that case, do you think if I register a Bonjour service through an App, will the service be still viewable from an other device after the app's closed ?

That depends on what you mean by "closed". When the app goes into the background it becomes eligible for suspension. If it does get suspended, all Bonjour operations are terminated—because the UNIX domain socket used by your app to communicate with the Bonjour daemon has its resources reclaimed—and that means that registered services are removed.

Technote 2277 Networking and Multitasking has more details on this.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I was thinking using other things, and I thaught about pinging and/or using other ports that would maybe stay open in sleep mode. I understand that iOS stays in permanent connection with the push notification servers, so I thaught it would maybe keep some other ports pen as well ?

AFAIK it doesn't keep open any listening ports, which are the only ones you can reliably ping.

Where can I find a complete listing of used ports by iOS ?

AFAIK there's no such list.

Regardless, the above approach makes no sense given the WWAN stuff I mentioned earlier. If you care about iPhone (and other WWAN-capable devices) then you have to recognise that the device will not necessarily associate with the home Wi-Fi when the user gets home and, as such, you can't use any Wi-Fi based technique to determine that case.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Bonjour detection
 
 
Q