简体   繁体   English

如何通过Blutooth HC-06在TextView上将Ping传感器数据获取到Android Studio

[英]How to get the Ping sensor data to Android Studio on a TextView via Blutooth HC-06

I have managed to set up the connection between my arduino and the App with the help of Instructables blog . 我已经在Instructables博客的帮助下成功建立了arduino和App之间的连接。 The connection is set but i need the sensor output value of the ardunio to be displayed on a TextView. 设置了连接,但我需要将ardunio的传感器输出值显示在TextView上。 This is the Arduino code . 这是Arduino 代码 How do i program my Android studio program to read the output value of the serial monitor in the Arduino? 我如何编程我的Android Studio程序以读取Arduino中串行监视器的输出值?

Okay, in the tutorial shows how send data from the app to arduino, but not how receive data from the arduino to your app for do this, you will need: 好的,在本教程中,您将展示如何从应用程序向arduino发送数据,而不是如何从arduino向您的应用程序接收数据,您将需要:

  1. Send data from arduino by HC-06. 通过HC-06从arduino发送数据。
  2. Receive and decode your data in the app. 在应用程序中接收和解码您的数据。
  3. show decoded data in the TextView. 在TextView中显示解码的数据。

a litle example for do this. 一个小例子。

In Arduino: can be the example of the arduino page you must only make sure that HC-06 is properly connected in Serial port. 在Arduino中:可以是arduino页面的示例,您只需要确保HC-06在串行端口中正确连接即可。

In the app code you can use this code to receive data, only change the name of SocketBT instance, for the instance that you need. 在应用程序代码中,您可以使用此代码来接收数据,仅更改所需实例的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();
    }
}

for show it on TextView you can do this. 可以在TextView上显示它。

MyTextView.setText(Recepcion);

this is a very general explanation, for do it , you need know how android code works basically, but is a good example for start Android and Arduino Programming. 这是一个非常笼统的解释,为此,您需要了解android代码的基本工作原理,但这是启动Android和Arduino编程的一个很好的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM