본문 바로가기
개발/C#

유니티 C# 블루투스 통신 간단 구현하기 Unity Bluetooth

by SPNK 2023. 3. 8.
반응형
  • 코드 작성
using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class BluetoothController : MonoBehaviour
{
    public string deviceName = "MyBluetoothDevice";
    public int baudRate = 9600;
    private SerialPort serialPort;

    void Start()
    {
        string[] ports = SerialPort.GetPortNames();
        foreach (string port in ports)
        {
            if (port.Contains("Bluetooth") && port.Contains(deviceName))
            {
                serialPort = new SerialPort(port, baudRate);
                serialPort.ReadTimeout = 1000;
                serialPort.Open();
                break;
            }
        }
    }

    void Update()
    {
        if (serialPort != null && serialPort.IsOpen && serialPort.BytesToRead > 0)
        {
            string data = serialPort.ReadLine();
            Debug.Log("Received data from Bluetooth device: " + data);
        }
    }

    public void SendData(string data)
    {
        if (serialPort != null && serialPort.IsOpen)
        {
            serialPort.Write(data);
        }
    }

    void OnDestroy()
    {
        if (serialPort != null && serialPort.IsOpen)
        {
            serialPort.Close();
        }
    }
}

 


참고할만한 글

 

 

유니티 C# 자이로센서 간단 구현하기

코드 작성 void Update() { if (SystemInfo.supportsGyroscope) { Gyroscope gyro = Input.gyro; gyro.enabled = true; transform.rotation = gyro.attitude; } }

parksh3641.tistory.com

 

 

유니티 C# RectTransform width height 간단 수정법

전체 수정 rectTransform.sizeDelta = new Vector2(width, height); Width 수정 private void SetWidth(float width) { rectTransform.sizeDelta = new Vector2(width, rectTransform.sizeDelta.y); } Height 수정 private void SetHeight(float height) { rectTransf

parksh3641.tistory.com

 

반응형

댓글