Android 和 Lego Mindstorm NXT 之间的蓝牙连接

发布于 2024-10-17 03:15:41 字数 148 浏览 2 评论 0原文

有谁知道如何在 Android 和 LEGO-Mindstorm-NXT 之间建立蓝牙连接? 两个 NXT 之间的连接工作正常。但其他 Connection-type 就不太容易了。 我正在使用 LeJOS 固件 0.85 和 Android SDK 工具 (2.2 Froyo)。

Does anybody know, how to build a bluetooth connection between Android and LEGO-Mindstorm-NXT?
The connection between two NXTs works fine. But the other Connection-type likes not so easy.
I am working with the LeJOS Firmware 0.85 and the Android SDK Tools (2.2 Froyo).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-10-24 03:15:41

所以我已经解决了这个问题,并将展示它是如何工作的,因为我看到很多人都遇到了问题。

该类包括 4 个功能:

  • 蓝牙启用(如果之前未启用)-> enableBT()
  • 连接到 2 个 NXT -> connectToNXTs()
  • 将消息写入 NXT 之一 -> writeMessage(byte msg, String nxt)
  • 从 NXT 之一读取消息 -> readMessage(String nxt)

以下是 Android 设备的代码 (BT_comm.java):

package de.joen.android.CubeScan;


import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.UUID;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import android.util.Log;

public class BT_Comm {

  //Target NXTs for communication
  final String nxt2 = "00:16:53:04:52:3A";
  final String nxt1 = "00:16:53:07:AA:F6";

  BluetoothAdapter localAdapter;
  BluetoothSocket socket_nxt1, socket_nxt2;
  boolean success = false;

  // Enables Bluetooth if not enabled
  public void enableBT(){
    localAdapter = BluetoothAdapter.getDefaultAdapter();
    // If Bluetooth not enable then do it
    if (!localAdapter.isEnabled()) {
      localAdapter.enable();
      while(!(localAdapter.isEnabled()));
    }
  }

  // Connect to both NXTs
  public boolean connectToNXTs() {

    // Get the BluetoothDevice of the NXT
    BluetoothDevice nxt_2 = localAdapter.getRemoteDevice(nxt2);
    BluetoothDevice nxt_1 = localAdapter.getRemoteDevice(nxt1);
    // Try to connect to the nxt
    try {
      socket_nxt2 = nxt_2.createRfcommSocketToServiceRecord(UUID
          .fromString("00001101-0000-1000-8000-00805F9B34FB"));

      socket_nxt1 = nxt_1.createRfcommSocketToServiceRecord(UUID
          .fromString("00001101-0000-1000-8000-00805F9B34FB"));

      socket_nxt2.connect();    
      socket_nxt1.connect();      

      success = true;

    } catch (IOException e) {
      Log.d("Bluetooth","Err: Device not found or cannot connect");
      success=false;
    }
    return success;    
  }


  public void writeMessage(byte msg, String nxt) throws InterruptedException {
    BluetoothSocket connSock;

    // Swith nxt socket
    if (nxt.equals("nxt2")) {
      connSock=socket_nxt2;
    } else if(nxt.equals("nxt1")) {
      connSock = socket_nxt1;
    } else {
      connSock=null;
    }

    if (connSock!=null) {
      try {

        OutputStreamWriter out = new OutputStreamWriter(connSock.getOutputStream());
        out.write(msg);
        out.flush();

        Thread.sleep(1000);

      } catch (IOException e) {
        // TODO: Auto-generated catch block
        e.printStackTrace();
      }
    } else {
      // Error
    }
  }

  public int readMessage(String nxt) {
    BluetoothSocket connSock;
    int n;

    // Swith nxt socket
    if (nxt.equals("nxt2")) {
      connSock=socket_nxt2;
    } else if (nxt.equals("nxt1")) {
      connSock=socket_nxt1;
    } else {
      connSock=null;
    }

    if (connSock!=null) {
      try {

        InputStreamReader in = new InputStreamReader(connSock.getInputStream());
        n = in.read();
        return n;

      } catch (IOException e) {
        // TODO: Auto-generated catch block
        e.printStackTrace();
        return -1;
      }
    } else {
      // Error
      return -1;
    }
  }
}

要从 Android 智能手机获取消息,您必须在 NXT 端进行读取调用。
这是来自 NXT 端的代码,它将接受来自智能手机的连接并从中读取消息:

Boolean isrunning = true;

// Main loop   
while (true)
{
  LCD.drawString(waiting,0,0);
  LCD.refresh();

  // Listen for incoming connection

  NXTConnection btc = Bluetooth.waitForConnection();

  btc.setIOMode(NXTConnection.RAW);

  LCD.clear();
  LCD.drawString(connected,0,0);
  LCD.refresh();  


  // The InputStream for read data 
  DataInputStream dis = btc.openDataInputStream();


  // Loop for read data  
  while (isrunning) {
    Byte n = dis.readByte();
    LCD.clear();
    LCD.drawInt(n, 4, 4);
  }

  dis.close();

  // Wait for data to drain
  Thread.sleep(100); 

  LCD.clear();
  LCD.drawString(closing,0,0);
  LCD.refresh();

  btc.close();

  LCD.clear();
}

