Raspberry Pi和Java应用程序之间的蓝牙数据传输速度缓慢

时间:2013-08-26 02:12:03

标签: java python bluetooth raspberry-pi

使用在线发现的一些示例,我可以使用下面的代码在我的Raspberry Pi和笔记本电脑之间使用蓝牙进行通信。但是,我注意到连接后立即需要很长时间(> 30秒)才能传输数据。最终它似乎加速到合理的时间,但反应似乎不可靠。如果这是正常的,我想知道是否有人可以解释它为什么会发生。如果不正常,任何关于更好的继续方式的建议都将受到赞赏。 感谢。

Java代码

import java.io.*;
import javax.microedition.io.*;
import javax.bluetooth.*;

public class RFCOMMServer {

    public static void main( String args[] ) {

        //display local device address and name
        LocalDevice localDevice;
        try {
            localDevice = LocalDevice.getLocalDevice();
            System.out.println("Address: "+localDevice.getBluetoothAddress());
            System.out.println("Name: "+localDevice.getFriendlyName());
        } catch (BluetoothStateException e1) {
            e1.printStackTrace();
        }
        try {
            startServer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void startServer() throws IOException{
        //Create a UUID
        UUID uuid = new UUID("1101", true);
        //Create the servicve url
        String connectionString = "btspp://localhost:" + uuid +";name=SampleServer";

        //open server url
        StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );

        //Wait for client connection
        System.out.println("\nServer Started. Waiting for clients to connect...");
        StreamConnection conn=streamConnNotifier.acceptAndOpen();

        RemoteDevice dev = RemoteDevice.getRemoteDevice(conn);
        System.out.println("Remote device address: "+dev.getBluetoothAddress());
        System.out.println("Remote device name: "+dev.getFriendlyName(true));

        InputStream is = conn.openInputStream();

        byte buffer[] = new byte[80];

        Boolean closeConnection = false;
        while(!closeConnection){
            try{

                int bytes_read = is.read( buffer );
                String received = new String(buffer, 0, bytes_read);
                if(received.equalsIgnoreCase("test")){
                    OutputStream outStream=conn.openOutputStream();
                    PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
                    pWriter.write("received test");
                    pWriter.flush();    
                    pWriter.close();
                }else
                    System.out.println(received);
            }catch(StringIndexOutOfBoundsException exc){
                System.out.println("Exception");
                conn.close();
                System.exit(0);
            }
        }
        conn.close();
    }
}

Python代码

#!/usr/bin/python2.7
import sys
import bluetooth
from bluetooth import *

name = "SampleServer"
service_matches = bluetooth.find_service(name)

if len(service_matchces) == 0
    print "couldn't find the server"
    sys.exit(0)

first_match = service_matches[0]
port = first_match["port"]
print "port: "+str(port)
name = first_match["name"]
print "name: "+str(name)
host = first_match["host"]
print "host: "+host

print "connecting to \"%s\" in "%s" % (name, host)

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((host, port))
sock.send("test")
data = sock.recv(80)
print "received: ", data
print "connected. type stuff"
while True:
    data = raw_input()
    if len(data) == 0: break
    sock.send(data)
sock.close()

0 个答案:

没有答案