Example #1
0
            public async Task <ConnectionEstablishState> ConnectAsync()
            {
                this.ConnectionEstablishState = ConnectionEstablishState.Connecting;
                _BluetoothManager._BluetoothConnections.Add(this);
                OnConnectionEstalblishResult?.Invoke(this, this.ConnectionEstablishState);
                ConnectionEstablishState state;

                do
                {
                    state = await establishConnectionAsync();
                }while (state == ConnectionEstablishState.Failed && !_AbortConnecting);
                if (_AbortConnecting)
                {
                    _BluetoothManager._BluetoothConnections.Remove(this);
                    this.ConnectionEstablishState = ConnectionEstablishState.Abort;
                    _AbortConnecting = false;
                }
                else
                {
                    this.ConnectionEstablishState = state;
                }
                if (this.ConnectionEstablishState == ConnectionEstablishState.Succeeded)
                {
                    //Device.StartTimer(new TimeSpan(0, 0, 0, 0, 500), sendControlCodeTimerFunc);
                }
                this.OnConnectionEstalblishResult?.Invoke(this, this.ConnectionEstablishState);
                return(state);
            }
 private async void OnConnectionRequested(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs args)
 {
     WiFiDirectConnectionRequest ConnectionRequest = args.GetConnectionRequest();
     WiFiDirectDevice wfdDevice = await WiFiDirectDevice.FromIdAsync(ConnectionRequest.DeviceInformation.Id);
     EndpointPairs = wfdDevice.GetConnectionEndpointPairs();
     _ConnectionEstablishState = ConnectionEstablishState.Succeeded;
     OnConnectionEstalblishResult?.Invoke(this, _ConnectionEstablishState);
 }
            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);
            }
            public async void StartAdvertisingAsync()
            {
                System.Diagnostics.Debug.WriteLine("Start Advertising");
                ConnectionEstablishState = ConnectionEstablishState.Connecting;
                OnConnectionEstalblishResult?.Invoke(this, ConnectionEstablishState);
                BluetoothManager._ConnectedConnections.Add(this);
                RfcommServiceId myId = RfcommServiceId.FromUuid(Uuid);

                _Provider = await RfcommServiceProvider.CreateAsync(myId);

                StreamSocketListener listener = new StreamSocketListener();

                listener.ConnectionReceived += _OnConnectionReceived;
                await listener.BindServiceNameAsync(_Provider.ServiceId.AsString(), SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);

                InitializeServiceSdpAttributes(_Provider);
                _Provider.StartAdvertising(listener, true);
            }
Example #5
0
 private void _OnConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
 {
     _Provider.StopAdvertising();
     Socket = args.Socket;
     System.Diagnostics.Debug.WriteLine("OnConnect");
     if (Socket != null)
     {
         SendDataWriter           = new DataWriter(Socket.OutputStream);
         ConnectionEstablishState = ConnectionEstablishState.Succeeded;
         _SendDetectorTimer       = new Timer(new TimerCallback(_SendDetectorTimerCallback), null, 0, 500);
         OnConnectionEstalblishResult?.Invoke(this, ConnectionEstablishState);
     }
     else
     {
         ConnectionEstablishState = ConnectionEstablishState.Failed;
         OnConnectionEstalblishResult?.Invoke(this, ConnectionEstablishState);
     }
     _ReceiveTask = ReceiveAsync();
 }
Example #6
0
            private bool sendControlCodeTimerFunc()
            {
                Task.Run(async() =>
                {
                    try
                    {
                        await SendAsync(1, new byte[] { 0 });
                    }
                    catch (Exception e)
                    {
                        if (e.Message == "Broken pipe")
                        {
                            _ReleaseAllConnectionResource();
                            this.ConnectionEstablishState = ConnectionEstablishState.Connecting;
                            System.Diagnostics.Debug.WriteLine("BROKEN PIPE F**K YOU ");
                            Task connectTask = ConnectAsync();
                            OnConnectionEstalblishResult?.Invoke(this, ConnectionEstablishState.Connecting);
                        }
                    }
                });

                return(true);
            }