Hello everybody !
I'm quite new to the world of Arduino, but since the beginning I wanted to use my phone (an Android) to control my Arduino through Bluetooth. (I'm also a beginner to Android by the way).
I did tones of research online in order to find something relevant and simple in order to do that. But I miserably failed.
But since patience is a virtue, I managed to gather information from almost everywhere and found this.
Without any pretension, here is a short how-to tutorial.
Maybe this post will help, maybe not, but at least I tried ^_^.
You need a Bluetooth Adapter plugged to your Arduino obviously.
For the Arduino code, I simply used the blink code and mixed it with Serial.read() function.
http://arduino.cc/en/Serial/read
I'm not gonna explain how android works but here's the link to download the IDE.
Before anything, I want to add something, I'm not a pro in programming, like not at all, and I'm not a native English speaker, so I apologize for the mistakes I will make and for the non-scholarly terms I will use. (Remember that while reading, please).
Since I just want to use my App with my arduino, I hardcoded its Mac Address in the code.
First thing first you're gonna need to test if the bluetooth in your phone is enabled or not and if you can use it.
private void CheckBt() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(getApplicationContext(), "Bluetooth Disabled !",
Toast.LENGTH_SHORT).show();
/* It tests if the bluetooth is enabled or not, if not the app will show a message. */
}
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),
"Bluetooth null !", Toast.LENGTH_SHORT)
.show();
}
}
You can invoke that method in the onCreate method or whenever you want to (before trying to make a connection obviously).
The connect part is quite simple, in that case the method was triggered by clicking on a button :
public void Connect() {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
Log.d("", "Connecting to ... " + device);
mBluetoothAdapter.cancelDiscovery();
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
/* Here is the part the connection is made, by asking the device to create a RfcommSocket (Unsecure socket I guess), It map a port for us or something like that */
btSocket.connect();
Log.d("", "Connection made.");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
Log.d("", "Unable to end the connection");
}
Log.d("", "Socket creation failed");
}
beginListenForData();
/* this is a method used to read what the Arduino says for example when you write Serial.print("Hello world.") in your Arduino code */
}
Then the method to actually Send stuff to the arduino.
private void writeData(String data) {
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Log.d(TAG, "Bug BEFORE Sending stuff", e);
}
String message = data;
/* In my example, I put a button that invoke this method and send a string to it */
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
Log.d(TAG, "Bug while sending stuff", e);
}
}
Finally the part where the Android application actually read data just like I said earlier.
public void beginListenForData() {
try {
inStream = btSocket.getInputStream();
} catch (IOException e) {
}
Thread workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = inStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
inStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
if(Result.getText().toString().equals("..")) {
Result.setText(data);
} else {
Result.append("\n"+data);
}
/* You also can use Result.setText(data); it won't display multilines
*/
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
I took it online so I can't really tell you much about that other that it works.
From what I can understand this method creates a Thread that checks whether there's something or not in the buffer and display it in the Result TextView.
Don't forget to add those two lines in your manifest.
Otherwise the app will crash.
Here's the full code :
There you go.
Hope it wasn't too useless and boring and maybe somewhat interesting, it helped me to understand Bluetooth SPP.
Maybe it will be simpler to upload the project so, here it is.
Project Android - ArduinoBluetooth.zip (1.21 MB)