Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4777

General discussion • How to establish a bluetooth connection between Raspberry Pi and Unity Game Engine?

$
0
0
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:

Image
IO Expectation: Network connection closed normally.

And, in Rasp:

Image
It seems to connect, but then disconnects

I tried to find some solution, but none worked.

C# code I use on Unity:

Code:

 using UnityEngine;using UnityEngine.UI;using System.IO.Ports;using System.Collections.Generic;public class bluetooth : MonoBehaviour {        SerialPort rasp = new SerialPort ("COM18", 9600);    void Men (){        if (rasp.IsOpen) {            Debug.Log("Rasp is open!");        }    }    void Start(){        rasp.Open();        Men();    }    void Update(){        rasp.Write("Hello from Unity");        Debug.Log("Message send!");    }}  
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?

Statistics: Posted by giibatata — Mon Apr 22, 2024 1:28 am



Viewing all articles
Browse latest Browse all 4777

Trending Articles