First Android App controls Arduino LEDs using bluetooth

by Oscar

A fairly simple one. I will only describe how the seek bar works, and part of how commands are recognized by arduino (arduino code). I will do another one about bluetooth another day.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

Full Arduino Code:

[sourcecode language=”cpp”]
byte packet[2];
int pIndex;int rPin = 9;
int yPin = 10;
int gPin = 11;byte rValue = 0;
byte yValue = 0;
byte gValue = 0;

void setup() {
Serial.begin(9600);
}

void loop() {

// see if there’s incoming serial data:
if (Serial.available() > 0) {
packet[pIndex++] = Serial.read();
}

if(pIndex >= 2){

switch(packet[0]){
case ‘r’:
rValue = packet[1];
break;
case ‘y’:
yValue = packet[1];
break;
case ‘g’:
gValue = packet[1];
break;
default:
;
}

analogWrite(rPin, rValue); // 0 – 255
analogWrite(yPin, yValue);
analogWrite(gPin, gValue);

Serial.print(packet[0],);
Serial.print(" ");
Serial.println(packet[1]);

pIndex = 0;

}

delay(100);
}
[/sourcecode]

Android Code (java):
I am not going to post all the source code because i have a lot of unneeded controls in the code, and I have no time to tidy it up, so I will only post the relevant snippets.
if you have any error, it’s probably want you to import the library :)
Create SeekBars, and it’s value (using byte because of the bluetooth nature)

[sourcecode language=”java”]
SeekBar sr;
SeekBar sy;
SeekBar sg;
byte srValue;
byte syValue;
byte sgValue;
[/sourcecode]

Set seekbar maximum value to 255, as the max of a byte is 255.

[sourcecode language=”java”]
sr.setMax(255);
sy.setMax(255);
sg.setMax(255);
[/sourcecode]

And lastly, setup the event listener for the seekbar, we put the progress value in memory, and only send it to target device when we finish dragging it to reduce data traffic, i will only show one of the seekbar listener.

[sourcecode language=”java”]
sr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv1.setText("Red " + progress);
srValue = (byte) progress;
}

public void onStopTrackingTouch(SeekBar seekBar) {
String temp = "r";
byte bytes[] = temp.getBytes();

try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}

try {
outputStream.write(srValue);
} catch (IOException e) {
e.printStackTrace();
}
}

});
[/sourcecode]

I don’t have the full source code anymore, but I have the sourcecode for the APP i used to control my hexapod robot, go to that post and scroll down to the bottom.

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

8 comments

narayan 27th November 2014 - 11:45 am

complee code plz i will modify it for other purpose

Reply
ng yu jie 24th April 2014 - 6:07 am

might i have a preview of all of your code that you had for android base ?

Reply
Oscar 24th April 2014 - 9:48 am

i don’t have it anymore, it’s been a long time.

Reply
David 4th March 2014 - 9:56 pm

Hi Oscar,

I am working on developing an android app to communicate and read data (sensor readings) from an Arduino Uno board via a bluetooth module.

How can this be done?

Thank you.

Reply
Oscar 5th March 2014 - 11:32 am

yes it can be done.

Reply
Vick 25th July 2013 - 2:39 pm

Hey, can I have contact with you ? I want to learn this so much!!! Hope you can help!

Thanks

Reply
Oscar 25th July 2013 - 3:45 pm

Hi, sure, just drop me a comment if you have any question.

Reply
Smashed 23rd July 2013 - 9:25 pm

Wait what is outputstream. Thats where i am stuck at!!

Reply