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.

58 comments:

Unknown 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?

Unknown 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 have run into the same issue as Peter. The server responds like this:
Server Started. Waiting for clients to connect...

I use a external bluetooth dongle for my pc.
Do you think that the server is not responding to the client because of the dongle?

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!

Ria said...

Its a great example...its working fine..but i want to use the functionality for file transfer once the connection is established..how to go about that??...plzzz help

Sunil Kumar R said...

a very easy to follow example..

BTW Could you please clarify :-

Does this require 2 separate machines for android client and java server?

I am on 1 machine (Win 7) running hte server (which starts well and waits for clients to connect).

I have an eclipse shell open with android emulator and bluetooth turned on my machine.. but the android client fails with NPE... :(

waiting to hear from you!

digitalhack said...

Sunil Kumar R.

Unfortunately this will not run in the emulator as it needs bluetooth and to my knowledge the emulator doesn't support bluetooth.

digitalhack

Mohamed Rafeeque said...

Nice Example Thanks

Sunil Kumar R said...

thank you digitalhack

Yes, you're right.. I tried directly on the android device and it worked.

A very nice sample! thanks a lot..

Anonymous said...

Hi, thanks for the code. It works if i have bluetooth already enabled, but if i dont it crashes the android app (logcat says: performing pause of activity that is not resumed) and points to outStream.flush() ).
If you could help me solving this with bt off it would be great ;)

digitalhack said...


Let me see what I can come up with. It has been a while since I have done anything with this.

digitalhack

Anonymous said...

Hi, im the 18 oct poster.
After some debugging and thinking about the problem, i managed to fix the crash of app when bt is off (entering or leaving the app by pressing home menu).
Heres the code that fixes the crash (changed onPause and onResume): http://pastebin.com/raw.php?i=NFerSwcb

Anonymous said...

Hai digitalhacks I keep trying but the client part seems not to be working wat could be the problem ?

Unknown said...

Hey dude ,
Actually i want to create a client side app in android where it will be on Recive mode , it means this app only recieved String from server and store that string into any file .
so is it possible ??
thanks
Hitesh singh
hiteshsingh1101@GMAIL.COM

Unknown said...

Hi Great example man, thanks!
How can I change it to send file from mobile to PC ?

Anonymous said...

Very helpfull man. Thanks a lot!

Anonymous said...

You are genius!

You posted what exactly I want.

This helped me to grasp connection between APP and PC.

Thank you :D

Anonymous said...

The connection seems to be closed after i send the first text. How do i resolve that?

gumuruhsspj said...

hi thanks for the article!

it's nice to see the client-server bluetooth testing is working.

But my question is.

If i have android device,
and i have 1 single pc. But the single pc is not having a bluetooth technlogy. Would it be possible to test the bluetooth between the client (android) and the server (pc) ?

Anonymous said...

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?

sag said...

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

gumuruhsspj,

If you don't have bluetooth on your computer it will not be able to act as a server.

digitalhack

digitalhack said...

sag,

It has been a while but I think you are getting that error because the SPP service isn't running on your server.

digitalhack

MUKUND said...

What is Used of UUID ?
IS UUID same for All client device ?

How make connection between different Client Device to same Server i.e( My client will be Android Device and Server will be java be application )

digitalhack said...

MUKUND,

UUIDs are used to identify services and characteristics on a device.

A device that has services would have a different UUID for each service.

You might try this line for more information: http://stackoverflow.com/questions/13964342/android-how-do-bluetooth-uuids-work.

Hope this helps,

digitalhack

Franzi said...

Hello, I try to send a String to the Client (Smartphone). I used the method with //send Response to Client, over it. On the Smartphone I use an InputStream, but it doesn't works. The App crashed. I don't know how to do it. Please help me^^

Unknown said...

Hello, thanks for your example code !
But I have a problem need your help.
I follow your step to copy your code to the new project in eclipse. After compiling, I push into my android phone and install it. When I open the app, it crash suddenly and display 「Unfortunately,app has stopped」...
Please help me thanks.

digitalhack said...


jay79227,

I am going to need a bit more information.

One thing, on line 34 did of ConnectTest.java did you update it with your servers MAC address?

digitalhack

Unknown said...

Hey,
Thank you so much for this tutorial.
Everything works for me perfectly, but i just wanted to ask if i wanted to send a string/int value(like "x") how would i send it to the server?

PS:Thanks again !!

Kind Regards,
AK

Unknown said...

Hey,
im making a controller for my finch robot to control it using bluetooth.
I just wanted to ask how do i keep the server open and send more data values because every time i send data it says "Bluetooth Stack Shutdown Complete"

digitalhack said...

Ayush Kapri,

In answer to your question on how to send an integer. You could use the same method as I am using but convert the integer to a string send it and convert it back to an integer.

Hope this helps,

digitalhack

digitalhack said...

Ayush Kapri,

In response to your second question of how to keep the server open. You will need to modify the server. The section of code that receives the message from the client starts on line 37 of the java code. Below this section on line 43 is the code that sends data back to the client. You could put these sections into some sort of loop that didn't exit until the client sent a specific message to the server to exit the loop.

Hope this helps,

digitalhack

Sri said...

This is a great post, and is something similar to what I was looking for. However it would be great if you could share the jar you are using for packages import javax.bluetooth.*;
import javax.microedition.io.*;

Thanks,
SRi

m said...

How do I read the response string from the SPP server on a client?

Anonymous said...

How do I receive the response string from the SPP server on an android client?

m said...

How do I fetch and retrieve the response string on an android SPP client?

Unknown said...

Hello,
I am new for Android so i couldn`t work your project. I have never used bluecove so I don`t understand how is working. Please can you send me your project? Maybe i could be insert codes not correctly. My mail: gulcanulke92@hotmail.com

Unknown said...

Hello,
Thanks a lot for your great code. I noticed that you said the com.example.bttest.R is generated by IDE. However, I still have no idea how to generate com.example.bttest.R. Any suggestions will be wonderful!!

Unknown said...

Thanks, working nice. I modified the server a little bit to listen all the time and write incoming strings and its cool.
For those who have this error: "In onResume() and an exception occured during write: socket closed." You have to set up your MAC address properly, not the 00:00:00...

Cupi said...

Hi,

Thanks for a great post. It's working fine except that both devices always asks to confirm the PIN (the pairing request). For the first time connection, this is expected. But how can this pairing request be bypassed from the second time connection?

Thanks,
cupi