连接Arduino和Android蓝牙

时间:2014-06-24 06:33:56

标签: android bluetooth arduino android-bluetooth arduino-uno

我试图通过蓝牙连接arduino和Android,它的工作非常好。但是在初始化连接时我在我的arduino中写了一个设置,我不知道如何调用它。

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中做监听器。

1 个答案:

答案 0 :(得分:0)

如果您确定在设置功能方面遇到问题,请检查结构Genotronex是否无错误。还要检查使用具有不同波特率的设备,如57600,115200。

相关问题