Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to establish a bluetooth connection between Raspberry Pi and Unity Game Engine?

Discussion in 'Editor & General Support' started by Tungstenio74, Apr 22, 2024.

  1. Tungstenio74


    Apr 17, 2023
    I'm doing a project that needs a bluetooth connection between the Raspeberry Pi 3 and the Unity Game Engine v.2021.3.23 (which will run on a Windows PC).

    I have a little experience connecting HC-05 modules (with an Arduino) to Unity via the COM port created by the bluetooth link and it worked very well. But, as Rasp already has an integrated bluetooth module, I would like not to use the HC-05 modules to intercept communication. I would like to connect Rasp with Unity directly.

    So, I looked on the internet for how to connect the Rasp directly to the PC following the same logic as connecting the HC-05, through these two posts:

    • Post 1 This enables SPP on Rasp, so the Serial Port service can appear on the PC
    • Post 2 This opens the serial port created using PuTTY
    I tested it and it worked well. So I tried opening the serial port directly in Unity but it didn't work. In Unity the following errors appear:

    IOException: Network connection closed normally.

    And, in Rasp:

    It seems to connect, but then disconnects.

    I tried to find some solution, but none worked.

    C# code I use on Unity:

    Code (CSharp):
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.IO.Ports;
    5. using System.Collections.Generic;
    7. public class bluetooth : MonoBehaviour
    8. {
    10.    SerialPort rasp = new SerialPort ("COM18", 9600);
    12.    void Men (){
    13.        if (rasp.IsOpen) {
    14.            Debug.Log("Rasp is open!");
    15.        }
    16.    }
    18.    void Start(){
    19.        rasp.Open();
    20.        Men();
    21.    }
    23.    void Update(){
    24.        rasp.Write("Hello from Unity");
    25.        Debug.Log("Message send!");
    26.    }
    28. }
    Trying to find a solution, I saw this post and found that using socket networking might work. So I tried to find examples of implementing bluetooth sockets with C# in Unity and found the 32feet.NET package. As the author himself says in this post the package does not work well in Unity because of the .NET framework used, but the update does . So I tried using the InTheHand.Net.Bluetooth package in Unity and it didn't work. I got it from nuget and after opening the .zip file, I got the .dll file and put it in Unity. But when I tried to compile a simple code like in this post , the compiler did not recognizes package expressions. Again, I tried to find a solution for this, but I didn't find anything.

    Finally, does anyone have any guesses as to what's going wrong or a solution for connecting Rasp and Unity directly using bluetooth?