MDNS and caffeine: How I got Bonjour running on my iPhone
See this picture? It's a picture of my iPod touch. But here is the interesting thing. The picture you see is actually a screen shot from my iPod touch. As you can see from the iPod logo, it has no on-board camera. Instead, it's subscribed to a Bonjour service on my iPhone and using its camera instead to take a picture of itself.
Holy. Cow. Do I ever need some rest. I just spent most of last night figuring out how to get Bonjour (aka mDNS) running on my iPhone. Bonjour offers configuration-free networking technology. Computers (and iPhones) automagically broadcast their services, which you can then listen for and take advantage of. This has been a long-term project that I've been plugging away at for months.
So what makes this exciting? With Bonjour, your iPhone and touch can talk to your Mac. They can talk to printers or to each other. They can browse for available services or provide their own as a server. What I put together was a pair of applications: PicSend and PicListen. These apps allow you to subscribe to a camera on another person's iPhone. Just tell that iPhone to snap a picture and within seconds that picture appears on your own screen.
PicSend and PicListen
You can download a copy of PicSend and PicListen from my FTP site. They work like this:
Install PicSend on any iPhone. This is the server app and needs to run on a unit with a working camera.
Install PicListen on either a touch or an iPhone. It subscribes to the PicSend service and tells the iPhone to snap pictures and send them on over.
PicSend has one button that freezes and unfreezes the camera display. When stopped, the live preview ends and a single image displays until you resume.
PicListen has one button too. It's labeled "snap". Tap it to tell the server to take a picture. If you're holding the server you know when someone snapped a picture--you'll hear the camera shutter "click".
PictureSharing and PictureSharingBrowser
Don't have two iPhones on-hand? Apple provids two sample code programs that tie right into these utilities. PictureSharingBrowser and PictureSharing both run on the Mac. Browser is the client, PictureSharing is the server. They're using the same Bonjour protocol so if you set your service to PicBrowser, you'll be able to send pictures to your iPhone or see pictures from the iPhone on your Mac.
To send a picture to, say, your iPod touch, just drop it into PictureSharing and press Start. PicListen will scan for Bonjour services, find the picture, and display it.
Similarly, PictureSharingBrowser allows you to see the pictures you snap on your iPhone.
Normally programming in Bonjour is really easy. That's because Apple offers some fabulous Objective-C classes (like NSNetServices) that take care of most of the messy bits. In the world of Apple, everything that is Cocoa and Objective-C is good and lovely and yummy. Unfortunately, although the Bonjour daemon comes pre-installed on iPhone, the supporting classes did not.
That leaves us in the world of Core Foundation. Everything that is CF is bad, and evil, and C++-like, and just...messy. Everything you can do in heavenly Objective-C, you can do in evil Core Foundation. I spent hours laboriously porting Apple's CFNetServices samples to iPhone.
So what's the big deal?
Sending pictures back and forth between iPhones isn't a huge deal until you realize that you're not limited to pictures. With Bonjour, you can send files, data, sounds, videos, anything that transfers from one computer to another. I'm hoping to adapt the code to work next with sound. Sure, you can do a lot better at Target in terms of buying sub-$400 walkie-talkies, but again we're dealing with baby steps here. I'm hoping that the iPhone will soon be able to "discover" printers in its area and offer to print out pictures or webpages or people can use Bonjour to play interactive games. The possibilities of social computing with iPhones, iPod touches and Bonjour are practically limitless.
Got some ideas, suggestions or requests? Let me know in the comments.
Thanks to GeeKdLL and Drudge for testing.
Subscribe to Newsletter
Software Updatesmore updates
- Fantastical 2.1 for iOS adds new snooze, search and notification features
- ExpanDrive 4, more services and faster sync
- Apple adds iTunes Extras to Apple TV
- Spotify updates with new iPhone controls in time for summer BBQs
- iTunes U update will bring course creation and student discussion to iPad app
- Dropbox for iOS update adds new setup and file management options