0

For the project I am working on I have a Raspberry Pi running an OBEX listening service that allows the device to distribute files from internal storage when a client (currently another Raspberry Pi) asks for a file that exists within a fixed "share" folder. I am working on getting this functionality to work as an android companion program. However, I am finding many resources on implementing sending a file from the android device to another device but can't really find much regarding how to get a file from a host.

At this point the functionality I am looking for is to have the program just send a request to a hard-coded Bluetooth MAC address "XX-XX-XX-XX-XX-XX" asking for a hard-coded file "test.txt" and placing this file in the downloads folder of the Android device with an accompanying toast message to indicate the transfer has completed.

For clarity the Raspberry Pi is running the ObexPushd as the listening program in case it is of pertinence.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
CJ Cahala
  • 13
  • 4

1 Answers1

0

Maybe this will help you. (Use javax.OBEX library)

private void GetFileViaBTFTP(UUID FTPUUID)
{
    try
    {
        mBtSocket = mBtDevice.createRfcommSocketToServiceRecord(FTPUUID);
    }
    catch (Exception e)
    {

        //e.printStackTrace();
    }

    Thread thread=new Thread(new Runnable() {
            public void run()
            {
                UUID uuid=UUID.fromString("F9EC7BC4-953C-11D2-984E-525400DC9E09");
                ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
                bb.putLong(uuid.getMostSignificantBits());
                bb.putLong(uuid.getLeastSignificantBits());
                byte [] bytes=bb.array();
                Operation putOperation=null;
                Operation getOperation=null;
                try
                {
                    // connect
                    mBtSocket.connect();
                    mSession = new ClientSession((ObexTransport)(mTransport = new BluetoothObexTransport(mBtSocket)));

                    HeaderSet headerset = new HeaderSet();
                    headerset.setHeader(HeaderSet.TARGET, bytes);

                    headerset = mSession.connect(headerset);

                    if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK)
                    {
                        mConnected = true;
                    }
                    else
                    {
                        mSession.disconnect(headerset);
                    }
/*
                    //In order to go the desired folder the OBEX SETPATH command is 
                    //being used 
                    //Prepare the header for the SETPATH command
                    HeaderSet header = new HeaderSet(); 
                    //folder_name is set to the name of the desired folder 
                    //if left blank the root folder will be used 
                    //header.setHeader(HeaderSet.NAME, ""); 
                    //Send the SETPATH command 
*/
                    /*result =*//* mSession.setPath(header, false, false); 
                    final HeaderSet geths = new HeaderSet();
                    //geths.setHeader(HeaderSet.NAME, null);
                    geths.setHeader(HeaderSet.TYPE, "x-obex/folder-listing");
                    //hs.setHeader(HeaderSet.LENGTH, new Long((long)filebytes.length));

                    getOperation = mSession.get(geths);
                    InputStreamReader din = new 
                        InputStreamReader(getOperation.openInputStream(), "UTF-8"); 

                    BufferedReader bufferedReader = new BufferedReader(din); 
                    String tmp2=new String();
                    String line = bufferedReader.readLine(); 
                    while (line != null)
                    { 
                        tmp2 += line;//System.out.println(line); 
                        line = bufferedReader.readLine(); 
                    } 
                    bufferedReader.close(); 
                    getOperation.close();  
*/
                    header=new HeaderSet();
                    header.setHeader(HeaderSet.NAME, "text.txt"); 
                    getOperation = mSession.get(header);
                    //InputStreamReader din = new InputStreamReader(getOperation.openInputStream(), "UTF-8"); 
                     // Retrieve the length of the object being sent back
                     int length = (int) getOperation.getLength();
                     // Create space for the object
                     byte[] obj = new byte[length];
                     // Get the object from the input stream
                     DataInputStream in = getOperation.openDataInputStream();
                     in.read(obj);
                     // End the transaction
                     in.close();    
                     String str=new String(obj);            
                }
                catch (Exception e)
                {
                    //e.printStackTrace();
                }
                finally
                {
                    try
                    {
                        mOutput.close();
                        putOperation.close();
                        mSession.disconnect(null);
                    }
                    catch (IOException e)
                    {}
                    //updateStatus("[CLIENT] Connection Closed");
                }

            }
        });
    thread.start();
}

I found some original codes from a site( I'll link it if I find again) and modified for your purpose.

Also refer to here and here to learn more about OBEX operations.

KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30