3

Basically, I've been working on a wireless mouse that utilizes bluetooth OR wifi. I've gotten everything working including reading and writing messages. However, the rate at which data transfers through bluetooth is too slow to compensate. I've looked all over the place and I can't figure out what is causing this speed. I am using a dedicated thread to do all the writing operations on.

Here is my ConnectedThread code (almost exactly like in the Android SDK example)

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

Here is my server code (Receiving messages)

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

    }
}
Jurko Guba
  • 630
  • 7
  • 17
  • 1
    Your server code isn't valid. You should use a `BufferedInputStream,` not a `BufferedReader`, and you can't assume that every `read()` fills the buffer. – user207421 Feb 17 '15 at 03:10
  • Thanks for a quick response! That BufferedReader is there because I was testing some stuff out but I don't actually use it! – Jurko Guba Feb 17 '15 at 03:16
  • EJP, I changed it to the BufferedInputStream and it seems a little bit quicker (I'll have to add timing to make sure) but it still lags. What would you recommend I do? – Jurko Guba Feb 17 '15 at 05:04
  • please share your solution if you had found a fix – William Ku Mar 08 '16 at 05:39

1 Answers1

0

Someone asked for an answer, so here it is. Out of complete coincidence (and boredom I guess), I decided to fix ALL of the warnings Android Studio was giving me.

One of the warnings indicated that I was instantiating new objects in the onDraw function. Turns out that the bluetooth was not slow, but in fact the onDraw took so long it delayed the transmission of new messages, making it appear as if a consistent lag was present.

tl;dr: Don't instantiate new objects in your onDraw function.

Jurko Guba
  • 630
  • 7
  • 17