Java Android BluetoothSocket outputstream写入速度太慢

Java Android BluetoothSocket outputstream写入速度太慢,java,android,bluetooth,outputstream,bluecove,Java,Android,Bluetooth,Outputstream,Bluecove,基本上,我一直在开发一款利用蓝牙或wifi的无线鼠标。我已经把一切都做好了,包括读和写信息。但是,通过蓝牙进行数据传输的速度太慢,无法进行补偿。我找遍了所有的地方,我不知道是什么导致了这种速度。我使用一个专用线程来完成所有的写操作 这是我的ConnectedThread代码(与Android SDK示例几乎完全相同) 这是我的服务器代码(接收消息) 有人问我答案,就在这里。出于完全的巧合(我猜是无聊),我决定修复Android Studio给我的所有警告 其中一个警告表明我正在onDraw函数中

基本上,我一直在开发一款利用蓝牙或wifi的无线鼠标。我已经把一切都做好了,包括读和写信息。但是,通过蓝牙进行数据传输的速度太慢,无法进行补偿。我找遍了所有的地方,我不知道是什么导致了这种速度。我使用一个专用线程来完成所有的写操作

这是我的ConnectedThread代码(与Android SDK示例几乎完全相同)

这是我的服务器代码(接收消息)


有人问我答案,就在这里。出于完全的巧合(我猜是无聊),我决定修复Android Studio给我的所有警告

其中一个警告表明我正在onDraw函数中实例化新对象。事实证明,蓝牙并不慢,但事实上,onDraw花了很长时间才延迟了新消息的传输,使其看起来似乎存在一致的延迟


tl;dr:不要在onDraw函数中实例化新对象。

您的服务器代码无效。您应该使用
BufferedInputStream,
而不是
BufferedReader
,并且您不能假设每个
read()
都填满了缓冲区。感谢您的快速响应!BufferedReader就在那里,因为我正在测试一些东西,但实际上我没有使用它!EJP,我将其更改为BufferedInputStream,它看起来快了一点(我必须添加时间来确保),但仍然滞后。你会建议我怎么做?如果你找到了解决方案,请分享你的解决方案
package com.tutorials.jurko.androidmouse;

import android.bluetooth.BluetoothSocket;
import android.net.ConnectivityManager;

import java.io.BufferedOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Jurko on 14/02/2015.
 */
public class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final BufferedOutputStream mmOutStream;
    public static int count = 0;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            tmpIn = mmSocket.getInputStream();
            tmpOut = mmSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mmInStream = tmpIn;
        mmOutStream = new BufferedOutputStream(tmpOut);
    }

    public void write(Byte[] bytes) {
        count++;
        try {
            byte x = bytes[0].byteValue();
            byte y = bytes[1].byteValue();
            System.out.println("Count: " + count);
            byte buf[] = {x, y};
            mmOutStream.write(buf);
            mmOutStream.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import java.awt.*;
import java.awt.event.InputEvent;
import java.io.*;

/**
 * Class that implements an SPP Server which accepts single line of
 * message from an SPP client and sends a single line of response to the client.
 */
public class SimpleSPPServer  {

    //start server
    private void startServer() throws IOException, AWTException {
        Robot r = new Robot();
        //Create a UUID for SPP
        UUID uuid = new UUID("1101", true);
        //Create the servicve url
        String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";

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

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

        StreamConnection connection=streamConnNotifier.acceptAndOpen();

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

        //read string from spp client
        InputStream inStream=connection.openInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
        byte[] lineRead = new byte[2];

        while(inStream.read(lineRead) != -1)  {
            System.out.println(lineRead[0] + " " + lineRead[1]);
// Code to control mouse here

        }


        //send response to spp client
        OutputStream outStream=connection.openOutputStream();
        PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
        pWriter.write("Response String from SPP Server\r\n");
        pWriter.flush();

        pWriter.close();
        streamConnNotifier.close();

    }


    public static void main(String[] args) throws IOException, AWTException {

        //display local device address and name
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        System.out.println("Address: "+localDevice.getBluetoothAddress());
        System.out.println("Name: "+localDevice.getFriendlyName());

        SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
        sampleSPPServer.startServer();

    }
}