【问题标题】:How to get the Ping sensor data to Android Studio on a TextView via Blutooth HC-06如何通过蓝牙 HC-06 将 Ping 传感器数据获取到 TextView 上的 Android Studio
【发布时间】:2016-01-21 14:32:10
【问题描述】:

Instructables blog 的帮助下,我设法在我的 arduino 和应用程序之间建立了连接。 连接已设置,但我需要将 ardunio 的传感器输出值显示在 TextView 上。这是 Arduino code。 如何编写我的 Android Studio 程序以读取 Arduino 中串行监视器的输出值?

【问题讨论】:

  • 如果您还没有,您需要设置一种从 arduino 接收数据的方法。然后,您可以将 textview 设置为该值。本教程应该为您提供所需的所有信息。 wingoodharry.wordpress.com/2014/04/15/…

标签: java android bluetooth arduino


【解决方案1】:

好的,在教程中展示了如何将数据从应用程序发送到 arduino,但没有说明如何从 arduino 接收数据到您的应用程序,您需要:

  1. 通过 HC-06 从 arduino 发送数据。
  2. 在应用中接收和解码您的数据。
  3. 在 TextView 中显示解码数据。

一个小例子。

在 Arduino 中: 可以是 arduino 页面的示例,您必须确保 HC-06 正确连接到串行端口。

在应用程序代码中,您可以使用此代码接收数据,只需更改SocketBT实例的名称,为您需要的实例。

 private class TareaLeer extends Thread
{
    @Override
    public void run()
    {
        while(SocketBT.isConnected())
        {
        InputStream inputStream;
        try {
            inputStream = SocketBT.getInputStream();

            byte[] buffer = new byte[256];
            if (inputStream.available() > 0) {
                inputStream.read(buffer);
                int i = 0;
                for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
                }
                String strInput = new String(buffer, 0, i);
                String Recepcion = strInput;

                Log.d("Recibi",Recepcion);
                //Here you can pass the value of recepcion to any globlal variable and show in TextView

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    Parar();
    }
}

为了在 TextView 上显示它,你可以这样做。

MyTextView.setText(Recepcion);

这是一个非常笼统的解释,要做到这一点,你需要知道 android 代码是如何工作的,但它是开始 Android 和 Arduino 编程的一个很好的例子。

【讨论】:

  • 嘿,我应该遵循 Instructables 博客中的代码还是 @Bone 链接中给出的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 2016-04-29
相关资源
最近更新 更多