App Inventor, Bluetooth and Arduino - Part 1




I was enthused by Pieter Bok’s post  involving an iPhone App connecting to an Arduino via WiFi:


I don’t have an iPhone, or a WiFi shield, but Bluetooth was mentioned in the thread,  so I Googled around for Android, Arduino and Bluetooth, finding several links which made use of an amazing, FREE resource from MIT called App Inventor, for developing Android Apps:


This link is to version 2 of App inventor. Several of the Arduino related links, were for App Inventor version 1, which is still available, although not for long, so you are advised not to start using it.


App Inventor 2 is entirely web-based, and includes the ability to connect to your target Android device via Wifi and run an emulation of your App in real time - the display on your device changes as you change the design in App Inventor! If you don’t even have an Android device, you can run an emulator on your PC and App Inventor will connect to it.


App Inventor 2 has two screens - Design and Blocks. In Design, you drag and drop components such as buttons, labels, text boxes, sliders, etc, on to a representation of your Android device screen and arrange them as you want them to appear.


In the Blocks screen, you drag and drop representations of code segments which relate the display components together and ultimately generate your app. Once you get into it, it’s actually very straightforward.


Initially, I got a little bogged down because I tried to follow a tutorial based on version 1 of App Inventor and I couldn’t get it to work. I thought it was problem with the code, but actually it was me! Because I was new to Bluetooth, I hadn’t realised that I had to pair the devices first! I’m learning fast!


Using this video tutorial  for version 2, to send data to the Arduino, I launched myself:


This Tutorial shows how to build an App which connects to an Arduino, via Bluetooth - I used a JY-MCU HC06 board. The Arduino sketch to receive commands from the App to turn an LED on and off is also supplied.


There seems to be some confusion about whether the JY-MCU HC06 Bluetooth board can accept a 5v Tx signal from the Arduino into its Rx pin. My JY-MCU board is clearly marked as expecting a 3.3v signal level, so I used a couple of resistors to make a simple voltage divider.


It was also said to be important that you disconnect the Bluetooth board from Arduino pins 0 and 1 when uploading a sketch. However, I forgot once and it didn’t  seem to make any difference!


You can have the IDE Serial Monitor open whilst the Bluetooth is working and see exactly what’s happening. In fact, you can test the sketch without the Bluetooth connected, by typing in the relevant characters on the keyboard and sending via the Serial Monitor.


In the Design screen, below, you can see the depiction of an Android device screen:


Design Screen.jpg


On the left of the screen is a list of the components which can be dragged and dropped onto the Android screen.

To the immediate right  of the Android screen is a list of the current screen components: a ListPicker, two Buttons in a Horizontal Arrangement and a “hidden” Label, hidden because it is initially blank. There is also a non-visible bluetooth client at the bottom of the screen.


To the far right of the Android screen are the properties of the currently selected component - in this case the Label, Label1 is selected.


If we click on the Blocks Tab and then click on the ListPicker1 component on the left hand side of the screen. A scrollable list of code blocks relevant to the ListPicker1 appears. Click  on “When ListPicker1 BeforePicking” and it will be placed onto the Blocks screen area.




We can now select further sub-Blocks to create the first full code Block, below..


This allows List Picker1 to display the available (already paired) Bluetooth devices: - This next code block has been corrected 18 January 2015

LED Control 2.jpg


The next Block allows ListPicker1 to select and connect to the desired Bluetooth device and to set the “hidden” Label, Label1 to read “CONNECTED”:




I’ve then set the default Text of Label1 to be “NOT CONNECTED” and the colour of Label1 to be Red in the Design Screen; clearly, Label1 is now no longer “hidden”.


I’ve also added a further component into the code Block, to set the Text colour of Label1 to be Green when the Bluetooth client is connected.


The next 2 Blocks send the text string “on” when Button1 is pressed and the text string “off” when Button2 is pressed.


Finally, I’ve  added a third Button in the Design screen with a Text value of “DISCONNECT”and a fifth Block of code which disconnects the Bluetooth client, sets Label1 text colour to Red and Label1 text to read “DISCONNECTED”





The Arduino sketch is  straightforward:


int ledPin = 13;

String readString;


void setup()



  pinMode(ledPin, OUTPUT);


void loop()


  while ( Serial.available() )

{    // While there is data in the buffer

    delay( 3 ); 

    char c =;

    readString += c;         // build the string - “on” or “off”


  if ( readString.length() >0 )


    Serial.println( readString );

    if ( readString == "on" )


      digitalWrite( ledPin, HIGH) ;


    if ( readString == "off" )


      digitalWrite( ledPin, LOW );








Obviously, the sketch could be simplified if the App merely sent, say ,“1” for ON and “0” for off, instead of “on” and “off”.