/// <summary>
 /// <see cref="IWifiP2pCallbacksHandler.OnWifiP2pConnectionChanged(NetworkInfo, WifiP2pInfo, WifiP2pGroup)"/>
 /// </summary>
 virtual public void OnWifiP2pConnectionChanged(NetworkInfo networkInfo, WifiP2pInfo p2pInfo, WifiP2pGroup groupInfo)
 {
     if (networkInfo.IsConnected && p2pInfo.GroupFormed)
     {
         if (p2pInfo.IsGroupOwner)
         {
             IsServer = true;
             AccessPoint.Instance.StartListening(this, p2pInfo.GroupOwnerAddress.HostAddress, 8898);
             foreach (WifiP2pDevice device in groupInfo.ClientList)
             {
                 AccessPoint.Instance.TryAddClient(new DeviceInfo(device.DeviceName, device.DeviceAddress, p2pInfo.GroupOwnerAddress.HostAddress));
             }
         }
         else
         {
             IsServer = false;
             Client.Instance.SetConnected(new DeviceInfo(groupInfo.Owner.DeviceName, groupInfo.Owner.DeviceAddress, p2pInfo.GroupOwnerAddress.HostAddress));
             Client.Instance.StartSending(this, p2pInfo.GroupOwnerAddress.HostAddress, 8898);
         }
     }
     else
     {
         if (IsServer)
         {
             AccessPoint.Instance.StopListening(this);
         }
         else
         {
             Client.Instance.StopSending(this);
         }
     }
 }
 /// <summary>
 /// Default constructor passing in the relevant connection information.
 /// </summary>
 /// <param name="context">The application activity - needed to update UI for instance.</param>
 /// <param name="connectionInfo">The Wifi P2P connection info.</param>
 /// <param name="port">The communication port.</param>
 /// <param name="requestTimeout">The time after which a request times out.</param>
 protected AbstractWifiP2pAsyncTask(Activity activity, WifiP2pInfo connectionInfo, int port, int requestTimeout)
 {
     m_Activity       = activity ?? throw new ArgumentNullException(nameof(activity));
     m_ConnectionInfo = connectionInfo ?? throw new ArgumentNullException(nameof(connectionInfo));
     m_Port           = port >= 1 ? port : throw new ArgumentException("Negative values are not valid port numbers", nameof(port));
     m_RequestTimeout = requestTimeout;
 }
            public async Task <ConnectionEstablishState> ConnectAsync()
            {
                await Task.Run(() =>
                {
                    WifiP2pConfig config = new WifiP2pConfig();
                    config.DeviceAddress = (WifiDirectDevice as WifiDirectDevice).Address;
                    WifiP2pManager droidWifiP2pManager = _WifiDirectManager._DroidWifiP2pManager;
                    droidWifiP2pManager.Connect(_WifiDirectManager._Channel, config, _ConnectStateListener);
                    while (!_ConnectingSucceeded)
                    {
                        if (_ReconnectMark)
                        {
                            _ReconnectMark = false;
                            droidWifiP2pManager.Connect(_WifiDirectManager._Channel, config, _ConnectStateListener);
                        }
                    }
                    WifiP2pActionListener connectionInfoListener = new WifiP2pActionListener(_WifiDirectManager);
                    _WifiDirectManager._DroidWifiP2pManager.RequestConnectionInfo(_WifiDirectManager._Channel, connectionInfoListener);
                    while (_WifiDirectManager._LatestWifiP2pInfo == null)
                    {
                        System.Diagnostics.Debug.WriteLine("HERE2");
                    }
                    WifiP2pInfo preInfo = null;
                    while (_WifiDirectManager._LatestWifiP2pInfo.GroupOwnerAddress == null)
                    {
                        if (preInfo != _WifiDirectManager._LatestWifiP2pInfo)
                        {
                            preInfo = _WifiDirectManager._LatestWifiP2pInfo;
                            _WifiDirectManager._DroidWifiP2pManager.RequestConnectionInfo(_WifiDirectManager._Channel, connectionInfoListener);
                            System.Diagnostics.Debug.WriteLine("CHANGE");
                        }
                        System.Diagnostics.Debug.WriteLine("HERE3");
                    }
                    if (_ConnectingSucceeded)
                    {
                        ConnectionEstablishState = ConnectionEstablishState.Succeeded;
                    }
                    OnConnectionEstalblishResult?.Invoke(this, ConnectionEstablishState);
                });

                try
                {
                    System.Diagnostics.Debug.WriteLine("Connect Succeed " + _WifiDirectManager._LatestWifiP2pInfo.GroupOwnerAddress.HostAddress);
                }
                catch (Exception)
                {
                    var f**k = _WifiDirectManager._LatestWifiP2pInfo;
                }


                return(ConnectionEstablishState);
            }
