示例#1
0
        /// <summary>
        /// Creates a <see cref="BluetoothConnection"/> and connects it.
        /// </summary>
        /// <param name="bluetoothDeviceInfo">The device information for the remote Bluetooth device.</param>
        /// <returns>A tuple with the <see cref="ConnectionResult"/> and created <see cref="BluetoothConnection"/>.</returns>
        public static Tuple <ConnectionResult, BluetoothConnection> CreateBluetoothConnection(DeviceInfo bluetoothDeviceInfo)
        {
            BluetoothConnection bluetoothConnection = new BluetoothConnection(bluetoothDeviceInfo);
            ConnectionResult    result = bluetoothConnection.TryConnect().Result;

            return(new Tuple <ConnectionResult, BluetoothConnection>(result, bluetoothConnection));
        }
示例#2
0
        /// <summary>
        /// Asynchronously creates a <see cref="BluetoothConnection"/> and connects it.
        /// </summary>
        /// <param name="bluetoothDeviceInfo">The device information for the remote Bluetooth device.</param>
        /// <returns>
        /// A <see cref="Task{T}"/> representing the asynchronous operation, with the promise of a tuple with the
        /// <see cref="ConnectionResult"/> and created <see cref="BluetoothConnection"/> on completion.
        /// </returns>
        public static async Task <Tuple <ConnectionResult, BluetoothConnection> > CreateBluetoothConnectionAsync(DeviceInfo bluetoothDeviceInfo)
        {
            BluetoothConnection bluetoothConnection = new BluetoothConnection(bluetoothDeviceInfo);
            ConnectionResult    result = await bluetoothConnection.TryConnect();

            return(new Tuple <ConnectionResult, BluetoothConnection>(result, bluetoothConnection));
        }
示例#3
0
        /// <summary>
        /// Starts a Bluetooth server and listens for incoming <see cref="BluetoothConnection"/>s.
        /// </summary>
        public async Task StartBluetoothListener()
        {
            if (IsBluetoothOnline || !AllowBluetoothConnections || !BluetoothConnection.IsBluetoothSupported)
            {
                return;
            }

            bluetoothListener = new BluetoothListener(ConnectionFactory.GUID);
            bluetoothListener.Start();
            IsBluetoothOnline = !IsBluetoothOnline;

            while (IsBluetoothOnline)
            {
                BluetoothClient bluetoothClient = await Task.Factory.FromAsync(bluetoothListener.BeginAcceptBluetoothClient, bluetoothListener.EndAcceptBluetoothClient, TaskCreationOptions.PreferFairness);

                BluetoothConnection bluetoothConnection = ConnectionFactory.CreateBluetoothConnection(bluetoothClient);
                bluetoothConnection.NetworkConnectionClosed += connectionClosed;

                //Inform all subscribers.
                if (connectionEstablished != null &&
                    connectionEstablished.GetInvocationList().Length > 0)
                {
                    connectionEstablished(bluetoothConnection, ConnectionType.Bluetooth);
                }
            }
        }