LEGO NXT のセンサーを読み取るように Minddroid github Android プログラムを変更しました (素晴らしいデバイスです!)。次に、NXT で実行されている Mindstorms プログラムに Bluetooth メッセージを読み書きしたいと思います。Androidが要求したときにNXTプログラムを実行し、結果/読み取り値をAndroidに送信できるようにします。
2 に答える
NXT がデータを Android デバイスに送り返すプロジェクトを作成しました。動作するはずのコードを次に示します。
これはすべての Android 側のコードです。
これは私が作成したクラスで、Bluetooth を介した接続と通信を処理します。
public class Connector {
public static final String TAG = "Connector";
public static final boolean BT_ON = true;
public static final boolean BT_OFF = false;
public BluetoothAdapter bluetoothAdapter;
public BluetoothSocket bluetoothSocket;
public String address;
public Connector(String address) {
this.address = address;
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
public void setBluetooth(boolean state) {
if(state == Connector.BT_ON) {
// Check if bluetooth is off
if(this.bluetoothAdapter.isEnabled() == false)
{
this.bluetoothAdapter.enable();
while(this.bluetoothAdapter.isEnabled() == false) {
}
Log.d(Connector.TAG, "Bluetooth turned on");
}
}
// Check if bluetooth is enabled
else if(state == Connector.BT_OFF) {
// Check if bluetooth is enabled
if(this.bluetoothAdapter.isEnabled() == true)
{
this.bluetoothAdapter.disable();
while(this.bluetoothAdapter.isEnabled() == true) {
}
Log.d(Connector.TAG, "Bluetooth turned off");
}
}
}
public boolean connect() {
boolean connected = false;
BluetoothDevice nxt = this.bluetoothAdapter.getRemoteDevice(this.address);
try {
this.bluetoothSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
this.bluetoothSocket.connect();
connected = true;
}
catch (IOException e) {
connected = false;
}
return connected;
}
public Integer readMessage() {
Integer message;
if(this.bluetoothSocket!= null) {
try {
InputStreamReader input = new InputStreamReader(this.bluetoothSocket.getInputStream());
message = input.read();
Log.d(Connector.TAG, "Successfully read message");
}
catch (IOException e) {
message = null;
Log.d(Connector.TAG, "Couldn't read message");
}
}
else {
message = null;
Log.d(Connector.TAG, "Couldn't read message");
}
return message;
}
}
アクティビティ クラスでは、オブジェクトを作成できConnector
ます。onCreate() メソッドでは、次のように NXT への接続を確立するために接続する必要があります。
// Establish a bluetooth connection to the NXT
this.connector = new Connector("00:16:53:12:B6:78");
this.connector.setBluetooth(Connector.BT_ON);
this.connector.connect();
NXT (整数オブジェクト) からメッセージを読み取るには、次のようにします。
this.connector.readMessage();
接続を閉じるには:
this.connector.setBluetooth(Connector.BT_OFF);
これはすべての NXT 側のコードです。
注:すべてのコードが機能するようにleJOSをダウンロードします (leJOS を使用すると、Java で NXT をコーディングできます)。
メイン クラスで次の 2 つのオブジェクトを定義します。
public static DataOutputStream dataOutputStream;
public static NXTConnection bluetoothConnection;
電話に接続するには:
bluetoothConnection = Bluetooth.waitForConnection();
bluetoothConnection.setIOMode(NXTConnection.RAW);
dataOutputStream = bluetoothConnection.openDataOutputStream();
データを整数オブジェクトの形式で電話に送信するには:
dataOutputStream.write(100);
dataOutputStream.flush();
切断するには、これを実行します。
dataOutputStream.close();
bluetoothConnection.close();
これが役立つことを願っています。
Bluetooth コマンドについて少し混乱していましたが、leJOS をダウンロードする必要があることがわかりました。
私は通常、NXT のファームウェアをいじらないようにしていますが、Java の方がはるかに扱いやすいです。
興味のある方は、Android から NXT にネイティブ形式でコマンドを送信できますが、上記のようにはきれいではありません。ここにすばらしいチュートリアルがあります: http://www.robotappstore.com/Knowledge-Base/Programming-LEGO-NXT-Mindstorms/92.html
しかし、アプリを無料でダウンロードしたい場合は、ここにあります: http://www.robotappstore.com/Apps/Lego-NXT-Mindstorms-Driver---Android-app.html?x=693A00AA-7F15-46E7 -9616-8101068DB58D
そこも検索すれば他にもたくさんあります
お役に立てれば!