当某些条件触发,都要做了采用Android蓝牙API自动蓝牙传输文件? [英] When some conditions triggers, have to do automatic file transfer over bluetooth using Android Bluetooth API?

查看:119
本文介绍了当某些条件触发,都要做了采用Android蓝牙API自动蓝牙传输文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想通过蓝牙文件或文本发送至其他蓝牙设备(接收设备可能是安卓,诺基亚,LG等)。我想只要发送断绝返回数据的文件。例如,如果气候水平低于任何特定的给定值正在检查的天气。它自动,需要通过蓝牙将数据发送到接收器装置。它不会允许用户进行发送。
我如何可以使用Android蓝牙API实现它?

In my application i want send a file or text over Bluetooth to another Bluetooth device(receiver device may be android,Nokia,LG,etc..). I want to send a file whenever the sever returns the data. For example am checking weather if the climate level falls below any particular given value. It automatically, need to send data over Bluetooth to the receiver device. It wont allow the user to send. How can I implement it using android Bluetooth API?

和我也需要通过将其转换为字节数组传递通过蓝牙的任何文件。我已经通过蓝牙聊天的例子了。在他们已经给1024的缓冲区大小如果文件大小超过1024字节意味着我应该怎么调。无论我每次要发送的每个字节1024并在接收端进行合并,或其他任何其他更好的方法可用?

And also I need to transfer any file via Bluetooth by converting it to byte array. I have gone through Bluetooth chat example. In that they have given the buffer size of 1024. If the file size more than 1024 byte means how should I transfer. Whether I have to sent each 1024 byte every time and have to merge it at the receiver side or else any other better way is available?

先谢谢了。

推荐答案

据我了解你问三个问题。

As far as I understand you're asking three questions.


  1. 如何发送一个文件,只要服务器返回数据的:你基本上打开到服务器的连接(如 HTTP ,但也可能是任何其它TCP或基于UDP的协议)。然后你听传入的数据;一旦你接收数据,触发任何你想要的操作。这些是一个起点,一些相关的呼叫时您的服务器不使用 HTTP (未经测试,请咨询详细信息和替代的文档):

  1. How to send a file whenever a server returns data: You basically open a connection to the server (e.g. http, but might also be any other TCP or UDP-based protocol). Then you listen for incoming data; once you receive data, you trigger whatever action you want. These are some relevant calls for a starting point when your server is not using http (untested, consult the docs for details and alternatives):

Socket s = new Socket('your.server.com', 47000);
s.connect();
SocketChannel c = s.getChannel();
ByteBuffer buffer = new ByteBuffer(1);
c.read(buffer); // blocks until bytes are available


  • 如何启动蓝牙连接自动的:获取目标设备的 BluetoothDevice类对象,然后连接到它 - 作为BluetoothChat演示。

  • How to initiate a Bluetooth connection automatically: Obtain the target device's BluetoothDevice object, then connect to it - as in the BluetoothChat demo.

    如何发送一个文件/通过超过1024字节蓝牙的:是的,你有你的数据分割成块的发送方和重组其在接收端(头脑派文件大小的实际数据之前,因此接收器知道该文件完成后)。您也可以使用更大的合理字节缓冲区。我使用推荐64 KB的最大块大小:这可以让你重发块没有太多的(时间)成本,并不会占用太多内存

    How to send a file / more than 1024 bytes via Bluetooth: Yes you have to split your data into blocks on the sending side and reassemble them on the receiving side (mind to send the filesize before the actual data, so the receiver knows when the file is complete). You can also use reasonably larger byte buffers. I'd recommand using a maximum block size of 64 Kb: This allows you to resend blocks without too much (time) cost and doesn't consume too much memory.

    作为对3首发,这样的事情可以在发送端的核心(未经测试并没有错误处理,只是为了让这个想法):

    As a starter regarding 3., something like this could the core of the sending side (untested and without error handling, just to give the idea):

    // Send the file size
    OutputStream out = socket.getOutputStream();
    ByteBuffer header = ByteBuffer.allocate(8);
    header.putLong(file.length();
    out.write(header.array());
    
    // Send the file in chunks
    byte buffer[1024];
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    int length = in.read(buffer);
    while (length > 0) {
        out.write(buffer, 0, length);
        if (length < 1024) break;
        length = in.read(buffer, 0, sizeof(buffer));
    };
    

    ...和接收侧

    ... and the receiving side:

    // Receive and unmarshal the file size
    InputStream in = socket.getInputStream();
    ByteBuffer header = ByteBuffer.allocate(8);
    byte buffer[1024];
    in.read(buffer, 0, 8);
    header.put(buffer);
    long filesize = header.getLong();
    long receivedBytes = 0;
    
    // Receive the file
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    int length = in.read(buffer);
    while ((receivedBytes < filesize) && (length > 0)){
        out.write(buffer, 0, length);
        receivedBytes += length;
        length = in.read(buffer);
    }
    if (receivedBytes != filesize) ... // Assure the transfer was successful
    

    这篇关于当某些条件触发,都要做了采用Android蓝牙API自动蓝牙传输文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