Android MIT App Inventor – Auto Connect To Bluetooth

In a previous post I showed how to connect an app inventor Android app to a Bluetooth module connected to an Arduino to control an LED. See Turning a LED on and off with an Arduino, a HC-06 and Android

A few people have asked how to make it so that the app auto-connect to the Arduino on start up and I thought I would offer my solution. This example adds to the previous guide.

The easiest way to add an auto connect would be to use a fixed address and put the connection function call in the Screen1.Initialize block. Since the Screen1.Initialize block runs automatically when the app is started, the app would try to make the connection every time it started.
This is quick and easy but it means the mac address for the BT module is hard coded in to the app and to change the address you would need to change the app and recompile. This makes it very difficult to use a different Bluetooth module.

A better way is to allow the user to connect to any BT module and then save the address for next time. Then, when the the app is run again it can use the saved address to auto-connect to the Bluetooth module. By saving the address every time a new connection is made the user can change Bluetooth modules with ease.

The App

The first time the app is run there is no saved address and the app waits for the user to make a connection.
– Click the BT button to bring up a list of paired devices.
– Select one of the paired devices, in my case the HC-06.
– If the connection is successful the BT button changes to “Connected”.
– The address of the connected BT device is displayed on screen.

The next time the app is run it will try to auto-connect to the saved device.
– While trying to connect the BT button will display “Connecting”.
– If the connection is successful the BT button will show “Connected”.

App Designer Screen


There are only a few screen elements to note:
A button to activate the connection process.
A label to show the saved address.
The LED button to turn an LED on or off.
A list to store the list of paired devices.

The non-visible components required are:
And of course the Bluetooth client.

The Blocks

Android MIT App Inventor - Auto Connect To Bluetooth_blocks_001

The Main Details

When the app starts, the Screen1.Initialize function is called. This retrieves the saved Bluetooth address from the TinyDB and copies in to the global variable “savedDeviceAddress”, it then starts the timer. If there isn’t a saved address the variable is set to “” and the timer is not started.

I use a timer to overcome a minor annoyance caused by how app inventor works. If the connection blocks are all placed in the Screen1.Initialize function, the connection process works but the screen is not updated correctly and the “Connecting” button text is not displayed.

When the timer fires, the Clock1.Timer function is called. It is here the app tries to connect to the saved Bluetooth address. If a connection is successful the BT_BUTTON text is changed to “Connected”. If the connection is not successful an error message is displayed. The function also checks that Bluetooth is turned on before trying to make a connection.

The BT_BUTTON allows for new connections, when it is clicked it activates the list picker and allows the user to select another device. When a new connection is made the mac address of the newly connected BT module is saved to the TinyDB.

The rest of the code is the same as the previous example. For further details see Turning a LED on and off with an Arduino, a HC-06 and Android



Download the app inventor aia file


