[android-beginners] Re: Serial over Bluetooth

2010-02-11 Thread XCaffeinated
Apologies for the lengthy post, but I wanted this to be at least semi-
useful, and there is not a lot of info out there. I see that the OP is
from awhile ago, but that this thread has been resurrected.

I am currently using my Droid (firmware push: 2.0.1) to communicate to
an ArduinoBT (Bluetooth) board via SPP (the ArduinoBT's default) using
the well-known SPP UUID (0x1101 host side, and the UUID-extended
version of 0x1101 on the client (Android) side; see the code below for
the actual UUID).  The ArduinoBT board uses a BlueGiga WT11 module and
iWrap firmware/API. We currently have it interfaced to a hobby robot.

*** Following pertains to Android 2.0 and up; there is no public
Bluetooth API before 2.0 ***

Given that the ArduinoBT comes out of the box set up for SPP, I
configured our Android client application to do the same. I've
commented the code, but you need to know something about Bluetooth to
get the most out of it. I highly recommend Bluetooth Essentials for
Programmers, available for free. On the Android side: the Android SDK
doc, and the Bluetooth Chat Sample are excellent references.

Before you run the client, you need to pair your robot controller with
your Android device. It doesn't have to be connected, just paired. You
can do the discovery/pairing from your handset's standard networking
settings. You will need a PIN; Android requires authentication even if
your robot controller doesn't (most controllers let you set this as an
option).  You will also need to enable Bluetooth.

This is a brain-dead simple client, no threading, no receiving; all it
does is send commands to our robot (modified a bit, since the OP only
cares about the SPP communication material), and should be very easy
to modify for various purposes.  All that needs to be changed on this
side is the MAC address.  Enter the robot controller's MAC address in
place of the XX:XX... string below.

package com.example.thinbtclient;

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.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ThinBTClient extends Activity {

private static final String TAG = THINBTCLIENT;
private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
//Well known SPP UUID (will *probably* map to RFCOMM channel 1
(default) if not in use);
//see comments in onResume().
private static final UUID MY_UUID =
UUID.fromString(1101--1000-8000-00805F9B34FB);

private static String address = XX:XX:XX:XX:XX:XX; //==
hardcode your robot (server) MAC address here...

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

if(D)
   Log.e(TAG, +++ ON CREATE +++);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, Bluetooth is not available.,
Toast.LENGTH_LONG).show();
finish();
return;
}

if (!mBluetoothAdapter.isEnabled()) {
   Toast.makeText(this, Please enable your BT and re-run
this program., Toast.LENGTH_LONG).show();
   finish();
   return;
}

if(D)
   Log.e(TAG, +++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER ++
+);
}

@Override
public void onStart() {
super.onStart();
if(D) Log.e(TAG, ++ ON START ++);
}

