- Home /
Send text via bluetooth
Please help me How to make android app to send texts via bluetooth ? This application will be used to control the arduino through hc06 module
Answer by Seg_fault_ · Sep 01, 2016 at 08:36 AM
Hi,
I think the best way is to use the SerialPort Class from the .Net 2.0 framwork : https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx
I use that for the communication between a Arduino like and a Unity app. It's a PC app but I guess it should work the same way in Android app.
You should need a code like that :
using System.IO.Ports;
public SerialPort sp;
public string port = "COM5"; //communication port
public int speed = 115200; //speed of the communication (baud)
//open bluetooth communication
public void openConnection()
{
if(sp == null)
{
sp = new SerialPort(port, speed, Parity.None, 8, StopBits.One);
}
if (sp != null)
{
if (!sp.IsOpen)
{
try
{
sp.Open(); // opens the connection
sp.ReadTimeout = 100; // sets the timeout value before reporting error
Debug.Log("Port Open!";);
}catch(System.Exception e)
{
Debug.LogWarning(e.ToString());
}
}
}
}
//close connexion
public void closeConnection()
{
if(sp != null && sp.IsOpen)
{
try
{
sp.Close();
}
catch(System.Exception e)
{
Debug.LogWarning(e.ToString());
}
}
}
//Send commande to the controller
public void sendCommand(string cmd)
{
try
{
sp.Write(cmd);
}catch(System.Exception e)
{
Debug.LogWarning(e.ToString());
}
}
Hi, I was wondering if this could work if I'm sending the 'text' to another android device or would I still need to change things in this code.
Well, I guess it should be the same for the sending side, but you need to add the reading function to the other android device. But I don’t know how you dot get the good port name on android… You can also look at this pluggin, it’s maybe easier (I didn’t test it so can say if it’s good or not) https://github.com/seiji/unity-bluetooth
I've read here that Android uses Linux CO$$anonymous$$ Ports so maybe it would work if I just replaced the port CO$$anonymous$$5
with its Linux equivalent? Also, can you give me a sample code/function that can read sent text
via Bluetooth? Thank, in advance!!!
Answer by Volstn1 · Jan 25, 2019 at 05:27 PM
Hi I am trying to use Unity on my PC to connect to an HC-05 module on an Arduino. Can I use my computer's built in bluetooth or should I set up a bluetooth dongle and connect that to a COM port? Thanks @Seg_fault_,Hi I am trying to use Unity on my PC to connect to an HC-05 module and an Arduino. Will I be able to use the computer's Bluetooth adapter or do I need to set up a Bluetooth dongle and attach that to the COM port? Thanks!@Seg_fault_
Hi Volstn1, You should be able to use the build in Bluetooth device of your computer. Normally it allows to do the same things that you could do with a Bluetooth dongle. You have to start your arduino in associating mode, then you should be able to see it on your computer (whatever you are using a dongle or the build in device), associate the Arduino Bluetooth device with your computer, it will create a CO$$anonymous$$ port. Then you can connect using this CO$$anonymous$$ Port in unity using the SerialPort Class.
Your answer

Follow this Question
Related Questions
Unity3D and Android Controller Support 1 Answer
How to get first person controller 2 Answers
Third Person Controller: Animates correctly, but can't move 5 Answers
How do I move the Camera using HTC VIVE trigger button 0 Answers
VR hands in floor when built,Oculus hands in wrong position when built 3 Answers