【问题标题】:Connecting Arduino and Android Bluetooth连接 Arduino 和 Android 蓝牙
【发布时间】:2014-06-24 06:33:55
【问题描述】:

我尝试通过蓝牙连接 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 中做监听器。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多