2014-06-24 232 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。