Mac 应用程序和 iOS 应用程序之间的蓝牙/WiFi [英] Bluetooth/WiFi between Mac app and iOS app

查看:29
本文介绍了Mac 应用程序和 iOS 应用程序之间的蓝牙/WiFi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作蓝牙 Mac 应用程序,连接 iOS 应用程序并发送消息?

How can I make a Bluetooth Mac app that connects with an iOS app and sends messages ?

或者是通过本地 WiFi 网络与 Mac 应用程序连接的 iOS 应用程序?

Or an iOS app that connects with a Mac app via a local WiFi network ?

推荐答案

注意:随着 CoreBluetooth (Bluetooth LE v4) 的引入,这个答案现在已经过时了,因为 iOS6 和 OSX 10.7 任何 iOS 设备都对任何类型的通信开放与任何其他 BLE 兼容设备,Mac 或 PC 或外部硬件.如果由于旧硬件或特定要求(例如所需带宽)而无法支持 BLE,则以下答案仍然有效

NOTE: this answer is now outdated as with the introduction of CoreBluetooth (Bluetooth LE v4) since iOS6 and OSX 10.7 any iOS device is open to any kind of communication with any other BLE compatible device, either a Mac or PC or external hardware. The answer below is still valid if you cannot support BLE, due to old hw or specific requirements (e.g. required bandwidth)

据我所知,这几乎是不可能的.GameKit 框架很好地支持 iOS 设备之间的蓝牙连接,但不支持与其他操作系统的连接,包括 OSX.我见过的 Mac 和 iPHone 之间的唯一连接是热点功能(当它工作时).以 Keynote Remote 应用程序为例.此应用程序允许您将 iPhone 用作遥控器来控制 Keynote 演示文稿.当两台设备在 WiFi 上进行 Keynote 配对时,它允许进行此控制,但不允许使用蓝牙进行此连接,而通过蓝牙,您可以控制两台 iOS 设备之间的 Keynote 演示.您可以在 Apple 技术说明 2152 中找到此声明:

As far as I know, this is almost impossible. Bluetooth connection between iOS devices is well supported with the GameKit framework but there is no support for connection with other OSes, OSX included. The only connection between Mac and iPHone I have ever seen is with the hotspot feature (when it works). Consider for example the Keynote Remote app. This app allows you to control a Keynote presentation using your iPhone as a remote. It allows this control when the two devices are Keynote-paired on the WiFi, but this connection is not allowed using Bluetooth, while with Bluetooth you can control a Keynote presentation between two iOS devices. And you can find this statement in Apple tech note 2152:

An important consequence of the first point is that Bluetooth peer-to-peer networking can only be used to communicate between iOS devices; you can't use it, for example, to communicate between an iOS device and a computer running Mac OS X.

至于 WiFi 问题(我在第一个问题中忘记回答它)本质上是您需要解决的问题:可发现性和数据传输.为了确定两个设备是否在同一网络中,您可以使用 Bonjour 协议.本质上,其中一台机器将自己发布为特定服务的服务提供者(您可以创建自己的服务:例如_mytype._myprotocol"),并且潜在的客户端将搜索服务器.除了 Bonjour 处理其他事情,如寻址和 DNS,而不需要这两个应用程序来了解它们所在的本地网络的详细信息. Apple 文档中对此进行了解释:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/关于.html请注意,Bonjour 是 AirPlay(Apple TV)、AirPrint 和可能还有 AirDrop 等功能的基础,但很多设备都支持它,例如一些NAS.在 Apple 开发人员站点的 SampleCodes 部分,您会找到一个很好的示例,名为BonjourWeb",它展示了服务发现的工作原理.您可以轻松地在您的 iPhone 模拟器或设备中试用它,如果您有一些支持 Bonjour 的设备,您将在列表中看到它.第二个问题当然是两个设备之间的真实数据通信,取决于您定义自定义客户端/服务器或使用基于标准 Web 服务器的方法.这取决于应用程序和数据流.第一种方法更好,因为它是高度定制的,您必须学习 NetworkStreams 概念并查看苹果库中的 SimpleNetworkStreams 代码(这是一个 Mac 和 iPhone 之间的简单通信应用程序;并不总是有效,但这是一个好的开始点)

As far as the WiFi question (I forgot to reply to it in my first question) essentially you to kind of problems to solve: discoverability and data transfer. In order to determine if two devices are in the same network, you can use the Bonjour protocol. Essentially one of the machines publish itself as service-provider for a specific service (you can create your own service: e.g. "_mytype._myprotocol") and the would-be client will search for a server. Besides Bonjour takes care of other things like addressing and DNS without the two apps to know the details of the local network they are in. This is explained in Apple docs here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NetServices/Articles/about.html Note that Bonjour is at the base of features like AirPlay (Apple TV), AirPrint and probably also AirDrop but it is supported by a lot of devices, e.g. some NAS. In the SampleCodes section of the Apple developers site you will find a great example, called "BonjourWeb" that shows how service discovery works. You can easily try it in your iPhone simulator or device and if you have some Bonjour-enabled device you will see it in the list. The second problem of course is then real data communication between the two devices, is up to you to define a custom client/server or use a standard web-server based approach. This depends on the application and the data flow. The first approach is better as it is highly customized, you must study the NetworkStreams concepts and check out the SimpleNetworkStreams code in the apple library (this is a simple communication app between a Mac and an iPhone; not always working but it is a good starting point)

这篇关于Mac 应用程序和 iOS 应用程序之间的蓝牙/WiFi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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