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.

57 comments:

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

    ReplyDelete

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

    ReplyDelete
  3. 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!

    ReplyDelete
  4. 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?

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

    ReplyDelete
  6. 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!

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

    ReplyDelete
  8. anonymous from 10/4,

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

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

    ReplyDelete
  10. 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)

    ReplyDelete
  11. 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?

    ReplyDelete
  12. Re: v

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

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

    ReplyDelete
  14. 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!

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

    ReplyDelete
  16. I´m using Win 7 Home Premium x64.

    Works great!

    Thank you very much !!!

    ReplyDelete

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

    ReplyDelete
  18. 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?

    ReplyDelete
  19. 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?

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

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

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

    ReplyDelete
  23. 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!

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

    ReplyDelete
  25. thank you digitalhack

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

    A very nice sample! thanks a lot..

    ReplyDelete
  26. 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 ;)

    ReplyDelete

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

    digitalhack

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

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

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

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

    ReplyDelete
  32. Very helpfull man. Thanks a lot!

    ReplyDelete
  33. You are genius!

    You posted what exactly I want.

    This helped me to grasp connection between APP and PC.

    Thank you :D

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

    ReplyDelete
  35. 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) ?

    ReplyDelete
  36. 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?

    ReplyDelete
  37. 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?

    ReplyDelete
  38. gumuruhsspj,

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

    digitalhack

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

    ReplyDelete
  40. 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 )

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

    ReplyDelete
  42. 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^^

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

    ReplyDelete

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

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

    ReplyDelete
  46. 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"

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

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

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

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

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

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

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

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

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

    ReplyDelete