I am developing a small program using unity and esp32. my problem is each time esp32 is connected to pc over Bluetooth the port changes. So I have to change the port each time on the script. how can I make the unity scan and connect to esp32 automatically each time it connects to pc over Bluetooth. below is my code .
using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;
public class Movement : MonoBehaviour
{
public Text outText;
public Color clr;
public string SerialOutData;
public Renderer myRenderer;
SerialPort sp=new SerialPort("COM8",9600);
// Start is called before the first frame update
void Start()
{
sp.Open();
sp.ReadTimeout =1;
myRenderer= gameObject.GetComponent<Renderer>();
//sp.DtrEnable= true;
}
// Update is called once per frame
void Update()
{
if(sp.IsOpen){
try
{
SerialOutData = sp.ReadLine().ToString();
outText.text=SerialOutData;
ColorChange(SerialOutData);
// sp.WriteLine("A");
}
catch(System.Exception e)
{
//print(e);
}
}
void ColorChange(string Dat)
{
if(Dat=="black")
{
print("one");
clr = new Color(0.0f,0.0f,0.0f,255f);
myRenderer.material.color=clr;
}
else if(Dat=="white")
{
clr = new Color(255f,255f,255f,255f);
myRenderer.material.color=clr;
}
}
}
}