Communication between Android and Arduino with Bluetooth(1)

BT & a
We all know Android is very popular now, especially ADK (Android Open Accessory Development Kit), which allows external 0pen source hardware to connect with Android system by USB and interact with an Android-powered device in a special “accessory” mode.
We keep working on it for weeks, and try to assemble some bata prototype and make a library for the communication between Android and Arduino by bluetooth and Freaduino for ADK.
With the Google providing source files, we are able to provide this board from now on. We produce a batch, you can purchase them here.We will provide the Bata version as soon as possible.
To make the communication between Android and Arduino easy, we would like show you a new way that android interact with Arduino and other similar boards. Bluetooth for example.
Step one: Make a APP to Android which could communicate with other devices by bluetooth.
Step two: Android APP connect to Arduino by Bluetooth Bee.
For step one(Part1), we have just made a little APP for Android, achieve simple bluetooth connection with Android.

This APP allows Android connect to each other by bluetooth, so you need at least two Android devices for this demo. Here we used two Android phones which are SAMSUNG I7680 and Lenovo O3. Then, there should be a complete development environment like Eclipse on your PC. You can find many of them in the Internet.
I guess you all get ready, let’s see more details.
·
Bluetooth
The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications connect wirelessly to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
·
The Basics
All of the Bluetooth APIs are available in the Android bluetooth package. Here is a summary of the classes you will need to create as below.
  • BluetoothAdapter: Represents the local Bluetooth adapter (Bluetooth radio)
  • BluetoothDevice: Represents a remote Bluetooth device, query information such as its name, address, class, and bonding state.
  • BluetoothSocket: Represents the interface for a Bluetooth socket (similar to a TCP Socket).
  • BluetoothServerSocket: Represents an open server socket that listens for incoming requests (similar to a TCP ServerSocket).
  • BluetoothClass: Describes the general characteristics and capabilities of a Bluetooth device.
·
Bluetooth Permissions
In order to use Bluetooth features in your application, you need to declare at least one of two Bluetooth permissions: BLUETOOTH and BLUETOOTH_ADMIN.
Declare the Bluetooth permission(s) in your application’s AndroidManifest.xml as below.

<manifest ... >
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  ...
</manifest>
Oh, Is it a little boring in front of this? OK, let us see some code, really start from here.
In order to connect by bluetooth, there should be four steps as below:
  • Setting Up Bluetooth
  • Finding Device
  • Connecting Device
  • Managing Device(server/client)

·

Setting Up Bluetooth
We made the APP include six java files. For the Bata, we only need to use Activity01, DiscoveryActivity, ClientSocketActivity and ServerSocketActivity.
Before your application can communicate with Bluetooth, you should verify Bluetooth is supported on the device and make sure that it is enabled. If Bluetooth is not supported, you should gracefully disable any Bluetooth features. If Bluetooth is supported, but disabled, you are able to request the user enable Bluetooth without leaving your application.
So, at Activity01 we get five buttons, as below:
Select “Open BT” button make the device’s own Bluetooth adapter (the Bluetooth radio) working. There is one Bluetooth adapter for the entire system, and your application can interact with it when it is open.

private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter()
There are two ways to enable bluetooth:

    //Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    //startActivityForResult(enabler, REQUEST_ENABLE);

    //enable
    _bluetooth.enable();
The frist way, create a new Intent as “Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)”, a dialog box will appear and request user permission to enable Bluetooth. Select “Yes” and the system will enable Bluetooth and focus will return to your application once the process completes (or fails). “Yes” returns RESULT_OK and “No” returns RESULT_CANCELED.
The other way is force enable Bluetooth instead of dialog, we used this way here.
Next, you need to ensure that Bluetooth is enabled and allowed other devices could discover it. Add the below code, and a dialog box will appear also, you should click “Yes”.

    Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(enabler, REQUEST_DISCOVERABLE);
OK, Setting up device is completed. The full code as below:

