我的EV3和我的安卓设备之间的连接有一些问题。我可以用我的EV3接收数据,但不能发送。如果你想看这个应用程序,我已经注册了我的Github帐户。
以下是我的代码:
public class Bluetooth {
public static int speed = 100;
public static BTConnector connector;
public static NXTConnection connection;
public static void main(String[] args) throws IOException {
openConnection();
Thread moveWithBluetoothThread = new Thread(new moveWithBluetooth());
moveWithBluetoothThread.start();
}
public static void openConnection() {
connector = new BTConnector();
LCD.drawString("Waiting for Connecrion", 3, 1);
connection = connector.waitForConnection(0, NXTConnection.RAW);
LCD.clear();
LCD.drawString("Connected", 3, 5);
}
}
class moveWithBluetooth implements Runnable {
@Override
public void run() {
while (true) {
InputStream is = Bluetooth.connection.openInputStream();
BufferedReader dis = new BufferedReader(new InputStreamReader(is), 1);
OutputStream os = Bluetooth.connection.openOutputStream();
BufferedWriter dos = new BufferedWriter(new OutputStreamWriter(os), 1);
try {
byte[] b;
for (int i = 0; i < 100; i++) {
String n = dis.readLine();
System.out.println(n);
b = n.getBytes();
Bluetooth.connection.write(b, b.length);
Thread.sleep(10);
dos.write(n);
dos.flush();
}
dis.close();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}发布于 2020-01-15 19:49:13
我发现,我的EV3没有问题。这意味着,您可以使用以下代码。如果你有同样的问题,这是我的解决方案:
问题出在我在应用程序中使用的库。在Github的"Tssues"-Tab是我的问题。库会等待,直到有一个完整的传输,但EV3会发送一个永久的流,所以我只能在我的传输的末尾添加\r\n。我的最终代码是这样的:
byte[] b = (n + "\r\n").getBytes();
Bluetooth.connection.write(b, b.length);https://stackoverflow.com/questions/59456889
复制相似问题