示例#1
0
        public async Task Connect()
        {
            PeerFinder.AlternateIdentities["Bluetooth:SDP"] = ServiceGUID.ToString();
            var peers = await PeerFinder.FindAllPeersAsync();

            foreach (var p in peers)
            {
                var h = p.ServiceName;
            }

            // TODO: obviously list these instead of taking the first
            var peer = peers.FirstOrDefault();

            if (peer != null)
            {
                var socket = new StreamSocket();
                try
                {
                    // default service name?
                    await socket.ConnectAsync(peer.HostName, ServiceName);

                    var connection = new DuplexConnection(socket);
                    var device = new BluetoothDevice(peer.DisplayName, peer.HostName, peer.ServiceName);

                    if (ConnectionEstablished != null)
                    {
                        ConnectionEstablished(this, new ClientConnectedEventArgs(device, connection));
                    }
                } 
                catch (Exception ex)
                {
                    
                }
            }
        }
示例#2
0
        private async void BluetoothServerOnClientConnected(BluetoothServer Server, ClientConnectedEventArgs Args)
        {
            var device = Args.Device;

            _connection = Args.Connection;

            SafeInvoke(() => {
                LogTextBlock.Text = string.Format("Connected to {0} hostname {1} service {2}", device.DisplayName, device.HostName, device.ServiceName);
            });

            var connection = Args.Connection;
            
            // The number of locations to sync
            var count = await connection.ReceiveUInt32();
            
            var app = (App)Application.Current;
            using (var db = new SQLiteConnection(app.DatabasePath))
            {
                for (int i = 0; i < count; ++i)
                {
                    var location = await connection.ReceiveObject<Models.Location>();
                    location.CountryId = _quadtree.Query(new Coordinate((float) location.Longitude, (float) location.Latitude));
                    db.Insert(location);
                }
            }

            SafeInvoke(() =>
            {
                LogTextBlock.Text += string.Format("\n\rSynced {0} locations!", count);
            });
        }
示例#3
0
        public async Task Disconnects_When_Receiving_Invalid_Packet()
        {
            var pipe         = new DuplexPipe();
            var cancellation = new CancellationTokenSource();

            using (IConnection connection = new DuplexConnection(pipe)) {
                connection.Disconnected += (_, ev) => cancellation.Cancel();
                _ = connection.BeginReceive();

                try {
                    await connection.Output.WriteAsync(new byte[] { 0xFF, 0xFF, 0xFF });

                    await connection.Output.FlushAsync();
                } catch {
                }

                await Task.Delay(100, cancellation.Token).ContinueWith(_ => { });

                Assert.IsTrue(cancellation.IsCancellationRequested);
                Assert.IsFalse(connection.IsConnected);
            }
        }
 public ClientConnectedEventArgs(BluetoothDevice Device, DuplexConnection Connection)
 {
     this.Device = Device;
     this.Connection = Connection;
 }
示例#5
0
        private void ListenerOnConnectionReceived(StreamSocketListener Listener, StreamSocketListenerConnectionReceivedEventArgs Args)
        {
            var deviceName = "";
            var serviceName = "";
            HostName hostName = null;
            
            try
            {
                // Only supports a single connection for the moment so we won't need these again
                _serviceProvider.StopAdvertising();
                Listener.Dispose();

                _socket = Args.Socket;
                _writer = new DataWriter(_socket.OutputStream);
                _reader = new DataReader(_socket.InputStream);

                if (_socket.Information != null)
                {
                    // Turns out this only gives us the host address and an
                    // arbitrary number for service name. Pretty useless.
                    deviceName = _socket.Information.RemoteHostName.DisplayName;
                    hostName = _socket.Information.RemoteHostName;
                    serviceName = _socket.Information.RemoteServiceName;
                }
            } 
            catch (Exception ex)
            {
                ServerState = BluetoothServerState.Faulted;
                if (ConnectionError != null)
                    ConnectionError(this, ex);

                return;
            }

            ServerState = BluetoothServerState.Connected;
            if (ClientConnected != null)
            {
                var device = new BluetoothDevice(deviceName, hostName, serviceName);
                var connection = new DuplexConnection(_reader, _writer);
                ClientConnected(this, new ClientConnectedEventArgs(device, connection));
            }
        }