首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EV3 Lejos蓝牙发送和接收数据

EV3 Lejos蓝牙发送和接收数据
EN

Stack Overflow用户
提问于 2019-12-23 22:22:18
回答 1查看 126关注 0票数 0

我的EV3和我的安卓设备之间的连接有一些问题。我可以用我的EV3接收数据,但不能发送。如果你想看这个应用程序,我已经注册了我的Github帐户。

以下是我的代码:

代码语言:javascript
复制
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();
            }
        }    
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-15 19:49:13

我发现,我的EV3没有问题。这意味着,您可以使用以下代码。如果你有同样的问题,这是我的解决方案:

问题出在我在应用程序中使用的库。在Github的"Tssues"-Tab是我的问题。库会等待,直到有一个完整的传输,但EV3会发送一个永久的流,所以我只能在我的传输的末尾添加\r\n。我的最终代码是这样的:

代码语言:javascript
复制
byte[] b = (n + "\r\n").getBytes();
Bluetooth.connection.write(b, b.length);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59456889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档