Pages

Saturday, May 5, 2012

Android Example - Bluetooth Simple SPP Client and Server

This example has both a client and a server. The client runs on Android and in my case I ran the server on Windows 7 x64. I tested both java server and a perl server.

A useful freeware tool for listing the services / UUIDs on a device is Medieval Software's Bluetooth Network Scanner.

I have included the files that have content that is key to setting up the application.

You should be able to create a new Android project in eclipse and then copy the code included below into the appropriate file.

Below the code attachments you will find a screen capture of what it looks like running on my Acer A100.

If you have any suggestions or questions please post them as comments.


Android SPP Client

The Android application is based on this post: Serial over Bluetooth simple test client. from anddev.org.

ConnectTest.java
goes in <ProjectRoot>\src\com\example\connecttest\
strings.xml goes in <ProjectRoot>\res\values\ main.xml
goes in <ProjectRoot>\res\layout\
AndroidManifest.xml
goes in <ProjectRoot>
Screen Capture from Acer A100


Java SPP Server

The java server was based on this the SimpleSPPSever example from JSR-82 Sample : SPP Server and Client. To get to run on Windows I used the bluecove-2.1.1-SNAPSHOT.jar library.

SimpleSPPServer.java
goes in <ProjectRoot>\src\
Screen Capture from PC


Perl Server

The perl server was built using the Net::Bluetooth module.

SimpleSPPServer.pl

Notes:

  1. As the SDK Emulator doesn’t emulate bluetooth this example will not run in the emulator.
  2. This example is built upon the work of others. I post it here not as an example of original work but rather as a complete working example for reference. If I didn't appropriately credit you for your work please let me know and I will add you.

21 comments:

Luciano Cruz said...

A great example man thanks

Anonymous said...

Hello.
Where I can find the R.file? (import com.example.bttest.R;)
Can you send me or publicate the project folder?.
jcllanos at hotmail.com

Greetings.
JC.

digitalhack said...


com.example.bttest.R is a generated file that should be generated by the IDE. It should be located under your project in the gen directory.

Hope this helps,

digitalhack

El caballero de la noche said...

Hi!! Great example man, thanx!

I run this on a windows 7 32 bits and works perfectly fine, but in another latop with Windows 7 64 bits it's not possible to run it... Do you know why is that?? You say in your post that for you it worked on a 64-bit pc, didn't it??

Cheers!

digitalhack said...

Yes, I am using Windows 7 64 bit. I am not sure why it wouldn't run for you on Windows 7 64 bit. Do you get any error message or does it just not run?

Anonymous said...

extremely useful program ,thank you!

what i have done:
1) android code
private static String address = "my address";
2) pc code , i have used
UUID uuid = new UUID("1101", true);
or
UUID uuid = new UUID("0000110100001000800000805f9b34fb",false);

the results:
code in android works(tested with tera term)
but in pc :
BlueCove version 2.1.1-SNAPSHOT on bluesoleil
Address: my address
Name: my PC

Server Started. Waiting for clients to connect...
(and then nothing)
PS. i use jre,jdk ,eclipse (32 bit) on a 64 bit machine.
Thanks in advance .

peter said...

Thank you for your program!

i have changed the address in the client-android.

in android : ...Sending message to server...

and in server-pc : Server Started. Waiting for clients to connect...

why android can't connect to pc?

Cheers!

digitalhack said...

peter,

How did you get your server's address?

Did you use the address listed for your Bluetooth Radio under Device Manager?

expand Bluetooth Radios -> right click on Bluetooth Radio -> properties -> Advanced Tab -> Address

digitalhack said...

anonymous from 10/4,

Just to double check is "My Address" your server's bluetooth address and not your Android client's?

Yugandhara Babu Potharlanka said...

Hi, I saw your post yesterday, it is very nice. But today I tested this in my PC which has Win7 32 bit OS and connected USB bluetooth device. After running java server application, I got below exception.

Exception in thread "main" javax.bluetooth.ServiceRegistrationException: Failed to register service; [10022] An invalid argument was supplied.

I used same code as you provided here. Suggest me to get out of this exception and to make it work.

Anonymous said...

Please note, the address called from getRemoteDevice, must be uppercase if the address is provided via a string.

http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String)

v said...

Does this code only work with BlueSoleil?
I'm trying to run the code and get the following errors on my Android device:
"In onResume() and an exception occured during write: socket closed."
and
"Check that the SPP UUID: 00001101-0000-1000-8000-00805F9B34FB exists on server."

Can anyone help me with this?

digitalhack said...

Re: v

I have only tested the code on Windows using the Windows BT Stack. I cannot say if it works with BlueSoleil.

Bilo said...

i get the following errors on my Android device:
"In onResume() and an exception occured during write: socket closed."
and
"Check that the SPP UUID: 00001101-0000-1000-8000-00805F9B34FB exists on server."

Can anyone help me with this?
PS: I have tested the code on Windows using the Windows BT Stack

Anonymous said...

Hi!
I tested it in Windows 8 x64 and it works great.
I wanted to know if it is possible to set the android application a a server and the java program as a client?
and if so, how to do that?
tnx!

Anonymous said...

Hi,

Great tutorial. I got this up and running with ease.

But I bumped into one problem, you might be able to help me with.

I send a string of Bytes to my BT Client and my Client replied immediately.

How do I catch these replies.

I did some searching and it has to do with threading. If possible, could you show me how to do it.

Thanks.

Anonymous said...

I´m using Win 7 Home Premium x64.

Works great!

Thank you very much !!!

digitalhack said...


Some follow ups:

Bilo on December 15, 2012 at 3:13 AM

I think the problem is that your server isn’t properly registering the service and therefore you get an error when you connect to it. You might try to connect to the server with another client and see if you get a similar error.

Anonymous on January 11, 2013 at 7:22 AM

It is possible to setup an Android program to receive data from a java program but I don’t have an example of that. Maybe sometime in the future I will put one together.

Anonymous on February 1, 2013 at 12:28 PM

I don’t have an example of this but a good place to start would be the Bluetooth Chat example in the SDK.

digitalhack

fiduliin said...

Hey,

thanx for the code.

I ran into the same issues as peter.
The server responds like this:
Server Started. Waiting for clients to connect... (and then nothing happens).

I am using a external bluetooth dongle for my pc.
Do you think that this is why the server does not let the client connect?

digitalhack said...

fiduliin,

I don't think it would have anything to do with an external BT dongle on your PC. That is the way I ran it.

Does the PC run Windows? If so, I would suggest installing Medieval Bluetooth Network Scanner and see if you can see the server running on the PC.

Hope this helps,

digitalhack

Valy said...

Hello!
Digitalhack could you send me this application because I can't do this working? Thanks!