Miniware logo
Articles / FTP Bluetooth profile

FTP Bluetooth profile

This article describes how to use the Bluetooth File Transfer profile (FTP) with Java on a desktop computer. More precisely it shows the code needed to browse a mobile phone folders, create new folders to get or store a new file one the mobile phone. In order this to be achieved a Bluetooth stack needs to be installed that is compatible with JSR 82 and it has support for OBEX. In my case I have used avetana Bluetooth Stuck. Avetana is a Bluetooth stuck that is compatible with JSR 82 and it has support for OBEX, moreover it offers a free version for Linux. The code has been tested on a Nokia 6230i mobile phone.

File Transfer is based on  OBEX profile. Both specifications can be found in bluetooth.org (free registration is required). The CONNECT, PUT, GET, SETPATH and DISCONNECT methods are being used with the appropriate headers.

In all cases initially a connection to the remote device must be established, a ClientSession class has to be created and its connect method should be used by setting the TARGET field of its header to the UUID of the Folder Browsing UUID which is always F9EC7BC4-953C-11D2-984E-525400DC9E09. Then for each case - browse a folder, read a file, write a file - different actions should be taken

In order to browse the folders that exist in our mobile phone, the following steps should be followed:

  • Connect to the phone
  • Go the folder whose contents we want to browse by using SETPATH
  • Retrieve the contents of the folder by using the GET method by setting the TYPE field of the header to "x-obex/folder-listing". The result will be an XML file

The following code demonstrates how to browse the contents of a folder in a mobile phone memory:

try{
    String btURL = "btgoep://0013FD94B84F:10;authenticate=false;encrypt=false;master=false";
    //The above is the url of the OBEX File Transfer service where:
    //0013FD94B84F = the Bluetooth address of the device
    //10 = the channel of the service

    String folder = "Nikos";
    //The above is the name of the folder which we want to browse
    //if set to "" it shows the contents of the root folder

    ClientSession conn = (ClientSession) Connector.open(btURL);
    //Target Header must be set to the Folder Browsing UUID:
    //F9EC7BC4-953C-11D2-984E-525400DC9E09.

    byte[] FBUiid = {(byte)0xF9,(byte)0xEC,(byte)0x7B,(byte)0xC4,(byte)0x95,(byte)0x3C,(byte)0x11,(byte)0xD2,
    (byte)0x98,(byte)0x4E,(byte)0x52,(byte)0x54,(byte)0x00,(byte)0xDC,(byte)0x9E,(byte)0x09};
    //Connect
    HeaderSet header = conn.createHeaderSet();
    header.setHeader(HeaderSet.TARGET, FBUiid);
    HeaderSet response = conn.connect(header);
    //Go the desired folder
    header = conn.createHeaderSet();
    header.setHeader(HeaderSet.NAME, folder);
    HeaderSet result = conn.setPath(header, false, false);//if the third option is set to true
    //the folder if not exist it is created
    header.setHeader(HeaderSet.NAME, "test"); //open the folder
    result = conn.setPath(header, false, true);
    //Retreive the contents of the folder
    header = conn.createHeaderSet();
    header.setHeader(HeaderSet.TYPE, "x-obex/folder-listing");
    Operation op = conn.get(header);
    BufferedReader br = new BufferedReader(new InputStreamReader(op.openInputStream()));
    String line;
    String xmlString = "";
    while ((line = br.readLine()) != null) {
        xmlString = xmlString + line + "\n";
    }
    br.close();
    op.close();
    conn.disconnect(conn.createHeaderSet());
    conn.close();
    System.out.println("Obex XML String:\n" + xmlString);
    }catch(Exception e){
        System.out.println("Exception " + e.toString());
    }

The result will be similar to the below string

