1

I am trying to make an android app that acts a wireless mouse for my computer. I decided to make it for Bluetooth and Wifi (the user chooses.) Wifi works incredibly but bluetooth lags a lot. All I am sending is a bunch of 2 byte arrays so I do not understand why it works so slowly. Here is my send and recieve code (Sending is on a seperate thread)

 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 OutputStream 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 = 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);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Recieving code:

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  {

    byte dx;
    byte dy;

    //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();
        BufferedInputStream bis = new BufferedInputStream(inStream);
        byte[] lineRead = new byte[2];

        while(true)  {
            int available = bis.available();
            if (available > 0) {
                bis.read(lineRead,0,2);

                System.out.println(lineRead[0] + " " + lineRead[1]);

                PointerInfo a = MouseInfo.getPointerInfo();
                Point b = a.getLocation();
                int x = (int)b.getX();
                int y = (int)b.getY();

                dx = lineRead[0];
                dy = lineRead[1];

                int newX = x + dx;
                int newY = y + dy;

                if(dx == -98 && dy == -98) {
                    // Right click
                    r.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

                } else if (dx == -99 && dy == -99) {
                    // Left click
                    r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                } else if (dx == -97 && dy == -97) {
                    // Left click
                    r.mousePress(InputEvent.BUTTON2_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
                } else {
                    for (int i = 0; i < 10; i++) {
                        r.mouseMove(newX + i * dx, newY + i *dy);
                        r.delay(10);
                    }
                }
            }

        }

    }

    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 Answers1

0

My guess is that somewhere along the line your code is causing a bluetooth scan or some other heavy BT operation. For example:

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

I believe these three lines can cause a BT scan to occur. Since it is just debugging I suggest you remove them and see if that helps.

kaylum
  • 13,833
  • 2
  • 22
  • 31