Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Communication between Android and Arduino with Bluetooth

Status
Not open for further replies.

leolegend

Newbie level 2
Joined
May 15, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,333
We keep working on ADK(Android Open Accessory Development Kit) for weeks, and try to assemble some bata prototype and make a library for the communication between Android and Arduino with bluetooth.
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.

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.

Code:
<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.

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

Code:
    //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”.

Code:
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

    startActivityForResult(enabler, REQUEST_DISCOVERABLE);
OK, Setting up device is completed. The code of this step as below:

Code:
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);

  }



}
It's too long to post the whole article here. You can see the following three steps(Finding Device, Connecting Device, Managing Device(server/client))and download the full code in the original post here: Android + BlueTooth.
 

vs3474

Newbie level 4
Joined
Oct 4, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,332
hi leolegend


Great work ..

want to ask some question.is it possible to send a file or image from android phone to bluetooth bee without using any android application?. i mean sending a file(for example jpeg,pdf,mp3 etc)like one android(or any)phone to other android(or any)phone but not using any application.

Regards..
VS3474

- - - Updated - - -
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top