@Override
public void onResume() {
super.onResume();

if(D) {
   Log.e(TAG, + ON RESUME +);
Log.e(TAG, + ABOUT TO ATTEMPT CLIENT CONNECT +);
}

//When this returns, it will 'know' about the server, via it's
MAC address.
BluetoothDevice device =
mBluetoothAdapter.getRemoteDevice(address);

//We need two things before we can successfully connect
(authentication issues
//aside): a MAC address, which we already have, and an RFCOMM
channel.
//Because RFCOMM channels (aka ports) are limited in number,
Android doesn't allow
//you to use them directly; instead you request a RFCOMM
mapping based on a service
//ID. In our case, we will use the well-known SPP Service ID.
This ID is in UUID
//(GUID to you Microsofties) format. Given the UUID, Android
will handle the
//mapping for you. Generally, this will return RFCOMM 1, but
not always; it
//depends what other BlueTooth services are in use on your
Android device.
try {
   btSocket =
device.createRfcommSocketToServiceRecord(MY_UUID);
  

[android-beginners] Re: Serial over Bluetooth

2010-02-11 Thread XCaffeinated
I've posted a nicely-formatted version of the code from the previous
post here:
http://www.anddev.org/viewtopic.php?p=35487#35487

-XCaf

On Feb 11, 7:02 pm, XCaffeinated ssatn...@gmail.com wrote:
 Apologies for the lengthy post, but I wanted this to be at least semi-
 useful, and there is not a lot of info out there. I see that the OP is
 from awhile ago, but that this thread has been resurrected.

 I am currently using my Droid (firmware push: 2.0.1) to communicate to
 an ArduinoBT (Bluetooth) board via SPP (the ArduinoBT's default) using
 the well-known SPP UUID (0x1101 host side, and the UUID-extended
 version of 0x1101 on the client (Android) side; see the code below for
 the actual UUID).  The ArduinoBT board uses a BlueGiga WT11 module and
 iWrap firmware/API. We currently have it interfaced to a hobby robot.

 *** Following pertains to Android 2.0 and up; there is no public
 Bluetooth API before 2.0 ***

 Given that the ArduinoBT comes out of the box set up for SPP, I
 configured our Android client application to do the same. I've
 commented the code, but you need to know something about Bluetooth to
 get the most out of it. I highly recommend Bluetooth Essentials for
 Programmers, available for free. On the Android side: the Android SDK
 doc, and the Bluetooth Chat Sample are excellent references.

 Before you run the client, you need to pair your robot controller with
 your Android device. It doesn't have to be connected, just paired. You
 can do the discovery/pairing from your handset's standard networking
 settings. You will need a PIN; Android requires authentication even if
 your robot controller doesn't (most controllers let you set this as an
 option).  You will also need to enable Bluetooth.

 This is a brain-dead simple client, no threading, no receiving; all it
 does is send commands to our robot (modified a bit, since the OP only
 cares about the SPP communication material), and should be very easy
 to modify for various purposes.  All that needs to be changed on this
 side is the MAC address.  Enter the robot controller's MAC address in
 place of the XX:XX... string below.

 package com.example.thinbtclient;

 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.os.Bundle;
 import android.util.Log;
 import android.widget.Toast;

 public class ThinBTClient extends Activity {

     private static final String TAG = THINBTCLIENT;
     private static final boolean D = true;
     private BluetoothAdapter mBluetoothAdapter = null;
     private BluetoothSocket btSocket = null;
     private OutputStream outStream = null;
     //Well known SPP UUID (will *probably* map to RFCOMM channel 1
 (default) if not in use);
     //see comments in onResume().
     private static final UUID MY_UUID =
 UUID.fromString(1101--1000-8000-00805F9B34FB);

     private static String address = XX:XX:XX:XX:XX:XX; //==
 hardcode your robot (server) MAC address here...

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

         if(D)
                    Log.e(TAG, +++ ON CREATE +++);

         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
         if (mBluetoothAdapter == null) {
             Toast.makeText(this, Bluetooth is not available.,
 Toast.LENGTH_LONG).show();
             finish();
             return;
         }

         if (!mBluetoothAdapter.isEnabled()) {
                    Toast.makeText(this, Please enable your BT and re-run
 this program., Toast.LENGTH_LONG).show();
                    finish();
                    return;
         }

         if(D)
                    Log.e(TAG, +++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER ++
 +);
     }

     @Override
     public void onStart() {
         super.onStart();
         if(D) Log.e(TAG, ++ ON START ++);
     }

     @Override
     public void onResume() {
         super.onResume();

         if(D) {
                    Log.e(TAG, + ON RESUME +);
             Log.e(TAG, + ABOUT TO ATTEMPT CLIENT CONNECT +);
         }

         //When this returns, it will 'know' about the server, via it's
 MAC address.
         BluetoothDevice device =
 mBluetoothAdapter.getRemoteDevice(address);

         //We need two things before we can successfully connect
 (authentication issues
         //aside): a MAC address, which we already have, and an RFCOMM
 channel.
         //Because RFCOMM channels (aka ports) are limited in number,
 Android doesn't allow
         //you to use them directly; instead you request a RFCOMM
 mapping based on a service
         //ID. In our case, we will use the well-known SPP Service ID.
 This ID is in UUID
         //(GUID to you Microsofties) format. Given 

[android-beginners] Re: Serial over Bluetooth

2010-02-10 Thread mikshel
Based on my experience so far (and the information provided on Android
site), SPP (Serial Port Profile) is not supported by existing Android
SDK (as of Feb 2010).

On a funny note, one of our developers managed to get a signal out and
receive a reply using Bluetooth SPP on Android Dev Phone running SDK
1.6.  I do not have any additional information at this time as that
developer was switched to another project.

Hope this answers the question (and know that it will not help at
all),
M.

-- 
You received this message because you are subscribed to the Google
Groups Android Beginners group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en