📜 ⬆️ ⬇️

Multipeer connectivity framework in iOS7

Multipeer Connectivity Framework transfers text messages, streams and files between iOS devices that are nearby. To do this, use the WiFi network, direct WiFI connection and Bluetooth. That is, the Internet is not required.

The framework allows devices to communicate from different networks, for example, smartphone No. 2 only has WiFi, and No. 3 only has Bluetooth. If within the radius of availability of these devices will be the third smartphone with enabled Wifi and Bluetooth, smartphones No. 2 and No. 3 will be able to transparently exchange
data.



')
The MCAdvertiserAssistant class makes the device detectable, and the MCBrowserViewController class allows the device to be searched. These classes implement the standard device search and session setup interface; if you want to implement your own, use the MCNearbyServiceAdvertiser and MCNearbyServiceBrowser classes.

Let's transfer the message between devices. The first device will search.
void StartMultipeerBrowser () { peer = new MCPeerID ("Monkey"); session = new MCSession (peer); session.Delegate = sessionDelegate; browser = new MCBrowserViewController (serviceType, session); browser.Delegate = browserDelegate; browser.ModalPresentationStyle = UIModalPresentationStyle.FormSheet; PresentViewController (browser, true, null); } 


MVBrowserViewController displays a window with found devices. On tapu to one of them, an invitation to exchange data appears on the remote device.


The delegate of the MCSession class receives events about the connection, disconnection of devices and received data.

 class MySessionDelegate : MCSessionDelegate { public override void DidChangeState (MCSession session, MCPeerID peerID, MCSessionState state) { switch (state) { case MCSessionState.Connected: Console.WriteLine ("Connected: {0}", peerID.DisplayName); break; case MCSessionState.Connecting: Console.WriteLine ("Connecting: {0}", peerID.DisplayName); break; case MCSessionState.NotConnected: Console.WriteLine ("Not Connected: {0}", peerID.DisplayName); break; } } public override void DidReceiveData (MCSession session, NSData data, MCPeerID peerID) { InvokeOnMainThread (() => { var alert = new UIAlertView ("", data.ToString (), null, "OK"); alert.Show (); }); } ... } 


When the user closes the device search window, the delegate of the MCBrowserViewController class will receive an event. The device search window must be hidden manually.
 class MyBrowserDelegate : MCBrowserViewControllerDelegate { public override void DidFinish (MCBrowserViewController browserViewController) { InvokeOnMainThread (() => { browserViewController.DismissViewController (true, null); }); } public override void WasCancelled (MCBrowserViewController browserViewController) { InvokeOnMainThread (() => { browserViewController.DismissViewController (true, null); }); } } 


Make the second device detectable:
 void StartMultipeerAdvertiser () { peer = new MCPeerID ("Player1"); session = new MCSession (peer); session.Delegate = sessionDelegate; assistant = new MCAdvertiserAssistant (serviceType, dict, session); assistant.Start (); } 


When an invitation to exchange data arrives, MCAdvertiserAssistant will ask for the user's consent.


Once the session is established, you can send a message, stream or resource. Message transfer example
 void SendMessage () { var message = NSData.FromString ( String.Format ("{0} found the monkey", peer.DisplayName)); NSError error; session.SendData (message, session.ConnectedPeers, MCSessionSendDataMode.Reliable, out error); } 


To send a resource, use the sendResourceAtUrl method. The object to be transferred is specified via NSUrl, it can be a local file or a network resource.

The startStreamWithName method is used to establish the byte stream between devices; it creates an NSOutputStream and NSInputStream at different ends of the connection. I don’t know why, but you can, for example, stream audio from the microphone of one device and play it on another.

Source: https://habr.com/ru/post/198814/


All Articles