我正在尝试制作一款能为我的电脑配备无线鼠标的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();
}
}
答案 0 :(得分:0)
我的猜测是你的代码导致蓝牙扫描或其他一些繁重的BT操作。例如:
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
我相信这三行会导致BT扫描。由于它只是调试我建议你删除它们,看看是否有帮助。