31 thoughts on “Android MIT App Inventor – Auto Connect To Bluetooth

  1. Pingback: Turning a LED on and off with an Arduino, a HC-06 and Android | Martyn Currey

  2. Thank you for your help.

    When the connection to the phone is lost, not appear a warning message “lost connection” or the button not change “not coonected”, when I move back into range … must reconnect manually.
    Is possible checking every second, whether the device is in range or not? And auto connect when is in range.

    • At the moment I don’t believe this is possible with MITs app inventor. It may be possible if using Eclipse or Android Studio but I have only used app inventor and know nothing about JAVA.

      You can test for an active connection by sending a “are you still there” message. If the Arduino replies then everything is good. If the Arduino does not reply (you will also get a system error) then the connection has been lost and the connection can be closed. However, you would need to manually reconnect once the connection was closed.

      I will look into this further but I think it will be a long term project and not something I can do in a day or two. If I find anything on the net I will post links.

  3. Is possible that the arduino sent the data permanent to phone? like this:
    int LEDState;
    void loop()
    LEDState = digitalRead(LEDpin);
    Serial.println(LEDState); // with BTserial.print(LEDState) don’t work …

    I don’t know, how can I make, when the phone not receive “1” OR “0” appear the warning message, and the BT button have the function like at startup (connecting mode).

    Sorry for my poor English.

    • Hi Saua,

      it is possible but would need a fair amount of work to add to the above example.

      This example, and also the LED on/off example, are basic “send data to the Arduino only”. I don’t have anything to show at the moment. You can take a look at This partially does what you what but not exactly.

      I am working on an Arduino monitor app but this is going to be a while before I have anything I can post.

    • Assuming I understand correctly, you want to have the Arduino control the LED and then show the LED status in the Android app.

      This sketch is not really a good place to start if you want the Android app to display the LED status where the LED is controlled by the Arduino. The app was designed purely to send controls to the Arduino.

      To work the other way around you need to read the data from the bluetooth (you have done this but in the wrong place), if you receive a “1” set the background colour of the LED button to green and if a “0” set the background to red.

      To do this you need to use a timer (add a “when Clock1.Timer” block) not a button, and every time the timer fires check for new data, if you receive a “1” set the background colour of the LED button to green (call buttonON) and if a “0” set the background to red (call buttonOFF). Set the timer interval to something like 100ms.
      The timer should not be started until after you have made the bluetooth connection otherwise you will get constant errors.

      Since you are receiving data not sending, you can remove the block.

    • You need to have a button switch and an LED on the Arduino. When the switch is closed toggle the LED status and at the same time send a control code to the Android app. For example, “1′ for on and “0” for off.

      The Android app needs to receive the control code and then act accordingly. In the app, don’t change the existing functions, instead, add new ones. You will need to

      – add a timer. This is used to check for new data received over bluetooth. Do not start the timer until you have a bluetooth connection.

      – add a “when Clock1.Timer” block. Use this to check to see if there is new data.

      – if you have new data,
      – if it is a “1′ change the LED button to “ON”
      – if it is a “0” change the LED button to “OFF”

      Unfortunately I don’t have examples but the project looks interesting and I sure you will get it working.

  4. Hello,

    I am trying to monitor 4 leds.
    The led’s are also connected to a switch. Therefore, when the switch is pushed, then 5V are passing through and the led is on. The android device should also display the change. Any leads how to transmit the data from one end to the other?

    Thanks in advance

    • You need to monitor the pins the LEDs are connected to. When one of these goes HIGH (and the LED turns on) send a “LEDx is ON” command to the android app. When the pin returns LOW send a “LEDx is off” command. x is the LED number.

      Have a look at the earlier ai examples. These should help with how to send data.

  5. dear sir,

    i have successfully auto connect the bluetooth,but is it possible with app inventor that when we open the app then if my bluetooth is not ON then one message box pop up and ask for turning on bluetooth.??
    if this possible ,please guide.

  6. Do you by chance have any idea how do I notify the user whenever there is some data coming on the phones Bluetooth. Like I’ve several sensors in the house and I’ve to report about their triggering to the user, on the app. But then I do not want the user to continuously stare the app. Only when there is some trigger, he should open the app, and check. Could you help me find solution to this.

  7. Super well done explanation.

    I have been struggling with this same concept for a long time in ai2.

    I am running a Climate control in a old honda.

    My particular problem is that the arduino/hc06 shuts off with the key switch. The tablet remains on its own battery. Next engine start requires re connect.

    Your example will solve most of that problem!

    I am already reporting the car’s voltage from the arduino/hc06 to the tablet. The field goes blank when the key is off.

    I was thinking that ai2 could watch that value, and when it goes blank AI2 could begin attempting a reconnect.

    Does that sound readonable?

    I sure do appreciate the effort you have put into this!


    • Since in AI you cannot tell when the connection is broken, this is basically how you check. Send data at a specific time period and if you do not receive the data it you know there is a problem.

      Remember that AI thinks it is still connected so you will need to disconnect first.

      I don’t have examples to post but it should not be too difficult to add a function that detects when there is not data and then disconnects and then reconnects to the Bluetooth module.


      • got it!

        1.The trick was to send data all the time from the arduino.
        2. in AI, test the incoming data field to see if it is empty.
        3.DISCONNECT the BT, even though it is already disconnected.
        4. reconnect to same connection.
        5. I error trapped the screen so that the nuisance errors would not take over the display.

        Thanks Marty


        • Hi Todd, can you please explain in more detail how your are able to auto reconnect to bluetooth? I am having difficulties in doing that.

          I would really appreciate any feedback. Thanks in advance.


  8. Hi,

    your tutorial is super helpful and informative. I’m currently trying to auto reconnect bluetooth after the HC-06 is turned off then on again. Do you have any tips that could be useful?

    Thanks in advance!


    • If you know that the connection is down, the easiest way is to manually disconnect and then reconnect.

      The issue with broken connections is that AI does not know when the connection is broken unless it tries to send data. If the connection is broken an error message is generated when it tries to send. You can use this error message in the app.

      My solution is to have a timed dummy send data function.
      If data has not been sent within the last second I send a dummy tag, if I get an error I know the connection is down. Using this method also keeps the connection active.

  9. Hello,

    Your tutorial was really helpful! However, I do have a question.

    I was wondering how to auto-connect to the same BT module every time I run the app? Do I just hard code the address into the purple call Connect block and if so, how do I obtain the address for the BT module I want to use?

    Also would the address of the BT module change for any reason other than using a different BT module?



Leave a Reply

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

six + = 14

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>