我正在尝试为我的计算机制作一个充当无线鼠标的 android 应用程序。我决定将其用于蓝牙和 Wifi(用户选择)。Wifi 工作得令人难以置信,但蓝牙滞后很多。我发送的只是一堆 2 字节数组,所以我不明白为什么它工作得这么慢。这是我的发送和接收代码(发送在单独的线程上)
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();
}
}
}
接收代码:
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();
}
}