希望这会帮助其他人......

So i've solved it and will show all how does it works, because i've seen that a lot of people have problems with that.

The class includes 4 functions:

  • Bluetooth enable if not enabled before -> enableBT()
  • Connect to 2 NXTs -> connectToNXTs()
  • Write Message to one of the NXTs -> writeMessage(byte msg, String nxt)
  • Read Message from one of the NXTs -> readMessage(String nxt)

Here is the code for the android device (BT_comm.java):

package de.joen.android.CubeScan;


import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.UUID;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import android.util.Log;

public class BT_Comm {

  //Target NXTs for communication
  final String nxt2 = "00:16:53:04:52:3A";
  final String nxt1 = "00:16:53:07:AA:F6";

  BluetoothAdapter localAdapter;
  BluetoothSocket socket_nxt1, socket_nxt2;
  boolean success = false;

  // Enables Bluetooth if not enabled
  public void enableBT(){
    localAdapter = BluetoothAdapter.getDefaultAdapter();
    // If Bluetooth not enable then do it
    if (!localAdapter.isEnabled()) {
      localAdapter.enable();
      while(!(localAdapter.isEnabled()));
    }
  }

  // Connect to both NXTs
  public boolean connectToNXTs() {

    // Get the BluetoothDevice of the NXT
    BluetoothDevice nxt_2 = localAdapter.getRemoteDevice(nxt2);
    BluetoothDevice nxt_1 = localAdapter.getRemoteDevice(nxt1);
    // Try to connect to the nxt
    try {
      socket_nxt2 = nxt_2.createRfcommSocketToServiceRecord(UUID
          .fromString("00001101-0000-1000-8000-00805F9B34FB"));

      socket_nxt1 = nxt_1.createRfcommSocketToServiceRecord(UUID
          .fromString("00001101-0000-1000-8000-00805F9B34FB"));

      socket_nxt2.connect();    
      socket_nxt1.connect();      

      success = true;

    } catch (IOException e) {
      Log.d("Bluetooth","Err: Device not found or cannot connect");
      success=false;
    }
    return success;    
  }


  public void writeMessage(byte msg, String nxt) throws InterruptedException {
    BluetoothSocket connSock;

    // Swith nxt socket
    if (nxt.equals("nxt2")) {
      connSock=socket_nxt2;
    } else if(nxt.equals("nxt1")) {
      connSock = socket_nxt1;
    } else {
      connSock=null;
    }

    if (connSock!=null) {
      try {

        OutputStreamWriter out = new OutputStreamWriter(connSock.getOutputStream());
        out.write(msg);
        out.flush();

        Thread.sleep(1000);

      } catch (IOException e) {
        // TODO: Auto-generated catch block
        e.printStackTrace();
      }
    } else {
      // Error
    }
  }

  public int readMessage(String nxt) {
    BluetoothSocket connSock;
    int n;

    // Swith nxt socket
    if (nxt.equals("nxt2")) {
      connSock=socket_nxt2;
    } else if (nxt.equals("nxt1")) {
      connSock=socket_nxt1;
    } else {
      connSock=null;
    }

    if (connSock!=null) {
      try {

        InputStreamReader in = new InputStreamReader(connSock.getInputStream());
        n = in.read();
        return n;

      } catch (IOException e) {
        // TODO: Auto-generated catch block
        e.printStackTrace();
        return -1;
      }
    } else {
      // Error
      return -1;
    }
  }
}

To get messages from the Android Smartphone you must have a read call on the NXT-side.
Here is the code from the NXT-side wich will accept the connection from the Smartphone and read messages from it:

Boolean isrunning = true;

// Main loop   
while (true)
{
  LCD.drawString(waiting,0,0);
  LCD.refresh();

  // Listen for incoming connection

  NXTConnection btc = Bluetooth.waitForConnection();

  btc.setIOMode(NXTConnection.RAW);

  LCD.clear();
  LCD.drawString(connected,0,0);
  LCD.refresh();  


  // The InputStream for read data 
  DataInputStream dis = btc.openDataInputStream();


  // Loop for read data  
  while (isrunning) {
    Byte n = dis.readByte();
    LCD.clear();
    LCD.drawInt(n, 4, 4);
  }

  dis.close();

  // Wait for data to drain
  Thread.sleep(100); 

  LCD.clear();
  LCD.drawString(closing,0,0);
  LCD.refresh();

  btc.close();

  LCD.clear();
}

Hope this will help others...

满栀 2024-10-24 03:15:41

有一个文档我觉得非常有用
http://wiki.tinyclr.com/images/d/df/LEGO_MINDSTORMS_NXT_Direct_commands.pdf

github 上的 MINDdroid 包是一个很好的起点。

There is a document I found very useful
http://wiki.tinyclr.com/images/d/df/LEGO_MINDSTORMS_NXT_Direct_commands.pdf

The MINDdroid package on github is a fine place to start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文