sensor data from Bluetooth (arduino + HC-05 module)

Hello friends, I need some help. I'm electric engineer and trying to build some app for my new device. On arduino there is some analog sensor connected. arduino read the sensor value and send it with hc-05 Bluetooth module. sensor data is refreshing every 100ms. At this time for testing android app there is a simple sketch on arduino:

int BluetoothData = 1;
BluetoothData ++;
myBluetooth.println(BluetoothData);
delay(100);

I have Bluetooth terminal app installed on my android from Play story and arduino sketch is working. On my app there is a strange problem. When I connect to Bluetooth device. I get data from Bluetooth, but with some errors. the arduino sketch send values from 1 to infinity. my app shows the numbers in textView, 1,2,3,4...9, after this it must be 10,11,12,13,14..19, but first number 1 is missing, so it counts 1,2,3..8,9 till the 99 and than it shows 01,02,03,04 and not 101, 102, 103, 104...
please help. What am I doing wrong? here is my MainActivity.cs code:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Bluetooth;
using System.Linq;
using Android.Graphics;

namespace Transponter
{
    [Activity(Label = "Transponter", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        BluetoothConnection myConnection = new BluetoothConnection();


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            var togglebutton1 = FindViewById<ToggleButton>(Resource.Id.toggleButton1);
            var statustext1 = FindViewById<TextView>(Resource.Id.textView1);
            BluetoothSocket _socket = null;
            System.Threading.Thread listenThread = new System.Threading.Thread(Listener);

            togglebutton1.CheckedChange += (s, e) =>
            {

                if (togglebutton1.Checked)
                {
                    myConnection = new BluetoothConnection();
                    myConnection.getAdapter();
                    myConnection.getDevice();
                    _socket = myConnection.thisDevice.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
                    myConnection.thisSocket = _socket;
                    myConnection.thisSocket.Connect();
                    togglebutton1.SetTextColor(Color.Green);
                    Toast.MakeText(this, "Bluetouth is On", ToastLength.Short).Show();
                    listenThread.Start();

                }
                else
                {
                    myConnection.thisDevice.Dispose();
                    myConnection.thisSocket.OutputStream.Close();
                    myConnection.thisSocket.Close();
                    togglebutton1.SetTextColor(Color.Red);
                    Toast.MakeText(this, "Bluetouth is Off", ToastLength.Short).Show();
                    listenThread.Abort();
                }

            };
        }

        private void Listener()
        {
            while (true)
            {
                try
                {
                    byte[] buffer = new byte[1];
                    TextView readTextView = FindViewById<TextView>(Resource.Id.textView2);
                    myConnection.thisSocket.InputStream.Read(buffer, 0, 1);
                    myConnection.thisSocket.InputStream.Close();
                    String dispString = System.Text.ASCIIEncoding.Default.GetString(buffer);
                    RunOnUiThread(() =>
                    {
                        readTextView.Text = dispString;
                        System.Console.WriteLine(dispString);
                    });
                }
                catch (Java.IO.IOException)
                {
                    TextView readTextView = FindViewById<TextView>(Resource.Id.textView2);
                    RunOnUiThread(() =>
                    {
                        readTextView.Text = string.Empty;
                    });
                    break;
                }
            }
        }
    }


    public class BluetoothConnection
    {
        public void getAdapter() { this.thisAdapter = BluetoothAdapter.DefaultAdapter; }
        public void getDevice() { this.thisDevice = (from bd in this.thisAdapter.BondedDevices where bd.Name == "hc-05" select bd).FirstOrDefault(); }

        public BluetoothAdapter thisAdapter { get; set; }
        public BluetoothDevice thisDevice { get; set; }

        public BluetoothSocket thisSocket { get; set; }
    }
}

Answers

  • RylaRyla Member ✭✭

    can you share if you solved the problem?

  • No, I didn't find the answer yet :(

  • yairdiesyairdies Member

    Hace un tiempo atrás yo encontré algo sobre este tema.
    Si mal yo no recuerdo ponían un asterisco () antes de cada número, con esto al enviar *3 se lee en el dispositivo solo el 3 y si usted envía *30, nuestro dispositivo leerá solo el 30.
    El documento que yo leí se enviaban textos y se hacía lo mismo “
    Hola Mundo” y se recibía “Hola Mundo”.
    Es igual en el Arduino, si usted va a leer 4 números, debe declararse una longitud de 5

Sign In or Register to comment.