Hola a todos ,es mi primer proyecto con arduino + android via bluetooth y quisiera que me ayudaran en algo que me estoy atorando, lo que pasa es que estoy haciendo un sensor de gas, y los datos que arroje el sensor Mq2, los quiero ver en mi telefono en una aplicacion que hice en ECLIPSE siguiendo varios tutoriales.
Bueno este es mi codigo en arduino:
///////////////Bluetooth/////////////////
#include <SoftwareSerial.h>
#define rxPin 5
#define txPin 6
#define ledPin 7
// configura un nuevo puerto
SoftwareSerial miPuertoSerie = SoftwareSerial(rxPin, txPin);
int unChar;
/////////////////alchoholimetro//////////////////
const int pinAnalogo = A0; // Es el pin en donde esta conectado el sensor
const int Nleds = 6; //Numero de leds que estarán funcionando
int ledPins[] = { 13,12,11,10,9,8}; // Array de los pines a los que estan conectados los led
void setup() {
/////////////////alchoholimetro//////////////////
//For del led
for (int led = 0; led < Nleds; led++) {
pinMode(ledPins[led], OUTPUT);
}
///////////////Bluetooth/////////////////
miPuertoSerie.begin(9600);
}
void loop() {
/////////////////alchoholimetro//////////////////
int sensorReading = analogRead(pinAnalogo); // Entrada del sensor análogo
int ledLevel = map(sensorReading, 0, 100, 0, Nleds); // asignar intervalo desde 0 a los leds
// for del led
for (int led = 0; led < Nleds; led++) {
if (led < ledLevel) { // Si el elemento del Array es menor que el nivel del led se prende el led de este pin
digitalWrite(ledPins[led], HIGH);
}
//Apaga todos los leds si no hay precencia en el pin analogo
else {
digitalWrite(ledPins[led], LOW);
}
}
/////////////////Bluetooh//////////////////
// escucha la llegada de nuevos datos serie:
miPuertoSerie.println("Hola");
delay(3000);
}
en la parte del print “hola” , usando “Blueterm” , si lo lee :
y en mi aplicacion no sé que codigo usar para que lo lea:
Donde señala la flecha deberia decir el “Hola” que estoy imprimiendo este es mi codigo en ECLIPSE:
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 2;
boolean conectado = false;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothDevice mBluetoothDevice = null;
protected BluetoothSocket mySocket = null;
private InputStream MyInStream;
Thread t = null;
byte[] bufferIn = new byte[100];
byte[] bufferIn_temp = new byte[10];
int bytesIn=0;
int bytesIn_tmp=0;
String strTempIn;
String strTempIn2;
String strBufferIn;
int primera_posi;
int segunda_posi;
private boolean isBtnDown;
/**
* Es el TextView para mostrar los mensajes
*/
TextView textViewLog;
TextView nivel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nivel=(TextView)findViewById(R.id.nivel);
textViewLog = (TextView)findViewById(R.id.editText1);
nivel.setEnabled(false);
textViewLog.setEnabled(false);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final Button button1 = (Button) findViewById(R.id.button1);
mBluetoothDevice = null;
isBtnDown = false;
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
conectar();
button1.setEnabled(false);
conectado = true;
}
private void conectar() {
try{
if (mBluetoothAdapter.isEnabled())
{
String address ="20:14:02:17:11:51";
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(address);
textViewLog.append("\n"+mBluetoothDevice.getName()+ " "+ mBluetoothDevice.toString());
nivel.setText(INPUT_SERVICE);
BluetoothSocket tmp = null;
try {
Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
} catch (Exception e) {
textViewLog.append("");
}
mySocket = tmp;
try {
mySocket.connect();
} catch (IOException e) {
textViewLog.append("\n"+e.getMessage());
textViewLog.append("\n"+"La conexion no puede ser posible");
}
try {
MyInStream = mySocket.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
if (t==null)
{
t = new Thread()
{
public void run()
{
while (true)
{
try {
bytesIn=MyInStream.read(bufferIn);
strTempIn = new String(bufferIn,0,bytesIn);
strBufferIn += strTempIn;
} catch (IOException e) {
e.printStackTrace();
}
}
};
};
}
t.start();
textViewLog.append("\n"+"Conectado con: "+"\n"+mBluetoothDevice.getName());
}
}catch(Exception e){
textViewLog.append("\n"+"Error conectando");
}
}
});
}
@Override
public void onStart() {
super.onStart();
if (!mBluetoothAdapter.isEnabled())
{
textViewLog.append("El Bluetooth se está activando");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onStop()
{
super.onStop();
}
}
Si alguien me pudiera ayudar, se los agradeceria mucho, despues subiré el proyecto ya terminado smiley
Buenas tardes.