Go Down

Topic: [Help]How the data transfer via bluetooth from Arduino with android? (Read 7976 times) previous topic - next topic

ltdat5169

Hi!
I'm learning about a project data transfer between android and arduino uno via bluetooth. I have found on the net but could not find. Can you help me?
Thanks.

Robin2

Do you have a problem at the Android end (which I am unlikely to be able to help with) or at the Arduino end.

Without seeing your code it is impossible to offer any advice.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ltdat5169


ltdat5169

Here is a sample project that I found.
If you want to receive data from the Arduino sent to the need to add anything?

/**
 *  @version 1.1 (28.01.2013)
 *  http://english.cxem.net/arduino/arduino5.php
 *  @author Koltykov A.V. (Êîëòûêîâ À.Â.)
 *
 */

package com.example.bluetooth1;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
 
import com.example.bluetooth1.R;
 
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
  private static final String TAG = "bluetooth1";
   
  Button btnOn, btnOff;
  TextView tvShow;
  int x;
  private BluetoothAdapter btAdapter = null;
  private BluetoothSocket btSocket = null;
  private OutputStream outStream = null;
   
  // SPP UUID service
  private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
 
  // MAC-address of Bluetooth module (you must edit this line)
  private static String address = "30:14:06:16:06:59";
   
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    setContentView(R.layout.activity_main);
 
    btnOn = (Button) findViewById(R.id.btnOn);
    btnOff = (Button) findViewById(R.id.btnOff);
    tvShow = (TextView) findViewById(R.id.tvShow);
    x=0;
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    checkBTState();
 
    btnOn.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        sendData("1");
        Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
      }
    });
 
    btnOff.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        sendData("0");
        Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show();
      }
    });
  }
 
  private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
      if(Build.VERSION.SDK_INT >= 10){
          try {
              final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
              return (BluetoothSocket) m.invoke(device, MY_UUID);
          } catch (Exception e) {
              Log.e(TAG, "Could not create Insecure RFComm Connection",e);
          }
      }
      return  device.createRfcommSocketToServiceRecord(MY_UUID);
  }
   
  @Override
  public void onResume() {
    super.onResume();
 
    Log.d(TAG, "...onResume - try connect...");
   
    // Set up a pointer to the remote node using it's address.
    BluetoothDevice device = btAdapter.getRemoteDevice(address);
   
    // Two things are needed to make a connection:
    //   A MAC address, which we got above.
    //   A Service ID or UUID.  In this case we are using the
    //     UUID for SPP.
   
   try {
      btSocket = createBluetoothSocket(device);
   } catch (IOException e1) {
      errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + ".");
   }
   
    /*try {
      btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
      errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }*/
   
    // Discovery is resource intensive.  Make sure it isn't going on
    // when you attempt to connect and pass your message.
    btAdapter.cancelDiscovery();
   
    // Establish the connection.  This will block until it connects.
    Log.d(TAG, "...Connecting...");
    try {
      btSocket.connect();
      Log.d(TAG, "...Connection ok...");
    } catch (IOException e) {
      try {
        btSocket.close();
      } catch (IOException e2) {
        errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
      }
    }
     
    // Create a data stream so we can talk to server.
    Log.d(TAG, "...Create Socket...");
 
    try {
      outStream = btSocket.getOutputStream();
    } catch (IOException e) {
      errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }
  }
 
  @Override
  public void onPause() {
    super.onPause();
 
    Log.d(TAG, "...In onPause()...");
 
    if (outStream != null) {
      try {
        outStream.flush();
      } catch (IOException e) {
        errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
      }
    }
 
    try     {
      btSocket.close();
    } catch (IOException e2) {
      errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
    }
  }
   
  private void checkBTState() {
    // Check for Bluetooth support and then check to make sure it is turned on
    // Emulator doesn't support Bluetooth and will return null
    if(btAdapter==null) {
      errorExit("Fatal Error", "Bluetooth not support");
    } else {
      if (btAdapter.isEnabled()) {
        Log.d(TAG, "...Bluetooth ON...");
      } else {
        //Prompt user to turn on Bluetooth
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
      }
    }
  }
 
  private void errorExit(String title, String message){
    Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
    finish();
  }
 
  private void sendData(String message) {
    byte[] msgBuffer = message.getBytes();
 
    Log.d(TAG, "...Send data: " + message + "...");
 
    try {
      outStream.write(msgBuffer);
    } catch (IOException e) {
      String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
      if (address.equals("00:00:00:00:00:00"))
        msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code";
         msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
       
         errorExit("Fatal Error", msg);       
    }
  }
  public boolean Bluetooth1(View v,MotionEvent event)
  {
     tvShow.setText("1 "+Integer.toString(x));
   return true;
     
  }
}


Nick_Pyner


ltdat5169


Robin2

Here is a sample project that I found.
If you want to receive data from the Arduino sent to the need to add anything?

/**
 *  @version 1.1 (28.01.2013)
 *  http://english.cxem.net/arduino/arduino5.php
 *  @author Koltykov A.V. (Êîëòûêîâ À.Â.)
 *
 */

package com.example.bluetooth1;

import java.io.IOException;
That is an Android java program.

This is an Arduino forum - don't expect Android expertise here. Especially as the Android people have gone out of their way to make Android programming as complicated as possible.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

where to enter.
Where to enter what?

You need to explain what you are asking in a lot more detail.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ltdat5169

I want to read ADC from Arduino then returns the smart phone via bluetooth.

Robin2

I want to read ADC from Arduino then returns the smart phone via bluetooth.
You need
+  an Arduino program (which should be fairly easy to do)
+  a bluetooth module to connect to your Arduino
+  a program for your Android phone which can receive the data

Have you any of these parts?
If you have give details of them - but remember we don't know Android coding in this Forum

If you need to write a simple program for your phone this link to SL4A may be of interest

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Nick_Pyner

I suspect you are trying to re-invent the wheel.

At a guess, you want to read an analogue signal on Arduino and send that to Android via Bluetooth.

This requires
 - an Arduino programme about ten lines long.
 - a bluetooth module for Arduino, cost about $7 on eBay
 - an app already written by somebody else for Android, of which there are about a dozen available, all for free.

My favourite is Bluetooth Graphics Terminal, which, surprise, can display your results live on a graph.



Go Up