Пример #4
0
        public void OnConnectionInfoAvailable(WifiP2pInfo info)
        {
            if (info.GroupFormed)
            {
                if (info.IsGroupOwner)
                {
                    MessagingCenter.Send("P2PShare", "WFDConnected", (string)null);
                }

                else
                {
                    MessagingCenter.Send("P2PShare", "WFDConnected", info.GroupOwnerAddress.HostAddress);
                }
            }
        }
        /// <summary>
        /// <see cref="IWifiP2pCallbacksHandler.OnWifiP2pConnectionChanged(NetworkInfo, WifiP2pInfo, WifiP2pGroup)"/>
        /// </summary>
        override public void OnWifiP2pConnectionChanged(NetworkInfo networkInfo, WifiP2pInfo p2pInfo, WifiP2pGroup groupInfo)
        {
            base.OnWifiP2pConnectionChanged(networkInfo, p2pInfo, groupInfo);
            if (networkInfo.IsConnected)
            {
                m_Views.SsidTextView.Text = groupInfo.NetworkName;

                foreach (WifiP2pDevice device in groupInfo.ClientList)
                {
                    m_Clients.Add(device);
                }

                m_Adapter.NotifyDataSetChanged();
            }
        }
Пример #6
0
        public void OnConnectionInfoAvailable(WifiP2pInfo info)
        {
            if (info.GroupFormed)
            {
                WifiManager.HostInfo = info;

                if (info.IsGroupOwner)
                {
                    WiFiService.wifiService.RefreshConnectionChanged("I am Host");
                    Task.Run(() => ReceiveMessageAsync(true));
                }
                else
                {
                    WiFiService.wifiService.RefreshConnectionChanged("I am Client");
                    Task.Run(() => ReceiveMessageAsync(false));
                }
            }
        }
        public void OnConnectionInfoAvailable(WifiP2pInfo info)
        {
            if (_progressDialog != null && _progressDialog.IsShowing)
            {
                _progressDialog.Dismiss();
            }

            _info = info;

            View.Visibility = ViewStates.Visible;

            // The owner IP is now known.
            var view = _contentView.FindViewById <TextView>(Resource.Id.group_owner);

            view.Text = Resources.GetString(Resource.String.group_owner_text)
                        + ((info.IsGroupOwner) ? Resources.GetString(Resource.String.yes)
                            : Resources.GetString(Resource.String.no));

            // InetAddress from WifiP2pInfo struct.
            view      = _contentView.FindViewById <TextView>(Resource.Id.device_info);
            view.Text = "Group Owner IP - " + _info.GroupOwnerAddress.HostAddress;

            // After the group negotiation, we assign the group owner as the file
            // server. The file server is single threaded, single connection server
            // socket.
            if (_info.GroupFormed && _info.IsGroupOwner)
            {
                var server = new WebServerService(Activity);
                server.Start();
            }
            else if (_info.GroupFormed)
            {
                _contentView.FindViewById <Button>(Resource.Id.btn_start_client).Visibility = ViewStates.Visible;
                _contentView.FindViewById <TextView>(Resource.Id.status_text).Text          =
                    Resources.GetString(Resource.String.client_text);
            }

            _contentView.FindViewById <Button>(Resource.Id.btn_connect).Visibility = ViewStates.Gone;
        }
Пример #8
0
 /// <summary>
 /// <see cref="AbstractWifiP2pAsyncTask(Activity,WifiP2pInfo, int, int)"/>
 /// </summary>
 public PingServerAsyncTask(Activity activity, WifiP2pInfo connectionInfo, int port, int requestTimeout) : base(activity, connectionInfo, port, requestTimeout)
 {
 }
