如何使用蓝牙在Android和正在运行的NXT程序之间进行交互

时间:2012-08-15 11:13:20

标签: android bluetooth nxt lego

我修改了Minddroid github Android程序来读取LEGO NXT上的传感器(很棒的设备!)。现在我想读取和写入NXT中运行的Mindstorms程序的蓝牙消息。 因此,当Android要求时,我可以运行NXT程序并将结果/读数发送到Android。

2 个答案:

答案 0 :(得分:3)

我创建了一个项目,NXT将数据发送回我的A​​ndroid设备。以下是一些应该有效的代码:

这是所有Android端代码:

这是我写的一个课程,它将负责通过蓝牙连接和通信

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(Integer对象)中读取消息,您可以这样做:

this.connector.readMessage();

关闭连接:

this.connector.setBluetooth(Connector.BT_OFF);

这是所有NXT边码:

注意:下载leJOS以使所有代码生效(leJOS允许您在java中编写NXT代码)。

在主类中定义这两个对象:

public static DataOutputStream dataOutputStream;
public static NXTConnection bluetoothConnection;

要连接到您的手机:

bluetoothConnection = Bluetooth.waitForConnection();
bluetoothConnection.setIOMode(NXTConnection.RAW);
dataOutputStream = bluetoothConnection.openDataOutputStream();

以Integer对象的形式向手机发送数据:

dataOutputStream.write(100);
dataOutputStream.flush();

断开连接运行:

dataOutputStream.close();
bluetoothConnection.close();

我希望这会有所帮助。

答案 1 :(得分:1)

我对蓝牙命令感到有些困惑,但现在我发现你需要下载leJOS!

我通常会尽量避免弄乱NXT上的固件,但java更易于处理!

对于任何有兴趣的人,你可以以原生格式从你的机器人向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

如果你只是在那里搜索那么还有更多的东西

希望这有帮助!

相关问题