Arduino bluetooth communication with Processing
in
Android Processing
•
3 months ago
I have this project where my arduino board is sending analog data wireless through a bluetooth module to an application made with "Processing" in my android phone.
The problem is this. The information is received as a byte,therefore I need to do some conversion so the value makes "sense", I know I have to convert the Inputstream to a string, but I don't know how to do it,I've been trying a lot of methods and technics and all of them fail, I'm including the code for arduino and the code for processing is kind of messy so for this last I'm including the "vital" lines that receives the inputstream signal.
Arduino code:
// Reading and sending to BT 1Kohm potentiometer value.
void setup () {
Serial.begin(57600);
}
void loop () {
int rawvalue=analogRead(A0);
Serial.write(rawvalue);
delay(1000);
}
Processing code:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
import android.content.pm.ActivityInfo;
private static final int REQUEST_ENABLE_BT = 3;
ArrayList dispositivos;
BluetoothAdapter adaptador;
BluetoothDevice dispositivo;
BluetoothSocket socket;
InputStream ins;
OutputStream ons;
boolean registrado = false;
PFont f1;
PFont f2;
int estado;
String error;
byte valor;
///////////////////////////////////////////////////////////
void connectDevice()
{
try
{
socket = dispositivo.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
/*
Method m = dispositivo.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(dispositivo, 1);
*/
socket.connect();
ins = socket.getInputStream();
ons = socket.getOutputStream();
estado = 3;
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
}
//////////////////////////////////////////////////////////
This line code receives the inputstrem and displays the value as "valor" in the middle of the screen
void showData()
{
try
{
while(ins.available() > 0)
{
valor = (byte)ins.read();
}
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
background(0);
fill(255);
text(valor, width / 2, height / 2);
stroke(255, 255, 0);
fill(255, 0, 0);
rect(120, 400, 80, 40);
fill(255, 255, 0);
text("Botón", 135, 425);
}
The problem is this. The information is received as a byte,therefore I need to do some conversion so the value makes "sense", I know I have to convert the Inputstream to a string, but I don't know how to do it,I've been trying a lot of methods and technics and all of them fail, I'm including the code for arduino and the code for processing is kind of messy so for this last I'm including the "vital" lines that receives the inputstream signal.
Arduino code:
// Reading and sending to BT 1Kohm potentiometer value.
void setup () {
Serial.begin(57600);
}
void loop () {
int rawvalue=analogRead(A0);
Serial.write(rawvalue);
delay(1000);
}
Processing code:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
import android.content.pm.ActivityInfo;
private static final int REQUEST_ENABLE_BT = 3;
ArrayList dispositivos;
BluetoothAdapter adaptador;
BluetoothDevice dispositivo;
BluetoothSocket socket;
InputStream ins;
OutputStream ons;
boolean registrado = false;
PFont f1;
PFont f2;
int estado;
String error;
byte valor;
///////////////////////////////////////////////////////////
void connectDevice()
{
try
{
socket = dispositivo.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
/*
Method m = dispositivo.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(dispositivo, 1);
*/
socket.connect();
ins = socket.getInputStream();
ons = socket.getOutputStream();
estado = 3;
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
}
//////////////////////////////////////////////////////////
This line code receives the inputstrem and displays the value as "valor" in the middle of the screen
void showData()
{
try
{
while(ins.available() > 0)
{
valor = (byte)ins.read();
}
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
background(0);
fill(255);
text(valor, width / 2, height / 2);
stroke(255, 255, 0);
fill(255, 0, 0);
rect(120, 400, 80, 40);
fill(255, 255, 0);
text("Botón", 135, 425);
}
1