Question

This may seem like a trite question as I am new to Object Oriented Programming, but I am working on a Windows Phone Application and I am curious about the _socket object within the program.

The code that I am using below works and I am sending this byte array via BlueTooth to my microprocessor through UART.

private async void ConnectToDevice(PeerInformation peer)
{
        if (_socket != null)
        {
            _socket.Dispose();
        }

        try
        {
            _socket = new StreamSocket();
            await _socket.ConnectAsync(peer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");

            byte[] bytesToSend = new byte[5] {8,2,3,14,5} ;
            var buffer = GenerateData(bytesToSend);

            await _socket.OutputStream.WriteAsync(buffer);

            MessageBox.Show("Connected to Bluetooth!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

            _socket.Dispose();
            _socket = null;
        }
    }

What I want to do is take the _socket object from above and use it in a different page of my application.

Basically, this method is being executed in the MainPage and I want to have access to the populated fields of the StreamSocket in the "ConfigurationPage" that I've created.

How would I go about getting this object to the ConfigurationPage without creating a new object?

Was it helpful?

Solution

One way could be to declare it in a static property in your App.xaml.cs:

public static StreamSocket Socket { get; set; }

Then you can access it from anywhere in the application:

await App.Socket.ConnectAsync(peer.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top