First Android App controls Arduino LEDs using bluetooth

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.

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() {

void loop() {

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

if(pIndex >= 2){

case ‘r’:
rValue = packet[1];
case ‘y’:
yValue = packet[1];
case ‘g’:
gValue = packet[1];

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

Serial.print(" ");

pIndex = 0;



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;

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

[sourcecode language=”java”]

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 {
} catch (IOException e) {

try {
} catch (IOException e) {


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.

8 thoughts on “First Android App controls Arduino LEDs using bluetooth

  1. David

    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.


Leave a Reply

Your email address will not be published. Required fields are marked *

For prompt technical support, please use our forum I check blog comments weekly.