Android + Arduino Bluetooth Connection Problem
hi, can android app connect via bluetooth arduino, no data can transmitted between them, below setup , code:
htc anroid v2.2
bluetooth mate gold modem
arduino mega (atmega1280)
---------------------------------------------------------------------------------------------------------------------------------------
android java code:
---------------------------------------------------------------------------------------------------------------------------------------
arduino code:
---------------------------------------------------------------------------------------------------------------------------------------
i've tried using 115200 , 9600 baud rates,
and i've tried setting bluetooth rx , tx pins input/output , output/input.
the arduino receiving serial data pc can't send android (i can see because of flashled() method). android can't send data @ arduino.
however both connected because green light on modem turns on , goes off , red led flashes when close connection.
the senddata() method doesn't throw exception because otherwise showmessage("send failed"); appear.
any appreciated!
code take from: http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/
htc anroid v2.2
bluetooth mate gold modem
arduino mega (atmega1280)
---------------------------------------------------------------------------------------------------------------------------------------
android java code:
code: [select]
package com.example.bluetoothexample;
import android.app.activity;
import android.bluetooth.bluetoothadapter;
import android.bluetooth.bluetoothdevice;
import android.bluetooth.bluetoothsocket;
import android.content.intent;
import android.os.bundle;
import android.os.handler;
import android.view.view;
import android.widget.textview;
import android.widget.edittext;
import android.widget.button;
import android.widget.toast;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.set;
import java.util.uuid;
public class bluetoothexampleactivity extends activity
{
textview mylabel;
edittext mytextbox;
bluetoothadapter mbluetoothadapter;
bluetoothsocket mmsocket;
bluetoothdevice mmdevice;
outputstream mmoutputstream;
inputstream mminputstream;
thread workerthread;
byte[] readbuffer;
int readbufferposition;
int counter;
volatile boolean stopworker;
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
button openbutton = (button)findviewbyid(r.id.open);
button sendbutton = (button)findviewbyid(r.id.send);
button closebutton = (button)findviewbyid(r.id.close);
mylabel = (textview)findviewbyid(r.id.label);
mytextbox = (edittext)findviewbyid(r.id.entry);
//open button
openbutton.setonclicklistener(new view.onclicklistener()
{
public void onclick(view v)
{
try
{
findbt();
openbt();
}
catch (ioexception ex) { }
}
});
//send button
sendbutton.setonclicklistener(new view.onclicklistener()
{
public void onclick(view v)
{
try
{
senddata();
}
catch (ioexception ex)
{
showmessage("send failed");
}
}
});
//close button
closebutton.setonclicklistener(new view.onclicklistener()
{
public void onclick(view v)
{
try
{
closebt();
}
catch (ioexception ex) { }
}
});
}
void findbt()
{
mbluetoothadapter = bluetoothadapter.getdefaultadapter();
if(mbluetoothadapter == null)
{
mylabel.settext("no bluetooth adapter available");
}
if(!mbluetoothadapter.isenabled())
{
intent enablebluetooth = new intent(bluetoothadapter.action_request_enable);
startactivityforresult(enablebluetooth, 0);
}
set<bluetoothdevice> paireddevices = mbluetoothadapter.getbondeddevices();
if(paireddevices.size() > 0)
{
for(bluetoothdevice device : paireddevices)
{
if(device.getname().equals("firefly-108b"))
{
mmdevice = device;
break;
}
}
}
mylabel.settext("bluetooth device found");
}
void openbt() throws ioexception
{
uuid uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); //standard serialportservice id
mmsocket = mmdevice.createrfcommsockettoservicerecord(uuid);
mmsocket.connect();
mmoutputstream = mmsocket.getoutputstream();
mminputstream = mmsocket.getinputstream();
beginlistenfordata();
mylabel.settext("bluetooth opened");
}
void beginlistenfordata()
{
final handler handler = new handler();
final byte delimiter = 10; //this ascii code newline character
stopworker = false;
readbufferposition = 0;
readbuffer = new byte[1024];
workerthread = new thread(new runnable()
{
public void run()
{
while(!thread.currentthread().isinterrupted() && !stopworker)
{
try
{
int bytesavailable = mminputstream.available();
if(bytesavailable > 0)
{
byte[] packetbytes = new byte[bytesavailable];
mminputstream.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()
{
mylabel.settext(data);
}
});
}
else
{
readbuffer[readbufferposition++] = b;
}
}
}
}
catch (ioexception ex)
{
stopworker = true;
}
}
}
});
workerthread.start();
}
void senddata() throws ioexception
{
string msg = mytextbox.gettext().tostring();
msg += "\n";
//mmoutputstream.write(msg.getbytes());
mmoutputstream.write('a');
mylabel.settext("data sent");
}
void closebt() throws ioexception
{
stopworker = true;
mmoutputstream.close();
mminputstream.close();
mmsocket.close();
mylabel.settext("bluetooth closed");
}
private void showmessage(string themsg) {
toast msg = toast.maketext(getbasecontext(),
themsg, (toast.length_long)/160);
msg.show();
}
}---------------------------------------------------------------------------------------------------------------------------------------
arduino code:
code: [select]
#include <softwareserial.h>
int bluetoothtx = 45;
int bluetoothrx = 47;
softwareserial bluetooth(bluetoothtx, bluetoothrx);
void setup()
{
//pinmode(45, output);
//pinmode(47, input);
pinmode(53, output);
//setup usb serial connection computer
serial.begin(9600);
//setup bluetooth serial connection android
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("u,9600,n");
bluetooth.begin(9600);
}
void loop()
{
//read bluetooth , write usb serial
if(bluetooth.available())
{
char tosend = (char)bluetooth.read();
serial.print(tosend);
flashled();
}
//read usb serial bluetooth
if(serial.available())
{
char tosend = (char)serial.read();
bluetooth.print(tosend);
flashled();
}
}
void flashled()
{
digitalwrite(53, high);
delay(500);
digitalwrite(53, low);
}---------------------------------------------------------------------------------------------------------------------------------------
i've tried using 115200 , 9600 baud rates,
and i've tried setting bluetooth rx , tx pins input/output , output/input.
the arduino receiving serial data pc can't send android (i can see because of flashled() method). android can't send data @ arduino.
however both connected because green light on modem turns on , goes off , red led flashes when close connection.
the senddata() method doesn't throw exception because otherwise showmessage("send failed"); appear.
any appreciated!
code take from: http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/
code: [select]
#include <softwareserial.h>
int bluetoothtx = 45;
int bluetoothrx = 47;you've got 4 f*ing hardware serial ports. use 1 of them!
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Android + Arduino Bluetooth Connection Problem
arduino
Comments
Post a Comment