javaandroidbluetoothlego-mindstorms-ev3

EV3 Lejos Bluetooth send and receive data


I have some problems with the connection between my EV3 and my Android-Device. I am able to receive Data with my EV3, but I can't send. I have signed up with my Github account if you like to see the App.

Here is my Code:

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();
            }
        }    
    }
}

Solution

  • I found out, that there was no Problem with my EV3. That means, you can use this code. If you are having the same Problem, here is my solution:

    The Problem was the Library I use in my Application. At the "Tssues"-Tab of Github was my Problem. The library waits until there is a complete Transmission, but the EV3 sends a permanent Stream, so I just had to add \r\n at the end of my Transmission. My final Code looked like that:

    byte[] b = (n + "\r\n").getBytes();
    Bluetooth.connection.write(b, b.length);