There are not very much recources that explain how to connect an Arduino BT to a mobile phone using a Java ME application.
I am not an advanced programmer so please take that into account. I simply want to send data back and forth from the arduino to the phone and the other way around. On the arduino side I have this code:
/*
simple LED test
*/
char val; // variable to receive data from the serial port
int ledpin = 12; // LED connected to pin 2 (on-board LED)
int checker = 0;
void setup()
{
pinMode(ledpin = 13, OUTPUT); // pin 13 (on-board LED) as OUTPUT
Serial.begin(115200); // start serial communication at 115200bps
}
void loop() {
if(checker == 0){
for (int v=0; v <= 5 ;v++){
digitalWrite(ledpin = 12, HIGH); // turn ON pin 13 on
delay(100); // waits for a second
digitalWrite(ledpin = 12, LOW); // turn ON pin 13 on
delay(100); // waits for a second
}
checker = 1;
}
if( Serial.available() ) // if data is available to read
{;}
val = Serial.read(); // read it and store it in 'val'
if( val == '0' ) // if '0' was received led 13 is switched off
{
digitalWrite(ledpin, LOW); // turn Off pin 13 off
delay(1000); // waits for a second
Serial.println("12 off");
}
if( val == '1' ) // if '1' was received led 13 on
{
digitalWrite(ledpin = 12, HIGH); // turn ON pin 13 on
delay(1000); // waits for a second
Serial.println("12 on");
}
}
Short explaination: The LED quickly blinks on program startup to see if the sketch is uploaded succesfully. Then it reads from the serial port and stores the value in a variable. According to this variable the led switches on and off. I used PuTTy to send a 1 or 0 to the board over bluetooth and the led turned on and off.
Let's switch to the phone side:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class BluetoothGPSMidlet extends MIDlet implements CommandListener {
private Display mDisplay;
private Command exitCmd;
private Command connectCmd = new Command("Connect", Command.ITEM, 1);
private Form mForm;
StreamConnection conn = null;
InputStream is = null;
OutputStream os = null;
String message = null;
public void startApp() {
if ( mForm == null ) {
mForm = new Form( "ArduinoBT connect!" );
exitCmd = new Command( "Exit", Command.EXIT, 0 );
mForm.addCommand( exitCmd );
mForm.addCommand( connectCmd );
mForm.setCommandListener( this );
mDisplay = Display.getDisplay( this );
}
mDisplay.setCurrent( mForm );
}
public void pauseApp() { }
public void destroyApp( boolean unconditional ) { }
public void commandAction( Command c, Displayable s ) {
if ( c == exitCmd ) {
destroyApp( true );
notifyDestroyed();
} else {
// -------------------------------------------------
try {
conn = (StreamConnection) Connector.open ("btspp://000780908C11:1;authenticate=false;master=false;encrypt=false", Connector.READ);
os = conn.openOutputStream();
os.write('1');
} catch (IOException io) {
// handle exception
mForm.append(new StringItem(null, "error"));
} finally {
if (conn != null) {
try {
conn.close();
} catch (IOException ignored) {}
}
if (is != null) {
try {
is.close();
} catch (IOException ignored) {}
}
}
// --------------------------------------------------
}
}
}
As I understand it well, the beginning of the code imports the classes, and creates the variables. Then a form is created where the choice is between exit and connect. When connect is pressed a connection is tried to be make to the adress of my Arduino BT.
If I upload the code to my mobile phone (Samsung SGH 480 (Touchwiz)) and press connect the error is thrown, where I would expect it to send a 1 to the arduino to turn the LED on.
I am not sure about the following part:
btspp://000780908C11:1;authen...
I found the "000780908C11" in the bluetooth properties of the Arduino BT. The 1 I am not sure about?
Another thing is, when I use PuTTy I have to set a baudrate that will be
used to send the data, otherwise it will not work. How does my phone handle this?
Can anybody help me making this work?
Thanks in advance!