none
How to send Strings via Bluetooth RRS feed

  • Question

  • How to send a string from Bluetooth ? I want to send some text commands to an Arduino. 
    Saturday, February 9, 2019 1:27 AM

All replies

  • hello, I need to send some commands via bluetooth. The commands like, 

    command_1

    command_2

    I tried this but, Did not work. I need to send those commands with no line breaks.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    namespace Smart_Buddy
    {
        public partial class Form1 : Form
        {
            SerialPort serialPort1 = new SerialPort();
            private string messege;
    
            public Form1()
            {
                InitializeComponent();
              
                getAvailableComPorts();
    
               
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
    
            void getAvailableComPorts()
                {
                String[] ports = SerialPort.GetPortNames();
                comboBox1.Items.AddRange(ports);
                }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                  
                        SerialPort serialPort1 = new SerialPort();
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = 9600;
                    if (serialPort1.IsOpen)
                        messege = "d1_on";
                    serialPort1.WriteLine(messege);
                    serialPort1.Close();
                }
                catch
                {
                    label1.Text = "not Connected";
                }
            }
        }
    }
    

    Saturday, February 9, 2019 4:25 AM
  • Try this, notice the first three lines in the try/catch have been modified

     public partial class Form1 : Form
        {
            SerialPort serialPort1 = new SerialPort();
            private string messege;
    
            public Form1()
            {
                InitializeComponent();
    
                getAvailableComPorts();
    
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            void getAvailableComPorts()
            {
                String[] ports = SerialPort.GetPortNames();
                comboBox1.Items.AddRange(ports);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
    
                   // SerialPort serialPort1 = new SerialPort();
                    serialPort1.PortName = comboBox1.SelectedItem.ToString();
                    serialPort1.Open();
                    serialPort1.BaudRate = 9600;
                    if (serialPort1.IsOpen)
                        messege = "d1_on";
                    serialPort1.WriteLine(messege);
                    serialPort1.Close();
                }
                catch
                {
                    label1.Text = "not Connected";
                }
            }
         
        }

    Saturday, February 9, 2019 3:52 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, February 19, 2019 8:09 AM
    Moderator