Home > Uncategorized > RFComm Server – Client JAVA

RFComm Server – Client JAVA

Hi folks and newbie’s in programming like me 🙂

Well maybe this post will not be helpful for experts programmers.
But maybe if some day newbie like me wants to start Bluetooth programming in Java. And need at some point string or file transfer program between BT devices (i hope it helps).

To be honest i am not expert programmer and most of the stuff i do is from google and reading forums.

I would like to say i am not going into explaintion about for RFComm or How Bluetooth works. Here is my scenario i had 2 PC’s with BT dongles.
1st one is from BK3 and another one is 3Com. Instead of having pre complied programs camed with the BT device. Use from the following links -> http://www.soft32.com/download_170588.html for my BK3 BT device driver.
Earlier i was using some Bluesoiel program which i never wanted at any time.

I installed the driver of 3Com BT device from http://www.3com.com/products/en_US/result.jsp?selected=all&sku=3CREB96&sort=effdt&order=desc from here.

Next thing i start exploring examples and found btessitnals http://www.btessentials.com/about.html.

You need also to download BT API i am using BlueCove API which can be download from here. http://bluecove.org/#

OK here is the server part which listen for incoming connection on opened service url stream. and send back the file or string to client.
= Feel free to copy and test it!

import java.io.*;
import java.net.*; import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
import javax.bluetooth.UUID;

public class rfcommserver {
	UUID uuid = new UUID("8848",true);
    public void startserver() {
                   try {
                String url = "btspp://localhost:" + uuid +
                  //  new UUID( 0x1101 ).toString() + 
                    ";name=File Server";
                StreamConnectionNotifier service = 
                    (StreamConnectionNotifier) Connector.open( url );

                StreamConnection con = 
                    (StreamConnection) service.acceptAndOpen();
                OutputStream dos = con.openOutputStream();
                InputStream dis = con.openInputStream();

                InputStreamReader daf = new InputStreamReader(System.in);
                BufferedReader sd = new BufferedReader(daf);                
                RemoteDevice dev = RemoteDevice.getRemoteDevice(con);    
            while (true) {	
            	//String greeting = sd.readLine();
          //      String greeting = "JSR-82 RFCOMM server says hello";
          //      dos.write( greeting.getBytes() );	
            	if (dev !=null) 
                    File f = new File("src/test.xml");
                    BufferedInputStream filein = 
                    new BufferedInputStream(new FileInputStream(f));
                    byte buf[] = new byte[1024];
                    int len;
                    while ((len=filein.read(buf))>0)
                   // dos.flush();
          	  byte buffer[] = new byte[1024];
                int bytes_read = dis.read( buffer );                
                String received = new String(buffer, 0, bytes_read);
                ("Message:"+ received +"From:"
               // con.close();
            } catch ( IOException e ) {

    public static void main( String args[] ) {
       try {
	LocalDevice local = LocalDevice.getLocalDevice();
        System.out.println("Server Started:\n"
	rfcommserver ff = new rfcommserver(); 
    while (true) {
    } //while 
    }  //try 
      catch (Exception e) {System.err.print(e.toString());
}  //main

Client part which connected to specified BT device and
reads the input stream either file or string.

import java.io.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
public class rfcommclient {
 public void startclient() {
   try {   
   String url = "btspp://01576e1a82a6:1";
        StreamConnection con = 
            (StreamConnection) Connector.open(url);
        OutputStream os = con.openOutputStream();
        InputStream is = con.openInputStream();
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader bufReader = new BufferedReader(isr);
        RemoteDevice dev = RemoteDevice.getRemoteDevice(con);

     /**   if (dev !=null) {
         File f = new File("test.xml");
         InputStream sis = new FileInputStream("test.xml");
         OutputStream oo = new FileOutputStream(f);
         byte buf[] = new byte[1024];
         int len;
         while ((len=sis.read(buf))>0)
        }  **/
     if (con !=null) {
      while (true) {   
         //sender string
     System.out.println("Server Found:" 
         +dev.getBluetoothAddress()+"\r\n"+"Put your string"+"\r\n");
        String str = bufReader.readLine();
        os.write( str.getBytes());
       //reciever string
        byte buffer[] = new byte[1024];
        int bytes_read = is.read( buffer );
        String received = new String(buffer, 0, bytes_read);
        System.out.println("client: " + received
         + "from:"+dev.getBluetoothAddress()); 
  catch(Exception e){}  
    public static void main( String args[] ) {
      LocalDevice local = LocalDevice.getLocalDevice();
       catch (Exception e) {System.err.print(e.toString());}
     try {        
     rfcommclient ss = new rfcommclient();
        } catch ( Exception e ) {


Thats all!!

Categories: Uncategorized Tags: , ,
  1. Palash
    December 14, 2010 at 2:41 am

    can u pls provide the exact java code as it is html and has some issues for example, the following code does not work

    String url = “btspp://localhost:” + uuid +
    // new UUID( 0×1101 ).toString() +
    “;name=File Server”;

    I was interested to test the code in .java

  2. May 19, 2011 at 2:23 am

    Friend, I am interesting about the widget you are using. Thank you sir forgive my english.

  3. May 31, 2011 at 11:29 am

    Hi , goto theme or just use find for this one ! Theme: INove by NeoEase.

  4. March 3, 2014 at 3:08 am

    This may be what I need. I have a HC-05. It is just a circuit board that connects to the Tx and Rx pins of a UART output of a microprocessor chip and transmits Bluetooth. On the PC I use WinXP Bluetooth Devices program to pair the PC to the HC-05. Pairing creates two new comports, Outgoing (usually COM10) and Incoming (usually COM11) and if I use a program like Hyperterminal I can connect to the outgoing comport and receive the serial data being sent by the UART.

    My problem is I can’t get java to find that comport. I have tried a number of java programs that search for comports. None of them seem able to find the Outgoing port COM10. They all can find COM11 and other comports. But COM10 seems to be where the data I want is arriving.

    Any ideas?

    • November 14, 2016 at 2:06 pm

      Hi, My apologies for not replying on time. Hope you have found the solution 🙂 it has been long long time ago.

  5. Bryan
    November 14, 2016 at 9:17 am

    Many thanks!!
    Please consider modifying syntax errors on some points such as “while ((len=filein.read(buf))>0 ” in the server code and “while ((len=sis.read(buf))>0 ” in the client code.
    Also, the part of opening the src/test.xml file in the server code needs to be removed if the file is not important.

    Except those issues, this example helped me a lot! 🙂

    • November 14, 2016 at 2:02 pm

      Hi Bryan, wow Im glad after many years. It was finally worth writing it up here.
      Thank you for notifying the mistake. I always like to learn and know my mistakes 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: