public static async Task <StreamSocketConnection> ConnectAsync(Guid serviceUuid, IObjectSerializer serializer)
        {
            // Find all paired instances of the Rfcomm service
            var serviceId      = RfcommServiceId.FromUuid(serviceUuid);
            var deviceSelector = RfcommDeviceService.GetDeviceSelector(serviceId);
            var devices        = await DeviceInformation.FindAllAsync(deviceSelector);

            if (devices.Count > 0)
            {
                var device = devices.First(); // TODO

                var deviceService = await RfcommDeviceService.FromIdAsync(device.Id);

                if (deviceService == null)
                {
                    // Access to the device is denied because the application was not granted access
                    return(null);
                }

                var attributes = await deviceService.GetSdpRawAttributesAsync();

                IBuffer serviceNameAttributeBuffer;

                if (!attributes.TryGetValue(SdpServiceNameAttributeId, out serviceNameAttributeBuffer))
                {
                    // The service is not advertising the Service Name attribute (attribute id = 0x100).
                    // Please verify that you are running a BluetoothConnectionListener.
                    return(null);
                }

                using (var attributeReader = DataReader.FromBuffer(serviceNameAttributeBuffer))
                {
                    var attributeType = attributeReader.ReadByte();

                    if (attributeType != SdpServiceNameAttributeType)
                    {
                        // The service is using an unexpected format for the Service Name attribute.
                        // Please verify that you are running a BluetoothConnectionListener.
                        return(null);
                    }

                    var serviceNameLength = attributeReader.ReadByte();

                    // The Service Name attribute requires UTF-8 encoding.
                    attributeReader.UnicodeEncoding = UnicodeEncoding.Utf8;
                    var serviceName = attributeReader.ReadString(serviceNameLength);

                    var socket = new StreamSocket();
                    await socket.ConnectAsync(deviceService.ConnectionHostName, deviceService.ConnectionServiceName);

                    var connection = await StreamSocketConnection.ConnectAsync(socket, serializer);

                    return(connection);
                }
            }

            // No devices found
            return(null);
        }
        public async Task ConnectViaStreamSocketsAsync(string port, string serverIp)
        {
            try
            {
                var serializer = new DefaultJsonSerializer(GetType().GetTypeInfo().Assembly);
                var connection = await StreamSocketConnection.ConnectAsync(serverIp, port, serializer);

                //var connection = await BluetoothConnection.ConnectAsync(MainViewModel.CustomBluetoothServiceId, serializer);
                Client = new RpcClient(connection, this);
                RaisePropertyChanged(nameof(Client));
            }
            catch
            {
                Messages.Add($"Failed to connect to {serverIp}:{port}");
            }
        }
        void ConnectCompleted(IAsyncResult result, object state)
        {
            try
            {
                _socket.EndConnect(result);
            }
            catch (SocketException ex)
            {
                _factory.ConnectionFailed(ex);

                return;
            }
            catch (ObjectDisposedException ex)
            {
                _factory.ConnectionFailed(ex);

                return;
            }

            Protocol protocol = _factory.BuildProtocol();

            SocketConnection connection = new StreamSocketConnection(_reactor, protocol);
            connection.StartConnection(_socket, protocol.ReceiveBufferSize);
            protocol.MakeConnection(connection);
        }