ackage com.yarin.android.Examples_08_09;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class Activity01 extends Activity
{
  /* Get Default Adapter */
  private BluetoothAdapter  _bluetooth        = BluetoothAdapter.getDefaultAdapter();

  /* request BT enable */
  private static final int  REQUEST_ENABLE      = 0x1;
  /* request BT discover */
  private static final int  REQUEST_DISCOVERABLE  = 0x2;

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

  /* Enable BT */
  public void onEnableButtonClicked(View view)
  {
    //Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    //startActivityForResult(enabler, REQUEST_ENABLE);

    //enable
    _bluetooth.enable();

    Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(enabler, REQUEST_DISCOVERABLE);
  }

  /* Close BT */
  public void onDisableButtonClicked(View view)
  {
    _bluetooth.disable();
  }

  /* Start search */
  public void onStartDiscoveryButtonClicked(View view)
  {
    Intent enabler = new Intent(this, DiscoveryActivity.class);
    startActivity(enabler);
  }

  /* Client */
  public void onOpenClientSocketButtonClicked(View view)
  {

    Intent enabler = new Intent(this, ClientSocketActivity.class);
    startActivity(enabler);
  }

  /* Server */
  public void onOpenServerSocketButtonClicked(View view)
  {

    Intent enabler = new Intent(this, ServerSocketActivity.class);
    startActivity(enabler);
  }

}
·
Finding Device
With Discovery Activity.java file, We will find around device. The process is asynchronous and the method will immediately return with a boolean indicating whether discovery has started successfully. The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each found device to retrieve its Bluetooth name. Your application must register a BroadcastReceiver for the ACTION_FOND. Intent in order to receive information about each device discovered.

/* Register Receiver*/
    IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(_discoveryReceiver, discoveryFilter);
    IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(_foundReceiver, foundFilter);

/**
   * Receiver
   * When the discovery finished be called.
   */

  private BroadcastReceiver _foundReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
      /* get the search results */
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      /* add to list */
      _devices.add(device);
      /* show the devices list */
      showDevices();
    }
  };
  private BroadcastReceiver _discoveryReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent)
    {
      /* unRegister Receiver */
      Log.d("EF-BTBee", ">>unregisterReceiver");
      unregisterReceiver(_foundReceiver);
      unregisterReceiver(this);
      _discoveryFinished = true;
    }
  };

After discovery will return remote device’s MAC address, device name and class. Using this information, the device performing discovery can then choose to initiate a connection with the discovered device. The following figure is click the “Search Device” button.

·
Connecting Device and Managing Device
In order to create a connection between your application and two devices, you must implement the both server-side and client-side mechanisms, because one device must open a server socket and the other one must initiate the connection (use the server device’s MAC address to initiate a connection). So you must used a same UUID, I used one find from internet and you can get the UUID here: the UUID Generator.
One implementation technique is to automatically prepare each device as a server, so that each one has a server socket open and listening for connections. Then either device can initiate a connection with the other one as the client. Alternatively, one device can explicitly “host” the connection and open a server socket on demand and the other device can simply initiate the connection.
As Server

_serverSocket = _bluetooth.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM,
          UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
As Client

socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
It must select a server and a client, we has made two buttons which are “AS Client” and “AS Server”. Then one phone will be server and the other will be client.
Server
Frist, this connection used Socket way, so there are some usually functions such as connect, close, listen and so on. and there are others stream control such as getInputStream, getOutputStream …
select “AS Server” button, then you will see the following show.
The sample code:

/* Client devices*/
      final List<String> lines = new ArrayList<String>();
      _handler.post(new Runnable() {
        public void run() {
          lines.add("Rfcomm server started...");
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(
              ServerSocketActivity.this,
              android.R.layout.simple_list_item_1, lines);
          setListAdapter(adapter);
        }
      });
      /* accept client request */
      BluetoothSocket socket = _serverSocket.accept();
      Log.d("EF-BTBee", ">>Accept Client Request");

      /* Processing the request content*/
      if (socket != null) {
        InputStream inputStream = socket.getInputStream();
        int read = -1;
        final byte[] bytes = new byte[2048];
        for (; (read = inputStream.read(bytes)) > -1;) {
          final int count = read;
          _handler.post(new Runnable() {
            public void run() {
              StringBuilder b = new StringBuilder();
              for (int i = 0; i < count; ++i) {
                if (i > 0) {
                  b.append(' ');
                }
                String s = Integer.toHexString(bytes[i] & 0xFF);
                if (s.length() < 2) {

                  b.append('0');
                }
                b.append(s);
              }
              String s = b.toString();
              lines.add(s);
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                  ServerSocketActivity.this,
                  android.R.layout.simple_list_item_1, lines);
              setListAdapter(adapter);
            }
          });
        }

Client

Click the “AS Client” button, it will discover remote devices and show the devices list. It will call DiscoveryActivity class and show the devices list at last, then click one device to connect to server, send socket.connect() to server and connect to RFCOMM Channel. We send a buffer to server as below.


