I am wondering the possibility of directly using bonjour over bluetooth in iPhone OS 3.0 or later without using GameKit. Can anyone provide any examples ?
-
3In your edit of March 11th 2012, you have completely changed the question. After receiving three answers and after being linked to from other questions, that's bad practice and makes it seem as if people who kindly answered to you are now rambling, providing unrelated answers. Could you please ask a NEW question, instead? :-) – Ivan Vučica Jun 06 '12 at 10:33
3 Answers
Just announce the service, just like tc. has said below:
self.netService = [[[NSNetService alloc] initWithDomain:@""
type:@"_http._tcp"
name:@""
port:8080] autorelease];
[self.netService publish];
With iOS5, however, let's-call-it "Bluetooth Bonjour" is disabled by default, so you have to use the C API declared in <dns_sd.h>
.
DNSServiceRef serviceRef;
DNSServiceRegister(&serviceRef, // sdRef
kDNSServiceFlagsIncludeP2P, // interfaceIndex
0, // flags
NULL, // name
"_http._tcp", // regtype
NULL, // domain
NULL, // host
1291, // port
0, // txtLen
NULL, // txtRecord
NULL, // callBack,
NULL // context
);
This is just the announcement part; resolving is a bit more complex. I suggest you take a look at the following examples from Apple:
- SRVResolver - demonstrates how you can look up a service using API declared in
<dns_sd.h>
. Targets OS X, but includes a class calledSRVResolver
which you can use on iOS as easily as you can use it on OS X. For iOS 5 Bluetooth P2P to work, update the call toDNSServiceQueryRecord()
to passkDNSServiceFlagsIncludeP2P
as theinterfaceIndex
. (NOTE! This sample does not seem to exist in OS X 10.8 docset. It can be found in 10.6 and 10.7 docsets. In 10.8, there's the DNSSDObjects example, but I didn't look exactly at what it does.) - WiTap - as long as you don't actually care about Bluetooth support on iOS 5, just look at the example called WiTap, which demonstrates not only the beautiful Objective-C API, but also how you can create a server using CFSocket APIs (thin wrappers around BSD sockets). You'll want to look at this even if you are using SRVResolver to see how to use C-based API from
<dns_sd.h>
.
After announcing or resolving your service, you use regular BSD sockets to listen or connect. When writing a server, you may even want to first listen()
on port 0 (zero), and then query which random available port was assigned to you. After querying for that, announce this port instead of a fixed one. That's exactly what WiTap example is doing (but with CFSocket API instead of BSD socket API).
For more info on BSD sockets, just Google around for a tutorial.
Note: information about iOS 5 comes from Apple's Technical Q&A QA1753.

- 1
- 1

- 9,529
- 9
- 60
- 111
-
I'm surprised you didn't mention [AsyncSocket](https://github.com/robbiehanson/CocoaAsyncSocket) anywhere.. do you recommend for/against it? or using the APIs mentioned above should be enough? – abbood Oct 18 '12 at 12:07
-
@abbood: I haven't used AsyncSocket. However, as long as you announce a service as described in my answer, the OS will fire up a Bluetooth PAN service that can connect two iOS devices. This enables you to establish a TCP or UDP connection over Bluetooth instead just WiFi or GSM. How will you actually go about establishing it (directly BSD sockets or via a wrapper such as AsyncSocket) -- that's actually irrelevant. :-) – Ivan Vučica Oct 18 '12 at 15:32
Read this article : Bonjour over Bluetooth on iOS 5.0 https://developer.apple.com/library/ios/#qa/qa1753/_index.html#//apple_ref/doc/uid/DTS40011315
It is a known issue in IOS 5.0 and need to be resolved using lower level API : DNSSDObjects.

- 861
- 1
- 9
- 23
If Bluetooth is enabled, on a new-enough device (3G and above, or iPod equivalent, or iPad) and a new-enough OS (3.1 apparently), Bonjour will automatically work over Bluetooth using link-local addresses (168.254.*). Then you just use TCP/UDP normally.
(Under the hood, I'm pretty sure GameKit uses Bonjour-over-IP-over-Bluetooth.)
Sypposedly the publishing/browsing is done at the Bluetooth layer, but if one publishes a Bonjour service and the other browses for it, an automatic IP-over-Bluetooth connection is established.
Any Bonjour examples should automatically work.

- 33,468
- 5
- 78
- 96
-
1Do you know which Bluetooth service is used, and how one can talk to iOS devices with Linux, Mac or one's own embedded device equipped with a Bluetooth chip? – Ivan Vučica Nov 09 '11 at 20:08
-
@IvanVučica: I believe it's a Bluetooth PAN, but I've heard that the service is also advertized over bluetooth and the PAN isn't started unless necessary. – tc. Nov 20 '11 at 23:41
-
2I have detailed some of my observations here: http://stackoverflow.com/questions/8070998/using-ios-gamekits-bluetooth-bonjour-with-other-platforms However, I need technical details. It does smell of PAN, but advertised in a slightly different way, just to make it look like it isn't PAN. I didn't have the chance to sniff the protocol yet, though. – Ivan Vučica Nov 22 '11 at 10:43
-
2It is even advertised as 0x1115 - PANU - except it looks like OS X does not know how to directly talk to another PANU device. Confusing. I'll explore more when I get to a Linux machine :) – Ivan Vučica Nov 25 '11 at 18:32