Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

iphone - Bonjour over bluetooth WITHOUT Gamekit ?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 called SRVResolver 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 to DNSServiceQueryRecord() to pass kDNSServiceFlagsIncludeP2P as the interfaceIndex. (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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...