2010-04-09 40 views
11

我对这一切都有点新,所以请耐心等待 - 我真的很感谢您的帮助。将Android Nexus One与Arduino + BlueSmirf接口

我在尝试将Android Nexus One与连接到BlueSmirf的arduino(Duemilanove)链接。我有一个程序,只是输出字符串“你好蓝牙”到任何设备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):萤火虫-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:错误/ BTArduino(17288):收到:15
04-09 16:27:57.702:错误/ BTArduino(17288):收到:1
04-09 16:27:57.702:错误/ 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(buffer)。我知道字节是一个整数,所以我尝试通过蓝牙发送整数,因为“字节”是一个整数,但它仍然没有意义。

它几乎看起来是发送不正确的波特率。这是真的吗?

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

+0

改为使用writeln/readln组合。 – 2010-04-21 06:03:29

回答

1

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

bytes = inStream.read(buffer); 

...您的信息将在第一bytes字节buffer发现(假设一切是正确的)。您可以将那些为一个字符串,像这样:

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

我掩饰了一些东西在这里(编码,串联多个缓冲区等),但这应该让你开始。