与MultipeerConnectivity Xamarin的连接问题

时间:2017-04-11 14:23:16

标签: xamarin bluetooth xamarin.ios multipeer-connectivity

无法使用MultipeerConnectivity(蓝牙)连接我的设备。

当我从iPhoneA启动我的广告商时,我可以在iPhoneB浏览器上看到iPhoneA。从我的iPhone2(浏览器)发送连接请求后,我在我的iPhoneA上收到请求,但没有连接都没有下降。

如果我在同一设备上试用,我连接到同一设备(iPhone1广告客户和iPhone1浏览器),它可以正常工作并连接。

以下是完整的代码:

using System;
using Foundation;
using MultipeerConnectivity;
using UIKit;

namespace Chat1
{
    public partial class ViewController : UIViewController
    {
        protected ViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
            StartMultipeerAdvertiser();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }

        partial void BtnBrowse_TouchUpInside(UIButton sender)
        {
            StartMultipeerBrowser();
        }

        #region Multipeer Connectivity Demo
        MCSession session;
        MCPeerID peer;
        MCBrowserViewController browser;
        MCAdvertiserAssistant assistant;
        MySessionDelegate sessionDelegate = new MySessionDelegate();
        MyBrowserDelegate browserDelegate = new MyBrowserDelegate();
        NSDictionary dict = new NSDictionary();
        static readonly string serviceType = "MyChatApp";

        void StartMultipeerAdvertiser()
        {
            peer = new MCPeerID("iPhone1");
            session = new MCSession(peer);
            session.Delegate = sessionDelegate;
            assistant = new MCAdvertiserAssistant(serviceType, dict, session);
            assistant.Start();
        }

        void StartMultipeerBrowser()
        {
            peer = new MCPeerID("iPhone2");
            session = new MCSession(peer);
            session.Delegate = sessionDelegate;
            browser = new MCBrowserViewController(serviceType, session);
            browser.Delegate = browserDelegate;
            browser.ModalPresentationStyle = UIModalPresentationStyle.FormSheet;
            this.PresentViewController(browser, true, null);
        }

        void SendMessage()
        {
            var message = NSData.FromString(
                String.Format("{0} found the Other iPhone", peer.DisplayName));
            NSError error;
            session.SendData(message, session.ConnectedPeers,
                MCSessionSendDataMode.Reliable, out error);
        }

        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();
                });
            }

            public override void DidStartReceivingResource(MCSession session, string resourceName, MCPeerID fromPeer, NSProgress progress)
            {
            }

            public override void DidFinishReceivingResource(MCSession session, string resourceName, MCPeerID formPeer, NSUrl localUrl, NSError error)
            {
                error = null;
            }

            public override void DidReceiveStream(MCSession session, NSInputStream stream, string streamName, MCPeerID peerID)
            {
            }
        }

        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);
                });
            }
        }
        #endregion
    }
}

0 个答案:

没有答案