java - Android BluetoothSocket 输出流写入速度太慢

标签 java android bluetooth outputstream bluecove

基本上,我一直在研究使用蓝牙或 wifi 的无线鼠标。我已经完成了所有工作,包括阅读和编写消息。但是,通过蓝牙传输数据的速度太慢,无法弥补。我到处都看了看,但我无法弄清楚是什么导致了这种速度。我正在使用专用线程来执行所有写入操作。

这是我的 ConnectedThread 代码(几乎与 Android SDK 示例中的完全一样)

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

    }
}

最佳答案

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

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

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

关于java - Android BluetoothSocket 输出流写入速度太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553514/

相关文章:

android - Activity 中的后退按钮无需重新创建先前的 Activity (Android)

bluetooth - 在 Ubuntu 命令行上打开蓝牙发现的正确方法

java - xstream:仅解析子元素

java - System.out.printLn() 如何接受整数?

android - 无法从 Activity 中调用 getSupportFragmentManager()

android - 如何在我的应用程序中实现系统闹钟?

java - 我可以在 Java 项目中使用 PhoneGap 吗?

java - 评分栏,如何获得星数?

linux - 根据数据包类型拆分 TTY 设备

android - 在 Android 上通过蓝牙从 InputStream 读取数据时出错