在没有 Gamekit 的情况下通过蓝牙体验 Bonjour ? [英] Bonjour over bluetooth WITHOUT Gamekit ?

查看:15
本文介绍了在没有 Gamekit 的情况下通过蓝牙体验 Bonjour ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在不使用 GameKit 的情况下在 iPhone OS 3.0 或更高版本中通过蓝牙直接使用 bonjour.谁能提供任何例子?

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 ?

推荐答案

只需宣布服务,就像 tc.已经在下面说:

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];

但是,在 iOS5 中,默认情况下禁用Bluetooth Bonjour",因此您必须使用 <dns_sd.h> 中声明的 C API.

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
                   );

这只是公告部分;解析有点复杂.我建议您看一下 Apple 的以下示例:

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 - 演示了如何使用 <dns_sd.h> 中声明的 API 来查找服务.针对 OS X,但包含一个名为 SRVResolver 的类,您可以像在 OS X 上一样轻松地在 iOS 上使用它.要使 iOS 5 蓝牙 P2P 正常工作,请将调用更新为 DNSServiceQueryRecord()kDNSServiceFlagsIncludeP2P 作为 interfaceIndex 传递.(注意!这个示例在 OS X 10.8 文档集中似乎不存在.它可以在 10.6 和 10.7 文档集中找到.在 10.8 中,有 DNSSDObjects 示例,但我没有看正是它所做的.)
  • WiTap - 只要您实际上并不关心 iOS 5 上的蓝牙支持,只需查看名为 WiTap 的示例,它不仅演示了漂亮的 Objective-C API,还演示了如何使用 CFSocket 创建服务器API(围绕 BSD 套接字的薄包装).即使您正在使用 SRVResolver 来查看如何使用 <dns_sd.h> 中的基于 C 的 API,您也会想看看这个.
  • 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>.

在宣布或解决您的服务后,您使用常规 BSD 套接字进行侦听或连接.在编写服务器时,您甚至可能希望首先在端口 0(零)上 listen(),然后查询分配给您的随机可用端口.查询后,宣布此端口而不是固定端口.这正是 WiTap 示例所做的(但使用 CFSocket API 而不是 BSD 套接字 API).

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).

有关 BSD 套接字的更多信息,请谷歌搜索教程.

For more info on BSD sockets, just Google around for a tutorial.

注:关于iOS 5的信息来自苹果的技术问答 QA1753.

这篇关于在没有 Gamekit 的情况下通过蓝牙体验 Bonjour ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