Выходной поток Android BluetoothSocket пишет слишком медленно

По сути, я работал над беспроводной мышью, которая использует Bluetooth или Wi-Fi. У меня все работает, включая чтение и написание сообщений. Однако скорость передачи данных через Bluetooth слишком мала для компенсации. Я посмотрел повсюду, и я не могу понять, что вызывает эту скорость. Я использую выделенный поток для выполнения всех операций записи.

Вот мой код 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();

    }
}

person Jurko Guba    schedule 17.02.2015    source источник
comment
Ваш код сервера недействителен. Вы должны использовать BufferedInputStream,, а не BufferedReader, и вы не можете предполагать, что каждый read() заполняет буфер.   -  person user207421    schedule 17.02.2015
comment
Спасибо за быстрый ответ! Этот BufferedReader существует, потому что я кое-что тестировал, но на самом деле я его не использую!   -  person Jurko Guba    schedule 17.02.2015
comment
EJP, я изменил его на BufferedInputStream, и он кажется немного быстрее (мне нужно добавить время, чтобы убедиться), но он все еще отстает. Что бы вы порекомендовали мне сделать?   -  person Jurko Guba    schedule 17.02.2015
comment
пожалуйста, поделитесь своим решением, если вы нашли исправление   -  person William Ku    schedule 08.03.2016


Ответы (1)


Кто-то просил ответа, так что вот он. По полному совпадению (и, наверное, от скуки) я решил исправить ВСЕ предупреждения, которые мне давала Android Studio.

Одно из предупреждений указывало на то, что я создавал экземпляры новых объектов в функции onDraw. Оказывается, Bluetooth не был медленным, но на самом деле onDraw занимал так много времени, что задерживал передачу новых сообщений, создавая впечатление постоянной задержки.

Вкратце: не создавайте экземпляры новых объектов в вашей функции onDraw.

person Jurko Guba    schedule 30.03.2017