2017年2月6日
- EV3技術情報
- Java
- ソフトウェア
EV3-Java EV3間のBluetooth通信制御プログラム
Java(leJOS)で作成したプログラムによる、2台のEV3間のBluetooth通信制御プログラムについて紹介します。
※EV3のJava言語プログラミング環境については、「教育版EV3 Javaプログラミングガイド」をご参照ください。
※ここで紹介する内容は、leJOS EV3 バージョン 0.9.1-betaを用いて動作を確認したものです。
◆準備
EV3を2台用意し、メッセージを受信するEV3と、送信するEV3を決めておきます。また、その2台のEV3をペアリングし、受信側EV3のアドレスを確認しておきます。(ペアリングおよびEV3のアドレスの確認方法は、こちらをご覧ください。)
◆プログラム
このプログラムは、EV3からEV3にBluetooth通信でメッセージを送り、それを受信したEV3は、メッセージに対応した色のLEDを点灯します。
プログラムを実行する際は、受信側EV3のプログラムを先に実行します。
◇メッセージ受信側EV3のプログラム
| 1 | import lejos.hardware.Bluetooth; |
|---|---|
| 2 | import lejos.remote.nxt.NXTCommConnector; |
| 3 | import lejos.remote.nxt.NXTConnection; |
| 4 | import lejos.utility.Delay; |
| 5 | import lejos.hardware.lcd.LCD; |
| 6 | import lejos.hardware.Button; |
| 7 | |
| 8 | public class BtsampleEv3_1 { |
| 9 | |
| 10 | public static void main(String[] args) { |
| 11 | // 接続待機 |
| 12 | LCD.drawString("Waiting...",1,1); |
| 13 | NXTCommConnector connector = Bluetooth.getNXTCommConnector(); |
| 14 | NXTConnection connection = |
| 15 | connector.waitForConnection(0, NXTConnection.RAW); |
| 16 | |
| 17 | // メッセージを受信してLED点灯 |
| 18 | // 1:Green 2:Red 3:Orange |
| 19 | int message = getMessage(connection); |
| 20 | LCD.clear(); |
| 21 | if(message != 0){ |
| 22 | LCD.drawString("message is " + message,1,1); |
| 23 | Button.LEDPattern(message); |
| 24 | Delay.msDelay(3000); |
| 25 | } else { |
| 26 | LCD.drawString("error...",1,1); |
| 27 | } |
| 28 | |
| 29 | // 接続解除 |
| 30 | try { |
| 31 | if (null != connection) { |
| 32 | connection.close(); |
| 33 | } |
| 34 | } catch (Exception e) { |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static int getMessage(NXTConnection connection) { |
| 39 | byte[] buff = new byte[16]; |
| 40 | |
| 41 | //メッセージを受信するまで待つ |
| 42 | while(buff[0] == 0){ |
| 43 | connection.read(buff, buff.length); |
| 44 | } |
| 45 | |
| 46 | //メッセージを確認 |
| 47 | for (int i = 0; i < buff.length; i++) { |
| 48 | if (0 <= buff[i] && buff[i] <= 3) { |
| 49 | return buff[i]; |
| 50 | } |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | } |
プログラムを実行すると、受信側EV3は送信側EV3から接続されるのを待ちます。(14行目)
接続後メッセージを受信したら、そのメッセージの内容を確認します。(19行目)
メッセージは「1~3」の範囲の数値です。
受信側EV3は、メッセージの値に対応した色でLEDを点灯します。(23行目)
◇メッセージ送信側EV3のプログラム
| 1 | import lejos.hardware.Bluetooth; |
|---|---|
| 2 | import lejos.remote.nxt.NXTCommConnector; |
| 3 | import lejos.remote.nxt.NXTConnection; |
| 4 | import lejos.utility.Delay; |
| 5 | import lejos.hardware.lcd.LCD; |
| 6 | import lejos.hardware.Button; |
| 7 | |
| 8 | public class BtsampleEv3_2 { |
| 9 | |
| 10 | public static void main(String[] args) { |
| 11 | String ev3add = "00:00:00:00:00:00"; // 接続先EV3アドレス |
| 12 | |
| 13 | // 接続 |
| 14 | LCD.drawString("Connecting...",1,1); |
| 15 | NXTCommConnector connector = Bluetooth.getNXTCommConnector(); |
| 16 | NXTConnection connection = |
| 17 | connector.connect(ev3add, NXTConnection.RAW); |
| 18 | if (connection == null) { |
| 19 | LCD.clear(); |
| 20 | LCD.drawString("Connect error",1,1); |
| 21 | Button.waitForAnyPress(); |
| 22 | System.exit(1); |
| 23 | } |
| 24 | |
| 25 | // 接続成功 |
| 26 | LCD.clear(); |
| 27 | LCD.drawString("Connected!",1,1); |
| 28 | Delay.msDelay(1000); |
| 29 | |
| 30 | // 1~3のランダムな数値メッセージを送信 |
| 31 | LCD.clear(); |
| 32 | LCD.drawString("Sending...",1,1); |
| 33 | int message = (int)(Math.random()*4); |
| 34 | sendMessage(connection, message); |
| 35 | |
| 36 | // 送ったメッセージをLCDに表示、いずれかのボタンが押されるまで待機 |
| 37 | LCD.clear(); |
| 38 | LCD.drawString("Send:" + message,1,1); |
| 39 | Button.waitForAnyPress(); |
| 40 | |
| 41 | // 接続解除 |
| 42 | try { |
| 43 | if (null != connection) { |
| 44 | connection.close(); |
| 45 | } |
| 46 | } catch (Exception e) { |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void sendMessage(NXTConnection connection, int message) { |
| 51 | // メッセージをバッファに設定 |
| 52 | byte[] buff = new byte[16]; |
| 53 | for (int i = 0; i < buff.length; i++) { |
| 54 | buff[i] = (byte) message; |
| 55 | } |
| 56 | |
| 57 | // メッセージを送信 |
| 58 | connection.write(buff, buff.length); |
| 59 | } |
| 60 | } |
プログラムの11行目「"00:00:00:00:00:00"」には、ペアリングの際に確認した受信側EV3のアドレスを設定します。
プログラムを実行すると、送信側EV3は受信側EV3に接続します。(16行目)
接続が完了したら、1~3のいずれかの数値をメッセージとして送信します。(33・34行目)
その後、送信したメッセージの値をLCDに表示します。(38行目)
LCDに表示されたメッセージを確認し、インテリジェントブロックのいずれかのボタンを押すと、受信側EV3との接続を解除します。(39行目~)
- Java(leJOS)で作成したプログラムによるEV3間のBluetooth通信制御の一例として紹介しています。
プログラムは動作確認済みですが、誤り等が含まれていた場合はご了承ください。
