5

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;
        }
    }       
}
Rice_Crisp
  • 1,242
  • 1
  • 16
  • 33
  • 1
    Has anyone successfully used SerialPort to access Bluetooth? – Rice_Crisp Mar 20 '13 at 19:22
  • Are you sure you've got the right COM port? – Joetjah Mar 21 '13 at 08:39
  • Yes, Device Manager and the Bluetooth software both show that COM11 and COM12 are the correct ports. I think that the issue could be related to Unity3D because I'm not getting errors when I run a similar program in Visual C#. – Rice_Crisp Mar 21 '13 at 14:39
  • Unity3D uses the Mono .NET runtime and not the MSFT one, as I understand it. It may be having problems accessing the serial port. – alanjmcf Apr 02 '13 at 08:36

2 Answers2

2

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();
    }

}
Tony
  • 16,527
  • 15
  • 80
  • 134
KrisCode
  • 36
  • 1
  • 4
  • yeah it wouldn't work for me without the baud rate set because i cant transfer data without the right speed on both sides... also when i changed the sp.ReadTimeout length in Unity the connection would not work unless they are the same in both Unity and the code on the device. I guess it depends... – KrisCode May 13 '13 at 03:36
  • I'm afraid I'm still not getting anything. I just get the error that the port is not open. – Rice_Crisp May 13 '13 at 14:42
  • Actually, I may have found the problem. I was just trying to use my phone as a test connection, but I don't think my phone uses the serial ports. It only sends audio, which I believe is its own sort of port. – Rice_Crisp May 13 '13 at 14:47
  • yup, i did the same thing. It is possible to get it to work on the phone too but I haven't got that far yet. If it says port not open then the problem is in your bluetooth settings and the COM port number. Every time you pair the device the port is opened and could be a different number so you have to change that in the code. You should see a incoming and outgoing port in the COM port area of your bluetooth settings. Use the incoming port that shows your device. If you don't see both then try deleting the device from your bluetooth devices and then pair it again. – KrisCode May 13 '13 at 18:46
  • I do this with the COM port window in the bluetooth settings open and you can actually watch the ports open up when you pair the device... – KrisCode May 13 '13 at 18:46
  • 1
    Your nullity check is bugged: you test if `sp` is null, and if it is, you try to access `sp.IsOpen`. – Right leg May 17 '17 at 14:02
1

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.

Eric Smekens
  • 1,602
  • 20
  • 32