Пример #9
0
        public void OnConnectionInfoAvailable(WifiP2pInfo info)
        {
            if (_progressDialog != null && _progressDialog.IsShowing)
            {
                _progressDialog.Dismiss();
            }

            _info = info;

            View.Visibility = ViewStates.Visible;

            // The owner IP is now known.
            var view = _contentView.FindViewById <TextView>(Resource.Id.group_owner);

            view.Text = Resources.GetString(Resource.String.group_owner_text)
                        + ((info.IsGroupOwner) ? Resources.GetString(Resource.String.yes)
                            : Resources.GetString(Resource.String.no));

            // InetAddress from WifiP2pInfo struct.
            view      = _contentView.FindViewById <TextView>(Resource.Id.device_info);
            view.Text = "Group Owner IP - " + _info.GroupOwnerAddress.HostAddress;

            // After the group negotiation, we assign the group owner as the file
            // server. The file server is single threaded, single connection server
            // socket.
            if (_info.GroupFormed && !_info.IsGroupOwner)
            {
                _contentView.FindViewById <Button>(Resource.Id.btn_start_client).Visibility = ViewStates.Visible;
                _contentView.FindViewById <TextView>(Resource.Id.status_text).Text          =
                    Resources.GetString(Resource.String.client_text);

                /*Task.Factory.StartNew(() =>
                 *  {
                 *      try
                 *      {
                 *          var serverSocket = new ServerSocket(8988);
                 *          _contentView.FindViewById<TextView>(Resource.Id.status_text).Text = "1. Server: Socket opened";
                 *          Log.Debug(WiFiDirectActivity.Tag, "Server: Socket opened");
                 *          var client = serverSocket.Accept();
                 *          Log.Debug(WiFiDirectActivity.Tag, "Server: connection done");
                 *          var f = new File(Environment.ExternalStorageDirectory + "/"
                 + Activity.PackageName + "/wifip2pshared-" + DateTime.Now.Ticks + ".jpg");
                 +
                 +          Log.Debug(WiFiDirectActivity.Tag, f.AbsolutePath);
                 +          _contentView.FindViewById<TextView>(Resource.Id.status_text).Text = "1. Server: Socket opened" + "2. "+f.AbsolutePath;
                 +          var dirs = new File(f.Parent);
                 +          if (!dirs.Exists())
                 +              dirs.Mkdirs();
                 +          f.CreateNewFile();
                 +
                 +          Log.Debug(WiFiDirectActivity.Tag, "Server: copying files " + f);
                 +          _contentView.FindViewById<TextView>(Resource.Id.status_text).Text = "1. Server: Socket opened" + "2. " + f.AbsolutePath;
                 +          var inputStream = client.InputStream;
                 +          CopyFile(inputStream, new FileStream(f.ToString(), FileMode.OpenOrCreate));
                 +          serverSocket.Close();
                 +          return f.AbsolutePath;
                 +      }
                 +      catch (IOException e)
                 +      {
                 +          Log.Error(WiFiDirectActivity.Tag, e.Message);
                 +          return null;
                 +      }
                 +  })
                 +  .ContinueWith(result =>
                 +  {
                 +      if (result != null)
                 +      {
                 +          _contentView.FindViewById<TextView>(Resource.Id.status_text).Text = "File copied - siddharth" +
                 +                                                                              result.Result;
                 +          var intent = new Intent();
                 +          intent.SetAction(Intent.ActionView);
                 +          intent.SetDataAndType(Android.Net.Uri.Parse("file://" + result.Result), "image/*");
                 +          Activity.StartActivity(intent);
                 +      }
                 +  });*/
            }
            else if (_info.GroupFormed)
            {
                _contentView.FindViewById <Button>(Resource.Id.btn_start_client).Visibility = ViewStates.Visible;
                _contentView.FindViewById <TextView>(Resource.Id.status_text).Text          =
                    Resources.GetString(Resource.String.client_text);
            }

            _contentView.FindViewById <Button>(Resource.Id.btn_connect).Visibility = ViewStates.Gone;
        }
