首页 文章

将Android Nexus One与Arduino BlueSmirf连接

提问于
浏览
11

我对这一切都有点新意,所以请耐心等待 - 我真的很感谢你的帮助 .

我正在尝试将Android Nexus One与连接到BlueSmirf的arduino(Duemilanove)相关联 . 我有一个程序只是将字符串“Hello Bluetooth”输出到BlueSmirf所连接的任何设备 . 这是Arduino计划:

void setup(){Serial.begin(115200); int i; }

void loop(){Serial.print(“Hello Bluetooth!”);延迟(1000); }

一台我的电脑BT终端我可以看到消息并连接没问题 . 麻烦在于我的android代码 . 我可以用android连接到设备,但是当我查看日志时它没有显示“Hello Bluetooth” . 这是调试日志:

04-09 16:27:49.022:ERROR / BTArduino(17288):连接FireFly-2583
04-09 16:27:49.022:ERROR / BTArduino(17288):开始连接插座
04-09 16:27:55.705:ERROR / BTArduino(17288):收到:16
04-09 16:27:56.702:ERROR / BTArduino(17288):收到:1
04-09 16:27:56.712:ERROR / BTArduino(17288):收到:15
04-09 16:27:57.702:ERROR / BTArduino(17288):收到:1
04-09 16:27:57.702:ERROR / BTArduino(17288):收到:15
04-09 16:27:58.704:ERROR / BTArduino(17288):收到:1
04-09 16:27:58.704:ERROR / BTArduino(17288):收到:15

等...

这是代码,我只想提供相关代码,但如果您需要更多代码请告诉我:

private class ConnectThread extends Thread {
    private final BluetoothSocket mySocket;
    private final BluetoothDevice myDevice;

    public ConnectThread(BluetoothDevice device) {
        myDevice = device;
        BluetoothSocket tmp = null;
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK");
        }
        mySocket = tmp;
    }
    public void run() {
        Log.e(TAG, "STARTING TO CONNECT THE SOCKET");
        InputStream inStream = null;
        boolean run = false;
        //...More Connection code here...

这里的相关代码越多:

byte[] buffer = new byte[1024];
        int bytes;

        // handle Connection
        try {
            inStream = mySocket.getInputStream();
            while (run) {
                try {
                    bytes = inStream.read(buffer);
                    Log.e(TAG, "Received: " + bytes);
                } catch (IOException e3) {
                    Log.e(TAG, "disconnected");
                }
            }

我正在读bytes = inStream.read(缓冲区) . 我知道字节是一个整数,所以我尝试通过蓝牙发送整数,因为“bytes”是一个整数,但它仍然没有意义 .

几乎看起来发送的波特率不正确 . 这可能是真的吗?

任何帮助,将不胜感激 . 非常感谢你 .

2 回答

  • 1

    read()返回成功读入缓冲区的字节数 . 因此,在这行代码中:

    bytes = inStream.read(buffer);
    

    ...您的消息将在 buffer 的第一个 bytes 字节中找到(假设其他一切都正确) . 您可以将它们转换为String,如下所示:

    String message = new String(buffer, 0, bytes);
    

    我在这里讨论了许多事情(编码,连接多个缓冲区等),但这应该让你开始 .

  • 1

    你看到这个项目了吗? http://code.google.com/p/android-arduino/

    干杯

相关问题