protected void connect(BluetoothDevice device) {
    //BluetoothSocket socket = null;
    try {
      //Create a Socket connection: need the server's UUID number of registered
      socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));

      socket.connect();
      Log.d("EF-BTBee", ">>Client connectted");

      InputStream inputStream = socket.getInputStream();
      OutputStream outputStream = socket.getOutputStream();
      outputStream.write(new byte[] { (byte) 0xa0, 0, 7, 16, 0, 4, 0 });

    } catch (IOException e) {
      Log.e("EF-BTBee", "", e);
    } finally {
      if (socket != null) {
        try {
          Log.d("EF-BTBee", ">>Client Close");
          socket.close();
          finish();
          return ;
        } catch (IOException e) {
          Log.e("EF-BTBee", "", e);
        }
      }
Once the Server received the data, it will show on the screen.
At last, of course is release resources, click “Stop Server” and return to main menu, click “Close BT” button.

/* Stop server */
  private void shutdownServer() {
    new Thread() {
      public void run() {
        _serverWorker.interrupt();
        if (_serverSocket != null) {
          try {
            /* close sever */
            _serverSocket.close();
          } catch (IOException e) {
            Log.e("EF-BTBee", "", e);
          }
          _serverSocket = null;
        }
      };
    }.start();
  }

/* Close BT */
  public void onDisableButtonClicked(View view)
  {
    _bluetooth.disable();
  }
OK, This is where the fun begins, you can connect the device and send data to each other. There are many things you can do, right?
Let us see this APP operational processes totally again:
Open BT (enable bluetooth and discoverable) -> AS Server(listen)/AS Clent(search devices and show devices list) -> Select Server from the devices list(client sends data and server receives) -> Show the data on the Server screen -> Stop Server -> Stop BT.
The demo is a Bata version, just a sample code for your reference. We will gradually enrich with it and you are appreciated to redistribute and/or modify it, and please share with us.
·
Download full code here:
·
Reference:
VN:F [1.9.13_1145]
Rating: 9.0/10 (23 votes cast)
VN:F [1.9.13_1145]
Rating: +7 (from 7 votes)
Communication between Android and Arduino with Bluetooth(1), 9.0 out of 10 based on 23 ratings

Related Posts

  • https://www.youtube.com/watch?v=GDOuni9lmkM emergency veterinarian Omaha

    The techhnique you employ is very similar to clipping a puppy’s nails.
    You can choose from- Netherlands dwarf, Lionhead, Dutch, Holland lop and mini lop, Miini
    Rex, Rex lop, Angora lop, New Zealand white and lot more.
    If your pet has a seizure he or she appear afraid or
    disoriented for pointlss first.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Haris

    Kindly ensure pairing the device explicitly from your Android phone’s settings. Usually the pair code is 1234
    The Hardware Tx,Rx pin are preferred if free on the Arduino

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Dexter

    i have error in these codes:
    /* Register Receiver*/
    IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(discoveryReceiver, discoveryFilter);
    IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(foundReceiver, foundFilter);

    for registerReceiver , it said “Return type for the method is missing” and “discoveryReceiver cannot be resolved to a type”

    and said “Syntax error, insert “}” to complete MethodBody” after
    registerReceiver(foundReceiver, foundFilter);

    what should i do?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • http://NA Nikhil

    Hello,
    I am new to Android SDK. When I import this project into the eclipse IDE I get the errors unable to resolve target ‘Google Inc.:Google API’s.
    Can anyon help
    Regards

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • http://wikipedia.org Android

    Just wish to say your article is as astonishing. The clearness in your post is just
    great and i can assume you are an expert on this subject.

    Fine with your permission let me to grab your feed to keep
    updated with forthcoming post. Thanks a million and please keep up the
    gratifying work.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Ome

    I am having this error::

    client_socket cannot be resolved or is not a field

    Can you help me put?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • dany

      if you have technical problem, please send email to support@elecfreaks.com or services@elecfreaks.com, and we are willing to help you to solve it.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • jack

    hi,
    im trying something really similiar.. android > arduino..

    at the moment im able to connect android android, but im struggling when it comes to connect ‘securly’ to the arduino. because i cant find the right source code (parts) to set the ‘pairing key’ as my arduino accepts a 4 digit code…. NOT as my android build-in pairing mechanism suggest 5 or 6 digits.. without an option to edit the code.

    how did u manage this in ur project?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • dany

      If you have technical problems to communicate with us, please feel free to send email to services@elecfreaks.com.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • Gerbon

    Really good work, Thanks a lot..

    @lily : you have to set a target version of android used to build this app : change the project properties (on eclipse : right-click on the_name_of_project and click on Android then select a target version (you need min version 5) – apply then ok

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • dany

      Really thank you for your effort and attention.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • lily

    Hi and thank you for your great job.
    I’ve imported your code and while trying to run it, some errors occured. Could you please help me to fix them ?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Mario

    Muchas gracias, me ha servido mucho para darme una mejor idea acerca de como trabajar con el BT en android

    Thank you very much, helped me a lot to give me a better idea about how to work with BT in android

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Kyle Terriere

    HI, I’m new to programming in Android and I was wondering if you could give me some instructions on how to install this app as well as any tool kits I need to install. When I open the project in Eclipse (for mac) none of the “import android…” lines at the top of the activity01.java are supported so I think I’m missing something here.
    Thanks.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • khalil

    Hi,

    thanks for your nice example,
    when i started importing the example i have small ‘x’ on the file name

    kindly, is there any thing missing

    regards

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • taher

    hi
    i have used this source to develop my Android Bluetooth Remote Control Application which communicate with micro controller. when i connect to Bluetooth modem with my Android app from my phone it stop unexpectedly. please i am not getting what’s wrong with application?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Macduf

    Hello!
    Thanks for great article. I need your help. I want to connect to bluetooth device without user action.
    I need to do next:
    after app is start and button “as client” is pressed it need to connect to some device (for example 00:12:03:09:71:61) and after succesful connection open chat activity. That mean that users must click only 1 button “as client” and app must connect to device (for example 00:12:03:09:71:61). I am noob in Java, can you help me?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 2 votes)
  • man

    hi, my apps crashs when try connect bluetooth, at client after search devices…

    i got this error 09-20 16:54:14.597: E/EF-BTBee(18243): java.io.IOException: Service discovery failed

    i think that can be uuid you mentioned, but link you says are offline, and im using same (a60f35f0-b93…)

    can you help me?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Samnime

    Can I ask how to connect bluetooth device?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Pete

    I am trying to test this by having an actual Bluetooth device attached to my Android running 2.2.1. Do you have recommendations for which product to use for bluetooth?

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 2 votes)
  • https://market.android.com/details?id=name.antonsmirnov.android.arduinocommander Anton

    You can also use ArduinoCommander app – https://market.android.com/details?id=name.antonsmirnov.android.arduinocommander

    VA:F [1.9.13_1145]
    Rating: 5.0/5 (3 votes cast)
    VA:F [1.9.13_1145]
    Rating: +2 (from 2 votes)
  • Salman Raza

    Hi.. when i click on Search Device Button the android application crash. I am using eclipse and using android emulator 2.3.3 to check my code. please help :(

    Error when Clicking search button : The Application Has stopped un expectedly

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • robi

      Hi Salman,

      I must admit that there are still some problems of the APP. When you click Search Device Button if there is not any Bluetooth device in around, maybe return error or crash.

      So, please make sure there is others Bluetooth devices which can be discover by Android’s Bluetooth.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
    • KW

      My understanding is that Eclipse emulators are unable to process any bluetooth requests. You have to test on physical device for bluetooth apps.

      Good luck.

      VA:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.13_1145]
      Rating: +1 (from 1 vote)
    • Haris

      Kindly try by first pairing the device explicitly from your Android phone’s settings. Usually the pair code is 1234

      VA:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
  • Alban

    Great ….very well explained and documented ….(even for french-speaking people)

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • http://www.beep.com/cgi-bin/forum_en/showthread.php?p=5259#post5259 Wendie Riccelli

    Keep working ,great job!

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  • http://why-will-need-to-you-pick-out-duct-cleaning-companies.bloghi.com Manhattan Studio cleaning service

    Hey! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really appreciate your content. Please let me know. Cheers

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • ElecFreaks

      Of course you can share our blog, it’s no problem. We would like that more and more people could read them.
      Just keep our links, please. Thanks.

      VN:F [1.9.13_1145]
      Rating: 5.0/5 (1 vote cast)
      VN:F [1.9.13_1145]
      Rating: +2 (from 2 votes)
Go To Top