Search code examples
iosios7wifixcode5multipeer-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. 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).

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 ?

Code : .h file

@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 :

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


Solution

  • MultipeerConnectivity provides abstraction away from infrastructure Wi-Fi, P2P, and Bluetooth. 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).

    MPC allows you to communicate with multiple devices connected via entirely different transports. 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.

    All of this functionality is provided by MultipeerConnectivity already.

    For a walkthrough of how to properly discover, connect, and communicate with peers check out the wwdc video "Nearby Networking with Multipeer Connectivity" here