Accessing Bluetooth data via Serialport in C#

26,467

Solution 1

Here is some code I'm working on and it gets data from the bluetooth connection to a standalone pc build (or in the editor) as long as the COM port (in my case COM9) is the same as the bluetooth device when you pair it.

After you pair it go to Bluetooth Settings > COM Ports and see what port is there with the name of your device. It might say COM8 or COM9 or whatever. If the device is paired and the COM Port is the same in the code as it is in your Bluetooth Settings, AND the timeout number and baud rate are the same as in the application you are sending the data from... then you will get something from this code when you run it. This is just meant to help make a connection to the serial over bluetooth connection.

Hope it helps someone. I've gotten a lot of great advice from reading these forums ;)

using System.Collections;
using System.IO.Ports;

public class checker : MonoBehaviour {

    public static SerialPort sp = new SerialPort("COM9", 9600, Parity.None, 8, StopBits.One);
    public string message, message1;
    public string message2;

    void Start() {
        OpenConnection();   
    }

    void Update() { 
        message2 = sp.ReadLine(); 
    } 

    void OnGUI()    {
        GUI.Label(new Rect(10, 180, 100, 220), "Sensor1: " + message2);
    }

    public void OpenConnection() {
        if (sp != null) 
        {
            if (sp.IsOpen) 
            {
                sp.Close();
                message = "Closing port, because it was already open!";
            }
            else 
            {
                sp.Open(); 
                sp.ReadTimeout = 1000;  
                message = "Port Opened!";
            }
        }
        else 
        {
            if (sp.IsOpen)
            {
                print("Port is already open");
            }
            else 
            {
                print("Port == null");
            }
        }
    }

    void OnApplicationQuit() {
        sp.Close();
    }

}

Solution 2

It should be possible. The bluetooth rfcomm/spp services emulate a serial port. A COM port if it's on Windows. Baudrate doesn't matter in this emulation, it will always go as fast as possible.

You need to have the devices paired and connected though. To what device are you connecting? Try to make a connection first with Putty or some terminal application.

Share:
26,467
Rice_Crisp
Author by

Rice_Crisp

Updated on May 13, 2020

Comments

  • Rice_Crisp
    Rice_Crisp almost 4 years

    So I'm working in Unity3D, programming in C#, and I heard that one can read data from a Bluetooth adaptor via SerialPort. I have several Bluetooth USB adaptors that I've tried to connect on my PC using this method. However, when I try to open the SerialPort, I get an error message that says port does not exist. I only included the code relevant to the question, but portI is a string ("COM11" or "COM12") and PortIn is of type SerialPort.

    void OnGUI() {
        GUI.Label(new Rect(btnX, btnY, btnW, btnH), "PortIn = " + portI);
        if(!connected) {
            for (int i = 0; i<ports.Length; i++) {
                if(GUI.Button(new Rect(btnX, btnY + btnH + (btnH * i), btnW, btnH), ports[i])) {
                    portI = ports[i];
                }
            }           
        }       
        if(GUI.Button(new Rect(btnX + (btnW * 2 + 20), btnY, btnW, btnH), "Connect")) {
            portIn = new SerialPort(portI, 9600);               
            portIn.ReadTimeout = 1000;
            if (!portIn.IsOpen) {
                portIn.Open();
            }
            connected = true;
            }
        }       
    }