2014-06-24 328 views
0

我試圖通過藍牙連接arduino和Android,它的工作非常好。但是,當初始化連接時,我在我的arduino中寫了一個設置,我不知道如何調用它。連接Arduino和Android藍牙

void setup() { 
    // put your setup code here, to run once: 
    Genotronex.begin(9600); 
    Genotronex.println("Bluetooth On please press 1 or 0 blink LED .."); 
    pinMode(ledpin,OUTPUT); 
} 

這是我在android系統

void beginListenForData() 
      { 
       final Handler handler = new Handler(); 
       final byte delimiter = 10; //This is the ASCII code for a newline character 

       stopWorker = false; 
       readBufferPosition = 0; 
       readBuffer = new byte[1024]; 
       workerThread = new Thread(new Runnable() 
       { 
        public void run() 
        { 
         while(!Thread.currentThread().isInterrupted() && !stopWorker) 
         { 
          try 
          { 
           int bytesAvailable = mmInputStream.available(); 
           if(bytesAvailable > 0) 
           { 
            byte[] packetBytes = new byte[bytesAvailable]; 
            mmInputStream.read(packetBytes); 
            for(int i=0;i<bytesAvailable;i++) 
            { 
             byte b = packetBytes[i]; 
             if(b == delimiter) 
             { 
              byte[] encodedBytes = new byte[readBufferPosition]; 
              System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); 
              final String data = new String(encodedBytes, "US-ASCII"); 
              readBufferPosition = 0; 

              handler.post(new Runnable() 
              { 
               public void run() 
               { 
                myLabel.setText(data); 
               } 
              }); 
             } 
             else 
             { 
              readBuffer[readBufferPosition++] = b; 
             } 
            } 
           } 

          } 
          catch (IOException ex) 
          { 
           stopWorker = true; 
          } 
         } 
        } 
       }); 

       workerThread.start(); 
      } 

幫我在這裏的代碼。我真正想要的是當我從Android打開連接時,它應該顯示Bluetooth On please press 1 or 0 blink LED ..另外如何在我的android中監聽器,如果我有定時器設置。

回答

0

如果您確定您在設置功能方面有問題,請檢查結構Genotronex是否無錯。還請檢查使用不同波特率的設備,如57600,115200。