public BluetoothConnectionResult Connect(object address)
        {
            var result = new BluetoothConnectionResult();

            try
            {
                if (address is BluetoothAddress bluetoothAddress)
                {
                    var endPoint = new BluetoothEndPoint(bluetoothAddress, BluetoothService.SerialPort);
                    bluetoothClient.Connect(endPoint);

                    result.IsSuccess = true;
                }
                else
                {
                    throw new Exception("The address should be a BluetoothAddress.");
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message   = ex.Message;
            }

            return(result);
        }
예제 #2
0
        private void ConnectionWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object address = null;

            devicesListBox.Invoke((Action)(() =>
            {
                if (devicesListBox.SelectedIndex > -1)
                {
                    address = devicesListBox.SelectedValue;
                    e.Result = devicesListBox.Text;
                }
                else
                {
                    MessageBox.Show("Select a device!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }));

            serviceResult = bluetoothService.Connect(address);
        }