LoginSignup
4
5

More than 5 years have passed since last update.

leJOSによるEV3同士のBluetooth通信

Last updated at Posted at 2016-06-14

NXTのプログラムをもとに、EV3用に作成しました。
デバイス間で事前にペアリングが済んでいること。
サーバー側のプログラムを先に起動してから、クライアント側を起動すること。

サーバー側

BluetoothServer.java
import lejos.hardware.Bluetooth;
import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.hardware.lcd.LCD;
import lejos.remote.nxt.NXTCommConnector;
import lejos.remote.nxt.NXTConnection;

public class BluetoothServer {

    public static void main(String[] args) {
        int recvMessageNo = 2;  //受信する番号
        int sendMessageNo = 3;  //送信する番号

        System.out.println("Waiting...");

        //送信先からの接続を待つ
        NXTCommConnector connector = Bluetooth.getNXTCommConnector();
        NXTConnection connection = connector.waitForConnection(0, NXTConnection.RAW);

        System.out.println("Connected");

        //受信番号が正しければ音を鳴らす
        if(isReceived(connection, recvMessageNo)){
            Sound.systemSound(false, 2);
        } else {
            Sound.buzz();
        }

        // メッセージを送信する
        send(connection, sendMessageNo);

        //通信を切断する
        System.out.println("Closing...");
        try {
            if (null != connection) {
                connection.close();
            }
        } catch (Exception ioe) {
        }

        System.out.println("Finished");

        Button.waitForAnyPress();
    }

    static boolean isReceived(NXTConnection connection, int messageNo) {
        byte[] recvBuff = new byte[16];

        //メッセージを受信するか、ESCAPEボタンが押されるまで待つ
        while(recvBuff[0] == 0 && Button.ESCAPE.isUp()){
            connection.read(recvBuff, recvBuff.length);
        }

        //メッセージが正しいかチェックする
        for (int i = 0; i < recvBuff.length; i++) {
            System.out.print(recvBuff[i]);
            if (recvBuff[i] != messageNo) {
                System.out.println();
                return false;
            }
        }
        System.out.println();
        return true;
    }

    static void send(NXTConnection connection, int messageNo) {
        byte[] sendBuff = new byte[16];
        for (int i = 0; i < sendBuff.length; i++) {
            sendBuff[i] = (byte)messageNo;
        }
        connection.write(sendBuff, sendBuff.length);
    }

}

クライアント側

BluetoothClient.java
package ev3;

import lejos.hardware.Bluetooth;
import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.remote.nxt.NXTCommConnector;
import lejos.remote.nxt.NXTConnection;

public class BluetoothClient {
    public static void main(String[] args) {
        int sendMessageNo = 2; // 送信する番号
        int recvMessageNo = 3; // 受信する番号

        String server = "00:16:53:99:99:99"; // サーバ側デバイスのMACアドレス

        System.out.println("Connecting...");

        // サーバー側のデバイスからコネクションを取得する
        NXTCommConnector connector = Bluetooth.getNXTCommConnector();
        NXTConnection connection = connector.connect(server, NXTConnection.RAW);
        if (connection == null) {
            System.out.println("Connect fail");
            Button.waitForAnyPress();
            System.exit(1);
        }
        System.out.println("Connected");

        // メッセージを送信する
        System.out.println("Sending...");
        send(connection, sendMessageNo);
        Sound.systemSound(false, 3);

        // メッセージを受信する
        System.out.println("Receivng...");
        isReceived(connection, recvMessageNo);

        // 通信の切断を行う
        System.out.println("Closing...");
        try {
            if (null != connection) {
                connection.close();
            }
        } catch (Exception ioe) {
        }

        System.out.println("Finished");

        Button.waitForAnyPress();
    }

    static boolean isReceived(NXTConnection connection, int messageNo) {
        byte[] recvBuff = new byte[16];

        //メッセージを受信するか、ESCAPEボタンが押されるまで待つ
        while(recvBuff[0] == 0 && Button.ESCAPE.isUp()){
            connection.read(recvBuff, recvBuff.length);
        }

        //メッセージが正しいかチェックする
        for (int i = 0; i < recvBuff.length; i++) {
            System.out.print(recvBuff[i]);
            if (recvBuff[i] != messageNo) {
                System.out.println();
                return false;
            }
        }
        System.out.println();
        return true;
    }

    static void send(NXTConnection connection, int messageNo) {
        // メッセージをバッファに設定する
        byte[] sendBuff = new byte[16];
        for (int i = 0; i < sendBuff.length; i++) {
            sendBuff[i] = (byte) messageNo;
        }

        // メッセージを送信する
        connection.write(sendBuff, sendBuff.length);
    }
}

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5