- Home /
Question by
brannrodriguez · Mar 13, 2018 at 04:18 PM ·
c#bluetooth
¿How to connect Unity with Bluetooth HC-05?
I want to send a data type "char o int" through a Unity script for the Bluetooth module HC-05 but I do not know how to do the configuration of this module in Unity
Comment
Hello, Have you found any answer? I am also looking for it. Thanks in advance
$$anonymous$$e Too please give me the suggestion @brannrodriguez @bharath09vadde
Hello, I have used the following code for Arduino
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(12,13);
const int xpin= 0;
const int ypin = 1;
int value2;
int value3;
int value;
const int numofinputs = 14;
const int inputpins[numofinputs] = {0,1,2,3,4,5,6,7,8,9,10,16,17,18};
const int joycent = 507;
const int joydeadband = 50;
void setup() {
// put your setup code here, to run once:
Bluetooth.begin(9600);
Serial.begin(9600);
for(int i = 0; i< numofinputs; i++)
{
pin$$anonymous$$ode(inputpins[i] , INPUT);
digitalWrite(inputpins[i] , HIGH);
}
}
void loop() {
value2 = analogRead(xpin);
value3 = analogRead(ypin);
if(value2 < joycent - joydeadband)
{
Bluetooth.write(115);
//delay(1);
}
if(value2 > joycent + joydeadband)
{
Bluetooth.write(116);
//delay(1);
}
if(value3 < joycent - joydeadband)
{
Bluetooth.write(117);
//delay(1);
}
if(value3 > joycent + joydeadband)
{
Bluetooth.write(118);
//delay(1);
}
for(int i = 0; i< numofinputs; i++)
{
if(digitalRead(inputpins[i]) == LOW)
Bluetooth.write(i + 101);
//delay(1);
}
}
using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;
using System;
public class Arduino : $$anonymous$$onoBehaviour
{
SerialPort sp = new SerialPort("\\\\.\\CO$$anonymous$$13", 9600);
public GameObject camerarig;
public Button oneplus, one$$anonymous$$us, two$$anonymous$$us, twoplus, three$$anonymous$$us, threeplus, four$$anonymous$$us, fourplus, five$$anonymous$$us, fiveplus, six$$anonymous$$us, sixplus;
public Transform axis1, axis2, axis3, axis4, axis5, driller, speed_controller, holder_left, holder_right, weldtool, Robotgripper;
float SpeedRot, SpeedRot2;
public float speed, speed2;
public Text speedtext, testtext;
public int x, y;
public GameObject box;
void Start()
{
x = 30;
y = 30;
SpeedRot = speed;
SpeedRot2 = speed2;
if (!sp.IsOpen)
{
try
{
sp.Open();
sp.ReadTimeout = 100;
}
catch (Exception)
{ }
}
}
void Update()
{
testtext.text = "Axis speed is " + SpeedRot.ToString() + Environment.NewLine + "Rotator speed is " + SpeedRot2.ToString();
if (driller.gameObject.activeSelf)
{
driller.Rotate(Vector3.right * 2000 * Time.deltaTime);
}
if (sp.IsOpen)
{
try
{
if (sp.ReadByte() == 101)
{
// one$$anonymous$$us.onClick.Invoke();
float angle = axis1.localEulerAngles.y;
angle = (angle > 180) ? angle - 360 : angle;
if (angle >= -165 && angle <= 165)
{
axis1.Rotate(Vector3.down * SpeedRot * Time.deltaTime);
}
else
{
if (angle <= -165 && angle > -175)
{
axis1.eulerAngles = new Vector3(axis1.eulerAngles.x, -165, axis1.eulerAngles.z);
}
}
Debug.Log("1 is pressed");
}
}
catch (System.Exception)
{
}
}
}
}
Answer by brannrodriguez · Jul 03, 2018 at 01:04 PM
If you want to do it for Android use the asset "Android & Microcontrollers / Bluetooth", you will find it in the Unity Assest Store. It has a cost of $ 20 and is very easy to use.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154522im_/https://answers.unity.com/themes/thub/images/avi.jpg)