<?xml version="1.0"?>
<!DOCTYPE folder-listing SYSTEM "obex-folder-listing.dtd"
[ <!ATTLIST folder mem-type CDATA #IMPLIED> ]>
    <folder-listing version="1.0">
        <parent-folder />
        <file name="A.jpg" size="53530" modified="20070101T124332" user-perm="RWD"/>
        <folder name="test" created="20070331T180602" user-perm="RWD" mem-type="MMC"/>
        <file name="video1.3gp" size="693052" modified="20070330T211802" user-perm="RWD"/>
        <file name="17.jpg" size="35557" modified="20070218T183626" user-perm="RWD"/>
        <file name="video2.3gp" size="709196" modified="20070330T212214" user-perm="RWD"/>
        <file name="video3.3gp" size="694920" modified="20070330T212600" user-perm="RWD"/>
    </folder-listing>
 

In order to read a file stored in our mobile phone and store it in our local disk, the following steps should be followed:

  • Connect to the phone
  • Go the folder where the file located by using SETPATH
  • Retrieve the file by using the GET method by setting the NAME filed of the header to the file name


try{
    String btURL = "btgoep://0013FD94B84F:10;authenticate=false;encrypt=false;master=false";
    //The above is the url of the OBEX File Transfer service where:
    //0013FD94B84F = the Bluetooth address of the device
    //10 = the channel of the service

    String folder = "Nikos";
    //The above is the name of the folder in which the file is located
    //If the file is loated in the root folder "" is used

    String file = "17.jpg"; //The name of the file we want to get
    ClientSession conn = (ClientSession) Connector.open(btURL);
    //Target Header must be set to the Folder Browsing UUID:
    //F9EC7BC4-953C-11D2-984E-525400DC9E09.

    byte[] FBUiid = {(byte)0xF9,(byte)0xEC,(byte)0x7B,(byte)0xC4,(byte)0x95,(byte)0x3C,(byte)0x11,(byte)0xD2,
    (byte)0x98,(byte)0x4E,(byte)0x52,(byte)0x54,(byte)0x00,(byte)0xDC,(byte)0x9E,(byte)0x09};
    //Connect
    HeaderSet header = conn.createHeaderSet();
    header.setHeader(HeaderSet.TARGET, FBUiid);
    HeaderSet response = conn.connect(header);
    //Go the desired folder
    header = conn.createHeaderSet();
    header.setHeader(HeaderSet.NAME, folder); //open the folder
    HeaderSet result = conn.setPath(header, false, false);//if the third option is set to true
    //the folder if not exist it is created
    //Retreive the file

    header = conn.createHeaderSet();
    header.setHeader(HeaderSet.NAME, file);
    Operation op = conn.get(header);
    InputStream is = op.openInputStream();
    File f = new File(file);
    FileOutputStream fos = new FileOutputStream (f);
    byte b[] = new byte[1000];
    int len;
    while (is.available() > 0 && (len = is.read(b)) > 0) {
        fos.write (b, 0, len);
    }
    fos.close();
    is.close();
    System.out.println ("File stored in: " + f.getAbsolutePath());
    conn.disconnect(conn.createHeaderSet());
    conn.close();
    }catch(Exception e){
        System.out.println("Exception " + e.toString());
    }


 

In order to store file stored in our local disk to our mobile phone, the following steps should be followed:

  • Connect to the phone
  • Go the folder where we want to store the file
  • Store the file by using the PUT method by setting the NAME filed of the header to the file name


try{
    String btURL = "btgoep://0013FD94B84F:10;authenticate=false;encrypt=false;master=false";
    //The above is the url of the OBEX File Transfer service where:
    //0013FD94B84F = the Bluetooth address of the device
    //10 = the channel of the service

    String folder = "Nikos";
    //The above is the name of the folder in which the file will be stored
    //if we want to store the file in the root folder "" is used

    String file = "/home/nikos/id.jpg"; //The name of the file we want to store
    //Target Header must be set to the Folder Browsing UUID:
    //F9EC7BC4-953C-11D2-984E-525400DC9E09.

    byte[] FBUiid = {(byte)0xF9,(byte)0xEC,(byte)0x7B,(byte)0xC4,(byte)0x95,(byte)0x3C,(byte)0x11,(byte)0xD2,
    (byte)0x98,(byte)0x4E,(byte)0x52,(byte)0x54,(byte)0x00,(byte)0xDC,(byte)0x9E,(byte)0x09};
    //Connect
    HeaderSet header = conn.createHeaderSet();
    header.setHeader(HeaderSet.TARGET, FBUiid);
    HeaderSet response = conn.connect(header);
    //Go to the desired folder
    header = conn.createHeaderSet();
    header.setHeader(HeaderSet.NAME,folder); //open the folder
    HeaderSet result = conn.setPath(header, false, false);
    //Send the file
    File sendFile = new File (file);
    header = conn.createHeaderSet();
    header.setHeader(HeaderSet.NAME, sendFile.getName());
    Operation op = conn.put(header);
    OutputStream os = op.openOutputStream();
    InputStream is = new FileInputStream (sendFile);
    byte[] b = new byte[400];
    int r;
    while ((r = is.read(b)) > 0) {
        os.write(b, 0, r);
    }
    is.close();
    os.close();
    op.close();
    conn.disconnect(conn.createHeaderSet());
    conn.close();
    System.out.println("finish sending file");
   }catch(Exception e){
     System.out.println("Exception " + e.toString());
   }

 

The content of this page can be reproduced as long as the author and the source are mentioned. For questions please use the forum. Nikos Fotiou
Comments
(Post new comment)

2008-07-17 05:50:23 Shiv wrote:
I have written FTP client and its is able to get folder list from phones like Nokia, Motorolla and doesn't works with Samsung phone. Can someone tell me what could be the possible problems when accessing folder listing from Samsung phone. I observe that CONNECT, SET_PATH will work fine with Samsung, but on PULL request for object "x-obex/folder-listing" system hangs. Thanks and Regards, Shiv.


2008-05-12 06:24:41 Soham Sengupta wrote:
In some of the Mobiles, the OBEX_HTTP_FORBIDDEN ERROR is confronted with. Plz help.. sohamsengupta@yahoo.com
2008-05-04 04:37:50 Ehsanjs wrote:
hi thanks for your note but wath is ClientSession? in My netbeans6.1 it doesnt exist.
2008-03-07 18:30:53 Manuel - AIRcable.net wrote:
THANKS!!!!!!!! I've been hitting my head too the wall for over 4 hours to get this thing working, until I found your text. I'm going to send a link to the bluecove forums so they can link your text.
2007-11-16 13:15:31 Michaël Degen wrote:
Hello, Thanks for your article, i use your code but i have a problem with the exception java.io.EOFException when the computer send the file. Can you help me please ? Thank you in advance. Michaël Degen