博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Multipeer Connectivity
阅读量:6001 次
发布时间:2019-06-20

本文共 4939 字,大约阅读时间需要 16 分钟。

 

Framework
/System/Library/Frameworks/MultipeerConnectivity.framework
Header file directories
/System/Library/Frameworks/MultipeerConnectivity.framework/Headers
Declared in
MCAdvertiserAssistant.h
MCBrowserViewController.h
MCError.h
MCNearbyServiceAdvertiser.h
MCNearbyServiceBrowser.h
MCPeerID.h
MCSession.h

使用Multipeer Connectivity framework,设备之间可以通过infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks进行通信。

Architecture

When working with the Multipeer Connectivity framework, your app must interact with several types of objects, as described below.

  • Session objects () :提供设备间的通信服务,如果创建了一个session,可以邀请其他的设备加入网络中,或者被其他设备邀请加入。

  • Advertiser objects () :表示设备是可以被邀请加入特定的session.

  • Advertiser assistant objects () :功能与类似,只是额外提供了接受邀请时的标准交互借口。

  • Browser objects () :搜索附近的特定session类型的设备。

  • Browser view controller objects (): 提供标准的接口,让用户能够选择附近的设备加入一个session。

  • Peer IDs (): uniquely identify an app running on a device to nearby peers.

Session objects 保持一组 连接到该session的peer ID objects。 Advertiser objects also use a single local peer object to provide information that identifies the device and its user to other nearby devices.

 

Using the Framework

框架使用过程: the discovery phase,   the session phase.

In the discovery phase, your app uses a browser object (described in ) 来搜索附近的设备, 最好采用  () 进行交互。

The app also uses an advertiser object (described in ) or an advertiser assistant object (described in ) ,表示这个设备是有效的,其他的设备可以邀请其加入session.

During the discovery phase, your app has limited communication with and knowledge of other peers; it has access to the discoveryInfo data that other nearby clients provide, and any context data that other peers provide when inviting it to join a session.

在发现阶段,app不能访问其他的设备,只有被邀请加入到session之后,才能访问discoveryInfo data和其他的context data.

After the user chooses which peers to add to a session, the app invites those peers to join the session. Apps running on the nearby devices can choose whether to accept or reject the invitation, and can ask their users for permission.

 

If the peer accepts the invitation, the browser establishes a connection with the advertiser and the session phase begins. In this phase, your app can perform direct communication to one or more peers within the session. The framework notifies your app through delegate callbacks when peers join the session and when they leave the session.

如果设备接受了邀请,那么browser object与advertiser object建立连接,开始session phase。在会话阶段,设备可以与sesion中的其他一个或多个设备进行直接通信。framework会在设备加入或者离开session时通知你的app.

MCSession Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/
Availability
Available in iOS 7.0 and later.
Declared in
MCError.h
MCSession.h

Overview

An MCSession object enables and manages communication among all peers in a Multipeer Connectivity session.

Initiating a Session

To set up a session, your app must do the following:

  1. Create an MCPeerID object that represents the local peer, and use it to initialize the session object.

  2. Add peers to the session using a browser object, a browser view controller, or manually.

  3. Wait until the session calls your delegate object’s  method with  as the new state, along with an object that tells you which peer became connected.

You should also set up an advertiser or advertiser assistant to allow other devices to ask your app to join a session that they create.

Communicating With Peers

建立session后,就可以向其他的设备发送数据,方法如下:

  •  向指定的设备发送 NSData object 

    On each recipient device, the delegate object’s  method is called with the data object when the data has been fully received.

   在接收端,当数据全部接收后,会调用代理的方法。

  •  发送一个NSURL object 到指定设备. The URL can be either a local file URL or a web URL. The completionHandler block is called when the resource is fully received by the recipient peer or when an error occurs during transmission.

    This method returns an NSProgress object that you can use to cancel the transfer or check the current status of the transfer.

   这个方法返回一个NSProgress 对象,可以使用这个对象取消传输过程或者检查当前传输状态。

  • On the recipient device, the session calls its delegate object’s  method when the device begins receiving the resource, and calls its method when the resource has been fully received or when an error occurs.

   在接收端,接收数据的时候,会调用代理的方法;接收完成时或者出错时,

  调用方法。

  •  creates a connected byte stream (NSOutputStream) that you can use to send data to the specified peer.

    On the recipient device, the session calls its delegate object’s  method with an NSInputStream object that represents the other endpoint of communication.

    On both sides, your code must set the stream’s delegate, schedule the stream on a run loop, and open the stream. Your code must also implement stream delegate methods to manage sending and receiving stream data.

Important: Delegate calls occur on a private operation queue. If your app needs to perform an action on a particular run loop or operation queue, its delegate method should explicitly dispatch or schedule that work.

 

转载于:https://www.cnblogs.com/fengyunyu/p/3416802.html

你可能感兴趣的文章
centos 安装mysql5.7
查看>>
@RequestParam注解的使用
查看>>
黑夜的精神力量
查看>>
Spring hibernate 事务的流程
查看>>
有关office 2010办公软件安装不上的问题解答
查看>>
centos7中php使用memcache
查看>>
亮剑:PHP,我的未来不是梦(7)
查看>>
Linux系统上传下载工具rz/sz
查看>>
matplotlib绑定到PyQt5(有菜单)
查看>>
利用Powershell和ceye.io实现Windows账户密码回传
查看>>
如何清理EBS R12 middle-tier cache
查看>>
Windows Azure HandBook (4) 分析Windows Azure如何处理Session
查看>>
25个Java机器学习工具和库
查看>>
对Windows Server容器的数据保护改进了吗?
查看>>
Kafka、RabbitMQ、RocketMQ 消息中间件的对比 | 消息发送性能篇
查看>>
业绩很美好的Facebook,如何让两款通讯工具变现?
查看>>
苹果企业账号遭滥用:iOS漏洞"留出"赌博应用通道
查看>>
越快越好 Linux用户需要赶紧补上glibc漏洞
查看>>
智能联接时代 黑客侵入汽车系统更轻松了
查看>>
《深入理解C++11:C++ 11新特性解析与应用》——2.3 扩展的整型
查看>>