简体   繁体   English

iOS 7:通过Wi-Fi或对等Wi-Fi使用Multipeer Connectivity连接设备

[英]iOS 7 : Connecting devices using Multipeer Connectivity via wi-fi or peer to peer wi-fi

I am trying to connect 2 iOS 7 devices via Multipeer connectivity framework in iOS 7. I am able to connect them using Bluetooth. 我正在尝试通过iOS 7中的Multipeer连接框架连接2个iOS 7设备。我能够使用蓝牙连接它们。 In the code I have not mentioned anything like bluetooth. 在代码中,我没有提到任何类似蓝牙的内容。

Multipeer Documentation 多人文档

The Multipeer Connectivity framework provides support for discovering services provided by nearby iOS devices using infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks and subsequently communicating with those services by sending message-based data, streaming data, and resources (such as files). Multipeer Connectivity框架支持使用基础设施Wi-Fi网络,对等Wi-Fi和蓝牙个人局域网来发现附近的iOS设备提供的服务,并随后通过发送基于消息的数据,流式数据与这些服务进行通信。以及资源(例如文件)。

If, I turn off bluetooth for one of the devices, It stops working. 如果我关闭了其中一台设备的蓝牙,它将停止工作。

As, bluetooth range is very slow I need a large area to cover. 由于蓝牙范围很慢,因此我需要大面积覆盖。 So, tell me how to connect two devices via Wi-Fi or peer to peer wi-fi ? 那么,告诉我如何通过Wi-Fi或对等wi-fi连接两个设备?

Code : .h file 代码:.h文件

@interface SessionManager : NSObject <MCSessionDelegate, MCAdvertiserAssistantDelegate, MCBrowserViewControllerDelegate>

// current peer
@property (nonatomic, readonly) MCPeerID *myPeer ;

// current session
@property (nonatomic, readonly) MCSession *session;

// current advertise
@property (nonatomic, readonly) MCAdvertiserAssistant *advertiser ;

// current browser
@property (nonatomic, readonly) MCBrowserViewController *browser ;

//To start advertising current device
- (void)start;

//To stop advertising current device
- (void)stop;

@end

.m file : .m文件:

- (id)init
{
    self = [super init];

    if (self)
    {
        _myPeer = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];

        _session = [[MCSession alloc] initWithPeer:_myPeer securityIdentity:nil encryptionPreference:MCEncryptionNone];
        _session.delegate = self;

        _advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"Connect" discoveryInfo:nil session:_session];
        _advertiser.delegate = self;

        _browser = [[MCBrowserViewController alloc] initWithServiceType:@"Connect" session:_session];
        _browser.delegate = self;
    }

    return self;
}

 - (void)start
 {
    [_advertiser start];
 }

Any help is appreciated. 任何帮助表示赞赏。

MultipeerConnectivity provides abstraction away from infrastructure Wi-Fi, P2P, and Bluetooth. MultipeerConnectivity提供远离基础设施Wi-Fi,P2P和蓝牙的抽象。 This means that your devices will communicate with each other however they are capable. 这意味着您的设备将能够相互通信,但是它们有能力。

If you can connect two devices over Bluetooth then they can also be connected over infrastructure wifi assuming they're on the same LAN with no restrictions (so if you're at a large corporation you might have some issue with this because of IT). 如果您可以通过蓝牙连接两个设备,那么它们也可以通过基础设施wifi进行连接,前提是它们位于同一个LAN中且不受限制(因此,如果您在一家大公司中,由于IT的原因,可能会遇到一些问题)。

MPC allows you to communicate with multiple devices connected via entirely different transports. MPC允许您与通过完全不同的传输方式连接的多个设备进行通信。 So if A, B, and C are all devices that can be discovered and connected using the UUID you specified, A only has bluetooth on, B has bluetooth and wifi, and C has only wifi on, then A will automatically be able to receive messages from C because B is responsible for completing the ring. 因此,如果A,B和C都是可以使用您指定的UUID进行发现和连接的设备,则A仅打开了蓝牙,B仅打开了蓝牙和wifi,而C仅打开了wifi,那么A将自动能够接收来自B的消息,因为B负责完成振铃。

All of this functionality is provided by MultipeerConnectivity already. 所有这些功能已经由MultipeerConnectivity提供。

For a walkthrough of how to properly discover, connect, and communicate with peers check out the wwdc video "Nearby Networking with Multipeer Connectivity" here 有关如何正确地发现,连接,并与同行交流检查出WWDC视频演练“与附近的Multipeer连接网络” 在这里

The checked answer is wrong because the framework doesn't run over LAN, but instead is a level lower, accessing the bluetooth or WI-FI directly. 检查的答案是错误的,因为该框架不是通过LAN运行的,而是较低级别的,可以直接访问蓝牙或WI-FI。 In other words, you don't need to be on the same network with the same SSID, only that the WI-FI is turned on. 换句话说,您不必位于具有相同SSID的同一网络上,只需打开WI-FI。 Keep both devices logged off of the LAN, like turn off your router and bluetooth, and you'll notice that they still can connect. 保持两个设备都退出LAN,例如关闭路由器和蓝牙,您会注意到它们仍然可以连接。 However, it's partly right in the sense that you can't drill down to tell the framework to use the bluetooth or WI-FI directly, since Apple decided to automate that part for us. 但是,从某种意义上来说,这是正确的,因为您无法深入告知框架直接使用蓝牙或WI-FI,因为Apple决定为我们自动化该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM