Android to Arduino Data Send via Bluetooth Serial – Part 3 — March 16, 2014

Android to Arduino Data Send via Bluetooth Serial – Part 3

Now to make our app actually do something useful with out BT enabled Arduino. Before we make the new activity a few changes must be made to the Device List activity to support the new activity we are about to make.

First, an intent EXTRA is needed to take the chosen device’s MAC address to the next activity. Just put this with all the the other declarations at the top of the class

//An EXTRA to take the device MAC to the next activity
public static String EXTRA_DEVICE_ADDRESS;

An onClick listener will then be added for the list view so that a device can be selected from the list. Inside the method triggered by the list selection the BT device MAC address will be parsed from the data in the listview and a new Intent will be instantiated to call the ArduinoMain activity. I put this at the bottom of my DeviceListActivity class.

 private OnItemClickListener mDeviceClickListener = new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3)
        {
               textConnectionStatus.setText("Connecting...");
            // Get the device MAC address, which is the last 17 chars in the View
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);

            // Make an intent to start next activity while taking an extra which is the MAC address.
            Intent i = new Intent(DeviceListActivity.this, ArduinoMain.class);
            i.putExtra(EXTRA_DEVICE_ADDRESS, address);
                                             startActivity(i);
        }
    };

Lastly we need to attach the new onClick listener to the list view so the Listview set up in onCreate should look like the below. (add the bottom line to your code, nothing else has changed)

  // Find and set up the ListView for paired devices
      ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
      pairedListView.setAdapter(mPairedDevicesArrayAdapter);
      pairedListView.setOnItemClickListener(mDeviceClickListener);

That is Device List activity done so on to the new activity. But just in case you are unsure here is the finished code.

package com.example.simplebluetooth;
 
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
public class DeviceListActivity extends Activity {
 
    // textview for connection status
    TextView textConnectionStatus;
    ListView pairedListView;
    
    //An EXTRA to take the device MAC to the next activity
    public static String EXTRA_DEVICE_ADDRESS;
 
    // Member fields
    private BluetoothAdapter mBtAdapter;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        
        textConnectionStatus = (TextView) findViewById(R.id.connecting);
        textConnectionStatus.setTextSize(40);
        
        // Initialize array adapter for paired devices
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
        
        // Find and set up the ListView for paired devices
        pairedListView = (ListView) findViewById(R.id.paired_devices);
        pairedListView.setOnItemClickListener(mDeviceClickListener);
        pairedListView.setAdapter(mPairedDevicesArrayAdapter);
        
    }
   
    @Override
    public void onResume()
    {
      super.onResume();
      //It is best to check BT status at onResume in case something has changed while app was paused etc
      checkBTState();
      
      mPairedDevicesArrayAdapter.clear();// clears the array so items aren't duplicated when resuming from onPause

      textConnectionStatus.setText(" "); //makes the textview blank
      
      // Get the local Bluetooth adapter
      mBtAdapter = BluetoothAdapter.getDefaultAdapter();
 
      // Get a set of currently paired devices and append to pairedDevices list
      Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
     
      // Add previously paired devices to the array
      if (pairedDevices.size() > 0) {
          findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);//make title viewable
          for (BluetoothDevice device : pairedDevices) {
              mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
          }
      } else {
          mPairedDevicesArrayAdapter.add("no devices paired");
      }
  }
 
    //method to check if the device has Bluetooth and if it is on.
    //Prompts the user to turn it on if it is off
    private void checkBTState()
    {
        // Check device has Bluetooth and that it is turned on
                mBtAdapter=BluetoothAdapter.getDefaultAdapter(); // CHECK THIS OUT THAT IT WORKS!!!
        if(mBtAdapter==null) {
               Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
               finish();
        } else {
          if (!mBtAdapter.isEnabled()) {
            //Prompt user to turn on Bluetooth
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
            }
          }
        }
    
    // Set up on-click listener for the listview
    private OnItemClickListener mDeviceClickListener = new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3)
        {
               textConnectionStatus.setText("Connecting...");
            // Get the device MAC address, which is the last 17 chars in the View
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);
 
            // Make an intent to start next activity while taking an extra which is the MAC address.
            Intent i = new Intent(DeviceListActivity.this, ArduinoMain.class);
            i.putExtra(EXTRA_DEVICE_ADDRESS, address);
                                             startActivity(i);
        }
    };
 
}
 

Create a new, blank Android activity in eclipse and name it ArduinoMain. All other things remain default. Open the layout file for the activity (this should be acticity_arduino_main). All we really need at first is two buttons for functions 1 & 2, and an editText field to enter text we want to send to the LCD screen. Add these features to the xml file so it looks like the one below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ArduinoMain" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_weight="2"
        android:ems="10"
        android:hint="Enter some text"
        android:imeOptions="actionSend"
        android:singleLine="true" />

    <Button
        android:id="@+id/functionOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="104dp"
        android:text="Function 1" />

    <Button
        android:id="@+id/functionTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/functionOne"
        android:layout_below="@+id/functionOne"
        android:layout_marginTop="67dp"
        android:text="Function 2" />

</RelativeLayout>

If you have a look at the editText attributes you will see I have opted to include a send key on the soft keyboard. This way we don’t need to waste a button on sending our text! It will all look like the below when finished. Note that I have optimised this for a Nexus 7.
Screenshot_2014-03-18-21-23-52

Now on to the Arduino Main activity java file. Open it up (src/com.example…) and get ready to get coding. To set up a Bluetooth connection we need to do a few things:

1. Initialise the buttons and add onClick listeners

2. Set a pointer to the BT device using its MAC address

3. Open a BT socket for communication with the BT device.

4. Create a data outstream so we can send data to the BT device.

5. Create a method to send data down the outstream.

6. Tidy up the process with proper use of onResume, onPause so that the BT connection is not wasting resources.

Before looking at the code below I would strongly recommend looking at the Android Developer Bluetooth documentation. It gives clear instruction on how communication is made. Its very helpful, they are Google after all. But without further delay lets write this activity!

Below is the code for the ArduinoMain activity. As usual I have commented in the code to show what is going on but send me any questions if it isn’t clear.

package com.example.simplebluetooth;

import java.io.IOException;

import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ArduinoMain extends Activity {

  //Declare buttons & editText
  Button functionOne, functionTwo;

  private EditText editText;

  //Memeber Fields
  private BluetoothAdapter btAdapter = null;
  private BluetoothSocket btSocket = null;
  private OutputStream outStream = null;

  // UUID service - This is the type of Bluetooth device that the BT module is
  // It is very likely yours will be the same, if not google UUID for your manufacturer
  private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  // MAC-address of Bluetooth module
  public String newAddress = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_arduino_main);

    addKeyListener();

    //Initialising buttons in the view
    //mDetect = (Button) findViewById(R.id.mDetect);
    functionOne = (Button) findViewById(R.id.functionOne);
    functionTwo = (Button) findViewById(R.id.functionTwo);

    //getting the bluetooth adapter value and calling checkBTstate function
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    checkBTState();

    /**************************************************************************************************************************8
     *  Buttons are set up with onclick listeners so when pressed a method is called
     *  In this case send data is called with a value and a toast is made
     *  to give visual feedback of the selection made
     */

    functionOne.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          sendData("1");
          Toast.makeText(getBaseContext(), "Function 1", Toast.LENGTH_SHORT).show();
        }
      });

    functionTwo.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
          sendData("2");
          Toast.makeText(getBaseContext(), "Function 2", Toast.LENGTH_SHORT).show();
        }
      });
  }

  @Override
  public void onResume() {
    super.onResume();
    // connection methods are best here in case program goes into the background etc

    //Get MAC address from DeviceListActivity
    Intent intent = getIntent();
    newAddress = intent.getStringExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS);

    // Set up a pointer to the remote device using its address.
    BluetoothDevice device = btAdapter.getRemoteDevice(newAddress);

    //Attempt to create a bluetooth socket for comms
    try {
    	btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e1) {
    	Toast.makeText(getBaseContext(), "ERROR - Could not create Bluetooth socket", Toast.LENGTH_SHORT).show();
    }

    // Establish the connection.
    try {
      btSocket.connect();
    } catch (IOException e) {
      try {
        btSocket.close();        //If IO exception occurs attempt to close socket
      } catch (IOException e2) {
    	  Toast.makeText(getBaseContext(), "ERROR - Could not close Bluetooth socket", Toast.LENGTH_SHORT).show();
      }
    }

    // Create a data stream so we can talk to the device
    try {
      outStream = btSocket.getOutputStream();
    } catch (IOException e) {
    	Toast.makeText(getBaseContext(), "ERROR - Could not create bluetooth outstream", Toast.LENGTH_SHORT).show();
    }
    //When activity is resumed, attempt to send a piece of junk data ('x') so that it will fail if not connected
    // i.e don't wait for a user to press button to recognise connection failure
    sendData("x");
  }

  @Override
  public void onPause() {
    super.onPause();
    //Pausing can be the end of an app if the device kills it or the user doesn't open it again
    //close all connections so resources are not wasted

    //Close BT socket to device
    try     {
      btSocket.close();
    } catch (IOException e2) {
    	Toast.makeText(getBaseContext(), "ERROR - Failed to close Bluetooth socket", Toast.LENGTH_SHORT).show();
    }
  }
  //takes the UUID and creates a comms socket
  private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {

      return  device.createRfcommSocketToServiceRecord(MY_UUID);
  }

  //same as in device list activity
  private void checkBTState() {
    // Check device has Bluetooth and that it is turned on
    if(btAdapter==null) {
    	Toast.makeText(getBaseContext(), "ERROR - Device does not support bluetooth", Toast.LENGTH_SHORT).show();
        finish();
    } else {
      if (btAdapter.isEnabled()) {
      } else {
        //Prompt user to turn on Bluetooth
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
      }
    }
  }

  // Method to send data
  private void sendData(String message) {
    byte[] msgBuffer = message.getBytes();

    try {
    //attempt to place data on the outstream to the BT device
      outStream.write(msgBuffer);
    } catch (IOException e) {
       //if the sending fails this is most likely because device is no longer there
       Toast.makeText(getBaseContext(), "ERROR - Device not found", Toast.LENGTH_SHORT).show();
       finish();
    }
  }
  public void addKeyListener() {

		// get edittext component
		editText = (EditText) findViewById(R.id.editText1);

		// add a keylistener to keep track user input
		editText.setOnKeyListener(new OnKeyListener() {
		public boolean onKey(View v, int keyCode, KeyEvent event) {

			// if keydown and send is pressed implement the sendData method
			if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
				//I have put the * in automatically so it is no longer needed when entering text
				sendData('*' + editText.getText().toString());
				Toast.makeText(getBaseContext(), "Sending text", Toast.LENGTH_SHORT).show();

				return true;
			}

			return false;
		}
	 });
	}

}

Now you have all that completed lets give it a go! Turn on your Arduino with the the BT module connected and code loaded up. Plug in your Android device to your PC/mac and run the project. Select your BT serial device from the paired list in the app and a new activity should open with two buttons and an editTextbox. Give it a go and send some text and it should pop up on the LCD screen. If so, give the buttons a press and make sure they trigger functions one and two.

There are obviously plenty of things that can be done with this type of functionality (remotes etc) so get adding buttons and extra functions to the Arduino. In The next article we will switch things up a bit though and add receiving comms to the app, which is a bit more fun (tricky).

Comment if there are any errors or if some bits need expanding upon, as when this was written I was the only audience.

Android to Arduino Data Send via Bluetooth Serial – Part 2 — March 15, 2014

Android to Arduino Data Send via Bluetooth Serial – Part 2

Now we are going to load up Eclipse and make a simple app to connect to our Bluetooth enabled Arduino. The first stage will be to make an activity to select an already paired BT device. Once working we will make another activity that will do something with that selected device in part 3.

Set up a new project named “simple bluetooth” with a minimum api of 8 and target of 18. Let eclipse make a main activity and name it “Device List”. Do not add a navigation type. Hit finish and let it all load up.

I have structured this so that we will get the manifest and layout files out of the way first and then tackle the activity programming.

Open up the file explorer for your new project and open the manifest file. Add Bluetooth permissions to your manifest under the <uses-sdk> declaration. I won’t post whole files of code for some things as it takes up a lot of room. There is a downloadable file of the full project at the end of the final part if you wish to see complete xml files etc.

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

We don’t need the ADMIN permission for this app but put it in anyway in case you get adventurous later. The times I have forgotten this step is unforgivable. Manifest done.

In the res/layout directory find your activity_device_list layout file. Delete the hello world stuff and put in a listView and 3 textViews. The listView will show our paired devices and the textViews are there to show instruction and information useful to the user.<code snippet>.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView android:id="@+id/title_paired_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Select btSerial device from paired devices:"
        android:visibility="gone"
        android:background="#666"
        android:textColor="#fff"
        android:paddingLeft="5dp"
    />
    <ListView android:id="@+id/paired_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stackFromBottom="false"
        android:layout_weight="1"
    />

    <TextView
        android:id="@+id/connecting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/infoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="If no devices are listed please pair your device in Android settings"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

    </LinearLayout>

</LinearLayout>

You will notice some yellow warnings underneath where I have used text strings. Usually I would suggest using the strings.xml in the values folder to keep all text strings and then reference to them via r.id but as this is a simple example I didn’t want to deviate from the main point.

That paragraph was quite the deviation. In the layout directory add another xml layout file called device_name (remember to have.xml end the filename). This will be used for the actual device names populating the list.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:padding="5dp">

    </TextView>

Now that all the layouts are complete we can move on to programming the Device List activity. This activity is based on the Google Chat example but has been slimmed down and simplified so there is a little less to learn. Functionality wise it will generate a list of the paired devices on an Android device. From there we will add the ability to pick a paired device and take its details through to a new activity, where we will send data to the Arduino.

Below is the code from the Device List activity. I have tried to comment as much as I can to give answers on what each bit of code is doing, rather than explain it all in one big paragraph here. I won’t go in to every detail, instead I will focus on the bits exclusive to the functionality of this app. The main parts to focus on are getting the bluetooth adapter for the device and setting up the array adapter and the list view. Paired devices are then retrieved from the Bluetooth adapter and added to the array adapter, which in turn are displayed using the list view.

package com.example.simplebluetooth;
 
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
public class DeviceListActivity extends Activity {
 
    // textview for connection status
    TextView textConnectionStatus;
    ListView pairedListView;
 
    // Member fields
    private BluetoothAdapter mBtAdapter;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        
        textConnectionStatus = (TextView) findViewById(R.id.connecting);
        textConnectionStatus.setTextSize(40);
        
        // Initialize array adapter for paired devices
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
        
        // Find and set up the ListView for paired devices
        pairedListView = (ListView) findViewById(R.id.paired_devices);
        pairedListView.setAdapter(mPairedDevicesArrayAdapter);
        
    }
   
    @Override
    public void onResume()
    {
      super.onResume();
      //It is best to check BT status at onResume in case something has changed while app was paused etc
      checkBTState();
      
      mPairedDevicesArrayAdapter.clear();// clears the array so items aren't duplicated when resuming from onPause

      textConnectionStatus.setText(" "); //makes the textview blank
      
      // Get the local Bluetooth adapter
      mBtAdapter = BluetoothAdapter.getDefaultAdapter();
 
      // Get a set of currently paired devices and append to pairedDevices list
      Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
     
      // Add previously paired devices to the array
      if (pairedDevices.size() > 0) {
          findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);//make title viewable
          for (BluetoothDevice device : pairedDevices) {
              mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
          }
      } else {
          mPairedDevicesArrayAdapter.add("no devices paired");
      }
  }
 
    //method to check if the device has Bluetooth and if it is on.
    //Prompts the user to turn it on if it is off
    private void checkBTState()
    {
        // Check device has Bluetooth and that it is turned on
                mBtAdapter=BluetoothAdapter.getDefaultAdapter(); // CHECK THIS OUT THAT IT WORKS!!!
        if(mBtAdapter==null) {
               Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
               finish();
        } else {
          if (!mBtAdapter.isEnabled()) {
            //Prompt user to turn on Bluetooth
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
            }
          }
        }
}

So hopefully nothing new here but in case a little learning/refresher is needed, I would recommend having a look at this post on Lists/arrayAdapter and the google documentation for setting up bluetooth. Both are explained in much more detail that I could ever give!

Now run the App on your device. Make sure your BT module has already been paired in settings and that it is on. I would leave BT off to check that the app correctly prompts you to turn it on. Once BT is enable and the app starts up you should see something similar to the below. You will see I changed the name of my BT module to ‘btserial’ for this tutorial.Screenshot_2014-03-18-20-47-57

If it works be proud, half of the work is done. If not, check your code against my version[adding this soon]. As useful as this is it doesn’t currently do anything with our Arduino, which isn’t ideal. So in part 3 we will create a new activity that will talk to our BT enabled Arduino. Have a break and look at a cat or something, then move on to part 3.

1016colonel_meow

Android to Arduino Data Send via Bluetooth Serial – Part 1 — March 11, 2014

Android to Arduino Data Send via Bluetooth Serial – Part 1

This has been done to death but most posts/blogs seem to not show a few steps or give little explanation in to what is happening.This may help anyone beginning such things.

The intention is to connect an LCD display and Bluetooth module to an Arduino so that it can receive and display information using the LCD and inbuilt LED. I will then show you how to make a simple app to control these features. By doing this you should be able to make alterations and create your own hardware and app (most likely better than mine). I am no expert but I have finished this project and it all works. By the end of of it you should have something that looks like this:IMG_0029

This post is pretty simple but I would recommend making sure you are ok with making a very basic Android app and serial comms on the Arduino before continuing. This website looked good to me for the serial stuff.

You will need an Arduino, Bluetooth serial module, some jumper wires, a PC/Mac with Eclipse and an Android device with bluetooth connectivity ( I haven’t seen one without yet!).

Lets get the simplest thing out of the way first. Create a circuit like the one below. I would check your Bluetooth and LCD pinouts though, as these vary by manufacturer. Pay attention to the fact we are using the default Tx and Rx pins for our serial comms. If you try to program your Arduino with the BT module plugged in to these it will most likely fail. Either take the Tx and Rx out when programming or use software serial on two other I/O pins for the Bluetooth module.

Screen Shot 2014-03-18 at 20.21.07

Test the LCD by using one of the examples in the Arduino software – it will be annoying to think the whole thing failed due to a poorly connected LCD!

To test the BT module (BT = bluetooth, tired of typing it!) just use search on a BT enabled device and find your BT module (mine was HC-06 by default with 1234 as its pairing password). If you pair with the device the flashing LED on the BT module will go solid showing you have a connection. This probably won’t work on iPhones though as they don’t support the BT serial profile.

Below is a basic Arduino sketch that will read the serial transmission from the serial Tx pin (soon to be the Bluetooth tx pin). If the incoming serial transmission is an ASCII ‘1’ the LCD will display ‘function 1’ and the inbuilt Arduino LED will turn on (pin 13 = high). If an ASCII ‘2’ is received the LCD will display ‘function 2’ and the LED will turn off (pin 13 = low). If a ‘*’ is received any following text will be displayed on the LCD. So to write hello you would send ‘*hello’. The default serial Baud rate of my BT module was 9600 but check yours and adjust Serial.begin appropriately.

// include the library code:
#include

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

char inbyte;    //Setting a char variable as inbyte
int LED = 13;   //LED on pin 13

void setup(){
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
  digitalWrite(LED, LOW);
  // initialize the LED pin as an output
  pinMode(LED, OUTPUT);

}

void loop()
{
  // When a character arrives on the serial port this will be true
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);

    //store the first character in var inbyte
    inbyte = Serial.read();
    if( inbyte == '1' ) function1(); //if the byte is an ASCII 0 execute function 1
    if( inbyte == '2' ) function2(); //if the byte is an ASCII 9 execute function 2

    //if it is * then we know to expect text input from Android
    if (inbyte == '*')
    {
      //clear lcd screen
      lcd.clear();
      // read all the available characters
      while (Serial.available() > 0)
      {
        // display each character to the LCD
        lcd.write(Serial.read());
      }
    }
  }
}

void function1()
{
  //Enter what you want the command to initiate (e.g LED, Servo etc)
  digitalWrite(LED, HIGH);
  lcd.clear();
  lcd.write("Function 1");
}

void function2()
{
  //Enter what you want the command to initiate (e.g LED, Servo etc)
  digitalWrite(LED, LOW);
  lcd.clear();
  lcd.write("Function 2");
}

By the way you don’t have to use the ‘*’ method for text it is just a quick thing I implemented to distinguish between commands and text.

To test the full Arduino and BT module connectivity you will need to install Amarino or similar on to your Android device. Make sure your BT module is paired with the Android device and open the Amarino program. Select ‘add BT device’ at the bottom, chose your BT module, press connect and then press the monitor button. In monitor, type in a 1 in the text field and press send. The LED on the Arduino should come on and the screen should read ‘function 1’. Send a 2 and it should go off and the screen should read ‘ function 2’. Type ‘*hello’ and hello should be on the LCD screen.

If all has gone to plan you should now have something like this (perhaps less messy):

IMG_0028

Like my carpet?

If the LED did not come on, first disconnect the BT module and use the serial monitor in Arduino, while the program is running send a 1 or a 2 to check your sketch works ok. If that is ok check that your BT module is behaving like it should – most have an online datasheet showing behaviours and commands for troubleshooting etc. This is also where you will find information on how to change the device name, PIN and Baud etc. This post was EXTREMELY useful.

Now see part 2 to move on to the fun part, ANDROID