Пример #10
0
 /// <summary>
 /// <see cref="AbstractWifiP2pAsyncTask(Context,WifiP2pInfo, int, int)"/>
 /// </summary>
 public ClientReceiveAsyncTask(Activity activity, WifiP2pInfo connectionInfo, int port, int requestTimeout) : base(activity, connectionInfo, port, requestTimeout)
 {
 }
 /// <summary>
 /// <see cref="IConnectionInfoListener.OnConnectionInfoAvailable(WifiP2pInfo)"/>
 /// </summary>
 abstract public void OnConnectionInfoAvailable(WifiP2pInfo info);
 /// <summary>
 /// <see cref="IWifiP2pCallbacksHandler.OnWifiP2pConnectionChanged(NetworkInfo, WifiP2pInfo, WifiP2pGroup)"/>
 /// </summary>
 abstract public void OnWifiP2pConnectionChanged(NetworkInfo networkInfo, WifiP2pInfo p2pInfo, WifiP2pGroup groupInfo);
 /// <summary>
 /// <see cref="IConnectionInfoListener.OnConnectionInfoAvailable(WifiP2pInfo)"/>
 /// </summary>
 virtual public void OnConnectionInfoAvailable(WifiP2pInfo info)
 {
 }
        /// <summary>
        /// <see cref="BroadcastReceiver.OnReceive(Context, Intent)"/>
        /// </summary>
        override public void OnReceive(Context context, Intent intent)
        {
            // Filter Messages intents
            if (intent is WifiP2pMessageIntent messageIntent)
            {
                // Load extra data from the intent.
                messageIntent.Load();
                switch (messageIntent.Action)
                {
                case WifiP2pMessageIntent.ActionMessageReceivedProgress:
                    if (messageIntent.IsCompleted)
                    {
                        m_CallbackHandler.OnMessageReceived(messageIntent.Message);
                    }
                    else
                    {
                        m_CallbackHandler.OnMessageReceivedProgressChanged(messageIntent.Message.MessageType, messageIntent.Progress);
                    }
                    break;

                case WifiP2pMessageIntent.ActionMessageSentProgress:
                    if (messageIntent.IsCompleted)
                    {
                        m_CallbackHandler.OnMessageSent(messageIntent.Message);
                    }
                    else
                    {
                        m_CallbackHandler.OnMessageSendingProgressChanged(messageIntent.Message, messageIntent.Progress);
                    }
                    break;
                }
            }
            else
            {
                switch (intent.Action)
                {
                case WifiP2pConnectionChangedAction:
                    NetworkInfo  networkInfo = (NetworkInfo)intent.GetParcelableExtra(ExtraNetworkInfo);
                    WifiP2pInfo  p2pInfo     = (WifiP2pInfo)intent.GetParcelableExtra(ExtraWifiP2pInfo);
                    WifiP2pGroup groupInfo   = (WifiP2pGroup)intent.GetParcelableExtra(ExtraWifiP2pGroup);
                    m_CallbackHandler.OnWifiP2pConnectionChanged(networkInfo, p2pInfo, groupInfo);
                    break;

                case WifiP2pPeersChangedAction:
                    WifiP2pDeviceList peers = (WifiP2pDeviceList)intent.GetParcelableExtra(ExtraP2pDeviceList);
                    m_CallbackHandler.OnPeersAvailable(peers);
                    break;

                case WifiP2pStateChangedAction:
                    EWifiState wifiState = (EWifiState)intent.GetIntExtra(ExtraWifiState, 0);
                    m_CallbackHandler.OnWifiP2pStateChanged(wifiState);
                    break;

                case WifiP2pThisDeviceChangedAction:
                    WifiP2pDevice deviceDetails = (WifiP2pDevice)intent.GetParcelableExtra(ExtraWifiP2pDevice);
                    m_CallbackHandler.OnThisDeviceChanged(deviceDetails);
                    break;

                default:
                    throw new NotSupportedException($"The action {intent.Action} is not handled by this receiver.");
                }
            }
        }
Пример #15
0
 void IConnectionInfoListener.OnConnectionInfoAvailable(WifiP2pInfo info)
 {
     _WifiDirectManager._LatestWifiP2pInfo = info;
     System.Diagnostics.Debug.WriteLine("SDFSDFHERE" + (info == null));
 }