Question

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.

Was it helpful?

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

OTHER TIPS

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. 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. Keep both devices logged off of the LAN, like turn off your router and bluetooth, and you'll notice that they still can connect. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top