使用蓝牙FTP从已知设备获取文件

问题描述 投票:0回答:1

对于我正在开发的项目,我有一个运行OBEX监听服务的Raspberry Pi,当客户端(当前是另一个Raspberry Pi)要求存在于固定“share”文件夹中的文件时,该服务允许设备从内部存储器分发文件。我正在努力使这个功能作为Android伴侣程序工作。但是,我发现了很多关于实现从Android设备向另一台设备发送文件的资源,但是对于如何从主机获取文件却无法真正找到。

此时,我正在寻找的功能是让程序只向硬编码的蓝牙MAC地址“XX-XX-XX-XX-XX-XX”发送请求,要求输入硬编码文件“test.txt “并将此文件放在Android设备的下载文件夹中,并附带一个toast消息,表明传输已完成。

为清楚起见,Raspberry Pi正在运行ObexPushd作为监听程序,以防它具有针对性。

android bluetooth ftp
1个回答
0
投票

也许这会对你有所帮助。 (使用javax.OBEX库)

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();
}

我从网站上找到了一些原始代码(如果我再找到,我会将其链接)并根据您的目的进行修改。

另请参阅herehere以了解有关OBEX操作的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.